/*
     * \brief Works out if a value is almost another value (for floating point accuracy)
     */
    public void CallOnCollisionEnter(Collision collision)
    {
        m_collisionState = CollisionState.None;

        foreach (ContactPoint contact in collision)
        {
            m_platform = contact.otherCollider.gameObject.GetComponent <CSceneObjectPlatform>();
            if (m_platform != null)
            {
                m_platform.resetDeltaA();
            }
            if (isNearly(contact.normal.y, 1.0f, 0.2f))
            {
                m_collisionState = CollisionState.OnFloor;

                // are we on a special material?
                m_footMaterial = FootMaterial.Stone;
                if (contact.otherCollider.tag == "Wood Object")
                {
                    m_footMaterial = FootMaterial.Wood;
                }
                else if (contact.otherCollider.tag == "Metal Object")
                {
                    m_footMaterial = FootMaterial.Metal;
                }
            }
            else if (isNearly(contact.normal.y, -1.0f, 0.2f))
            {
                m_collisionState = CollisionState.OnRoof;
            }
            else
            {
                m_collisionState    = CollisionState.OnWall;
                m_ladderClimb.State = LadderState.None;
            }
        }

        if (m_collisionState == CollisionState.OnFloor)
        {
            if (m_jumpState != JumpState.Landed)
            {
                m_player.GetPlayerAnimation().PlayFootstepAudio(m_footMaterial);
            }

            m_jumpState         = JumpState.Landed;
            m_ladderClimb.State = LadderState.None;

            if (m_player.GetPlayerState() != PlayerState.Turning)
            {
                m_player.SetPlayerState(PlayerState.Standing);
            }

            //m_ledgeGrabBox.collider.enabled = true;
        }
    }
    /*
     * \brief Called on player update
     */
    public void OnFixedUpdate(ref PlayerState playerState)
    {
        if (playerState == PlayerState.FallingFromTower)
        {
            return;
        }

        float velocity = (Input.GetAxis("Horizontal") * MaxSpeed) * m_invert;

        if (Application.platform == RuntimePlatform.Android)
        {
            velocity = Input.acceleration.y;
        }


        if ((Time.time * 1000.0f) - m_velocityLockTimer < 100)
        {
            velocity = m_velocity;
        }

        if (m_ladderClimb.State == LadderState.AtMiddle || m_ladderClimb.State == LadderState.AtTop)
        {
            velocity = 0;
        }

        int direction = isNearly(velocity, 0.0f, 0.1f) ? 0 : velocity > 0 ? 1 : -1;

        //platform update
        if (m_platform && m_collisionState == CollisionState.OnFloor)
        {
            m_platformVelocity += m_platform.DeltaA;
            m_platform.resetDeltaA();
        }

        // Ledge hanging code start
        if (playerState == PlayerState.LedgeHang || playerState == PlayerState.LedgeClimb || playerState == PlayerState.LedgeClimbComplete)
        {
            m_velocity = 0.0f;

            // If we are trying to move in the opposite way of the wall, fall off the wall
            if (direction != 0 && direction != m_movingDirection)
            {
                m_velocity         = velocity * 4.0f;
                playerState        = PlayerState.Walking;
                m_jumpState        = JumpState.Landed;
                m_body.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationZ;
                //	m_ledgeGrabBox.collider.enabled = true;
                m_movingDirection *= -1;
                m_direction       *= -1;
                m_collisionState   = CollisionState.None;

                //	Transform t = m_ledgeGrabBox.transform;
                //	t.localPosition = new Vector3(m_direction > 0 ? -0.18f : 0.18f, t.localPosition.y, t.localPosition.z);
                return;
            }

            // if the player is not already climbing the wall, check keys
            if (playerState != PlayerState.LedgeClimb)
            {
                // if the user pressed up, climb the wall
                if (Input.GetAxis("Vertical") > 0 && playerState != PlayerState.LedgeClimbComplete)
                {
                    playerState = PlayerState.LedgeClimb;
                    m_jumpState = JumpState.Landed;
                    //	m_ledgeGrabBox.collider.enabled = false;
                }
                // if the user pressed space, jump off the wall
                else if (m_isJumpDown)
                {
                    m_body.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationZ;
                    m_velocity         = -(m_movingDirection) * 4.0f;             // kick it away from the wall
                    m_body.AddForce(new Vector3(0, PlayerJumpHeight * 0.25f, 0), ForceMode.Impulse);
                    playerState        = PlayerState.Jumping;
                    m_collisionState   = CollisionState.None;
                    m_movingDirection *= -1;
                    m_direction       *= -1;
                    //	m_ledgeGrabBox.collider.enabled = true;

                    //	Transform t = m_ledgeGrabBox.transform;
                    //	t.localPosition = new Vector3(m_direction > 0 ? -0.18f : 0.18f, t.localPosition.y, t.localPosition.z);
                }
                return;
            }

            return;
        }
        // Ledge hanging code end

        if (playerState == PlayerState.WallJumpStart)
        {
            if ((Time.time * 1000.0f) - m_wallJump.StartHangTime > m_wallJump.WallHangTime)
            {
                m_body.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationZ;
                m_velocity         = -m_movingDirection;
                playerState        = PlayerState.Walking;
                m_jumpState        = JumpState.Landed;
            }
            else if (m_isJumpDown && direction != 0 && direction != m_movingDirection)
            {
                m_body.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationZ;
                m_velocity         = -(m_movingDirection);         // kick it away from the wall
                m_body.AddForce(new Vector3(0, PlayerJumpHeight * 1.1f, 0), ForceMode.Impulse);
                playerState        = PlayerState.Jumping;
                m_jumpTimer        = (Time.time * 1000.0f);
                m_collisionState   = CollisionState.None;
                m_movingDirection *= -1;
                m_direction       *= -1;
            }
            return;
        }

        if (!(m_collisionState == CollisionState.OnWall && m_jumpState == JumpState.Jumping))
        {
            m_velocity = velocity;
        }

        int lastDirection       = m_direction;
        int lastMovingDirection = m_movingDirection;

        if (playerState != PlayerState.Turning)
        {
            m_direction = direction;
            if (m_direction != 0)
            {
                m_movingDirection = m_direction;
            }
        }

        if (m_collisionState != CollisionState.None && m_jumpState != JumpState.Jumping)
        {
            if (m_direction == 0 && playerState != PlayerState.Turning)
            {
                playerState = PlayerState.Standing;
            }
            else
            {
                if (playerState == PlayerState.Turning)
                {
                    m_velocity = 0.0f;
                    return;
                }

                // are we tuning round?
                if (lastDirection != direction && ((Time.time * 1000.0f) - m_turnLockTimer > 1000.0f))
                {
                    playerState = PlayerState.Walking;
                    if (m_velocity != 0.0f && lastMovingDirection != m_direction)
                    {
                        playerState     = PlayerState.Turning;
                        m_velocity      = 0.0f;
                        m_turnLockTimer = Time.time * 1000.0f;
                    }
                }
                else
                {
                    playerState = PlayerState.Walking;
                }
            }
        }
    }
