From 98402a90f7e9d1ba05a46bee833b119231073871 Mon Sep 17 00:00:00 2001 From: trotFunky Date: Tue, 17 Sep 2019 20:30:20 +0200 Subject: [PATCH 1/2] Protect against NPE if no AbstractCharacter Made Speed a property to allow other Objects that are not Characters to use the Mover Init in Awake() to allow other objects to modify its properties during their Start() --- Assets/Scripts/Mover.cs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/Assets/Scripts/Mover.cs b/Assets/Scripts/Mover.cs index e403f02..f044b7b 100644 --- a/Assets/Scripts/Mover.cs +++ b/Assets/Scripts/Mover.cs @@ -8,6 +8,11 @@ public class Mover : MonoBehaviour private Transform _transform; private Vector2 _direction; private float _maxSpeed; + public float MaxSpeed + { + get { return _maxSpeed; } + set { _maxSpeed = value; } + } public Vector2 Direction { @@ -15,11 +20,11 @@ public class Mover : MonoBehaviour set { _direction = value; } } - void Start() + void Awake() { _transform = GetComponentInParent(); _character = GetComponentInParent(); - _maxSpeed = _character.MaxSpeed; + _maxSpeed = _character != null ? _character.MaxSpeed : 1; _direction = Vector2.zero; } From 37e8c8d6892d1aeefbf561514edb78f5a086b419 Mon Sep 17 00:00:00 2001 From: trotFunky Date: Tue, 17 Sep 2019 20:45:08 +0200 Subject: [PATCH 2/2] Implemented Bullet component Added Death() method to AbstractCharacter Implemented basic bullet collision detection and effect --- Assets/Scripts/AbstractCharacter.cs | 24 +++++++++++++++--- Assets/Scripts/Bullet.cs | 39 ++++++++++++++++++++++++++--- Assets/Scripts/PlayerCharacter.cs | 5 ++++ 3 files changed, 60 insertions(+), 8 deletions(-) diff --git a/Assets/Scripts/AbstractCharacter.cs b/Assets/Scripts/AbstractCharacter.cs index d001464..5e9fb91 100644 --- a/Assets/Scripts/AbstractCharacter.cs +++ b/Assets/Scripts/AbstractCharacter.cs @@ -1,4 +1,5 @@ -using System.Collections; +using System; +using System.Collections; using System.Collections.Generic; using UnityEngine; @@ -93,9 +94,24 @@ public abstract class AbstractCharacter : MonoBehaviour public abstract void Move(Vector2 movementDirection); public abstract void Shoot(); - // TODO : Manage hurt + OnCollision + Death'n stuff - public void Hurt() + private void OnCollisionEnter2D(Collision2D other) { - throw new System.NotImplementedException(); + Bullet bullet = other.gameObject.GetComponent(); + if (bullet == null) + { + return; + } + Hurt(bullet.Damage); } + + public void Hurt(int damage) + { + CurrentHealth -= damage; + if (CurrentHealth <= 0) + { + Death(); + } + } + // Handles the death of the character, should be overriden + protected abstract void Death(); } diff --git a/Assets/Scripts/Bullet.cs b/Assets/Scripts/Bullet.cs index ad87b01..0ef04c7 100644 --- a/Assets/Scripts/Bullet.cs +++ b/Assets/Scripts/Bullet.cs @@ -1,13 +1,38 @@ -using System.Collections; +using System; +using System.Collections; using System.Collections.Generic; using UnityEngine; public class Bullet : MonoBehaviour { - // Start is called before the first frame update - void Start() + [SerializeField] private int _damage; + public int Damage => _damage; + + // Speed in m/s + [SerializeField] private float _speed; + public float Speed { - + get { return _speed; } + set { _speed = value; } + } + + [SerializeField] private Vector2 _direction; + public Vector2 Direction + { + get { return _direction; } + set { _direction = value; } + } + + private void Start() + { + Mover mover = GetComponentInParent(); + if (mover == null) + { + throw new MissingComponentException("Missing Mover!"); + } + + mover.Direction = Direction; + mover.MaxSpeed = Speed; } // Update is called once per frame @@ -15,4 +40,10 @@ public class Bullet : MonoBehaviour { } + + // Destroy on Stay, collision effects should be handled on Enter + private void OnCollisionStay2D(Collision2D other) + { + Destroy(gameObject); + } } diff --git a/Assets/Scripts/PlayerCharacter.cs b/Assets/Scripts/PlayerCharacter.cs index 1908ae3..a73a9ed 100644 --- a/Assets/Scripts/PlayerCharacter.cs +++ b/Assets/Scripts/PlayerCharacter.cs @@ -17,4 +17,9 @@ public class PlayerCharacter : AbstractCharacter // TODO : Implement throw new System.NotImplementedException(); } + + protected override void Death() + { + throw new System.NotImplementedException(); + } }