Exemple #1
0
    void Update()
    {
        // handle distance, isStarting
        distance += Time.deltaTime;
        if (distance > 2)
        {
            isStarting = false;
        }

        // find player and monster positions
        Vector3 playerV    = mazeStruct.Vector3FromSphereToCube(player.position.normalized * mazeStruct.radius);
        Vector3 monsterV   = mazeStruct.Vector3FromSphereToCube(transform.position.normalized * mazeStruct.radius);
        Point3  playerPos  = mazeStruct.FromCubeToGame(playerV);
        Point3  monsterPos = mazeStruct.FromCubeToGame(monsterV);

        // handle first path generation
        if (path == null || path.Length == 0)
        {
            CalcPath(playerPos, monsterPos);

            // insert 2 positions in front to make the monster rise
            Vector3[] temp = new Vector3[positions.Length + 2];
            for (int i = 0; i < positions.Length; ++i)
            {
                temp[i + 2] = positions[i];
            }
            for (int i = 1; i >= 0; --i)
            {
                temp[i] = temp[i + 1].normalized * (temp[i + 1].magnitude + 2 * mazeStruct.radius / mazeStruct.length);
            }
            temp[0]   = temp[2].normalized * (temp[2].magnitude + 2);
            temp[1]   = temp[2].normalized * (temp[2].magnitude + 1);
            positions = temp;
        }

        // if the monster and player are in the same cell
        if ((int)distance + 1 > path.Length)
        {
            Application.LoadLevel(Application.loadedLevelName);
            //throw new Exception("monster hit the player");
        }

        // handle player movement
        while (path[path.Length - 1] != playerPos && !isStarting)
        {
            float   dist  = distance;
            Vector3 prev1 = positions[(int)distance];
            Vector3 prev2 = positions[(int)distance + 1];
            CalcPath(playerPos, monsterPos);

            // handle each special case
            Vector3[] temp = new Vector3[positions.Length + 1];
            for (int i = 0; i < positions.Length; ++i)
            {
                temp[i + 1] = positions[i];
            }
            if (positions[0] == prev1)
            {
                if (positions[1] == prev2)
                {
                    distance = dist - Mathf.Floor(dist);
                    break;
                }
                temp[0]  = prev2;
                distance = Mathf.Ceil(dist) - dist;
                //Debug.Log("start at prev2");
            }
            else if (positions[0] == prev2)
            {
                if (positions[1] == prev1)
                {
                    distance = Mathf.Ceil(dist) - dist;
                    break;
                }
                temp[0]  = prev1;
                distance = dist - Mathf.Floor(dist);
                //Debug.Log("start at prev1");
            }
            else
            {
                throw new Exception("monster pathfinding error");
            }
            positions = temp;
            break;
        }

        // move the monster
        int   lower  = (int)distance;
        int   higher = lower + 1;
        float weight = (distance - lower);

        transform.position = ((1 - weight) * positions[lower] + weight * positions[higher]);

        // rotate the monster
        Vector3 up      = -transform.position.normalized;
        Vector3 forward = positions[higher] - positions[lower];

        transform.rotation = Quaternion.LookRotation(forward, up);

        // handle sight of the player
        bool  isInSight         = IsInSight();
        float newTimeSinceSight = (isInSight?0:timeSinceSight + Time.deltaTime);

        // handle sight being established
        if (isInSight && !wasInSight)
        {
            if (sounds.isPlaying)
            {
                sounds.Stop();
            }
            sounds.clip = sightEstab;
            sounds.Play();
            print("sight established");
        }
        // handle sight being lsot
        else if (sounds.isPlaying && sounds.clip == sightEstab && !isInSight)
        {
            sounds.Stop();
            print("sight lost");
        }
        // handle sight lost for too long
        if (newTimeSinceSight > 10 && timeSinceSight < 10)
        {
            sounds.clip = sightLost;
            sounds.Play();
            print("sight lost for 10 seconds");
        }
        // store sight vars
        wasInSight     = isInSight;
        timeSinceSight = newTimeSinceSight;
    }