Esempio n. 1
0
    private void UpdatePosture(float timePassed)
    {
        if (Function == CreatureFunction.Dead)
        {
            return;
        }

        if (Function == CreatureFunction.GoLeft ||
            Function == CreatureFunction.GoRight)
        {
            return;
        }

        if (Function == CreatureFunction.BuildingBridge ||
            Function == CreatureFunction.BuildingStairUp ||
            Function == CreatureFunction.BuildingStairDown)
        {
            float buildingTimeDelta = Time.time - _buildingStartTime;
            if (buildingTimeDelta > _buildingDuration)
            {
                switch (Function)
                {
                case CreatureFunction.BuildingBridge:
                    Function = CreatureFunction.BuildBridge;
                    break;

                case CreatureFunction.BuildingStairUp:
                    Function = CreatureFunction.BuildStairUp;
                    break;

                case CreatureFunction.BuildingStairDown:
                    Function = CreatureFunction.BuildStairDown;
                    break;
                }
            }

            return;
        }

        if (Function == CreatureFunction.TransitionGoLeft)
        {
            Vector3 eulerDirection          = Quaternion.LookRotation(_direction).eulerAngles;
            float   eulerDirectionYRotation = eulerDirection.y;

            float rotationTimeDelta = Time.time - _rotateStartTime;
            float rotationDelta     = rotationTimeDelta * 180 / _rotate180Duration;
            if (rotationTimeDelta > _rotate180Duration)
            {
                rotationDelta = 180;
                Function      = CreatureFunction.GoLeft;
            }

            this.transform.rotation = Quaternion.Euler(0, eulerDirectionYRotation - rotationDelta, 0);

            return;
        }

        if (Function == CreatureFunction.TransitionGoRight)
        {
            Vector3 eulerDirection          = Quaternion.LookRotation(_direction).eulerAngles;
            float   eulerDirectionYRotation = eulerDirection.y;

            float rotationTimeDelta = Time.time - _rotateStartTime;
            float rotationDelta     = rotationTimeDelta * 180 / _rotate180Duration;
            if (rotationTimeDelta > _rotate180Duration)
            {
                rotationDelta = 180;
                Function      = CreatureFunction.GoRight;
            }

            this.transform.rotation = Quaternion.Euler(0, eulerDirectionYRotation + rotationDelta, 0);

            return;
        }

        Vector3    forwardBumpHeadPollingPosition = this.transform.position + (Vector3.up * _bodyHeight);
        RaycastHit forwardBumpHeadHitInfo;

        if (Physics.Raycast(
                forwardBumpHeadPollingPosition,
                Direction,
                out forwardBumpHeadHitInfo,
                _faceDistance
                ))
        {
            //Debug.Log("It is about to bump its head into something !!!");
            return;
        }

        Vector3    forwardBumpBodyPollingPosition = this.transform.position + (Vector3.up * (_bodyHeight / 2));
        RaycastHit forwardBumpBodyHitInfo;

        if (Physics.Raycast(
                forwardBumpBodyPollingPosition,
                Direction,
                out forwardBumpBodyHitInfo,
                _space
                ))
        {
            GameObject hitObject = forwardBumpBodyHitInfo.collider.gameObject;
            //Debug.Log("It is about to walk into something !!!");
            if (Creature.IsCreature(hitObject))
            {
                //Debug.Log("It is about to walk into another creature !!!");
                Creature creature = hitObject.GetComponent <Creature>();
                if (creature.Function == CreatureFunction.GoLeft)
                {
                    Direction = Quaternion.AngleAxis(-90, Vector3.up) * Direction;
                }
                else if (creature.Function == CreatureFunction.GoRight)
                {
                    Direction = Quaternion.AngleAxis(90, Vector3.up) * Direction;
                }
                return;
            }
        }

        float distanceTravelled = _movingSpeed * timePassed;

        Vector3 nextPosition          = this.transform.position + Direction * distanceTravelled;
        Vector3 groundPollingPosition = nextPosition + (Vector3.up * _bodyHeight);

        int checkLayerMask = CreatureManager.FloorMask | CreatureManager.PathModifierMask;

        RaycastHit groundHitInfo;

        if (Physics.Raycast(
                groundPollingPosition,
                Vector3.down,
                out groundHitInfo,
                20.0f,
                checkLayerMask))
        {
            GameObject hitObject = groundHitInfo.collider.gameObject;

            //Debug.Log("Pingus hit gameobject: " + hitObject.name);

            //if (BridgeCreationBehaviour.IsBridge(hitObject)) {
            //    //Debug.Log("It stepped on the bridge !!!");
            //}

            if (CreatureManager.IsEndPosition(hitObject))
            {
                float distanceToEnd = Vector3.Distance(hitObject.transform.position, groundHitInfo.point);
                if (distanceToEnd < CreatureManager._endDistance)
                {
                    Function = CreatureFunction.Dead;
                    GameObject gameObject = this.gameObject;
                    gameObject.SetActive(false);
                }
            }

            if ((this.transform.position.y - groundHitInfo.point.y) > _maxFallHeight)
            {
                Function = CreatureFunction.Dead;
                //GameObject gameObject = this.gameObject;
                //gameObject.SetActive(false);
            }

            nextPosition = groundHitInfo.point;
        }

        this.transform.position = nextPosition;
    }