/*
 * \brief Called whilst a collision is taking place
 */
    public void CallOnCollisionStay(Collision collision, ref PlayerState playerState, ref float playerAlpha)
    {
        m_collisionState = CollisionState.None;

        foreach (ContactPoint contact in collision)
        {
            Debug.DrawRay(contact.point, contact.normal);

            //
            if (contact.otherCollider != null && contact.otherCollider.gameObject != null)
            {
                CSceneObjectPlatform platform = contact.otherCollider.gameObject.GetComponent <CSceneObjectPlatform>();
                if (platform != null && m_platform == null)
                {
                    m_platform = platform;
                    m_platform.resetDeltaA();
                }
            }

            //
            CSceneObject obj = null;
            if (contact.otherCollider)
            {
                obj = contact.otherCollider.gameObject.GetComponent <CSceneObject>();
                if (obj == null && contact.otherCollider.gameObject.transform.parent != null)
                {
                    GameObject parent = contact.otherCollider.gameObject.transform.parent.gameObject;
                    if (parent != null)
                    {
                        obj = parent.GetComponent <CSceneObject>();
                    }
                }
            }

            if (contact.thisCollider != null && contact.thisCollider.gameObject != null && contact.thisCollider.gameObject.name == "Ledge_Grab_Detection" && (obj == null || obj.CanLedgeGrab))
            {
                if (CSceneObject.CheckLedgeGrab(collision))
                {
                    continue;
                }
            }

            if (contact.otherCollider != null && contact.otherCollider.gameObject != null && contact.otherCollider.gameObject.name == "Ledge_Grab_Detection" && (obj == null || obj.CanLedgeGrab))
            {
                if (CSceneObject.CheckLedgeGrab(collision))
                {
                    continue;
                }
            }

            // wall jumping
            if (obj != null && obj.CanWallJump == true && m_jumpState != JumpState.Landed && !isNearly(contact.normal.y, 1.0f, 0.2f) && !isNearly(contact.normal.y, -1.0f, 0.1f))
            {
                m_collisionState         = CollisionState.OnWall;
                playerState              = PlayerState.WallJumpStart;
                m_jumpTimer              = (Time.time * 1000.0f);
                m_body.constraints       = RigidbodyConstraints.FreezeAll;
                m_velocity               = 0.0f;
                m_wallJump.StartHangTime = Time.time * 1000.0f;
            }
            // floor check
            else if (isNearly(contact.normal.y, 1.0f, 0.8f))
            {
                m_collisionState = CollisionState.OnFloor;
                if (!isNearly(contact.normal.y, 1.0f, 0.15f) && collision.contacts.Length == 1)
                {
                    m_velocity          = (m_movingDirection * 0.15f);
                    m_velocityLockTimer = (Time.time * 1000.0f);
                }

                // are we on a special material?
                m_footMaterial = FootMaterial.Stone;
                if (contact.otherCollider.tag == "Wood Object")
                {
                    m_footMaterial = FootMaterial.Wood;
                }
                else if (contact.otherCollider.tag == "Metal Object")
                {
                    m_footMaterial = FootMaterial.Metal;
                }
            }
            // head check
            else if (isNearly(contact.normal.y, -1.0f, 0.1f))
            {
                m_collisionState = CollisionState.OnRoof;
            }
            // wall check
            else
            {
                if (isFacingCollision(m_movingDirection, m_body.transform.position, contact.point, playerAlpha))
                {
                    m_collisionState = CollisionState.OnWall;
                    break;
                }
            }
        }

        if (m_collisionState == CollisionState.OnWall && m_jumpState == JumpState.Jumping && playerState != PlayerState.WallJumpStart)
        {
            m_velocity = -(m_movingDirection * 0.15f);
        }

        if (m_collisionState == CollisionState.OnFloor && ((Time.time * 1000.0f) - m_jumpTimer > 200.0f))
        {
            m_jumpState = JumpState.Landed;

            if (m_ladderClimb.State != LadderState.AtBase)
            {
                m_ladderClimb.State = LadderState.None;
            }
            else
            {
                playerState = PlayerState.UpALadder;
            }

            if (m_player.GetPlayerState() != PlayerState.Turning)
            {
                m_player.SetPlayerState(PlayerState.Standing);
            }
            //m_ledgeGrabBox.collider.enabled = true;
        }
    }
    /*
     * \brief Works out if a value is almost another value (for floating point accuracy)
    */
    public void CallOnCollisionEnter(Collision collision)
    {
        m_collisionState = CollisionState.None;

        foreach (ContactPoint contact in collision)
        {
            m_platform = contact.otherCollider.gameObject.GetComponent<CSceneObjectPlatform>();
            if (m_platform != null) {
                m_platform.resetDeltaA();
            }
            if (isNearly(contact.normal.y, 1.0f, 0.2f))
            {
                m_collisionState = CollisionState.OnFloor;

                // are we on a special material?
                m_footMaterial = FootMaterial.Stone;
                if (contact.otherCollider.tag == "Wood Object")
                    m_footMaterial = FootMaterial.Wood;
                else if (contact.otherCollider.tag == "Metal Object")
                    m_footMaterial = FootMaterial.Metal;

            }
            else if (isNearly(contact.normal.y, -1.0f, 0.2f))
            {
                m_collisionState = CollisionState.OnRoof;
            }
            else
            {
                m_collisionState = CollisionState.OnWall;
                m_ladderClimb.State = LadderState.None;
            }
        }

        if (m_collisionState == CollisionState.OnFloor)
        {
            if (m_jumpState != JumpState.Landed)
                m_player.GetPlayerAnimation().PlayFootstepAudio(m_footMaterial);

            m_jumpState = JumpState.Landed;
            m_ladderClimb.State = LadderState.None;

            if (m_player.GetPlayerState() != PlayerState.Turning) m_player.SetPlayerState(PlayerState.Standing);

            //m_ledgeGrabBox.collider.enabled = true;
        }
    }
    /*
     * \brief Called whilst a collision is taking place
    */
    public void CallOnCollisionStay(Collision collision, ref PlayerState playerState, ref float playerAlpha)
    {
        m_collisionState = CollisionState.None;

        foreach (ContactPoint contact in collision)
        {
            Debug.DrawRay(contact.point, contact.normal);

            //
            if (contact.otherCollider != null && contact.otherCollider.gameObject != null)
            {
                CSceneObjectPlatform platform = contact.otherCollider.gameObject.GetComponent<CSceneObjectPlatform>();
                if (platform != null && m_platform == null) {
                    m_platform = platform;
                    m_platform.resetDeltaA();
                }
            }

            //
            CSceneObject obj = null;
            if (contact.otherCollider)
            {
                obj = contact.otherCollider.gameObject.GetComponent<CSceneObject>();
                if (obj == null && contact.otherCollider.gameObject.transform.parent != null) {
                    GameObject parent = contact.otherCollider.gameObject.transform.parent.gameObject;
                    if (parent != null) {
                        obj = parent.GetComponent<CSceneObject>();
                    }
                }
            }

            if (contact.thisCollider != null && contact.thisCollider.gameObject != null && contact.thisCollider.gameObject.name == "Ledge_Grab_Detection" && (obj == null || obj.CanLedgeGrab))
            {
                if(CSceneObject.CheckLedgeGrab(collision))
                    continue;
            }

            if (contact.otherCollider != null && contact.otherCollider.gameObject != null && contact.otherCollider.gameObject.name == "Ledge_Grab_Detection" && (obj == null || obj.CanLedgeGrab))
            {
                if (CSceneObject.CheckLedgeGrab(collision))
                    continue;
            }

            // wall jumping
            if (obj != null && obj.CanWallJump == true && m_jumpState != JumpState.Landed && !isNearly(contact.normal.y, 1.0f, 0.2f) && !isNearly(contact.normal.y, -1.0f, 0.1f))
            {
                m_collisionState = CollisionState.OnWall;
                playerState = PlayerState.WallJumpStart;
                m_jumpTimer = (Time.time * 1000.0f);
                m_body.constraints = RigidbodyConstraints.FreezeAll;
                m_velocity = 0.0f;
                m_wallJump.StartHangTime = Time.time * 1000.0f;
            }
            // floor check
            else if (isNearly(contact.normal.y, 1.0f, 0.8f))
            {
                m_collisionState = CollisionState.OnFloor;
                if (!isNearly(contact.normal.y, 1.0f, 0.15f) && collision.contacts.Length == 1)
                {
                    m_velocity = (m_movingDirection * 0.15f);
                    m_velocityLockTimer = (Time.time * 1000.0f);
                }

                // are we on a special material?
                m_footMaterial = FootMaterial.Stone;
                if (contact.otherCollider.tag == "Wood Object")
                    m_footMaterial = FootMaterial.Wood;
                else if (contact.otherCollider.tag == "Metal Object")
                    m_footMaterial = FootMaterial.Metal;
            }
            // head check
            else if (isNearly(contact.normal.y, -1.0f, 0.1f))
            {
                m_collisionState = CollisionState.OnRoof;
            }
            // wall check
            else
            {
                if (isFacingCollision(m_movingDirection, m_body.transform.position, contact.point, playerAlpha)) {
                    m_collisionState = CollisionState.OnWall;
                    break;
                }
            }
        }

        if (m_collisionState == CollisionState.OnWall && m_jumpState == JumpState.Jumping && playerState != PlayerState.WallJumpStart)
        {
            m_velocity = -(m_movingDirection * 0.15f);
        }

        if (m_collisionState == CollisionState.OnFloor && ((Time.time * 1000.0f) - m_jumpTimer > 200.0f))
        {
            m_jumpState = JumpState.Landed;

            if (m_ladderClimb.State != LadderState.AtBase)
            {
                m_ladderClimb.State = LadderState.None;
            }
            else
            {
                playerState = PlayerState.UpALadder;
            }

            if (m_player.GetPlayerState() != PlayerState.Turning) m_player.SetPlayerState(PlayerState.Standing);
            //m_ledgeGrabBox.collider.enabled = true;
        }
    }
示例#6
0
    /*
     * \brief Called on player update
     */
    public void OnFixedUpdate(ref GruntState playerState, bool playerDetected)
    {
        if (playerState == GruntState.FallingFromTower)
        {
            return;
        }

        GameObject player      = GameObject.Find("Player Spawn");
        float      playerAlpha = player.GetComponent <CEntityPlayer>().CurrentPlayerAlpha;
        float      gruntAlpha  = m_grunt.CurrentPlayerAlpha;

        if (playerDetected)
        {
            if (playerAlpha > gruntAlpha + 180)
            {
                gruntAlpha += 360;
            }
            else if (playerAlpha + 180 < gruntAlpha)
            {
                gruntAlpha -= 360;
            }
            if (playerAlpha > gruntAlpha)
            {
                m_movingDirection = -1;
                m_direction       = -1;
            }
            else if (gruntAlpha > playerAlpha)
            {
                m_movingDirection = 1;
                m_direction       = 1;
            }
        }

        //	float velocity = (Input.GetAxis("Horizontal") * MaxSpeed) * m_invert;
        //	if (Application.platform == RuntimePlatform.Android)
        //		velocity = Input.acceleration.y;
        float velocity = 0.0f;

        if (m_movingDirection != 0)
        {
            if (playerState != GruntState.Turning && playerState != GruntState.Attacking && playerState != GruntState.Standing)
            {
                if (m_grunt.GetGruntPlayerDetected())
                {
                    velocity = (m_movingDirection * DetectedSpeed);
                }
                else
                {
                    velocity = (m_movingDirection * PeacefulSpeed);
                }
            }
        }

        if ((Time.time * 1000.0f) - m_velocityLockTimer < 100)
        {
            velocity = m_velocity;
        }

        int direction = isNearly(velocity, 0.0f, 0.1f) ? 0 : velocity > 0 ? 1 : -1;

        //platform update
        if (m_platform && m_collisionState == CollisionState.OnFloor)
        {
            m_platformVelocity += m_platform.DeltaA;
            m_platform.resetDeltaA();
        }


        if (!(m_collisionState == CollisionState.OnWall && m_jumpState == JumpState.Jumping))
        {
            m_velocity = velocity;
        }

        if (playerState != GruntState.Turning)
        {
            m_direction = direction;
            if (m_direction != 0)
            {
                m_movingDirection = m_direction;
            }
        }

        if (m_collisionState != CollisionState.None && m_jumpState != JumpState.Jumping)
        {
            if (m_movingDirection == 0 && playerState != GruntState.Turning)
            {
                //playerState = GruntState.Standing;
            }
            else
            {
                if (playerState == GruntState.Turning)
                {
                    m_velocity = 0.0f;
                    return;
                }
            }
        }
    }
    /*
     * \brief Works out if a value is almost another value (for floating point accuracy)
    */
    public void CallOnCollisionEnter(Collision collision)
    {
        m_collisionState = CollisionState.None;

        foreach (ContactPoint contact in collision)
        {
            m_platform = contact.otherCollider.gameObject.GetComponent<CSceneObjectPlatform>();
            if (m_platform != null) {
                m_platform.resetDeltaA();
            }
            if (isNearly(contact.normal.y, 1.0f, 0.2f))
            {
                m_collisionState = CollisionState.OnFloor;

                if (m_player.GetPlayerState() == PlayerState.OnLadder)
                {
                    GetLadder.state = LadderState.AtBase;
                    m_player.SetPlayerState(PlayerState.Standing);
                }

                // are we on a special material?
                m_footMaterial = FootMaterial.Stone;
                if (contact.otherCollider.tag == "Wood Object")
                    m_footMaterial = FootMaterial.Wood;
                else if (contact.otherCollider.tag == "Metal Object")
                    m_footMaterial = FootMaterial.Metal;

            }
            else if (isNearly(contact.normal.y, -1.0f, 0.2f))
            {
                m_collisionState = CollisionState.OnRoof;
                if (contact.otherCollider != null && contact.otherCollider.GetComponent<CSceneObjectPlatform>() != null)
                {
                    m_player.PushPlayerFromTower();
                }
            }
            else
            {
                if (m_player.GetPlayerState() != PlayerState.OnLadder)
                    m_collisionState = CollisionState.OnWall;
            }
        }

        if (m_collisionState == CollisionState.OnFloor)
        {
            if (m_jumpState != JumpState.Landed)
                m_player.GetPlayerAnimation().PlayFootstepAudio(m_footMaterial);

            m_jumpState = JumpState.Landed;

            if (GetLadder.state == LadderState.JumpingOff)
                GetLadder.state = LadderState.None;

            if (m_player.GetPlayerState() != PlayerState.Turning && m_player.GetPlayerState() != PlayerState.OnLadder && !m_player.PullingLever(false))
            {
                m_player.SetPlayerState(PlayerState.Standing);
            }
        }
    }
示例#8
0
    /*
     * \brief Works out if a value is almost another value (for floating point accuracy)
     */
    public void CallOnCollisionEnter(Collision collision, bool playerDetected)
    {
        m_collisionState = CollisionState.None;

        foreach (ContactPoint contact in collision)
        {
            m_platform = contact.otherCollider.gameObject.GetComponent <CSceneObjectPlatform>();
            if (m_platform != null)
            {
                m_platform.resetDeltaA();
            }
            else if (isNearly(contact.normal.y, 1.0f, 0.2f))
            {
                m_collisionState = CollisionState.OnFloor;

                // are we on a special material?
                m_footMaterial = FootMaterial.Stone;
                if (contact.otherCollider.tag == "Wood Object")
                {
                    m_footMaterial = FootMaterial.Wood;
                }
                else if (contact.otherCollider.tag == "Metal Object")
                {
                    m_footMaterial = FootMaterial.Metal;
                }
            }
            else if (isNearly(contact.normal.y, -1.0f, 0.2f))
            {
                m_collisionState = CollisionState.OnRoof;
            }
            else
            {
                m_collisionState = CollisionState.OnWall;
            }
        }

        if (m_collisionState == CollisionState.OnFloor)
        {
            if (m_jumpState != JumpState.Landed)
            {
                m_grunt.GetPlayerAnimation().PlayFootstepAudio(m_footMaterial);
            }

            m_jumpState = JumpState.Landed;
        }
        if ((m_collisionState == CollisionState.OnWall || collision.collider.gameObject.name == "GruntBarrier") && m_grunt.GetGruntState() != GruntState.Turning && m_grunt.GetGruntState() != GruntState.Attacking)
        {
            if (!playerDetected)
            {
                m_grunt.SetGruntState(GruntState.Turning);
                if (m_movingDirection == 1)
                {
                    m_movingDirection = -1;
                    m_direction       = -1;
                }
                else if (m_movingDirection == -1)
                {
                    m_movingDirection = 1;
                    m_direction       = 1;
                }
            }
            else
            {
                m_grunt.SetGruntState(GruntState.Standing);
            }
        }
    }
