From 81b0797774d0d13e1d3a699fc1ff7cb517fb96f3 Mon Sep 17 00:00:00 2001 From: ProtoSharkk Date: Sun, 20 Oct 2024 00:40:53 -0700 Subject: [PATCH] Health system for ratbots, can now stab ratbots --- Assets/GameState.cs | 2 +- Assets/Ratbot.cs | 7 ++ Assets/Scenes/SampleScene.unity | 110 +++++++++++++++++++++++++++++++- Assets/Teo.cs | 18 ++++++ 4 files changed, 135 insertions(+), 2 deletions(-) diff --git a/Assets/GameState.cs b/Assets/GameState.cs index 9640ad5..0189a73 100644 --- a/Assets/GameState.cs +++ b/Assets/GameState.cs @@ -5,7 +5,6 @@ public class GameState : MonoBehaviour public string state; public uint wave = 1; public GameObject ratbot; - void Start() { NewWave(); } @@ -30,6 +29,7 @@ public class GameState : MonoBehaviour for (uint _ = 0; _ < wave; _++) { GameObject newRatbot = Instantiate(ratbot); newRatbot.GetComponent().damageStrength = 5+wave*5; + newRatbot.GetComponent().health = 5+wave*2; } wave++; } diff --git a/Assets/Ratbot.cs b/Assets/Ratbot.cs index 687c447..8e9bae7 100644 --- a/Assets/Ratbot.cs +++ b/Assets/Ratbot.cs @@ -5,6 +5,7 @@ public class Ratbot : MonoBehaviour public float moveSpeed; public float damageStrength; public float damageTimeoutSeconds; + public float health = 10; float lastDamageTime; GameState gameState; GameObject player; @@ -29,4 +30,10 @@ public class Ratbot : MonoBehaviour lastDamageTime = Time.fixedTime; collision.gameObject.GetComponent().health -= damageStrength; } + public void Damage(float hurtyAmount) { + health -= hurtyAmount; + if (health <= 0) { + Destroy(gameObject); + } + } } diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index 38fcc7e..af2e874 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -316,7 +316,10 @@ PrefabInstance: m_RemovedComponents: - {fileID: 0} m_RemovedGameObjects: [] - m_AddedGameObjects: [] + m_AddedGameObjects: + - targetCorrespondingSourceObject: {fileID: 4154134366646162009, guid: e05928a0d673caf999d31b31b994a112, type: 3} + insertIndex: -1 + addedObject: {fileID: 1873397571} m_AddedComponents: - targetCorrespondingSourceObject: {fileID: 8669953498445216587, guid: e05928a0d673caf999d31b31b994a112, type: 3} insertIndex: 1 @@ -546,6 +549,11 @@ Skybox: m_GameObject: {fileID: 977303806} m_Enabled: 1 m_CustomSkybox: {fileID: 10304, guid: 0000000000000000f000000000000000, type: 0} +--- !u!4 &1246075822 stripped +Transform: + m_CorrespondingSourceObject: {fileID: 4154134366646162009, guid: e05928a0d673caf999d31b31b994a112, type: 3} + m_PrefabInstance: {fileID: 309882168} + m_PrefabAsset: {fileID: 0} --- !u!1 &1394869898 GameObject: m_ObjectHideFlags: 0 @@ -695,6 +703,106 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 07e7096bcd8103c3faadeefc30c8a2f4, type: 3} m_Name: m_EditorClassIdentifier: +--- !u!1 &1873397570 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 1873397571} + - component: {fileID: 1873397573} + - component: {fileID: 1873397572} + m_Layer: 0 + m_Name: sword_0 + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &1873397571 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1873397570} + serializedVersion: 2 + m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 2.001004, y: 2.001004, z: 2.001004} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 1246075822} + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!114 &1873397572 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1873397570} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 12e2e0dada30fe215be589422e1df9b7, type: 3} + m_Name: + m_EditorClassIdentifier: +--- !u!212 &1873397573 +SpriteRenderer: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1873397570} + m_Enabled: 1 + m_CastShadows: 0 + m_ReceiveShadows: 0 + m_DynamicOccludee: 1 + m_StaticShadowCaster: 0 + m_MotionVectors: 1 + m_LightProbeUsage: 1 + m_ReflectionProbeUsage: 1 + m_RayTracingMode: 0 + m_RayTraceProcedural: 0 + m_RayTracingAccelStructBuildFlagsOverride: 0 + m_RayTracingAccelStructBuildFlags: 1 + m_SmallMeshCulling: 1 + m_RenderingLayerMask: 1 + m_RendererPriority: 0 + m_Materials: + - {fileID: 2100000, guid: a97c105638bdf8b4a8650670310a4cd3, type: 2} + m_StaticBatchInfo: + firstSubMesh: 0 + subMeshCount: 0 + m_StaticBatchRoot: {fileID: 0} + m_ProbeAnchor: {fileID: 0} + m_LightProbeVolumeOverride: {fileID: 0} + m_ScaleInLightmap: 1 + m_ReceiveGI: 1 + m_PreserveUVs: 0 + m_IgnoreNormalsForChartDetection: 0 + m_ImportantGI: 0 + m_StitchLightmapSeams: 1 + m_SelectedEditorRenderState: 0 + m_MinimumChartSize: 4 + m_AutoUVMaxDistance: 0.5 + m_AutoUVMaxAngle: 89 + m_LightmapParameters: {fileID: 0} + m_SortingLayerID: 0 + m_SortingLayer: 0 + m_SortingOrder: 1 + m_Sprite: {fileID: -3753825769889500098, guid: 9216a0f04ef201ab08cf158320feac60, type: 3} + m_Color: {r: 1, g: 1, b: 1, a: 1} + m_FlipX: 0 + m_FlipY: 0 + m_DrawMode: 0 + m_Size: {x: 4.82, y: 4.82} + m_AdaptiveModeThreshold: 0.5 + m_SpriteTileMode: 0 + m_WasSpriteAssigned: 1 + m_MaskInteraction: 0 + m_SpriteSortPoint: 0 --- !u!1660057539 &9223372036854775807 SceneRoots: m_ObjectHideFlags: 0 diff --git a/Assets/Teo.cs b/Assets/Teo.cs index 24aa1f5..bc2f992 100644 --- a/Assets/Teo.cs +++ b/Assets/Teo.cs @@ -4,6 +4,8 @@ public class Teo : MonoBehaviour { public float health = 100; public float moveSpeed = 10; + public float swingRange = 3; + public float damageStrength = 10; Rigidbody2D controller; GameState gameState; // Start is called once before the first execution of Update after the MonoBehaviour is created @@ -18,5 +20,21 @@ public class Teo : MonoBehaviour Input.GetAxis("Horizontal"), Input.GetAxis("Vertical") ).normalized * moveSpeed; + + if (Input.GetMouseButtonDown(0)) { + RaycastHit2D[] hits = Physics2D.CircleCastAll( + transform.position, + swingRange, + new Vector2( + Screen.width/2 - Input.mousePosition.x, + Screen.height/2 - Input.mousePosition.y + ) + ); + foreach (RaycastHit2D hit in hits) { + Ratbot ratbot = hit.collider.GetComponent(); + if (ratbot == null) continue; + ratbot.Damage(damageStrength); + } + } } }