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);
        }
Beispiel #2
0
        public TestRoom()
        {
            size    = new Vector2u(2560, 1280);
            viewPos = new Vector2u(0, 0);
            view    = new View(
                new Vector2f(
                    Settings.ScreenSize.X / 2 + viewPos.X,
                    Settings.ScreenSize.Y / 2 + viewPos.Y
                    ),
                (Vector2f)Settings.ScreenSize
                );

            var gameObjList = new List <GameObject>()
            {
                new MessageBox("Hello, world!", "hworld"),
                new MessageBox("Game Paused", "pause"),
                new TestPlayer(new Vector2f(512, 512))
            };

            for (int i = 0; i < size.Y / 128 - 2; i++)
            {
                gameObjList.Add(
                    new Block(
                        new Vector2f(64, i * 128 + 64),
                        TileOrientation.Right,
                        Sprites.getInstance().GrassTiles
                        )
                    );
                gameObjList.Add(
                    new Block(
                        new Vector2f(size.X - 64, i * 128 + 64),
                        TileOrientation.Left,
                        Sprites.getInstance().GrassTiles
                        )
                    );
            }

            for (int i = 1; i < size.X / 128 - 1; i++)
            {
                gameObjList.Add(
                    new Block(
                        new Vector2f(i * 128 + 64, size.Y - 2 * 128 + 64),
                        TileOrientation.Top,
                        Sprites.getInstance().GrassTiles
                        )
                    );
                gameObjList.Add(
                    new Block(
                        new Vector2f(i * 128 + 64, size.Y - 128 + 64),
                        TileOrientation.Center,
                        Sprites.getInstance().GrassTiles
                        )
                    );
            }

            gameObjList.Add(
                new Block(
                    new Vector2f(64, size.Y - 128 + 64),
                    TileOrientation.Center,
                    Sprites.getInstance().GrassTiles
                    )
                );
            gameObjList.Add(
                new Block(
                    new Vector2f(128, size.Y - 128 + 64),
                    TileOrientation.Center,
                    Sprites.getInstance().GrassTiles
                    )
                );
            gameObjList.Add(
                new Block(
                    new Vector2f(size.X - 64, size.Y - 128 + 64),
                    TileOrientation.Center,
                    Sprites.getInstance().GrassTiles
                    )
                );
            gameObjList.Add(
                new Block(
                    new Vector2f(size.X - 128, size.Y - 128 + 64),
                    TileOrientation.Center,
                    Sprites.getInstance().GrassTiles
                    )
                );
            gameObjList.Add(
                new Block(
                    new Vector2f(64, size.Y - 2 * 128 + 64),
                    TileOrientation.InnerTopRight,
                    Sprites.getInstance().GrassTiles
                    )
                );
            gameObjList.Add(
                new Block(
                    new Vector2f(size.X - 64, size.Y - 2 * 128 + 64),
                    TileOrientation.InnerTopLeft,
                    Sprites.getInstance().GrassTiles
                    )
                );

            gameObjList.Add(
                new Block(
                    new Vector2f(6 * 128 + 64, size.Y - 3 * 128 + 64),
                    TileOrientation.Solid,
                    Sprites.getInstance().GrassTiles
                    )
                );
            gameObjList.Add(
                new Block(
                    new Vector2f(7 * 128 + 64, size.Y - 3 * 128 + 64),
                    TileOrientation.Solid,
                    Sprites.getInstance().GrassTiles
                    )
                );
            gameObjList.Add(
                new Block(
                    new Vector2f(8 * 128 + 64, size.Y - 3 * 128 + 64),
                    TileOrientation.Solid,
                    Sprites.getInstance().GrassTiles
                    )
                );
            gameObjList.Add(
                new Block(
                    new Vector2f(7 * 128 + 64, size.Y - 4 * 128 + 64),
                    TileOrientation.Solid,
                    Sprites.getInstance().GrassTiles
                    )
                );
            gameObjList.Add(
                new Block(
                    new Vector2f(8 * 128 + 64, size.Y - 4 * 128 + 64),
                    TileOrientation.Solid,
                    Sprites.getInstance().GrassTiles
                    )
                );
            gameObjList.Add(
                new Block(
                    new Vector2f(8 * 128 + 64, size.Y - 5 * 128 + 64),
                    TileOrientation.Solid,
                    Sprites.getInstance().GrassTiles
                    )
                );
            gameObjList.Add(
                new Block(
                    new Vector2f(12 * 128 + 64, size.Y - 4 * 128 + 64),
                    TileOrientation.Solid,
                    Sprites.getInstance().GrassTiles
                    )
                );
            gameObjList.Add(
                new Block(
                    new Vector2f(14 * 128 + 64, size.Y - 6 * 128 + 64),
                    TileOrientation.Solid,
                    Sprites.getInstance().GrassTiles
                    )
                );
            gameObjList.Add(
                new Block(
                    new Vector2f(9 * 128 + 64, size.Y - 7 * 128 + 64),
                    TileOrientation.Solid,
                    Sprites.getInstance().GrassTiles
                    )
                );
            gameObjList.Add(
                new Block(
                    new Vector2f(10 * 128 + 64, size.Y - 7 * 128 + 64),
                    TileOrientation.Solid,
                    Sprites.getInstance().GrassTiles
                    )
                );
            gameObjList.Add(
                new Block(
                    new Vector2f(11 * 128 + 64, size.Y - 7 * 128 + 64),
                    TileOrientation.Solid,
                    Sprites.getInstance().GrassTiles
                    )
                );

            gameObjects = gameObjList.ToArray();
            Array.Sort(gameObjects);

            isPaused       = false;
            canChangePause = true;
        }
 public GameSprite GetSpriteIndex() => Sprites.getInstance().Empty;