Skip to content
Snippets Groups Projects
Commit 6bfe0aa8 authored by Robin Halseth Sandvik's avatar Robin Halseth Sandvik
Browse files

Abstracted EnemyController to work as a root class of all enemies.

parent 9a035b8a
No related branches found
No related tags found
1 merge request!20New items system.
...@@ -7,21 +7,21 @@ using System; ...@@ -7,21 +7,21 @@ using System;
namespace BigSock { namespace BigSock {
public partial class SlimeController : Character { public partial class SlimeController : EnemyController {
public float collisionOffset = 0.05f; //public float collisionOffset = 0.05f;
public ContactFilter2D movementFilter; //public ContactFilter2D movementFilter;
List<RaycastHit2D> castCollisions = new List<RaycastHit2D>(); //List<RaycastHit2D> castCollisions = new List<RaycastHit2D>();
private Transform target; //private Transform target;
Animator m_Animator; //Animator m_Animator;
private float distance; //private float distance;
private float canAttack; //private float canAttack;
private EmptyCollider followCollider; //private EmptyCollider followCollider;
private EmptyCollider attackCollider; //private EmptyCollider attackCollider;
private bool isInMelee = false; //private bool isInMelee = false;
//Rigidbody2D rb; //Rigidbody2D rb;
...@@ -63,40 +63,24 @@ namespace BigSock { ...@@ -63,40 +63,24 @@ namespace BigSock {
public double LeapForce => MovementSpeed * 4; public double LeapForce => MovementSpeed * 4;
void Start() { //void Start() {
rb = GetComponent<Rigidbody2D>(); // rb = GetComponent<Rigidbody2D>();
// m_Animator = gameObject.GetComponent<Animator>();
m_Animator = gameObject.GetComponent<Animator>(); // followCollider = transform.Find("followCollider").GetComponent<EmptyCollider>();
// followCollider.OnColliderEnter2D_Action += Move_OnColliderEnter2D;
followCollider = transform.Find("followCollider").GetComponent<EmptyCollider>(); // followCollider.OnColliderStay2D_Action += Move_OnColliderStay2D;
followCollider.OnColliderEnter2D_Action += Move_OnColliderEnter2D; // followCollider.OnColliderExit2D_Action += Move_OnColliderExit2D;
followCollider.OnColliderStay2D_Action += Move_OnColliderStay2D; // attackCollider = transform.Find("MeleeCollider").GetComponent<EmptyCollider>();
followCollider.OnColliderExit2D_Action += Move_OnColliderExit2D; // attackCollider.OnColliderEnter2D_Action += Attack_OnColliderEnter2D;
// attackCollider.OnColliderStay2D_Action += Attack_OnColliderStay2D;
attackCollider = transform.Find("MeleeCollider").GetComponent<EmptyCollider>(); // attackCollider.OnColliderExit2D_Action += Attack_OnColliderExit2D;
attackCollider.OnColliderEnter2D_Action += Attack_OnColliderEnter2D; //}
attackCollider.OnColliderStay2D_Action += Attack_OnColliderStay2D;
attackCollider.OnColliderExit2D_Action += Attack_OnColliderExit2D;
}
protected override void Update() {
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() {
if(State == SlimeState.Idle) { if(State == SlimeState.Idle) {
// If it has a target and has idled long enough. // If it has a target and has idled long enough.
if(target != null && DateTime.Now >= NextTimeStateCanChange) { if(target != null && DateTime.Now >= NextTimeStateCanChange) {
...@@ -160,37 +144,36 @@ namespace BigSock { ...@@ -160,37 +144,36 @@ namespace BigSock {
*/ */
public partial class SlimeController { public partial class SlimeController {
private void Attack_OnColliderEnter2D(Collider2D other) { //private void Attack_OnColliderEnter2D(Collider2D other) {
if (other.gameObject.tag == "Player") // if (other.gameObject.tag == "Player")
isInMelee = true; // isInMelee = true;
} //}
private void Attack_OnColliderStay2D(Collider2D other) { //private void Attack_OnColliderStay2D(Collider2D other) {
var player = other.gameObject.GetComponent<PlayerController>(); // var player = other.gameObject.GetComponent<PlayerController>();
if (player != null) { // if (player != null) {
// Create attack object. // // Create attack object.
var attack = new AttackStats{ // var attack = new AttackStats{
Damage = Damage, // Damage = Damage,
Knockback = KnockbackForce, // Knockback = KnockbackForce,
Range = 0, // Range = 0,
AttackSpeed = AttackSpeed, // AttackSpeed = AttackSpeed,
Source = transform.position, // Source = transform.position,
}; // };
// // Get the player to take the damage.
// Get the player to take the damage. // if(player.TakeDamage(attack)) {
if(player.TakeDamage(attack)) { // //knockback ?
//knockback ? // //animer nå ?
//animer nå ? //
// }
} // }
} //}
}
//private void Attack_OnColliderExit2D(Collider2D other){
private void Attack_OnColliderExit2D(Collider2D other){ // if (other.gameObject.tag == "Player")
if (other.gameObject.tag == "Player") // isInMelee = false;
isInMelee = false; //}
}
} }
...@@ -199,18 +182,15 @@ namespace BigSock { ...@@ -199,18 +182,15 @@ namespace BigSock {
*/ */
public partial class SlimeController { public partial class SlimeController {
private void Move_OnColliderEnter2D(Collider2D other) { protected override void Move_OnColliderEnter2D(Collider2D other) {
if (other.gameObject.tag == "Player") { if (other.gameObject.tag == "Player") {
//m_Animator.SetTrigger("walk"); //m_Animator.SetTrigger("walk");
target = other.transform; 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") { if (other.gameObject.tag == "Player") {
//m_Animator.SetTrigger("idle"); //m_Animator.SetTrigger("idle");
target = null; target = null;
......
...@@ -10,25 +10,25 @@ namespace BigSock { ...@@ -10,25 +10,25 @@ namespace BigSock {
public partial class EnemyController : Character { public partial class EnemyController : Character {
public float collisionOffset = 0.05f; public float collisionOffset = 0.05f;
public ContactFilter2D movementFilter; public ContactFilter2D movementFilter;
List<RaycastHit2D> castCollisions = new List<RaycastHit2D>(); protected List<RaycastHit2D> castCollisions = new List<RaycastHit2D>();
private Transform target; protected Transform target;
Animator m_Animator; protected Animator m_Animator;
private float distance; protected float distance;
private float canAttack; protected float canAttack;
private EmptyCollider followCollider; protected EmptyCollider followCollider;
private EmptyCollider attackCollider; protected EmptyCollider attackCollider;
private bool isInMelee = false; protected bool isInMelee = false;
//Rigidbody2D rb; //Rigidbody2D rb;
// private Collider2D_Proxy secondCollider; // private Collider2D_Proxy secondCollider;
void Start(){ protected virtual void Start(){
rb = GetComponent<Rigidbody2D>(); rb = GetComponent<Rigidbody2D>();
m_Animator = gameObject.GetComponent<Animator>(); m_Animator = gameObject.GetComponent<Animator>();
...@@ -50,7 +50,7 @@ namespace BigSock { ...@@ -50,7 +50,7 @@ namespace BigSock {
private void RotateAnimation(Vector2 direction) { protected virtual void RotateAnimation(Vector2 direction) {
if (direction.x > 0.01f){ if (direction.x > 0.01f){
gameObject.GetComponent<SpriteRenderer>().flipX = false; gameObject.GetComponent<SpriteRenderer>().flipX = false;
} }
...@@ -59,7 +59,7 @@ namespace BigSock { ...@@ -59,7 +59,7 @@ namespace BigSock {
} }
} }
private void Update(){ protected virtual void Update() {
if (target != null && !isInMelee){ if (target != null && !isInMelee){
/* //walk /* //walk
...@@ -78,38 +78,7 @@ namespace BigSock { ...@@ -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 { ...@@ -118,14 +87,13 @@ namespace BigSock {
*/ */
public partial class EnemyController { public partial class EnemyController {
private void Attack_OnColliderEnter2D(Collider2D other){ protected virtual void Attack_OnColliderEnter2D(Collider2D other) {
if (other.gameObject.tag == "Player") if (other.gameObject.tag == "Player") isInMelee = true;
isInMelee = true;
} }
private void Attack_OnColliderStay2D(Collider2D other) { protected virtual void Attack_OnColliderStay2D(Collider2D other) {
var player = other.gameObject.GetComponent<PlayerController>(); var player = other.gameObject.GetComponent<PlayerController>();
if(player != null) { if(player != null) {
// Create attack object. // Create attack object.
...@@ -146,9 +114,9 @@ namespace BigSock { ...@@ -146,9 +114,9 @@ namespace BigSock {
} }
} }
} }
private void Attack_OnColliderExit2D(Collider2D other){ protected virtual void Attack_OnColliderExit2D(Collider2D other) {
if (other.gameObject.tag == "Player") if (other.gameObject.tag == "Player")
isInMelee = false; isInMelee = false;
} }
} }
...@@ -158,32 +126,26 @@ namespace BigSock { ...@@ -158,32 +126,26 @@ namespace BigSock {
*/ */
public partial class EnemyController { public partial class EnemyController {
private void Move_OnColliderEnter2D(Collider2D other) protected virtual void Move_OnColliderEnter2D(Collider2D other) {
{ //Debug.Log("enter");
Debug.Log("enter"); if (other.gameObject.tag == "Player"){
if (other.gameObject.tag == "Player"){ //Debug.Log("enter if");
Debug.Log("enter if");
m_Animator.SetTrigger("walk");
m_Animator.SetTrigger("walk"); target = other.transform;
target = other.transform; }
}
} }
private void Move_OnColliderStay2D(Collider2D other) protected virtual void Move_OnColliderStay2D(Collider2D other) {
{
if (other.gameObject.tag == "Player"){
}
} }
private void Move_OnColliderExit2D(Collider2D other) protected virtual void Move_OnColliderExit2D(Collider2D other) {
{ if (other.gameObject.tag == "Player"){
if (other.gameObject.tag == "Player"){ m_Animator.SetTrigger("idle");
m_Animator.SetTrigger("idle"); target = other.transform;
target = other.transform; target = null;
target = null; }
}
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment