Esempio n. 1
0
    // Update is called once per frame
    void Update()
    {
        //if (!HasPath) Debug.Log(Tile.VectorFromEntryPoint(Exit).normalized);
        if (!HasPath)
        {
            HasPath = UpdatePath(Tile.VectorFromEntryPoint(Exit).normalized);
        }
        if (!HasPath || awaiting)
        {
            float r = (RageIncreaseOverTime + RageIncreaseWhileStationary) * Time.deltaTime;
            Rage         += r;
            RageBar.rage += r;
        }
        if (!HasPath)
        {
            if (parentTile is Exit)
            {
                Exit ex = (Exit)parentTile;
                if (ex.Color == this.Color)
                {
                    //Despawn
                    Debug.Log("Despawning");
                    RageBar.rage -= Rage;

                    Destroy(gameObject);
                }
                else
                {
                    RageBar.rage += RageIncreaseWrongDestination;

                    SpawnLocation.AddToRespawnQueue(this);
                }
            }
            else if (parentTile is Spawn)
            {
                Spawn spawn = (Spawn)parentTile;
                if (spawn.Color == this.Color)
                {
                    //Despawn
                    Debug.Log("Despawning");
                    RageBar.rage -= Rage;

                    Destroy(gameObject);
                }
                else
                {
                    /*Debug.Log("Turning around");
                     * //Turn back
                     * //Reverse Path
                     * for (int i = 0; i < Path.Count / 2; ++i)
                     * {
                     *  Vector3 vec = Path[i];
                     *  Path[i] = Path[Path.Count - 1 - i];
                     *  Path[Path.Count - 1 - i] = vec;
                     *  next = 1;
                     * }
                     *
                     * //Change Exit
                     * foreach (var a in parentTile.Entries) Debug.Log(a);
                     *
                     * if (this.Exit == parentTile.Entries[0])
                     *  this.Exit = parentTile.Entries[1];
                     * else
                     *  this.Exit = parentTile.Entries[0];
                     * HasPath = true;*/

                    RageBar.rage += RageIncreaseWrongDestination;

                    SpawnLocation.AddToRespawnQueue(this);
                    //Destroy(gameObject);
                }
            }
            else if (parentTile is PotHole)
            {
                PotHole hole = (PotHole)parentTile;

                RageBar.rage += RageIncreasePotHole;

                SpawnLocation.AddToRespawnQueue(this);
                //Destroy(gameObject);
            }
            else
            {
                return;
            }
        }

        if (HasPath)
        {
            float r = RageIncreaseOverTime * Time.deltaTime;
            Rage         += r;
            RageBar.rage += r;
        }

        if (Path == null || Path.Count == 0)
        {
            return;
        }

        //Debug.Log($"Next: {next}, Path Count: {Path.Count}");

        Vector3 to  = Path[next] - transform.localPosition;
        Vector3 dir = to.normalized;
        float   mag = to.magnitude;

        float distTravelled = Time.deltaTime * Speed;

        while (distTravelled >= mag)
        {
            Transform otherKiwi = Cast(distTravelled - mag);

            if (otherKiwi != null)
            {
                awaiting = true;
                return;
                //transform.localPosition = transform.InverseTransformDirection(otherKiwi.position) - dir * SpaceBetweenKiwis;
                //distTravelled = 0;
                //break;
            }
            else
            {
                awaiting = false;
            }

            //distTravelled -= mag;
            distTravelled = 0;

            transform.localPosition = Path[next];

            next++;

            if (next >= Path.Count)
            {
                //Next tile
                //Update path
                HasPath = UpdatePath(dir);
                if (!HasPath)
                {
                    return;
                }
            }
            else
            {
                to  = Path[next] - transform.localPosition;
                dir = to.normalized;
                mag = to.magnitude;
            }
        }

        MovementDirection   = transform.TransformDirection(dir);
        MovementDirection.z = 0;

        //Update Rotation to point in Movement Direction
        ImageOrientation.rotation = Quaternion.FromToRotation(Vector2.right, MovementDirection);

        if (distTravelled > 0)
        {
            Transform otherKiwi = Cast(distTravelled);

            if (otherKiwi != null)
            {
                awaiting = true;
                return;
                //transform.localPosition = transform.InverseTransformPoint(otherKiwi.position) - dir * SpaceBetweenKiwis;
                //awaiting = otherKiwi;
                //lastPositionOfAwaiting = otherKiwi.position;
            }
            else
            {
                awaiting = false;
                transform.localPosition += dir * distTravelled;
            }
        }
    }