|
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using CIGEnums;
- using SUISS.Core;
- using SUISS.Scheduling;
- using SUISSEngine;
- using UnityEngine;
-
- public class Boid : MonoBehaviour
- {
- private int totalChance
- {
- get
- {
- return this.seperateChance + this.alignChance + this.cohesionChance;
- }
- }
-
- private void Start()
- {
- SingletonMonobehaviour<Scheduler>.Instance.StartRoutine(this.moveBehaviour = this.MoveBehaviour());
- }
-
- private void OnDestroy()
- {
- Scheduler instanceIfAvailable = SingletonMonobehaviour<Scheduler>.InstanceIfAvailable;
- if (instanceIfAvailable != null && this.moveBehaviour != null)
- {
- instanceIfAvailable.StopRoutine(this.moveBehaviour);
- this.moveBehaviour = null;
- }
- }
-
- private IEnumerator MoveBehaviour()
- {
- for (;;)
- {
- GridPoint? gridPoint = this.globalTarget;
- if (gridPoint == null)
- {
- goto IL_7B;
- }
- GridPoint position = this.movingUnit.position;
- GridPoint? gridPoint2 = this.globalTarget;
- if (position.DistanceTo(gridPoint2.Value) < UnityEngine.Random.Range(1f, 4f))
- {
- goto IL_7B;
- }
- IL_9B:
- yield return Timing.time + (double)UnityEngine.Random.Range(2f, 5f);
- continue;
- IL_7B:
- this.globalTarget = null;
- this.ChangeDirection();
- goto IL_9B;
- }
- yield break;
- }
-
- private void ChangeDirection()
- {
- GridPoint gridPoint = new GridPoint(0f, 0f);
- GridPoint gridPoint2 = new GridPoint(0f, 0f);
- GridPoint gridPoint3 = new GridPoint(0f, 0f);
- foreach (Boid boid in this.animals)
- {
- if (!(boid == this))
- {
- GridPoint right = boid.movingUnit.position - this.movingUnit.position;
- if (right.u * right.u + right.v * right.v < this.seperationDistance * this.seperationDistance)
- {
- gridPoint -= right.times(1f / (float)this.animals.Count);
- }
- else
- {
- gridPoint3 += right;
- }
- gridPoint2 += boid.movingUnit.movement;
- }
- }
- int num = UnityEngine.Random.Range(0, this.totalChance);
- if (num < this.seperateChance)
- {
- this.movingUnit.SetTargetLocation(this.movingUnit.position + gridPoint);
- }
- else if (num < this.seperateChance + this.alignChance)
- {
- this.movingUnit.SetTargetLocation(this.movingUnit.position + gridPoint2);
- }
- else
- {
- this.movingUnit.SetTargetLocation(this.movingUnit.position + gridPoint3);
- }
- }
-
- public void SetHerd(List<Boid> an)
- {
- this.animals = an;
- }
-
- public void SetGlobalTarget(GridPoint t)
- {
- this.movingUnit.SetTargetLocation(t);
- this.globalTarget = new GridPoint?(t);
- }
-
- public static void RunAwayFrom(GridIndex t)
- {
- GridPoint gridPoint = new GridPoint(t);
- Boid[] array = UnityEngine.Object.FindObjectsOfType<Boid>();
- foreach (Boid boid in array)
- {
- IsometricGrid grid = boid.movingUnit.grid;
- float num = boid.movingUnit.position.DistanceTo(gridPoint);
- if (num <= 5f)
- {
- GridPoint gridPoint2 = gridPoint - boid.movingUnit.position;
- GridPoint point = boid.movingUnit.position - gridPoint2.normalized.times(5f);
- GridIndex index = new GridIndex(point);
- if (grid.IsWithinBounds(index) && boid.GetComponent<AttachedElementTypeList>().Check((SurfaceType)grid[index].Type))
- {
- boid.SetGlobalTarget(point);
- }
- }
- }
- }
-
- [SerializeField]
- private CIGMovingUnit movingUnit;
-
- public float seperationDistance = 1f;
-
- public int seperateChance = 15;
-
- public int alignChance = 3;
-
- public int cohesionChance = 10;
-
- private IEnumerator moveBehaviour;
-
- private GridPoint? globalTarget;
-
- private List<Boid> animals;
- }
|