From 6bfe0aa8f97f3a8fa217f5be3351a1cec608637c Mon Sep 17 00:00:00 2001 From: Ny Bruker <robinhs@stud.ntnu.no> Date: Thu, 29 Sep 2022 20:06:09 +0200 Subject: [PATCH] Abstracted EnemyController to work as a root class of all enemies. --- .../Assets/Code/Slime/SlimeController.cs | 132 ++++++++---------- MrBigsock/Assets/Code/orc/EnemyController.cs | 104 +++++--------- 2 files changed, 89 insertions(+), 147 deletions(-) diff --git a/MrBigsock/Assets/Code/Slime/SlimeController.cs b/MrBigsock/Assets/Code/Slime/SlimeController.cs index f8b89ba4..743445cb 100644 --- a/MrBigsock/Assets/Code/Slime/SlimeController.cs +++ b/MrBigsock/Assets/Code/Slime/SlimeController.cs @@ -7,21 +7,21 @@ using System; namespace BigSock { - public partial class SlimeController : Character { - public float collisionOffset = 0.05f; - public ContactFilter2D movementFilter; - List<RaycastHit2D> castCollisions = new List<RaycastHit2D>(); + public partial class SlimeController : EnemyController { + //public float collisionOffset = 0.05f; + //public ContactFilter2D movementFilter; + //List<RaycastHit2D> castCollisions = new List<RaycastHit2D>(); - private Transform target; - Animator m_Animator; - private float distance; + //private Transform target; + //Animator m_Animator; + //private float distance; - private float canAttack; + //private float canAttack; - private EmptyCollider followCollider; - private EmptyCollider attackCollider; + //private EmptyCollider followCollider; + //private EmptyCollider attackCollider; - private bool isInMelee = false; + //private bool isInMelee = false; //Rigidbody2D rb; @@ -63,40 +63,24 @@ namespace BigSock { public double LeapForce => MovementSpeed * 4; - void Start() { - rb = GetComponent<Rigidbody2D>(); - - m_Animator = gameObject.GetComponent<Animator>(); - - followCollider = transform.Find("followCollider").GetComponent<EmptyCollider>(); - followCollider.OnColliderEnter2D_Action += Move_OnColliderEnter2D; - followCollider.OnColliderStay2D_Action += Move_OnColliderStay2D; - followCollider.OnColliderExit2D_Action += Move_OnColliderExit2D; - - attackCollider = transform.Find("MeleeCollider").GetComponent<EmptyCollider>(); - attackCollider.OnColliderEnter2D_Action += Attack_OnColliderEnter2D; - attackCollider.OnColliderStay2D_Action += Attack_OnColliderStay2D; - attackCollider.OnColliderExit2D_Action += Attack_OnColliderExit2D; - } - + //void Start() { + // rb = GetComponent<Rigidbody2D>(); + // m_Animator = gameObject.GetComponent<Animator>(); + // followCollider = transform.Find("followCollider").GetComponent<EmptyCollider>(); + // followCollider.OnColliderEnter2D_Action += Move_OnColliderEnter2D; + // followCollider.OnColliderStay2D_Action += Move_OnColliderStay2D; + // followCollider.OnColliderExit2D_Action += Move_OnColliderExit2D; + // attackCollider = transform.Find("MeleeCollider").GetComponent<EmptyCollider>(); + // attackCollider.OnColliderEnter2D_Action += Attack_OnColliderEnter2D; + // attackCollider.OnColliderStay2D_Action += Attack_OnColliderStay2D; + // attackCollider.OnColliderExit2D_Action += Attack_OnColliderExit2D; + //} - - private void RotateAnimation(Vector2 direction) - { - if (direction.x > 0.01f){ - gameObject.GetComponent<SpriteRenderer>().flipX = false; - } - else if (direction.x < -0.01f){ - gameObject.GetComponent<SpriteRenderer>().flipX = true; - } - - } - - private void Update() { + protected override void Update() { if(State == SlimeState.Idle) { // If it has a target and has idled long enough. if(target != null && DateTime.Now >= NextTimeStateCanChange) { @@ -160,37 +144,36 @@ namespace BigSock { */ public partial class SlimeController { - private void Attack_OnColliderEnter2D(Collider2D other) { - if (other.gameObject.tag == "Player") - isInMelee = true; - } - - - private void Attack_OnColliderStay2D(Collider2D other) { - var player = other.gameObject.GetComponent<PlayerController>(); - if (player != null) { - // Create attack object. - var attack = new AttackStats{ - Damage = Damage, - Knockback = KnockbackForce, - Range = 0, - AttackSpeed = AttackSpeed, - Source = transform.position, - }; - - // Get the player to take the damage. - if(player.TakeDamage(attack)) { - //knockback ? - //animer nå ? - - } - } - } - - private void Attack_OnColliderExit2D(Collider2D other){ - if (other.gameObject.tag == "Player") - isInMelee = false; - } + //private void Attack_OnColliderEnter2D(Collider2D other) { + // if (other.gameObject.tag == "Player") + // isInMelee = true; + //} + + + //private void Attack_OnColliderStay2D(Collider2D other) { + // var player = other.gameObject.GetComponent<PlayerController>(); + // if (player != null) { + // // Create attack object. + // var attack = new AttackStats{ + // Damage = Damage, + // Knockback = KnockbackForce, + // Range = 0, + // AttackSpeed = AttackSpeed, + // Source = transform.position, + // }; + // // Get the player to take the damage. + // if(player.TakeDamage(attack)) { + // //knockback ? + // //animer nå ? + // + // } + // } + //} + + //private void Attack_OnColliderExit2D(Collider2D other){ + // if (other.gameObject.tag == "Player") + // isInMelee = false; + //} } @@ -199,18 +182,15 @@ namespace BigSock { */ public partial class SlimeController { - private void Move_OnColliderEnter2D(Collider2D other) { + protected override void Move_OnColliderEnter2D(Collider2D other) { if (other.gameObject.tag == "Player") { //m_Animator.SetTrigger("walk"); target = other.transform; } } - private void Move_OnColliderStay2D(Collider2D other) { - if (other.gameObject.tag == "Player") { } - } - private void Move_OnColliderExit2D(Collider2D other) { + protected override void Move_OnColliderExit2D(Collider2D other) { if (other.gameObject.tag == "Player") { //m_Animator.SetTrigger("idle"); target = null; diff --git a/MrBigsock/Assets/Code/orc/EnemyController.cs b/MrBigsock/Assets/Code/orc/EnemyController.cs index fb98ebe5..d6e7282d 100644 --- a/MrBigsock/Assets/Code/orc/EnemyController.cs +++ b/MrBigsock/Assets/Code/orc/EnemyController.cs @@ -10,25 +10,25 @@ namespace BigSock { public partial class EnemyController : Character { public float collisionOffset = 0.05f; public ContactFilter2D movementFilter; - List<RaycastHit2D> castCollisions = new List<RaycastHit2D>(); + protected List<RaycastHit2D> castCollisions = new List<RaycastHit2D>(); - private Transform target; - Animator m_Animator; - private float distance; + protected Transform target; + protected Animator m_Animator; + protected float distance; - private float canAttack; + protected float canAttack; - private EmptyCollider followCollider; - private EmptyCollider attackCollider; + protected EmptyCollider followCollider; + protected EmptyCollider attackCollider; - private bool isInMelee = false; + protected bool isInMelee = false; //Rigidbody2D rb; // private Collider2D_Proxy secondCollider; - void Start(){ + protected virtual void Start(){ rb = GetComponent<Rigidbody2D>(); m_Animator = gameObject.GetComponent<Animator>(); @@ -50,7 +50,7 @@ namespace BigSock { - private void RotateAnimation(Vector2 direction) { + protected virtual void RotateAnimation(Vector2 direction) { if (direction.x > 0.01f){ gameObject.GetComponent<SpriteRenderer>().flipX = false; } @@ -59,7 +59,7 @@ namespace BigSock { } } - private void Update(){ + protected virtual void Update() { if (target != null && !isInMelee){ /* //walk @@ -78,38 +78,7 @@ namespace BigSock { } } - - - private bool TryMove_OLD(Vector2 direction) { - if(direction != Vector2.zero) { - - // Check for potential collisions - int count = rb.Cast( - direction, // X and Y values between -1 and 1 that represent the direction from the body to look for collisions - movementFilter, // The settings that determine where a collision can occur on such as layers to collide with - castCollisions, // List of collisions to store the found collisions into after the Cast is finished - (float) MovementSpeed * Time.fixedDeltaTime + collisionOffset); // The amount to cast equal to the movement plus an offset - //Debug.Log($"cast {string.Join(", ", castCollisions.Select(x => $"{x.collider.isTrigger}"))}"); - //Debug.Log($"rb.position : {rb.position}"); - //Debug.Log($"target.position : {target.position}"); - //Debug.Log($"direction : {direction}"); - if(count == 0){ - rb.MovePosition(rb.position + direction * (float) MovementSpeed * Time.fixedDeltaTime); - //print($"rb.position {rb.position}"); - //print($"direction {direction}"); - RotateAnimation(direction); - - return true; - } else { - return false; - } - } else { - // Can't move if there's no direction to move in - return false; - } - - } } @@ -118,14 +87,13 @@ namespace BigSock { */ public partial class EnemyController { - private void Attack_OnColliderEnter2D(Collider2D other){ - if (other.gameObject.tag == "Player") - isInMelee = true; + protected virtual void Attack_OnColliderEnter2D(Collider2D other) { + if (other.gameObject.tag == "Player") isInMelee = true; } - private void Attack_OnColliderStay2D(Collider2D other) { + protected virtual void Attack_OnColliderStay2D(Collider2D other) { var player = other.gameObject.GetComponent<PlayerController>(); if(player != null) { // Create attack object. @@ -146,9 +114,9 @@ namespace BigSock { } } } - private void Attack_OnColliderExit2D(Collider2D other){ - if (other.gameObject.tag == "Player") - isInMelee = false; + protected virtual void Attack_OnColliderExit2D(Collider2D other) { + if (other.gameObject.tag == "Player") + isInMelee = false; } } @@ -158,32 +126,26 @@ namespace BigSock { */ public partial class EnemyController { - private void Move_OnColliderEnter2D(Collider2D other) - { - Debug.Log("enter"); - if (other.gameObject.tag == "Player"){ - Debug.Log("enter if"); - - m_Animator.SetTrigger("walk"); - target = other.transform; - } + protected virtual void Move_OnColliderEnter2D(Collider2D other) { + //Debug.Log("enter"); + if (other.gameObject.tag == "Player"){ + //Debug.Log("enter if"); + + m_Animator.SetTrigger("walk"); + target = other.transform; + } } - private void Move_OnColliderStay2D(Collider2D other) - { - - if (other.gameObject.tag == "Player"){ - - } + protected virtual void Move_OnColliderStay2D(Collider2D other) { + } - private void Move_OnColliderExit2D(Collider2D other) - { - if (other.gameObject.tag == "Player"){ - m_Animator.SetTrigger("idle"); - target = other.transform; - target = null; - } + protected virtual void Move_OnColliderExit2D(Collider2D other) { + if (other.gameObject.tag == "Player"){ + m_Animator.SetTrigger("idle"); + target = other.transform; + target = null; + } } } -- GitLab