How to do it...

We will work on two classes: the enemy generator containing the evolutionary algorithm and the enemy controller for handling the real-time instances from the enemy templates created in the previous section. We'll also use some game logic.

We need to define the controller for such a template with EvolEnemyController:

  1. Create the class named EvolEnemyController:
using UnityEngine;
using System;
using System.Collections;

public class EvolEnemyController :
MonoBehaviour, IComparable<EvolEnemyController>
{
// next steps
}
  1. Define its member variables:
public static int counter = 0;
[HideInInspector]
public EvolEnemy template;
public float time;
protected Vector2 bounds;
protected SpriteRenderer _renderer;
protected BoxCollider2D _collider;
  1. Implement the member function from the IComparable interface:
public int CompareTo(EvolEnemyController other)
{
return other.time > time ? 0 : 1;
}
  1. Implement the initialization function:
public void Init(EvolEnemy template, Vector2 bounds)
{
this.template = template;
this.bounds = bounds;
Revive();
}

  1. Implement the function for reactivating the object:
public void Revive()
{
gameObject.SetActive(true);
counter++;
gameObject.name = "EvolEnemy" + counter;

Vector3 newPosition = UnityEngine.Random.insideUnitCircle;
newPosition *= bounds;
_renderer.sprite = template.sprite;
_collider = gameObject.AddComponent<BoxCollider2D>();
}
  1. Implement the Update function:
private void Update()
{
if (template == null)
return;
time += Time.deltaTime;
}
  1. Implement the function for defeating the enemy with a click:
private void OnMouseDown()
{
Destroy(_collider);
gameObject.SendMessageUpwards("KillEnemy", this);
}

Finally, we work on the enemy generator:

  1. Create a new class named EvolEnemyGenerator:
using UnityEngine;
using System.Collections.Generic;

public class EvolEnemyGenerator : MonoBehaviour
{
// next
}
  1. Declare all the necessary member variables:
public int mu;
public int lambda;
public int generations;
public GameObject prefab;
public Vector2 prefabBounds;
protected int gen;
private int total;
private int numAlive;
public EvolEnemy[] enemyList;
private List<EvolEnemyController> population;
  1. Implement the Start member function:
private void Start()
{
Init();
}
  1. Declare the initialization function:
public void Init()
{
// next steps
}
  1. Declare its internal variables:
gen = 0;
total = mu + lambda;
population = new List<EvolEnemyController>();
int i, x;
bool isRandom = total != enemyList.Length;
  1. Create the initial generation or wave of enemies:
for (i = 0; i < enemyList.Length; i++)
{
EvolEnemyController enemy;
enemy = Instantiate(prefab).GetComponent<EvolEnemyController>();
enemy.transform.parent = transform;
EvolEnemy template;
x = i;
if (isRandom)
x = Random.Range(0, enemyList.Length - 1);
template = enemyList[x];
enemy.Init(template, prefabBounds);
population.Add(enemy);
}

  1. Initialize the number of enemies alive relative to the size of population:
numAlive = population.Count;
  1. Define the function for creating a new generation:
public void CreateGeneration()
{
// next steps
}
  1. Check whether the number of generations created is greater than allowed:
if (gen > generations)
return;
  1. Sort the individuals in ascending order, and create a list of surviving individuals:
population.Sort();
List<EvolEnemy> templateList = new List<EvolEnemy>();
int i, x;
for (i = mu; i < population.Count; i++)
{
EvolEnemy template = population[i].template;
templateList.Add(template);
population[i].Revive();
}
  1. Create new individuals from the surviving types:
bool isRandom = templateList.Count != mu;
for (i = 0; i < mu; i++)
{
x = i;
if (isRandom)
x = Random.Range(0, templateList.Count - 1);
population[i].template = templateList[x];
population[i].Revive();
}
  1. Increase the number of generations, and reset the number of individuals alive:
gen++;
numAlive = population.Count;
  1. Implement the function for defeating enemies:
public void KillEnemy(EvolEnemyController enemy)
{
enemy.gameObject.SetActive(false);
numAlive--;
if (numAlive > 0)
return;
Invoke("CreateGeneration", 3f);
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.80.123