Esempio n. 1
0
    /// <summary>
    /// Physics update for the entity
    /// TODO - fix jump gravity...
    /// </summary>
    private void FixedUpdate()
    {
        if (!(Entity is Player))
        {
            float gHeight = GetWorldHeight();

            if (transform.position.y < gHeight)
            {
                transform.position = new Vector3(transform.position.x, gHeight, transform.position.z);
            }
            AnimationManager.SetSpeedPercentage(LEPathFinder.CurrentSpeed() / Entity.MovementData.RunSpeed);
            EntityHealthBar.SetHealthPct(Entity.CombatManager.CurrentHealth / Entity.CombatManager.MaxHealth);

            UpdateLookAngle();

            /*
             * if (ShouldLook)
             * {
             *  float angle = Vector3.SignedAngle(Vector3.forward, LookTowardsPointTarget - transform.position, Vector3.up);
             *  Quaternion quat = Quaternion.Euler(new Vector3(0, angle, 0));
             *  transform.rotation = Quaternion.Slerp(transform.rotation, quat, Time.fixedDeltaTime * LOOK_ROTATION_SPEED);
             *  Entity.SetLookAngle(transform.rotation.eulerAngles.y);
             * }
             *
             * Entity.SetLookAngle(transform.rotation.eulerAngles.y);*/
        }



        Entity.SetPosition(transform.position);
        //RigidBody.angularVelocity = Vector3.zero;
        if (GameManager.Paused)
        {
            return;
        }
        if (IsIdle)
        {
            //  RigidBody.velocity = Vector3.zero;
            return;
        }

        if (!IsPlayer)
        {
            return;
        }

        Debug.BeginDeepProfile("le_fixed_update");


        EntityHealthBar?.SetHealthPct(Entity.CombatManager.CurrentHealth / Entity.CombatManager.MaxHealth);


        Vec2i cPos = World.GetChunkPosition(transform.position);

        /*
         *
         * //Check if the current chunk is loaded
         * if (!GameManager.WorldManager.CRManager.IsCurrentChunkPositionLoaded(cPos))
         * {
         *  //If not, keep position dixed with +ve y value, to prevent falling through world.
         *  transform.position = new Vector3(transform.position.x, 6, transform.position.z);
         *  return;
         * }
         */

        float ground = GetWorldHeight();

        float velY = RigidBody.velocity.y;

        //If we are below the ground, reset position to be at ground level
        if (transform.position.y < ground)
        {
            transform.position = new Vector3(transform.position.x, ground, transform.position.z);
        }
        //If our DesiredVelocity is non 0
        if (DesiredVelocity != Vector2.zero)
        {
            //We check if the terrain we are moving into is valid to move
            if (CheckMoveableTerrain(transform.position, new Vector3(DesiredVelocity.x, 0, DesiredVelocity.y), 10f))
            {
                //Calculate and set velocity
                float moveSpeed = IsRunning ? Entity.MovementData.RunSpeed : Entity.MovementData.WalkSpeed;
                RigidBody.velocity = new Vector3(DesiredVelocity.x * moveSpeed, velY, DesiredVelocity.y * moveSpeed);
                //inform animation manager of velocity
                AnimationManager.SetSpeedPercentage(moveSpeed / Entity.MovementData.RunSpeed);
            }
            DesiredVelocity = Vector2.zero;
            AnimationManager.SetSpeedPercentage(0);
        }
        else
        {
            AnimationManager.SetSpeedPercentage(0);
        }


        //The final y coord we will end up at
        float finalY = transform.position.y;

        if (finalY < ground)
        {
            finalY = ground;
        }
        bool IsGrounded = transform.position.y - ground <= 0.1f;


        //Debug info for player Jumping
        if (Entity is Player)
        {
            DebugGUI.Instance.SetData("jump_vel", VerticalVelocity);
            DebugGUI.Instance.SetData("onGround", IsGrounded);
            DebugGUI.Instance.SetData("isJumping", IsJumping);
            DebugGUI.Instance.SetData("isFalling", IsFalling);
            DebugGUI.Instance.SetData("groundHeight", ground);
        }

        /*
         * //Falling & jumping code:
         *
         * //We change our vertical velocity as required
         * VerticalVelocity -= 9.81f * Time.fixedDeltaTime;
         * if(IsGrounded && VerticalVelocity < 0)
         * {
         *  //if we are grounded, and our velocity is negative:
         *  //If we were falling or jumping, we now land
         *  if (IsJumping || IsFalling)
         *  {
         *      AnimationManager.LandJump();
         *  }
         *
         *  VerticalVelocity = 0;
         *  IsJumping = false;
         *  IsFalling = false;
         * }
         *
         * //If the entity is currently grounded and moving down
         * if (IsGrounded && VerticalVelocity < 0)
         * {
         *  //If we were falling or jumping, we now land
         *  if (IsJumping || IsFalling)
         *  {
         *      AnimationManager.LandJump();
         *  }
         *
         *  velY = 0;
         *  IsJumping = false;
         *  IsFalling = false;
         *
         *  finalY = ground;
         *
         *  //maybe we remove this?
         * // transform.position = new Vector3(transform.position.x, ground, transform.position.z);
         *
         * }
         * else if (IsGrounded && VerticalVelocity > 0)
         * {
         *  //Update velocity
         *  velY -= 9.81f * Time.fixedDeltaTime;
         *
         *  //Change height due to velocity + acc
         *  //finalY = transform.position.y + VerticalVelocity * Time.fixedDeltaTime - 9.81f * Time.fixedDeltaTime * Time.fixedDeltaTime;
         *  //if (finalY < ground)
         *  //    finalY = ground;
         *  //transform.position = new Vector3(transform.position.x, y, transform.position.z);
         * }//We are on the ground, and our vertical velocity is small enough to cancle
         * else if (IsGrounded && Mathf.Abs(VerticalVelocity) < 0.1f)
         * {
         *  VerticalVelocity = 0;
         *  IsFalling = false;
         *  IsJumping = false;
         * }
         * else
         * {
         *  //We are not grounded
         *  //We are not jumping or falling (yet
         *  if(!(IsJumping || IsFalling))
         *  {
         *      //If the down is only a little bit down,
         *      //We move to the down position
         *      if (transform.position.y - ground < 0.3f)
         *      {
         *          finalY = ground;
         *         // transform.position = new Vector3(transform.position.x, ground, transform.position.z);
         *      }
         *      else
         *      {
         *          //if we falling further, we set falling to true
         *          IsFalling = true;
         *          AnimationManager.SetFalling();
         *      }
         *  }
         *  else
         *  {
         *      //If we are not grounded, and we are either jumping or falling, we must update our velocity.
         *      //Update velocity
         *      VerticalVelocity -= 9.81f * Time.fixedDeltaTime;
         *      //Change height due to velocity + acc
         *      finalY = transform.position.y + VerticalVelocity * Time.fixedDeltaTime - 9.81f * Time.fixedDeltaTime * Time.fixedDeltaTime;
         *      if (finalY < ground)
         *          finalY = ground;
         *     // transform.position = new Vector3(transform.position.x, y, transform.position.z);
         *  }
         *
         *
         *  /*
         *  //If we are not grounded, but we aren't currently falling or jumping,
         *  //then we must be falling
         *  if (!(IsJumping || IsFalling))
         *  {
         *      IsFalling = true;
         *      AnimationManager.SetFalling();
         *  }
         *
         *  //Update velocity
         *  VerticalVelocity -= 9.81f * Time.fixedDeltaTime;
         *  //Change height due to velocity + acc
         *  float y = transform.position.y + VerticalVelocity * Time.fixedDeltaTime - 9.81f * Time.fixedDeltaTime * Time.fixedDeltaTime;
         *  if (y < ground)
         *      y = ground;
         *  transform.position =
         * }*/
        transform.position = new Vector3(transform.position.x, finalY, transform.position.z);



        Entity.SetPosition(transform.position);

        Debug.EndDeepProfile("le_fixed_update");

        return;

        //If we have a specified move direction
        if (!(MoveDirection == Vector2.zero))
        {
            float tileSpeed = 1; //TODO - get tile speed
            //Finds correct speed associated with running/walking for this entity
            float entitySpeed = IsRunning ? Entity.MovementData.RunSpeed : Entity.MovementData.WalkSpeed;

            AnimationManager.SetSpeedPercentage(entitySpeed / Entity.MovementData.RunSpeed);

            Vector2 v2Pos      = new Vector2(transform.position.x, transform.position.z);
            Vector2 targetDisp = TargetPosition - v2Pos;
            float   targetMag  = targetDisp.magnitude;
            Vector3 vel        = new Vector3(MoveDirection.x, 0, MoveDirection.y) * tileSpeed * entitySpeed;

            //controller.SimpleMove(vel);



            float oldVy = RigidBody.velocity.y;

            //Vector3 vel = new Vector3(MoveDirection.x, 0, MoveDirection.y) * tileSpeed * entitySpeed;
            RigidBody.velocity = new Vector3(MoveDirection.x, 0, MoveDirection.y) * tileSpeed * entitySpeed;
            //If the distance the body will move in a frame (|velocity|*dt) is more than the desired amount (target mag)
            //Then we must scale the velocity down
            if (RigidBody.velocity.magnitude * Time.fixedDeltaTime > targetMag)
            {
                RigidBody.velocity = RigidBody.velocity * targetMag / (Time.fixedDeltaTime * RigidBody.velocity.magnitude);
            }

            RigidBody.velocity += Vector3.up * oldVy;

            Vector3 moveTo       = transform.position + vel * Time.fixedDeltaTime * 5;
            float   moveToHeight = GetWorldHeight(moveTo.x, moveTo.z);

            if (Entity is Player)
            {
                Debug.Log("ground: " + ground + " move to" + moveToHeight);
            }
            if (moveToHeight > ground)
            {
                if ((moveToHeight - ground) * Time.fixedDeltaTime < 0.5f)
                {
                    transform.position = new Vector3(transform.position.x, moveToHeight, transform.position.z);
                }
            }
            else
            {
                transform.position = new Vector3(transform.position.x, moveToHeight, transform.position.z);
            }

            if (Entity is Player)
            {
                DebugGUI.Instance.SetData("Vel", RigidBody.velocity + " | " + vel);
            }
        }
        else
        {
            AnimationManager.SetSpeedPercentage(0);
        }



        //If the entity is currently grounded and moving down
        if (IsGrounded && VerticalVelocity < 0)
        {
            //If we were falling or jumping, we now land
            if (IsJumping || IsFalling)
            {
                AnimationManager.LandJump();
            }

            VerticalVelocity = 0;
            IsJumping        = false;
            IsFalling        = false;
            //maybe we remove this?
            transform.position = new Vector3(transform.position.x, ground, transform.position.z);
        }
        else if (IsGrounded && VerticalVelocity > 0)
        {
            //Update velocity
            VerticalVelocity -= 9.81f * Time.fixedDeltaTime;
            //Change height due to velocity + acc
            float y = transform.position.y + VerticalVelocity * Time.fixedDeltaTime - 9.81f * Time.fixedDeltaTime * Time.fixedDeltaTime;
            if (y < ground)
            {
                y = ground;
            }
            transform.position = new Vector3(transform.position.x, y, transform.position.z);
        }
        else if (IsGrounded && Mathf.Abs(VerticalVelocity) < 0.1f)
        {
            VerticalVelocity = 0;
            IsFalling        = false;
            IsJumping        = false;
        }
        else
        {
            //We are not grounded

            //If we are not grounded, but we aren't currently falling or jumping,
            //then we must be falling
            if (!(IsJumping || IsFalling))
            {
                IsFalling = true;
                AnimationManager.SetFalling();
            }

            //Update velocity
            VerticalVelocity -= 9.81f * Time.fixedDeltaTime;
            //Change height due to velocity + acc
            float y = transform.position.y + VerticalVelocity * Time.fixedDeltaTime - 9.81f * Time.fixedDeltaTime * Time.fixedDeltaTime;
            if (y < ground)
            {
                y = ground;
            }
            transform.position = new Vector3(transform.position.x, y, transform.position.z);
        }

        //Check if we are on the ground
        bool IsOnGround = OnGround();

        //Debug info for player Jumping
        if (Entity is Player)
        {
            DebugGUI.Instance.SetData("jump_vel", VerticalVelocity);
            DebugGUI.Instance.SetData("onGround", IsOnGround);
            DebugGUI.Instance.SetData("isJumping", IsJumping);
            DebugGUI.Instance.SetData("isFalling", IsFalling);
        }

        /*
         * if(VerticalVelocity > 0)
         * {
         *  VerticalVelocity += Physics.gravity.y * Time.fixedDeltaTime;
         *  RigidBody.velocity = RigidBody.velocity + Vector3.up * VerticalVelocity;
         *  if (VerticalVelocity < 0)
         *      VerticalVelocity = 0;
         * }
         */

        /*
         * if (IsOnGround && VerticalVelocity < 0)
         * {
         *  VerticalVelocity = 0;
         *  IsJumping = false;
         *  IsFalling = false;
         * }*/
        /*
         * //Check if we are off the ground
         * if (!IsOnGround)
         * {
         *  //If so, update vertical position and velocity accordingly
         *  VerticalVelocity -= 9.81f * Time.fixedDeltaTime;
         *  Height += VerticalVelocity * Time.fixedDeltaTime - 9.81f * Time.fixedDeltaTime * Time.fixedDeltaTime;
         *  //transform.position += Vector3.up * (VerticalVelocity * Time.fixedDeltaTime+9.81f*Time.fixedDeltaTime*Time.fixedDeltaTime);
         *  //Next, if we are off the ground but not jumping, it must mean we're falling
         *  if (!IsJumping)
         *  {
         *      //If not currently falling, set it to true
         *      if (!IsFalling)
         *      {
         *          IsFalling = true;
         *          AnimationManager.SetFalling();
         *      }
         *  }
         * }
         * else if (VerticalVelocity > 0) {
         *  //If we are on the ground, but our velocity is positive, we must be jumping
         *  VerticalVelocity -= 9.81f * Time.fixedDeltaTime;
         *  Height += VerticalVelocity * Time.fixedDeltaTime;
         *
         * }
         * else if ((IsJumping || IsFalling) && !IsWaitingForJump)
         * {
         *  //If we are on the ground, but still jumping or falling, set both to false. reset velocity
         *  IsJumping = false;
         *  VerticalVelocity = 0;
         *  IsFalling = false;
         *  //Then play the land animation
         *  AnimationManager.LandJump();
         * }
         * float worldHeight = GetWorldHeight();
         * if (transform.position.y < worldHeight)
         * {
         *  transform.position = new Vector3(transform.position.x, worldHeight, transform.position.z);
         *  VerticalVelocity = 0;
         * }
         *
         * //transform.position = new Vector3(transform.position.x, Height, transform.position.z);
         */
        /* float worldHeight = GetWorldHeight();
         * if (transform.position.y < worldHeight)
         * {
         *   transform.position = new Vector3(transform.position.x, worldHeight, transform.position.z);
         *   VerticalVelocity = 0;
         * }*/
        //Reset variables



        Entity.MoveEntity(transform.position);

        Debug.EndDeepProfile("le_fixed_update");

        return;
    }
