Flocking

Over dit spel...

Omdat ik geen mooie vissen of vogel sprite kon vinden heb ik in plaats daarvan prachtige ruimteschepen gebruikt. Vind ik er toch eigenlijk veel cooler uit zien.

Ik heb deze behaviour gemaakt met behulp van deze tutorial, dus als je op zoek bent naar meer informatie hierover is deze pagina zeker aan te raden!

Na het volgen van de tutorial heb ik zelf nog een van kleur veranderende achtergrond en de mogelijkheid om de ruimte schepen te beïnvloeden. Alle controls staan uitgelegd in het spel!

De ruimteschip sprite komt van: www.kenney.nl

Engine: Unity/C#
Platform: Windows

Flocking.cs

using UnityEngine;
using System.Collections;

public class Flocking : MonoBehaviour
{
    GameObject[] _ships;
    Vector2 _velocity;
    Quaternion _rot;
    GameObject _mouseFollower;

    public void Setup(GameObject[] ships, GameObject mouseFollower)
    {
        _ships = ships;
        _mouseFollower = mouseFollower;
        _velocity = new Vector2(Random.Range(0, 5), Random.Range(0, 5));
    }

    // Update is called once per frame
    void Update()
    {
        Vector2 alignment = CalculateAlignment();
        Vector2 cohesion = CalculateCohesion();
        Vector2 seperation = CalculateSeperation();
        Vector2 collision = CalculateCollision();

        Vector2 seperationFollower = CalculateSeperationFollower();

        _velocity += alignment + cohesion + seperation + seperationFollower;

        //_velocity += collision;
        _velocity.Normalize();
        if (_velocity != Vector2.zero)
        {
            float angle = Mathf.Atan2(_velocity.y, _velocity.x) * Mathf.Rad2Deg;
            _rot = Quaternion.AngleAxis(angle - 90, Vector3.forward);
        }
        transform.rotation = Quaternion.Slerp(transform.rotation, _rot, 5 * Time.deltaTime);

        //Collision();
        transform.position += new Vector3(_velocity.x, _velocity.y) * Time.deltaTime;
    }

    Vector2 CalculateAlignment()
    {
        Vector2 vector = new Vector2();
        int neighbourCount = 0;
        foreach (GameObject ship in _ships)
        {
            if (ship != gameObject)
            {
                if (Vector3.Distance(transform.position, ship.transform.position) < 2)
                {
                    vector += ship.GetComponent()._velocity;
                    neighbourCount++;
                }
            }
        }

        if (neighbourCount == 0) return vector;
        else
        {
            vector.x /= neighbourCount;
            vector.y /= neighbourCount;
            vector.Normalize();
            return vector;
        }
    }

    Vector2 CalculateCohesion()
    {
        Vector2 vector = new Vector2();
        int neighbourCount = 0;
        foreach (GameObject ship in _ships)
        {
            if (ship != gameObject)
            {
                if (Vector3.Distance(transform.position, ship.transform.position) < 2)
                {
                    vector += new Vector2(ship.transform.position.x, ship.transform.position.y);
                    neighbourCount++;
                }
            }
        }

        if (neighbourCount == 0) return vector;
        else
        {
            vector /= neighbourCount;
            vector = new Vector2(vector.x - transform.position.x, vector.y - transform.position.y);
            vector.Normalize();
            return vector;
        }
    }

    Vector2 CalculateSeperation()
    {
        Vector2 vector = new Vector2();
        int neighbourCount = 0;
        foreach (GameObject ship in _ships)
        {
            if (ship != gameObject)
            {
                if (Vector3.Distance(transform.position, ship.transform.position) < 2)
                {
                    vector += new Vector2(transform.position.x, transform.position.y) - new Vector2(ship.transform.position.x, ship.transform.position.y);
                }
            }
        }

        if (neighbourCount == 0) return vector;
        else
        {
            vector /= neighbourCount;
            vector.Normalize();
            return vector;
        }
    }

    Vector2 CalculateSeperationFollower()
    {
        Vector2 vector = new Vector2();
        if ((_mouseFollower.GetComponent().seperationActive ||_mouseFollower.GetComponent().attractionActive) && Vector3.Distance(transform.position, _mouseFollower.transform.position) < _mouseFollower.GetComponent().distance)
        {
            vector += new Vector2(transform.position.x, transform.position.y) - new Vector2(_mouseFollower.transform.position.x, _mouseFollower.transform.position.y);
        }
        if (_mouseFollower.GetComponent().attractionActive) vector *= -1;
        vector.Normalize();
        return vector;
    }

    Vector2 CalculateCollision()
    {
        Vector2 vector = new Vector2(0, 0);
        if (transform.position.y > 6) vector = new Vector2(0, 1);
        if (transform.position.y < -6) vector = new Vector2(0, -1);
        if (transform.position.x > 10) vector = new Vector2(-1, 0);
        if (transform.position.x > -10) vector = new Vector2(1, 0);

        return vector;
    }
}

Downloads

Windows
Source