An Entity-Colony Framework WIP

Hi Unity Community

I would like to share a framework that has been on my mind for quite some time while working inside of Unity and I would appreciate any input you might have. What I am most curious about is whether you have any use for such a framework and whether you would use it in your own projects. Even though I agree it is not perfect for every case you might have I reckon it presents a stable foundation.

The framework came from experimenting with an Ant game and requiring communication among the Colony and Ant and vice versa. So if any of you out there would appreciate such a framework as much as I do then you can check out my personal GitHub here for all the source and examples https://github.com/turboignited/Entity-Colony-Framework-for-Unity-C-. Feel free to contribute if you have any ideas. Thank you very much. Darren

Here is a screenshot showing the different entities resembling colony behaviour.

Here is the Ant class that extends Entity as an example:

using UnityEngine;
using UnityEngine.AI;

public class Ant : Entity
{
    [SerializeField]
    private Transform _foodTransform;

    private Material _trailMaterial;

    public Color TrailColor {
        set { _trailMaterial.SetColor("_EmissionColor", value); }
    }

    public override void Start()
    {
        _trailMaterial = GetComponent<TrailRenderer>().material;
        Movement = new NavMeshMovement(GetComponent<NavMeshAgent>());
    }

    public override void OnEntityStateTransition(EntityState entityState)
    {
        base.OnEntityStateTransition(entityState);
        switch (entityState)
        {
            case EntityState.Idle:
                switch (EntityType)
                {
                    case EntityType.Leader:
                        Destination = Colony.SpawnPosition;
                        break;
                    case EntityType.Forager:
                        if (_foodTransform.childCount > 0)
                        {
                            Destination = Colony.GetEntityOfType(EntityType.Defender).Position;
                        }
                        else
                        {
                            Destination = Colony.PositionQueued ? Colony.QueuedPosition : Colony.MovePosition;
                        }
                        break;
                    case EntityType.Defender:
                        if (_foodTransform.childCount > 0)
                        {
                            Destination = Colony.GetEntityOfType(EntityType.Leader).Position;
                        }
                        else
                        {
                            Destination = Colony.MovePosition;
                            TrailColor = Color.white;
                        }
                        break;
                    case EntityType.Attacker:
                        Destination = Colony.MovePosition;
                        TrailColor = Color.white;
                        break;
                    default:
                        break;
                }
                break;
            case EntityState.Moving:
                break;
            case EntityState.Invisible:
                break;
            case EntityState.Visible:
                break;
            default:
                break;
        }
    }

    public override void OnFoodTrigger(Transform foodTransform)
    {
        switch (EntityType)
        {
            case EntityType.Leader:
                DetachFoodTransform(foodTransform);
                Colony.SpawnEntityOfType(EntityType.Defender);
                break;
            case EntityType.Forager:
                if (_foodTransform.childCount == 0)
                {
                    AttachFoodTransform(foodTransform);
                    Destination = Colony.GetEntityOfType(EntityType.Defender).Position;
                    TrailColor = Color.green;
                }
                break;
            case EntityType.Defender:
                if (_foodTransform.childCount == 0 && foodTransform.parent == null)
                {
                    AttachFoodTransform(foodTransform);
                    Destination = Colony.GetEntityOfType(EntityType.Leader).Position;
                    TrailColor = Color.blue;
                }
                break;
            case EntityType.Attacker:
                if (foodTransform.parent == null)
                {
                    Colony.QueuedPosition = foodTransform.position;
                }
                break;
            default:
                break;
        }
    }

    private void AttachFoodTransform(Transform foodTransform)
    {
        foodTransform.parent = _foodTransform;
        foodTransform.position = _foodTransform.position;
        foodTransform.rotation = _foodTransform.rotation;
    }

    private void DetachFoodTransform(Transform foodTransform)
    {
        foodTransform.parent = null;
        foodTransform.position = Colony.MovePosition;
    }
}

Here is the AntColony class that extends Colony as an example:

public class AntColony : Colony
{
    private void Start()
    {
        SpawnEntityOfType(EntityType.Leader);
        SpawnEntityOfType(EntityType.Forager);
        SpawnEntityOfType(EntityType.Defender);
        SpawnEntityOfType(EntityType.Attacker);
    }
}

I’m actually a big fan of ants. They’re so efficient and interesting in solving their problems, in many different ways even on a per-species basis (same problem - completely different solution). Just amazing.

Should have called it ANTity-Colony Framework. :smile:

Actually, your name has made me have a look at this thread, you didn’t find a shorter one, did you? :smile:

I might have a look at it when I’ve got more spare time as I’ve thought about doing something similar quite a few times.

There is some good ant games out there but none which really fill the void of actually going out and seeing ants in the real world. I would love to see more. Especially after seeing Unity’s demo for 1,000,000 fishes on screen I’m sure seeing ant’s in this capacity would provide much more interest as opposed to 100 ants.