Hello,
I’m building a shoot’em up where a script called “Scene_Director” instantiates the waves of enemies at regular times.
Now, each single enemy of each single wave has to follow a path which will be different from another enemy of the same wave, so, I built a script called “PatternScript” which basically holds a bunch of methods which (depending on an argument I’m passing from the Scene_Director) will move the enemy through a path.
In code:
Scene_Director:
public class TestScene_DirectorScript : MonoBehaviour {
private float time;
private bool[] flags = new bool [10]; // tool for controlling waves
public Transform enemy1_prefab;
// Use this for initialization
void Start () {
time = 0;
for (int i = 0; i < 10; i++)
flags [i] = false;
}
// Update is called once per frame
void Update () {
time += Time.deltaTime;
if (time > 1.0f && flags[0] == false)
{
flags [0] = true;
WaveOne ();
}
}
void WaveOne()
{
var spawn = Instantiate (enemy1_prefab) as Transform;
spawn.position = new Vector3 (-2.0f, 4.0f, 5.0f);
MovePatternScript mvs = spawn.GetComponent<PatternScript> ();
mvs.SetTargetPos (new Vector3(-2.0f, 0.0f, 5.0f));
mvs.SetPattern (1);
spawn = Instantiate (enemy1_prefab) as Transform;
spawn.position = new Vector3 (2.0f, 4.0f, 5.0f);
mvs = spawn.GetComponent<PatternScript> ();
mvs.SetTargetPos (new Vector3(-2.0f, 0.0f, 5.0f));
mvs.SetPattern (1);
}
}
PatternScript:
public class PatternScript : MonoBehaviour {
public int pattern = 0;
private bool initializated;
private Vector3 v;
private Vector3 movement;
public Vector3 targetPos;
// Use this for initialization
void Start () {
targetPos = new Vector3 (0.0f, 0.0f, 5.0f);
}
// Update is called once per frame
void Update () {
switch (pattern)
{
case 1:
PatternOne ();
break;
case 2:
break;
case 3:
break;
case 4:
break;
}
}
public void SetPattern(int n)
{
pattern = n;
}
public void SetTargetPos(Vector3 x)
{
targetPos = x;
}
void PatternOne()
{
// Example, it might also be a Bezier Curve
transform.position = Vector3.MoveTowards (transform.position, targetPos, 0.05f);
}
}
You see how TEDIOUS it is? The Scene_Director has, for each enemy, grab the Pattern_Script, point the target position and which pattern to use. Also, you need to write at least 5 lines of code for each enemy, while a wave usually holds 10+ enemies. Plus, that is the easiest pattern, try to figure out how to describe a Bezier Curve.
In C++ it would be extremely easy by having a custom constructor:
Enemy::Enemy(glm::vec3 startPos, glm::vec3 endPos, int pattern)
{
// spawn the enemy at startPos, bring it to endPos using pattern
}
So you can call:
Enemy* e = new Enemy (glm::vec3, glm::vec3, int)
Finally, the question is: what solution do you suggest to shorten and optimize the code for Scene_Director/Pattern_Script by also keeping an eye to abstraction?
Sorry for the long post, thanks in advance.