コード例 #1
0
 public void Update(float deltaTime, KeyState keys, Room room)
 {
 }
コード例 #2
0
        public void Update(float deltaTime, KeyState keys, Room room)
        {
            if (keys.Jump && canJump)
            {
                var platforms = Engine.FindGameObjectsByTag("block", room);
                if (Engine.IsPlaceMeeting(new Vector2f(pos.X, pos.Y + 1), this, platforms))
                {
                    vel.Y   = -jumpSpeed;
                    canJump = false;
                }
            }

            if (keys.Left && pos.X > 0)
            {
                if (vel.X != -moveSpeed)
                {
                    vel.X       = -moveSpeed;
                    spriteIndex = new GameSprite(Sprites.getInstance().PlayerWalkLeft);
                }

                facingLeft = true;
            }
            else if (keys.Right && pos.X < room.GetSize().X)
            {
                if (vel.X != moveSpeed)
                {
                    vel.X       = moveSpeed;
                    spriteIndex = new GameSprite(Sprites.getInstance().PlayerWalkRight);
                }

                facingLeft = false;
            }
            else
            {
                vel.X = 0;

                if (facingLeft)
                {
                    spriteIndex = new GameSprite(Sprites.getInstance().PlayerStandLeft);
                }
                else
                {
                    spriteIndex = new GameSprite(Sprites.getInstance().PlayerStandRight);
                }
            }

            var blocks = Engine.FindGameObjectsByTag("block", room);

            if (Engine.IsPlaceMeeting(new Vector2f(pos.X + vel.X * deltaTime, pos.Y), this, blocks))
            {
                while (!Engine.IsPlaceMeeting(new Vector2f(pos.X, pos.Y), this, blocks))
                {
                    pos.X += Math.Sign(vel.X);
                }
                pos.X -= Math.Sign(vel.X);
                vel.X  = 0;
            }

            if (Engine.IsPlaceMeeting(new Vector2f(pos.X + 2, pos.Y + vel.Y * deltaTime), this, blocks) &&
                Engine.IsPlaceMeeting(new Vector2f(pos.X - 2, pos.Y + vel.Y * deltaTime), this, blocks))
            {
                while (!Engine.IsPlaceMeeting(new Vector2f(pos.X, pos.Y), this, blocks))
                {
                    pos.Y += Math.Sign(vel.Y);
                }
                pos.Y -= Math.Sign(vel.Y);

                vel.Y = 0;
                acc.Y = 0;

                if (!keys.Jump)
                {
                    canJump = true;
                }
            }
            else
            {
                acc.Y = gravity;
            }

            if (vel.Y > 0)
            {
                if (facingLeft)
                {
                    spriteIndex = new GameSprite(Sprites.getInstance().PlayerFallLeft);
                }
                else
                {
                    spriteIndex = new GameSprite(Sprites.getInstance().PlayerFallRight);
                }
            }
            else if (vel.Y < 0)
            {
                if (facingLeft)
                {
                    spriteIndex = new GameSprite(Sprites.getInstance().PlayerJumpLeft);
                }
                else
                {
                    spriteIndex = new GameSprite(Sprites.getInstance().PlayerJumpRight);
                }
            }
            else if (spriteIndex.ImageSpeed == 0)
            {
                if (facingLeft)
                {
                    spriteIndex = new GameSprite(Sprites.getInstance().PlayerWalkLeft);
                }
                else
                {
                    spriteIndex = new GameSprite(Sprites.getInstance().PlayerWalkRight);
                }
            }

            spriteIndex.Position = pos;
            spriteIndex.Update(deltaTime);
        }