From f9804b48bb6c63109644f991cdb4b4d6090f304e Mon Sep 17 00:00:00 2001 From: Krzysztof Rudnicki Date: Mon, 23 May 2022 13:18:37 +0200 Subject: [PATCH] Soldier is a child of base --- theProject/Assets/Prefabs/Base.prefab | 2 +- theProject/Assets/Prefabs/Soldier.prefab | 11 +++-- theProject/Assets/Scripts/Base.cs | 10 ++-- theProject/Assets/Scripts/Soldier.cs | 62 ++---------------------- 4 files changed, 17 insertions(+), 68 deletions(-) diff --git a/theProject/Assets/Prefabs/Base.prefab b/theProject/Assets/Prefabs/Base.prefab index e5a3e8c5..c421aec5 100644 --- a/theProject/Assets/Prefabs/Base.prefab +++ b/theProject/Assets/Prefabs/Base.prefab @@ -64,7 +64,7 @@ Rigidbody: m_UseGravity: 0 m_IsKinematic: 0 m_Interpolate: 0 - m_Constraints: 0 + m_Constraints: 126 m_CollisionDetection: 0 --- !u!114 &-4006122134844739070 MonoBehaviour: diff --git a/theProject/Assets/Prefabs/Soldier.prefab b/theProject/Assets/Prefabs/Soldier.prefab index c470c611..4fdf4327 100644 --- a/theProject/Assets/Prefabs/Soldier.prefab +++ b/theProject/Assets/Prefabs/Soldier.prefab @@ -64,7 +64,7 @@ Rigidbody: m_UseGravity: 0 m_IsKinematic: 0 m_Interpolate: 0 - m_Constraints: 0 + m_Constraints: 126 m_CollisionDetection: 0 --- !u!114 &1033819704020761458 MonoBehaviour: @@ -78,9 +78,6 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: d34caf35d0ce610f8bc87cf815ba1915, type: 3} m_Name: m_EditorClassIdentifier: - target: {fileID: 0} - enemyType: 0 - ourType: 0 maxHealthPoints: 100 healthPoints: 100 rangeAttack: 3 @@ -90,7 +87,13 @@ MonoBehaviour: lastAttackTick: -1 nameText: {fileID: 6838251604148675782} healthPointsText: {fileID: 8012357605284019061} + target: {fileID: 0} + enemyType: 0 + ourType: 0 movementDestination: {x: 0, y: 0} + onDeath: + m_PersistentCalls: + m_Calls: [] --- !u!1 &1997457243244158843 GameObject: m_ObjectHideFlags: 0 diff --git a/theProject/Assets/Scripts/Base.cs b/theProject/Assets/Scripts/Base.cs index dd437599..3b3c9365 100644 --- a/theProject/Assets/Scripts/Base.cs +++ b/theProject/Assets/Scripts/Base.cs @@ -10,7 +10,7 @@ public class Base : MonoBehaviour Ally, Enemy } - [SerializeField] private SoldierType ourType; + [SerializeField] protected SoldierType ourType; [SerializeField] private float maxHealthPoints = 100; [SerializeField] private float healthPoints = 100; @@ -23,7 +23,7 @@ public class Base : MonoBehaviour } // Start is called before the first frame update - void Start(){ + protected void Start(){ healthPoints = maxHealthPoints; // initialize health UpdateHPDisplay(); @@ -51,11 +51,11 @@ public class Base : MonoBehaviour ourType = type; } - private void OnDestroy() + protected void OnDestroy() { Debug.Log("Soldier: " + ourType.ToString() + " has died", gameObject); } - private void ReduceHP(float damage) + protected void ReduceHP(float damage) { healthPoints -= damage; @@ -66,7 +66,7 @@ public class Base : MonoBehaviour Debug.Log("I took damage, my HP is now: " + healthPoints + " noooo!!!!", gameObject); } - private void UpdateHPDisplay() + protected void UpdateHPDisplay() { healthPointsText.text = healthPoints.ToString() + "/" + maxHealthPoints.ToString(); } diff --git a/theProject/Assets/Scripts/Soldier.cs b/theProject/Assets/Scripts/Soldier.cs index dbeba977..16a94e3b 100644 --- a/theProject/Assets/Scripts/Soldier.cs +++ b/theProject/Assets/Scripts/Soldier.cs @@ -4,7 +4,7 @@ using TMPro; using UnityEngine; using UnityEngine.Events; -public class Soldier : MonoBehaviour +public class Soldier : Base { private Queue actions = new Queue(); private Queue interrupts = new Queue(); @@ -40,26 +40,17 @@ public class Soldier : MonoBehaviour interrupts.Enqueue(new Movement()); // force soldier to find path to the new destination } #endregion - public enum SoldierType - { - Ally, - Enemy - } + [Header("Values")] - [SerializeField] private float maxHealthPoints = 10; - [SerializeField] private float healthPoints = 1; [SerializeField] private float rangeAttack = 100; [SerializeField] private float rangeView = 1; [SerializeField] private float damageAttack = 1; [SerializeField] private int speedAttack = 1; // ticks between attacks [SerializeField] private int lastAttackTick = -1; [Header("References")] - [SerializeField] private TMP_Text nameText = null; - [SerializeField] private TMP_Text healthPointsText = null; [Header("Do-not-change-in-game values")] [SerializeField] private Soldier target; [SerializeField] private SoldierType enemyType; - [SerializeField] private SoldierType ourType; [SerializeField] private Vector2Int movementDestination = Vector2Int.zero; @@ -67,39 +58,11 @@ public class Soldier : MonoBehaviour [HideInInspector] public UnityEvent onDeath = new UnityEvent(); - public SoldierType TempGetOwnType() - { - return ourType; - } // Start is called before the first frame update void Start(){ - healthPoints = maxHealthPoints; // initialize health - UpdateHPDisplay(); + base.Start(); setEnemyTag(); - - Debug.Log("Soldier: " + ourType.ToString() + " has appeared", gameObject); - - switch (ourType) - { - case SoldierType.Ally: - nameText.text = "Ally"; - nameText.color = Color.blue; - break; - case SoldierType.Enemy: - nameText.text = "Enemy"; - nameText.color = Color.red; - break; - default: - nameText.text = "how did we get here (forever)"; - nameText.color = new Color(255, 192, 203); - break; - } - } - - public void setOwnTag(SoldierType type) - { - ourType = type; } public void setEnemyTag() @@ -115,10 +78,9 @@ public class Soldier : MonoBehaviour private void OnDestroy() { + base.OnDestroy(); TickSystem.OnTick -= HandleTick; onDeath.Invoke(); - - Debug.Log("Soldier: " + ourType.ToString() + " has died", gameObject); } private void HandleTick(TickSystem.OnTickEventArgs tickEventArgs) @@ -195,20 +157,4 @@ public class Soldier : MonoBehaviour } */ - - private void ReduceHP(float damage) - { - healthPoints -= damage; - - if (healthPoints <= 0) - Destroy(gameObject); - - UpdateHPDisplay(); - Debug.Log("I took damage, oh my HP is now: " + healthPoints + " noooo!!!!", gameObject); - } - - private void UpdateHPDisplay() - { - healthPointsText.text = healthPoints.ToString() + "/" + maxHealthPoints.ToString(); - } }