Refactoring
In this article, we will talk about Refactoring.
Refactoring is a code enhancement technique, its goal is to improve the code structure, without modifying the behaviors, thus improving your organization, and making maintenance easier.
For this article I will use my code named Enemy_2, and also the Enemy_1' code that I created in Article about DRY. If you do not readed, the link is this mauroubaldo.com/using-principle-dry/.
Now here is our class Enemy_1:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy_1 : MonoBehaviour
{
float _Speed_X = 5;
int _HP = 100;
bool _Right_Movementation;
bool _Left_Movementation;
Rigidbody2D _Rdb;
Vector2 _Right_Vector;
Vector2 _Left_Vector;
Health_System_Enemy _Health_System_Enemy_Script;
void Start()
{
_Right_Movementation = true;
_Rdb = GetComponent<Rigidbody2D>();
_Health_System_Enemy_Script = GetComponent<Health_System_Enemy>();
_Right_Vector = new Vector2(_Speed_X, _Rdb.velocity.y);
_Left_Vector = new Vector2(-_Speed_X, _Rdb.velocity.y);
}
void Update()
{
if (_Right_Movementation == true)
_Rdb.velocity = _Right_Vector;
if (_Left_Movementation == true)
_Rdb.velocity = _Left_Vector;
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.CompareTag("Left_Limit"))
{
_Right_Movementation = true;
_Left_Movementation = false;
}
if (collision.gameObject.CompareTag("Right_Limit"))
{
_Right_Movementation = false;
_Left_Movementation = true;
}
if (collision.gameObject.CompareTag("Player"))
{
_Health_System_Enemy_Script.Reduce_HP(15);
}
}
}
And here is our Enemy_2:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy_2 : MonoBehaviour
{
float _Speed_X = 5;
float _Speed_Y = 5;
float _Time;
int _HP = 100;
bool _Right_Movementation;
bool _Left_Movementation;
bool _Up_Movementation;
bool _Down_Movementation;
Rigidbody2D _Rdb;
Vector2 _Right_Up_Diagonal;
Vector2 _Right_Down_Diagonal;
Vector2 _Left_Up_Diagonal;
Vector2 _Left_Down_Diagonal;
Health_System_Enemy _Health_System_Enemy_Script;
void Start()
{
_Left_Movementation = true;
_Up_Movementation = true;
_Rdb = GetComponent<Rigidbody2D>();
_Health_System_Enemy_Script = GetComponent<Health_System_Enemy>();
_Right_Up_Diagonal = new Vector2(_Speed_X, _Speed_Y);
_Right_Down_Diagonal = new Vector2(_Speed_X, -_Speed_Y);
_Left_Up_Diagonal = new Vector2(-_Speed_X, _Speed_Y);
_Left_Down_Diagonal = new Vector2(-_Speed_X, -_Speed_Y);
}
void Update()
{
if (_Right_Movementation == true && _Up_Movementation == true)
_Rdb.velocity = _Right_Up_Diagonal;
if (_Right_Movementation == true && _Down_Movementation == true)
_Rdb.velocity = _Right_Down_Diagonal;
if (_Left_Movementation == true && _Up_Movementation == true)
_Rdb.velocity = _Left_Up_Diagonal;
if (_Left_Movementation == true && _Down_Movementation == true)
_Rdb.velocity = _Left_Down_Diagonal;
_Time += Time.deltaTime;
if(_Time > 5)
{
_Time = 0;
if(_Up_Movementation == true)
{
Change_For_Down_Movementation();
}
if (_Down_Movementation == true)
{
Change_For_Up_Movementation();
}
if(_Left_Movementation == true)
{
Change_For_Right_Movementation();
}
if (_Right_Movementation == true)
{
Change_For_Left_Movementation();
}
}
}
void Change_For_Up_Movementation()
{
_Up_Movementation = true;
_Down_Movementation = false;
}
void Change_For_Down_Movementation()
{
_Up_Movementation = false;
_Down_Movementation = true;
}
void Change_For_Left_Movementation()
{
_Left_Movementation = true;
_Right_Movementation = false;
}
void Change_For_Right_Movementation()
{
_Left_Movementation = false;
_Right_Movementation = true;
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.CompareTag("Left_Limit"))
{
Change_For_Right_Movementation();
}
if (collision.gameObject.CompareTag("Right_Limit"))
{
Change_For_Left_Movementation();
}
if (collision.gameObject.CompareTag("Up_Limit"))
{
Change_For_Down_Movementation();
}
if (collision.gameObject.CompareTag("Down_Limit"))
{
Change_For_Up_Movementation();
}
if (collision.gameObject.CompareTag("Player"))
{
_Health_System_Enemy_Script.Reduce_HP(15);
}
}
}
For this case, I will create a class for each behavior, and in the final, I will create a class for to make managing behaviors of Enemies.
Now that We saw our classes, Let’s go.
Firstly, I will start with the Enemy class 1. So, our Enemy 1 has the following behaviors: Walk to the right side and to the left side, and He also causes damage to the Player, as I created the Damage class, I will create just the walking class, here is our new class:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Walk_Enemy : MonoBehaviour
{
float _Speed_X = 5;
Rigidbody2D _Rdb;
Vector2 _Right_Vector;
Vector2 _Left_Vector;
void Start()
{
_Right_Vector = new Vector2(_Speed_X, _Rdb.velocity.y);
_Left_Vector = new Vector2(-_Speed_X, _Rdb.velocity.y);
_Rdb = GetComponent<Rigidbody2D>();
}
public void Right_Movementation()
{
_Rdb.velocity = _Right_Vector;
}
public void Left_Movementation()
{
_Rdb.velocity = _Left_Vector;
}
}
As you can see, this class has a unique responsibility, which is to make Enemy_1 walk, our damage class was created in the DRY article, but I will show the class here as well.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Health_System_Enemy : MonoBehaviour
{
int _HP = 100;
//This method is used in Enemy_1 Class, and also Enemy_2 Class
public void Reduce_HP(int Damage)
{
_HP -= Damage;
if (_HP <= 0)
print("Die");
}
}
Now I will modify our class Enemy_1 for she to only manage the behaviors, but before I need to clear Enemy_1 class, because the class has many code without utility, she is dirty and with unnecessary code. I will show our Enemy_1 class before and after the changes.
Enemy_1 Before the changes:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy_1 : MonoBehaviour
{
float _Speed_X = 5;
int _HP = 100;
bool _Right_Movementation;
bool _Left_Movementation;
Rigidbody2D _Rdb;
Vector2 _Right_Vector;
Vector2 _Left_Vector;
Health_System_Enemy _Health_System_Enemy_Script;
void Start()
{
_Right_Movementation = true;
_Rdb = GetComponent<Rigidbody2D>();
_Health_System_Enemy_Script = GetComponent<Health_System_Enemy>();
_Right_Vector = new Vector2(_Speed_X, _Rdb.velocity.y);
_Left_Vector = new Vector2(-_Speed_X, _Rdb.velocity.y);
}
void Update()
{
if (_Right_Movementation == true)
_Rdb.velocity = _Right_Vector;
if (_Left_Movementation == true)
_Rdb.velocity = _Left_Vector;
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.CompareTag("Left_Limit"))
{
_Right_Movementation = true;
_Left_Movementation = false;
}
if (collision.gameObject.CompareTag("Right_Limit"))
{
_Right_Movementation = false;
_Left_Movementation = true;
}
if (collision.gameObject.CompareTag("Player"))
{
_Health_System_Enemy_Script.Reduce_HP(15);
}
}
}
After the changes:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy_1 : MonoBehaviour
{
bool _Right_Movementation;
bool _Left_Movementation;
Walk_Enemy _Walk_Left_And_Right_Side_Script;
Health_System_Enemy _Health_System_Enemy_Script;
void Start()
{
_Health_System_Enemy_Script = GetComponent<Health_System_Enemy>();
_Walk_Left_And_Right_Side_Script = GetComponent<Walk_Enemy>();
}
void Update()
{
if (_Right_Movementation == true)
_Walk_Left_And_Right_Side_Script.Right_Movementation();
if (_Left_Movementation == true)
_Walk_Left_And_Right_Side_Script.Left_Movementation();
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.CompareTag("Left_Limit"))
{
_Right_Movementation = true;
_Left_Movementation = false;
}
if (collision.gameObject.CompareTag("Right_Limit"))
{
_Right_Movementation = false;
_Left_Movementation = true;
}
if (collision.gameObject.CompareTag("Player"))
{
_Health_System_Enemy_Script.Reduce_HP(15);
}
}
}
Repair that I removed all the unnecessary code, and I kept only motion control movement variables, because the Enemy_1 class only will to manage the behaviors, she will control movement and to apply damage.
As this is a very simple class, all the modifies look small, but in the Enemy_2 class, the changes will be more expressive.
I modified this class because for to show better the changes.
So Let’s go to Enemy_2 Class:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy_2 : MonoBehaviour
{
float _Speed_X = 5;
float _Speed_Y = 5;
float _Time;
int _HP = 100;
int _Bullet_Counter;
bool _Follow_Autorization;
bool _Fire_Autorization;
bool _Right_Movementation;
bool _Left_Movementation;
bool _Up_Movementation;
bool _Down_Movementation;
Rigidbody2D _Rdb;
Vector2 _Right_Up_Diagonal;
Vector2 _Right_Down_Diagonal;
Vector2 _Left_Up_Diagonal;
Vector2 _Left_Down_Diagonal;
public GameObject _Bullet;
public Transform _Cannon;
Health_System_Enemy _Health_System_Enemy_Script;
void Start()
{
_Left_Movementation = true;
_Up_Movementation = true;
_Rdb = GetComponent<Rigidbody2D>();
_Health_System_Enemy_Script = GetComponent<Health_System_Enemy>();
_Right_Up_Diagonal = new Vector2(_Speed_X, _Speed_Y);
_Right_Down_Diagonal = new Vector2(_Speed_X, -_Speed_Y);
_Left_Up_Diagonal = new Vector2(-_Speed_X, _Speed_Y);
_Left_Down_Diagonal = new Vector2(-_Speed_X, -_Speed_Y);
}
void Update()
{
if (_Right_Movementation == true && _Up_Movementation == true)
_Rdb.velocity = _Right_Up_Diagonal;
if (_Right_Movementation == true && _Down_Movementation == true)
_Rdb.velocity = _Right_Down_Diagonal;
if (_Left_Movementation == true && _Up_Movementation == true)
_Rdb.velocity = _Left_Up_Diagonal;
if (_Left_Movementation == true && _Down_Movementation == true)
_Rdb.velocity = _Left_Down_Diagonal;
_Time += Time.deltaTime;
if(_Time > 5)
{
_Time = 0;
if(_Up_Movementation == true)
{
Change_For_Down_Movementation();
}
if (_Down_Movementation == true)
{
Change_For_Up_Movementation();
}
if(_Left_Movementation == true)
{
Change_For_Right_Movementation();
}
if (_Right_Movementation == true)
{
Change_For_Left_Movementation();
}
}
}
void Add_Value_In_Bullet_Counter()
{
_Bullet_Counter++;
}
void Shoot()
{
//Create bullet
Instantiate(_Bullet, _Cannon.transform.position, Quaternion.identity);
Add_Value_In_Bullet_Counter();
}
void Follow()
{
//To make the enemy follow player
}
void Change_For_Up_Movementation()
{
_Up_Movementation = true;
_Down_Movementation = false;
}
void Change_For_Down_Movementation()
{
_Up_Movementation = false;
_Down_Movementation = true;
}
void Change_For_Left_Movementation()
{
_Left_Movementation = true;
_Right_Movementation = false;
}
void Change_For_Right_Movementation()
{
_Left_Movementation = false;
_Right_Movementation = true;
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.CompareTag("Left_Limit"))
{
Change_For_Right_Movementation();
}
if (collision.gameObject.CompareTag("Right_Limit"))
{
Change_For_Left_Movementation();
}
if (collision.gameObject.CompareTag("Up_Limit"))
{
Change_For_Down_Movementation();
}
if (collision.gameObject.CompareTag("Down_Limit"))
{
Change_For_Up_Movementation();
}
if (collision.gameObject.CompareTag("Player"))
{
_Health_System_Enemy_Script.Reduce_HP(15);
}
}
}
So, We can to see that, exist many useless code, as methods: Add_Value_In_Bullet_Counter, Shoot, Follow, and as variables: _Follow_Autorization, _Fire_Autorization, _Bullet_Counter, _HP, _Bullet, _Cannon.
Well, Let’s go, firstly I will start removing useless code, now this is our class:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy_2 : MonoBehaviour
{
float _Speed_X = 5;
float _Speed_Y = 5;
float _Time;
bool _Right_Movementation;
bool _Left_Movementation;
bool _Up_Movementation;
bool _Down_Movementation;
Rigidbody2D _Rdb;
Vector2 _Right_Up_Diagonal;
Vector2 _Right_Down_Diagonal;
Vector2 _Left_Up_Diagonal;
Vector2 _Left_Down_Diagonal;
Health_System_Enemy _Health_System_Enemy_Script;
void Start()
{
_Left_Movementation = true;
_Up_Movementation = true;
_Rdb = GetComponent<Rigidbody2D>();
_Health_System_Enemy_Script = GetComponent<Health_System_Enemy>();
_Right_Up_Diagonal = new Vector2(_Speed_X, _Speed_Y);
_Right_Down_Diagonal = new Vector2(_Speed_X, -_Speed_Y);
_Left_Up_Diagonal = new Vector2(-_Speed_X, _Speed_Y);
_Left_Down_Diagonal = new Vector2(-_Speed_X, -_Speed_Y);
}
void Update()
{
if (_Right_Movementation == true && _Up_Movementation == true)
_Rdb.velocity = _Right_Up_Diagonal;
if (_Right_Movementation == true && _Down_Movementation == true)
_Rdb.velocity = _Right_Down_Diagonal;
if (_Left_Movementation == true && _Up_Movementation == true)
_Rdb.velocity = _Left_Up_Diagonal;
if (_Left_Movementation == true && _Down_Movementation == true)
_Rdb.velocity = _Left_Down_Diagonal;
_Time += Time.deltaTime;
if(_Time > 5)
{
_Time = 0;
if(_Up_Movementation == true)
{
Change_For_Down_Movementation();
}
if (_Down_Movementation == true)
{
Change_For_Up_Movementation();
}
if(_Left_Movementation == true)
{
Change_For_Right_Movementation();
}
if (_Right_Movementation == true)
{
Change_For_Left_Movementation();
}
}
}
void Change_For_Up_Movementation()
{
_Up_Movementation = true;
_Down_Movementation = false;
}
void Change_For_Down_Movementation()
{
_Up_Movementation = false;
_Down_Movementation = true;
}
void Change_For_Left_Movementation()
{
_Left_Movementation = true;
_Right_Movementation = false;
}
void Change_For_Right_Movementation()
{
_Left_Movementation = false;
_Right_Movementation = true;
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.CompareTag("Left_Limit"))
{
Change_For_Right_Movementation();
}
if (collision.gameObject.CompareTag("Right_Limit"))
{
Change_For_Left_Movementation();
}
if (collision.gameObject.CompareTag("Up_Limit"))
{
Change_For_Down_Movementation();
}
if (collision.gameObject.CompareTag("Down_Limit"))
{
Change_For_Up_Movementation();
}
if (collision.gameObject.CompareTag("Player"))
{
_Health_System_Enemy_Script.Reduce_HP(15);
}
}
}
But, You can see that the class is very big, Update method has 3 different functionalities, and this is not good, so now I will change this. For this case, the correct way is the same as Enemy_1, our class Enemy_2 only will to manage behaviors, for the sake of good practice’s question, I will add our movement in our Walk_Enemy_Script.
Now here is our Walk_Enemy_Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Walk_Enemy : MonoBehaviour
{
float _Speed_X = 5;
float _Speed_Y = 5;
Rigidbody2D _Rdb;
Vector2 _Right_Vector;
Vector2 _Left_Vector;
Vector2 _Right_Up_Diagonal;
Vector2 _Right_Down_Diagonal;
Vector2 _Left_Up_Diagonal;
Vector2 _Left_Down_Diagonal;
void Start()
{
_Right_Vector = new Vector2(_Speed_X, _Rdb.velocity.y);
_Left_Vector = new Vector2(-_Speed_X, _Rdb.velocity.y);
_Right_Up_Diagonal = new Vector2(_Speed_X, _Speed_Y);
_Right_Down_Diagonal = new Vector2(_Speed_X, -_Speed_Y);
_Left_Up_Diagonal = new Vector2(-_Speed_X, _Speed_Y);
_Left_Down_Diagonal = new Vector2(-_Speed_X, -_Speed_Y);
_Rdb = GetComponent<Rigidbody2D>();
}
public void Right_Movementation()
{
_Rdb.velocity = _Right_Vector;
}
public void Left_Movementation()
{
_Rdb.velocity = _Left_Vector;
}
public void Right_Up_Diagonal()
{
_Rdb.velocity = _Right_Up_Diagonal;
}
public void Right_Down_Diagonal()
{
_Rdb.velocity = _Right_Down_Diagonal;
}
public void Left_Up_Diagonal()
{
_Rdb.velocity = _Left_Up_Diagonal;
}
public void Left_Down_Diagonal()
{
_Rdb.velocity = _Left_Down_Diagonal;
}
}
Now, I will to remove the walk behavior in the Enemy_2, so for this.
This is our class:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy_2 : MonoBehaviour
{
float _Time;
bool _Right_Movementation;
bool _Left_Movementation;
bool _Up_Movementation;
bool _Down_Movementation;
Health_System_Enemy _Health_System_Enemy_Script;
void Start()
{
_Left_Movementation = true;
_Up_Movementation = true;
_Health_System_Enemy_Script = GetComponent<Health_System_Enemy>();
}
void Update()
{
if (_Right_Movementation == true && _Up_Movementation == true)
if (_Right_Movementation == true && _Down_Movementation == true)
if (_Left_Movementation == true && _Up_Movementation == true)
if (_Left_Movementation == true && _Down_Movementation == true)
_Time += Time.deltaTime;
if(_Time > 5)
{
_Time = 0;
if(_Up_Movementation == true)
{
Change_For_Down_Movementation();
}
if (_Down_Movementation == true)
{
Change_For_Up_Movementation();
}
if(_Left_Movementation == true)
{
Change_For_Right_Movementation();
}
if (_Right_Movementation == true)
{
Change_For_Left_Movementation();
}
}
}
void Change_For_Up_Movementation()
{
_Up_Movementation = true;
_Down_Movementation = false;
}
void Change_For_Down_Movementation()
{
_Up_Movementation = false;
_Down_Movementation = true;
}
void Change_For_Left_Movementation()
{
_Left_Movementation = true;
_Right_Movementation = false;
}
void Change_For_Right_Movementation()
{
_Left_Movementation = false;
_Right_Movementation = true;
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.CompareTag("Left_Limit"))
{
Change_For_Right_Movementation();
}
if (collision.gameObject.CompareTag("Right_Limit"))
{
Change_For_Left_Movementation();
}
if (collision.gameObject.CompareTag("Up_Limit"))
{
Change_For_Down_Movementation();
}
if (collision.gameObject.CompareTag("Down_Limit"))
{
Change_For_Up_Movementation();
}
if (collision.gameObject.CompareTag("Player"))
{
_Health_System_Enemy_Script.Reduce_HP(15);
}
}
}
Now I will to insert the walk movimentation using our class Walk_Enemy.
So here is our class:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy_2 : MonoBehaviour
{
float _Time;
bool _Right_Movementation;
bool _Left_Movementation;
bool _Up_Movementation;
bool _Down_Movementation;
Health_System_Enemy _Health_System_Enemy_Script;
Walk_Enemy _Walk_Enemy_Script;
void Start()
{
_Left_Movementation = true;
_Up_Movementation = true;
_Health_System_Enemy_Script = GetComponent<Health_System_Enemy>();
_Walk_Enemy_Script = GetComponent<Walk_Enemy>();
}
void Update()
{
if (_Right_Movementation == true && _Up_Movementation == true)
_Walk_Enemy_Script.Right_Up_Diagonal();
if (_Right_Movementation == true && _Down_Movementation == true)
_Walk_Enemy_Script.Right_Down_Diagonal();
if (_Left_Movementation == true && _Up_Movementation == true)
_Walk_Enemy_Script.Left_Up_Diagonal();
if (_Left_Movementation == true && _Down_Movementation == true)
_Walk_Enemy_Script.Left_Down_Diagonal();
_Time += Time.deltaTime;
if(_Time > 5)
{
_Time = 0;
if(_Up_Movementation == true)
{
Change_For_Down_Movementation();
}
if (_Down_Movementation == true)
{
Change_For_Up_Movementation();
}
if(_Left_Movementation == true)
{
Change_For_Right_Movementation();
}
if (_Right_Movementation == true)
{
Change_For_Left_Movementation();
}
}
}
void Change_For_Up_Movementation()
{
_Up_Movementation = true;
_Down_Movementation = false;
}
void Change_For_Down_Movementation()
{
_Up_Movementation = false;
_Down_Movementation = true;
}
void Change_For_Left_Movementation()
{
_Left_Movementation = true;
_Right_Movementation = false;
}
void Change_For_Right_Movementation()
{
_Left_Movementation = false;
_Right_Movementation = true;
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.CompareTag("Left_Limit"))
{
Change_For_Right_Movementation();
}
if (collision.gameObject.CompareTag("Right_Limit"))
{
Change_For_Left_Movementation();
}
if (collision.gameObject.CompareTag("Up_Limit"))
{
Change_For_Down_Movementation();
}
if (collision.gameObject.CompareTag("Down_Limit"))
{
Change_For_Up_Movementation();
}
if (collision.gameObject.CompareTag("Player"))
{
_Health_System_Enemy_Script.Reduce_HP(15);
}
}
}
Looks better, but still not is good, because in Update method has many things happening, now I will change this.
Firstly is necessary to understand what this method is making.
Our variable _Time is receiving increment, after this, a verification is made, _Time receives zero, and the behavior is changed.
So I will to create a method for making the increment, and another for changing the behavior.
This is our class after to make the changes:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy_2 : MonoBehaviour
{
float _Time;
bool _Right_Movementation;
bool _Left_Movementation;
bool _Up_Movementation;
bool _Down_Movementation;
Health_System_Enemy _Health_System_Enemy_Script;
Walk_Enemy _Walk_Enemy_Script;
void Start()
{
_Left_Movementation = true;
_Up_Movementation = true;
_Health_System_Enemy_Script = GetComponent<Health_System_Enemy>();
_Walk_Enemy_Script = GetComponent<Walk_Enemy>();
}
void Update()
{
if (_Right_Movementation == true && _Up_Movementation == true)
_Walk_Enemy_Script.Right_Up_Diagonal();
if (_Right_Movementation == true && _Down_Movementation == true)
_Walk_Enemy_Script.Right_Down_Diagonal();
if (_Left_Movementation == true && _Up_Movementation == true)
_Walk_Enemy_Script.Left_Up_Diagonal();
if (_Left_Movementation == true && _Down_Movementation == true)
_Walk_Enemy_Script.Left_Down_Diagonal();
Manage_Timer();
}
void Manage_Timer()
{
_Time += Time.deltaTime;
if (_Time > 5)
{
_Time = 0;
Change_Behavior();
}
}
void Change_Behavior()
{
if (_Up_Movementation == true)
{
Change_For_Down_Movementation();
}
if (_Down_Movementation == true)
{
Change_For_Up_Movementation();
}
if (_Left_Movementation == true)
{
Change_For_Right_Movementation();
}
if (_Right_Movementation == true)
{
Change_For_Left_Movementation();
}
}
void Change_For_Up_Movementation()
{
_Up_Movementation = true;
_Down_Movementation = false;
}
void Change_For_Down_Movementation()
{
_Up_Movementation = false;
_Down_Movementation = true;
}
void Change_For_Left_Movementation()
{
_Left_Movementation = true;
_Right_Movementation = false;
}
void Change_For_Right_Movementation()
{
_Left_Movementation = false;
_Right_Movementation = true;
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.CompareTag("Left_Limit"))
{
Change_For_Right_Movementation();
}
if (collision.gameObject.CompareTag("Right_Limit"))
{
Change_For_Left_Movementation();
}
if (collision.gameObject.CompareTag("Up_Limit"))
{
Change_For_Down_Movementation();
}
if (collision.gameObject.CompareTag("Down_Limit"))
{
Change_For_Up_Movementation();
}
if (collision.gameObject.CompareTag("Player"))
{
_Health_System_Enemy_Script.Reduce_HP(15);
}
}
}
Now, all changes have been made, but for a legibility question, I will remove the { } in conditionals that have only one instruction.
So here is our new Enemy_2 class with all changes finished:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy_2 : MonoBehaviour
{
float _Time;
bool _Right_Movementation;
bool _Left_Movementation;
bool _Up_Movementation;
bool _Down_Movementation;
Health_System_Enemy _Health_System_Enemy_Script;
Walk_Enemy _Walk_Enemy_Script;
void Start()
{
_Left_Movementation = true;
_Up_Movementation = true;
_Health_System_Enemy_Script = GetComponent<Health_System_Enemy>();
_Walk_Enemy_Script = GetComponent<Walk_Enemy>();
}
void Update()
{
if (_Right_Movementation == true && _Up_Movementation == true)
_Walk_Enemy_Script.Right_Up_Diagonal();
if (_Right_Movementation == true && _Down_Movementation == true)
_Walk_Enemy_Script.Right_Down_Diagonal();
if (_Left_Movementation == true && _Up_Movementation == true)
_Walk_Enemy_Script.Left_Up_Diagonal();
if (_Left_Movementation == true && _Down_Movementation == true)
_Walk_Enemy_Script.Left_Down_Diagonal();
Manage_Timer();
}
void Manage_Timer()
{
_Time += Time.deltaTime;
if (_Time > 5)
{
_Time = 0;
Change_Behavior();
}
}
void Change_Behavior()
{
if (_Up_Movementation == true)
Change_For_Down_Movementation();
if (_Down_Movementation == true)
Change_For_Up_Movementation();
if (_Left_Movementation == true)
Change_For_Right_Movementation();
if (_Right_Movementation == true)
Change_For_Left_Movementation();
}
void Change_For_Up_Movementation()
{
_Up_Movementation = true;
_Down_Movementation = false;
}
void Change_For_Down_Movementation()
{
_Up_Movementation = false;
_Down_Movementation = true;
}
void Change_For_Left_Movementation()
{
_Left_Movementation = true;
_Right_Movementation = false;
}
void Change_For_Right_Movementation()
{
_Left_Movementation = false;
_Right_Movementation = true;
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.CompareTag("Left_Limit"))
Change_For_Right_Movementation();
if (collision.gameObject.CompareTag("Right_Limit"))
Change_For_Left_Movementation();
if (collision.gameObject.CompareTag("Up_Limit"))
Change_For_Down_Movementation();
if (collision.gameObject.CompareTag("Down_Limit"))
Change_For_Up_Movementation();
if (collision.gameObject.CompareTag("Player"))
_Health_System_Enemy_Script.Reduce_HP(15);
}
}
Very well, in this article, I talked about refactoring, a very important programming’s concept.
Follow-me in Instagram for stay knowing when I create new posts @mauro_developer
You can also leave your feedback, and your suggestion, I will read everything and answer your message
This project is uploaded in my github: github.com/MauroJrDeveloper/Refactoring-Article
Thank you for reading.