示例#9
0
/*
 * \brief Called whilst a collision is taking place
 */
    public void CallOnCollisionStay(Collision collision, ref GruntState playerState, ref float playerAlpha)
    {
        m_collisionState = CollisionState.None;

        foreach (ContactPoint contact in collision)
        {
            Debug.DrawRay(contact.point, contact.normal);
            //
            if (contact.otherCollider != null && contact.otherCollider.gameObject != null)
            {
                CSceneObjectPlatform platform = contact.otherCollider.gameObject.GetComponent <CSceneObjectPlatform>();
                if (platform != null && m_platform == null)
                {
                    m_platform = platform;
                    m_platform.resetDeltaA();
                }
            }

            //
            CSceneObject obj = null;
            if (contact.otherCollider)
            {
                obj = contact.otherCollider.gameObject.GetComponent <CSceneObject>();
                if (obj == null && contact.otherCollider.gameObject.transform.parent != null)
                {
                    GameObject parent = contact.otherCollider.gameObject.transform.parent.gameObject;
                    if (parent != null)
                    {
                        obj = parent.GetComponent <CSceneObject>();
                    }
                }
            }

            // floor check
            else if (isNearly(contact.normal.y, 1.0f, 0.8f))
            {
                m_collisionState = CollisionState.OnFloor;
                if (!isNearly(contact.normal.y, 1.0f, 0.15f) && collision.contacts.Length == 1)
                {
                    if (m_grunt.GetGruntState() == GruntState.Walking)
                    {
                        if (m_grunt.GetGruntPlayerDetected())
                        {
                            m_velocity = (m_movingDirection * 0.6f);
                        }
                        else
                        {
                            m_velocity = (m_movingDirection * 0.15f);
                        }
                        m_velocityLockTimer = (Time.time * 1000.0f);
                    }
                }
                if (contact.otherCollider)
                {
                    // are we on a special material?
                    m_footMaterial = FootMaterial.Stone;
                    if (contact.otherCollider.tag == "Wood Object")
                    {
                        m_footMaterial = FootMaterial.Wood;
                    }
                    else if (contact.otherCollider.tag == "Metal Object")
                    {
                        m_footMaterial = FootMaterial.Metal;
                    }
                }
            }
            // head check
            else if (isNearly(contact.normal.y, -1.0f, 0.1f))
            {
                m_collisionState = CollisionState.OnRoof;
            }
            // wall check
            else
            {
                if (isFacingCollision(m_movingDirection, m_body.transform.position, contact.point, playerAlpha))
                {
                    m_collisionState = CollisionState.OnWall;
                    break;
                }
            }
        }

        if (m_collisionState == CollisionState.OnWall && m_jumpState == JumpState.Jumping && playerState != GruntState.WallJumpStart)
        {
            m_velocity = -(m_movingDirection * 0.15f);
        }

        if (m_collisionState == CollisionState.OnFloor && ((Time.time * 1000.0f) - m_jumpTimer > 200.0f))
        {
            m_jumpState = JumpState.Landed;

            if (m_grunt.GetGruntState() != GruntState.Turning)
            {
                m_grunt.SetGruntState(GruntState.Standing);
            }
        }
    }
    /*
     * \brief Called whilst a collision is taking place
    */
    public void CallOnCollisionStay(Collision collision, ref GruntState playerState, ref float playerAlpha)
    {
        m_collisionState = CollisionState.None;

        foreach (ContactPoint contact in collision)
        {
            Debug.DrawRay(contact.point, contact.normal);
            //
            if (contact.otherCollider != null && contact.otherCollider.gameObject != null)
            {
                CSceneObjectPlatform platform = contact.otherCollider.gameObject.GetComponent<CSceneObjectPlatform>();
                if (platform != null && m_platform == null) {
                    m_platform = platform;
                    m_platform.resetDeltaA();
                }
            }

            //
            CSceneObject obj = null;
            if (contact.otherCollider)
            {
                obj = contact.otherCollider.gameObject.GetComponent<CSceneObject>();
                if (obj == null && contact.otherCollider.gameObject.transform.parent != null) {
                    GameObject parent = contact.otherCollider.gameObject.transform.parent.gameObject;
                    if (parent != null) {
                        obj = parent.GetComponent<CSceneObject>();
                    }
                }
            }

            // floor check
            else if (isNearly(contact.normal.y, 1.0f, 0.8f))
            {
                m_collisionState = CollisionState.OnFloor;
                if (!isNearly(contact.normal.y, 1.0f, 0.15f) && collision.contacts.Length == 1)
                {
                    m_velocity = (m_movingDirection * 0.15f);
                    m_velocityLockTimer = (Time.time * 1000.0f);
                }
                if(contact.otherCollider)
                {
                    // are we on a special material?
                    m_footMaterial = FootMaterial.Stone;
                    if (contact.otherCollider.tag == "Wood Object")
                        m_footMaterial = FootMaterial.Wood;
                    else if (contact.otherCollider.tag == "Metal Object")
                        m_footMaterial = FootMaterial.Metal;
                }
            }
            // head check
            else if (isNearly(contact.normal.y, -1.0f, 0.1f))
            {
                m_collisionState = CollisionState.OnRoof;
            }
            // wall check
            else
            {
                if (isFacingCollision(m_movingDirection, m_body.transform.position, contact.point, playerAlpha)) {
                    m_collisionState = CollisionState.OnWall;
                    break;
                }
            }
        }

        if (m_collisionState == CollisionState.OnWall && m_jumpState == JumpState.Jumping && playerState != GruntState.WallJumpStart)
        {
            m_velocity = -(m_movingDirection * 0.15f);
        }

        if (m_collisionState == CollisionState.OnFloor && ((Time.time * 1000.0f) - m_jumpTimer > 200.0f))
        {
            m_jumpState = JumpState.Landed;

            if (m_grunt.GetGruntState() != GruntState.Turning) m_grunt.SetGruntState(GruntState.Standing);
        }
    }
    /*
     * \brief Called on player update
     */
    public void OnFixedUpdate(ref PlayerState playerState)
    {
        if (playerState == PlayerState.FallingFromTower)
        {
            return;
        }

        ////////////////////////

        bool wasJump = m_isJumpDown;

        m_isJumpDown = Input.GetButtonDown("Jump");

        if (Application.platform == RuntimePlatform.Android)
        {
            m_isJumpDown = Input.touchCount != 0;
        }

        if (m_isJumpDown && !wasJump)
        {
            m_canJumpFromLedge = true;
        }
        else
        {
            m_canJumpFromLedge = false;
        }

        if (m_isJumpDown && m_jumpState == JumpState.Landed && CanJump(playerState))
        {
            if ((Time.time * 1000.0f) - m_jumpTimer > JumpDelayMS)
            {
                m_jumpTimer = (Time.time * 1000.0f);
                m_body.AddForce(new Vector3(0, PlayerJumpHeight, 0), ForceMode.Impulse);
                m_jumpState      = JumpState.Jumping;
                playerState      = PlayerState.Jumping;
                m_collisionState = CollisionState.None;
            }
        }

        if (m_jumpState == JumpState.Jumping && playerState == PlayerState.Jumping)
        {
            if ((Time.time * 1000.0f) - m_jumpTimer > 2000.0f)
            {
                playerState = PlayerState.FallJumping;
                if (!m_fakeJump && ((Time.time * 1000.0f) - m_jumpTimer > 3000.0f))
                {
                    m_player.PushPlayerFromTower();
                }
            }
        }

        if (m_isJumpDown == true && (GetLadder.state == LadderState.OnMiddle || GetLadder.state == LadderState.OnTop))
        {
            GetLadder.state = LadderState.JumpingOff;
            m_jumpTimer     = (Time.time * 1000.0f);
            m_body.velocity = Vector3.zero;
            m_body.AddForce(new Vector3(0, PlayerJumpHeight, 0), ForceMode.Impulse);
            m_jumpState           = JumpState.Jumping;
            playerState           = PlayerState.Jumping;
            m_collisionState      = CollisionState.None;
            rigidbody.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationZ;
        }

        ////////////////////////

        float velocity = (Input.GetAxis("Horizontal") * MaxSpeed) * m_invert;

        if (Application.platform == RuntimePlatform.Android)
        {
            velocity = Input.acceleration.y;
        }

        if (playerState == PlayerState.OnLadder || playerState == PlayerState.LedgeClimb || playerState == PlayerState.LedgeClimbComplete)
        {
            velocity = 0.0f;
        }

        if (m_player.PullingLever(false))
        {
            velocity = 0.0f;
        }

        if ((Time.time * 1000.0f) - m_velocityLockTimer < 350)
        {
            velocity = m_velocity;
        }

        if (velocity != 0.0f && (m_ladder.state != LadderState.AtBase && m_ladder.state != LadderState.JumpingOff))
        {
            m_ladder.state = LadderState.None;
        }

        int direction = isNearly(velocity, 0.0f, 0.1f) ? 0 : velocity > 0 ? 1 : -1;

        //platform update
        if (m_platform && m_collisionState == CollisionState.OnFloor)
        {
            m_platformVelocity += m_platform.DeltaA;
            m_platform.resetDeltaA();
        }

        // Ledge hanging code start
        if (playerState == PlayerState.LedgeHang || playerState == PlayerState.LedgeClimb || playerState == PlayerState.LedgeClimbComplete)
        {
            m_velocity = 0.0f;

            // If we are trying to move in the opposite way of the wall, fall off the wall
            if (direction != 0 && direction != m_movingDirection)
            {
                m_velocity         = velocity * 4.0f;
                playerState        = PlayerState.Walking;
                m_jumpState        = JumpState.Landed;
                m_body.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationZ;
                m_movingDirection *= -1;
                m_direction       *= -1;
                m_collisionState   = CollisionState.None;

                Transform t = m_ledgeGrabBox.transform;
                t.localPosition = new Vector3(m_direction > 0 ? -0.18f : 0.18f, t.localPosition.y, t.localPosition.z);
                return;
            }

            // if the player is not already climbing the wall, check keys
            if (playerState != PlayerState.LedgeClimb)
            {
                // if the user pressed up, climb the wall
                if (Input.GetAxis("Vertical") > 0 && playerState != PlayerState.LedgeClimbComplete)
                {
                    playerState = PlayerState.LedgeClimb;
                    m_jumpState = JumpState.Landed;
                }
                // if the user pressed space, jump off the wall
                else if (m_isJumpDown && m_canJumpFromLedge)
                {
                    m_body.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationZ;
                    m_velocity         = -(m_movingDirection) * 4.0f;             // kick it away from the wall
                    m_body.AddForce(new Vector3(0, PlayerJumpHeight * 0.25f, 0), ForceMode.Impulse);
                    playerState        = PlayerState.Jumping;
                    m_collisionState   = CollisionState.None;
                    m_movingDirection *= -1;
                    m_direction       *= -1;

                    Transform t = m_ledgeGrabBox.transform;
                    t.localPosition = new Vector3(m_direction > 0 ? -0.18f : 0.18f, t.localPosition.y, t.localPosition.z);
                }
                return;
            }

            return;
        }
        // Ledge hanging code end

        if (playerState == PlayerState.WallJumpStart)
        {
            if (Time.time - m_wallJump.StartHangTime > 1.0f)
            {
                m_body.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationZ;
                m_velocity         = -m_wallJump.WallJumpDirection;
                playerState        = PlayerState.Walking;
                m_jumpState        = JumpState.Landed;
                m_collisionState   = CollisionState.None;
            }
            else if (Input.GetButton("Jump") && (direction != 0 && direction != m_wallJump.WallJumpDirection))
            {
                m_body.constraints  = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationZ;
                m_velocity          = -(m_wallJump.WallJumpDirection * 0.5f);        // kick it away from the wall
                m_velocityLockTimer = (Time.time * 1000.0f) + 50;
                m_body.AddForce(new Vector3(0, PlayerJumpHeight * 1.1f, 0), ForceMode.Impulse);
                playerState       = PlayerState.Jumping;
                m_jumpState       = JumpState.Jumping;
                m_jumpTimer       = (Time.time * 1000.0f);
                m_collisionState  = CollisionState.None;
                m_movingDirection = m_wallJump.WallJumpDirection;
                m_direction       = m_wallJump.WallJumpDirection;
            }
            return;
        }

        if (!(m_collisionState == CollisionState.OnWall && m_jumpState == JumpState.Jumping))
        {
            m_velocity = velocity;
        }

        int lastDirection       = m_direction;
        int lastMovingDirection = m_movingDirection;

        if (playerState != PlayerState.Turning)
        {
            m_direction = direction;
            if (m_direction != 0)
            {
                m_movingDirection = m_direction;
            }
        }

        if (m_collisionState != CollisionState.None && m_jumpState != JumpState.Jumping)
        {
            if (m_direction == 0 && playerState != PlayerState.Turning)
            {
                if (playerState != PlayerState.OnLadder && !m_player.PullingLever(false))
                {
                    playerState = PlayerState.Standing;
                }
            }
            else
            {
                if (playerState == PlayerState.Turning)
                {
                    m_velocity = 0.0f;
                    return;
                }

                // are we tuning round?
                if (lastDirection != direction && ((Time.time * 1000.0f) - m_turnLockTimer > 1000.0f))
                {
                    playerState = PlayerState.Walking;
                    if (m_velocity != 0.0f && lastMovingDirection != m_direction)
                    {
                        playerState     = PlayerState.Turning;
                        m_velocity      = 0.0f;
                        m_turnLockTimer = Time.time * 1000.0f;
                    }
                }
                else
                {
                    playerState = PlayerState.Walking;
                }
            }
        }
    }
    /*
     * \brief Works out if a value is almost another value (for floating point accuracy)
     */
    public void CallOnCollisionEnter(Collision collision)
    {
        m_collisionState = CollisionState.None;

        foreach (ContactPoint contact in collision)
        {
            m_platform = contact.otherCollider.gameObject.GetComponent <CSceneObjectPlatform>();
            if (m_platform != null)
            {
                m_platform.resetDeltaA();
            }
            if (isNearly(contact.normal.y, 1.0f, 0.2f))
            {
                m_collisionState = CollisionState.OnFloor;

                if (m_player.GetPlayerState() == PlayerState.OnLadder)
                {
                    GetLadder.state = LadderState.AtBase;
                    m_player.SetPlayerState(PlayerState.Standing);
                }

                // are we on a special material?
                m_footMaterial = FootMaterial.Stone;
                if (contact.otherCollider.tag == "Wood Object")
                {
                    m_footMaterial = FootMaterial.Wood;
                }
                else if (contact.otherCollider.tag == "Metal Object")
                {
                    m_footMaterial = FootMaterial.Metal;
                }
            }
            else if (isNearly(contact.normal.y, -1.0f, 0.2f))
            {
                m_collisionState = CollisionState.OnRoof;
                if (contact.otherCollider != null && contact.otherCollider.GetComponent <CSceneObjectPlatform>() != null)
                {
                    m_player.PushPlayerFromTower();
                }
            }
            else
            {
                if (m_player.GetPlayerState() != PlayerState.OnLadder)
                {
                    m_collisionState = CollisionState.OnWall;
                }
            }
        }

        if (m_collisionState == CollisionState.OnFloor)
        {
            if (m_jumpState != JumpState.Landed)
            {
                m_player.GetPlayerAnimation().PlayFootstepAudio(m_footMaterial);
            }

            m_jumpState = JumpState.Landed;

            if (GetLadder.state == LadderState.JumpingOff)
            {
                GetLadder.state = LadderState.None;
            }

            if (m_player.GetPlayerState() != PlayerState.Turning && m_player.GetPlayerState() != PlayerState.OnLadder && !m_player.PullingLever(false))
            {
                m_player.SetPlayerState(PlayerState.Standing);
            }
        }
    }
    /*
     * \brief Works out if a value is almost another value (for floating point accuracy)
    */
    public void CallOnCollisionEnter(Collision collision, bool playerDetected)
    {
        m_collisionState = CollisionState.None;

        foreach (ContactPoint contact in collision)
        {
            m_platform = contact.otherCollider.gameObject.GetComponent<CSceneObjectPlatform>();
            if (m_platform != null) {
                m_platform.resetDeltaA();
            }
            else if (isNearly(contact.normal.y, 1.0f, 0.2f))
            {
                m_collisionState = CollisionState.OnFloor;

                // are we on a special material?
                m_footMaterial = FootMaterial.Stone;
                if (contact.otherCollider.tag == "Wood Object")
                    m_footMaterial = FootMaterial.Wood;
                else if (contact.otherCollider.tag == "Metal Object")
                    m_footMaterial = FootMaterial.Metal;

            }
            else if (isNearly(contact.normal.y, -1.0f, 0.2f))
            {
                m_collisionState = CollisionState.OnRoof;
            }
            else
            {
                m_collisionState = CollisionState.OnWall;
            }
        }

        if (m_collisionState == CollisionState.OnFloor)
        {
            if (m_jumpState != JumpState.Landed)
                m_grunt.GetPlayerAnimation().PlayFootstepAudio(m_footMaterial);

            m_jumpState = JumpState.Landed;
        }
        if ( (m_collisionState == CollisionState.OnWall || collision.collider.gameObject.name=="GruntBarrier") &&  m_grunt.GetGruntState() != GruntState.Turning && m_grunt.GetGruntState() != GruntState.Attacking)
        {
            if( !playerDetected )
            {
                m_grunt.SetGruntState( GruntState.Turning );
                if( m_movingDirection == 1)
                {
                    m_movingDirection = -1;
                    m_direction = -1;
                }
                else if( m_movingDirection == -1 )
                {
                    m_movingDirection = 1;
                    m_direction = 1;
                }
            }
            else
            {
                m_grunt.SetGruntState( GruntState.Standing );
            }
        }
    }