Esempio n. 2
0
    /// <summary>
    /// Physics update for the entity
    ///
    /// </summary>
    private void FixedUpdate()
    {
        //RigidBody.angularVelocity = Vector3.zero;
        if (GameManager.Paused)
        {
            return;
        }
        if (IsIdle)
        {
            return;
        }



        Debug.BeginDeepProfile("le_fixed_update");

        if (!(Entity is Player))
        {
            DebugGUI.Instance.SetData("ent", GameManager.Paused + "_" + IsIdle + "_" + MoveDirection);
            if (MiscMaths.DistanceSqr(transform.position, GameManager.PlayerManager.Player.Position) > (World.ChunkSize * 4) * (World.ChunkSize * 4))
            {
                IsIdle = true;
            }
        }

        EntityHealthBar.SetHealthPct(Entity.CombatManager.CurrentHealth / Entity.CombatManager.MaxHealth);

        if (transform.position.y < 0)
        {
            transform.position = new Vector3(transform.position.x, 0, transform.position.z);
        }

        Vec2i cPos = World.GetChunkPosition(transform.position);

        //Check if the current chunk is loaded
        if (!GameManager.WorldManager.CRManager.IsCurrentChunkPositionLoaded(cPos))
        {
            //If not, keep position dixed with +ve y value, to prevent falling through world.
            transform.position = new Vector3(transform.position.x, 1, transform.position.z);
        }



        //If we have a specified move direction
        if (MoveDirection != Vector2.zero)
        {
            float tileSpeed = 1; //TODO - get tile speed
            //Finds correct speed associated with running/walking for this entity
            float entitySpeed = IsRunning?Entity.MovementData.RunSpeed:Entity.MovementData.WalkSpeed;

            AnimationManager.SetSpeedPercentage(entitySpeed / Entity.MovementData.RunSpeed);

            Vector2 v2Pos      = new Vector2(transform.position.x, transform.position.z);
            Vector2 targetDisp = TargetPosition - v2Pos;
            float   targetMag  = targetDisp.magnitude;
            Vector3 vel        = new Vector3(MoveDirection.x, 0, MoveDirection.y) * tileSpeed * entitySpeed;
            RigidBody.velocity = new Vector3(MoveDirection.x, 0, MoveDirection.y) * tileSpeed * entitySpeed;

            //If the distance the body will move in a frame (|velocity|*dt) is more than the desired amount (target mag)
            //Then we must scale the velocity down
            if (RigidBody.velocity.magnitude * Time.fixedDeltaTime > targetMag)
            {
                RigidBody.velocity = RigidBody.velocity * targetMag / (Time.fixedDeltaTime * RigidBody.velocity.magnitude);
            }


            if (Entity is Player)
            {
                DebugGUI.Instance.SetData("Vel", RigidBody.velocity + " | " + vel);
            }
        }
        else
        {
            AnimationManager.SetSpeedPercentage(0);
        }
        //If we have a specified look direction
        if (LookTowards != Vector3.zero)
        {
            float      angle = Vector3.SignedAngle(Vector3.forward, LookTowards - transform.position, Vector3.up);
            Quaternion quat  = Quaternion.Euler(new Vector3(0, angle, 0));
            transform.rotation = Quaternion.Slerp(transform.rotation, quat, Time.fixedDeltaTime * LOOK_ROTATION_SPEED);
            Entity.SetLookAngle(transform.rotation.eulerAngles.y);
        }

        //Check if we are on the ground
        bool IsOnGround = OnGround();

        //Debug info for player Jumping
        if (Entity is Player)
        {
            DebugGUI.Instance.SetData("jump_vel", VerticalVelocity);
            DebugGUI.Instance.SetData("onGround", IsOnGround);
            DebugGUI.Instance.SetData("isJumping", IsJumping);
            DebugGUI.Instance.SetData("isFalling", IsFalling);
        }

        //Check if we are off the ground
        if (!IsOnGround)
        {
            //If so, update vertical position and velocity accordingly
            VerticalVelocity   -= 9.81f * Time.fixedDeltaTime;
            transform.position += Vector3.up * VerticalVelocity * Time.fixedDeltaTime;
            //Next, if we are off the ground but not jumping, it must mean we're falling
            if (!IsJumping)
            {
                //If not currently falling, set it to true
                if (!IsFalling)
                {
                    IsFalling = true;
                    AnimationManager.SetFalling();
                }
            }
        }
        else if (VerticalVelocity > 0)
        {
            //If we are on the ground, but our velocity is positive, we must be jumping
            VerticalVelocity   -= 9.81f * Time.fixedDeltaTime;
            transform.position += Vector3.up * VerticalVelocity * Time.fixedDeltaTime;
        }
        else if ((IsJumping || IsFalling) && !IsWaitingForJump)
        {
            //If we are on the ground, but still jumping or falling, set both to false. reset velocity
            IsJumping        = false;
            VerticalVelocity = 0;
            IsFalling        = false;
            //Then play the land animation
            AnimationManager.LandJump();
        }

        //Reset variables
        LookTowards   = Vector3.zero;
        MoveDirection = Vector2.zero;
        Entity.SetPosition(transform.position);

        Debug.EndDeepProfile("le_fixed_update");

        return;
    }