示例#14
0
    /*
     * \brief Called on player update
     */
    public void OnFixedUpdate(ref GruntState playerState)
    {
        if (playerState == GruntState.FallingFromTower)
        {
            return;
        }

        //	float velocity = (Input.GetAxis("Horizontal") * MaxSpeed) * m_invert;
        //	if (Application.platform == RuntimePlatform.Android)
        //		velocity = Input.acceleration.y;
        float velocity = 0.0f;

        if (m_movingDirection != 0)
        {
            if (playerState != GruntState.Turning)
            {
                velocity = 0.2f * m_movingDirection;
            }
        }

        if ((Time.time * 1000.0f) - m_velocityLockTimer < 100)
        {
            velocity = m_velocity;
        }

        int direction = isNearly(velocity, 0.0f, 0.1f) ? 0 : velocity > 0 ? 1 : -1;

        //platform update
        if (m_platform && m_collisionState == CollisionState.OnFloor)
        {
            m_platformVelocity += m_platform.DeltaA;
            m_platform.resetDeltaA();
        }


        if (!(m_collisionState == CollisionState.OnWall && m_jumpState == JumpState.Jumping))
        {
            m_velocity = velocity;
        }

        int lastDirection       = m_direction;
        int lastMovingDirection = m_movingDirection;

        if (playerState != GruntState.Turning)
        {
            m_direction = direction;
            if (m_direction != 0)
            {
                m_movingDirection = m_direction;
            }
        }

        if (m_collisionState != CollisionState.None && m_jumpState != JumpState.Jumping)
        {
            if (m_movingDirection == 0 && playerState != GruntState.Turning)
            {
                //playerState = GruntState.Standing;
            }
            else
            {
                if (playerState == GruntState.Turning)
                {
                    m_velocity = 0.0f;
                    return;
                }

                // are we tuning round?
                if (lastDirection != direction && ((Time.time * 1000.0f) - m_turnLockTimer > 1000.0f))
                {
                    playerState = GruntState.Walking;
                    if (m_velocity != 0.0f && lastMovingDirection != m_direction)
                    {
                        playerState     = GruntState.Turning;
                        m_velocity      = 0.0f;
                        m_turnLockTimer = Time.time * 1000.0f;
                    }
                }
                else
                {
                    playerState = GruntState.Walking;
                }
            }
        }
    }