コード例 #1
0
        private void UpdateInteractions()
        {
            // Space
            bool space = Keyboard.IsKeyPressed(Keyboard.Key.Space);
            if (space)
            {
                switch (lastAnim)
                {
                    case MovementType.Horizontal:
                        Side side;
                        side = Direction == 1 ? Side.RIGHT : Side.LEFT;
                        CheckForItems(side);
                        AttackEnemy(side);
                        break;
                    case MovementType.Up:
                        CheckForItems(Side.UP);
                        AttackEnemy(Side.UP);
                        break;
                    case MovementType.Down:
                    case MovementType.Idle:
                        CheckForItems(Side.DOWN);
                        AttackEnemy(Side.DOWN);
                        break;
                    default:
                        break;
                }
            }

            // Mouse
            Vector2i mousePos = Mouse.GetPosition(GameLoop.Window);
            mousePos = (Vector2i)GameLoop.Window.MapPixelToCoords(mousePos);

            Tile tile = world.GetTile(mousePos.X / Tile.TILE_SIZE, mousePos.Y / Tile.TILE_SIZE);
            if (tile != null)
            {
                FloatRect tileRect = tile.GetFloatRect();
                DebugRender.AddRectangle(tileRect, Color.Green);
                // Left Mouse to Erase
                if (Mouse.IsButtonPressed(Mouse.Button.Left))
                {
                    int i = (mousePos.X) / Tile.TILE_SIZE;
                    int j = (mousePos.Y) / Tile.TILE_SIZE;
                    int boderMax = World.WORLD_SIZE * Chunk.CHUNK_SIZE * Tile.TILE_SIZE - Tile.TILE_SIZE;
                    bool noBorders = tile.Position.X != 0 && tile.Position.Y != 0 && tile.Position.X != boderMax && tile.Position.Y != boderMax;
                    if (tile.type != TileType.ITEM && tile.type != TileType.GROUND && noBorders)
                        world.SetTile(TileType.GROUND, i, j);
                        ChangeErase(-1);
                    }

                }
            }
コード例 #2
0
        private void UpdatePhysicsWall(FloatRect playerRect, int pX, int pY)
        {
            List <Tile> walls = new List <Tile>
            {
                // Left
                world.GetTile(pX - 1, pY),
                world.GetTile(pX - 1, pY - 1),
                world.GetTile(pX - 1, pY + 1),
                // Right
                world.GetTile(pX + 1, pY),
                world.GetTile(pX + 1, pY - 1),
                world.GetTile(pX + 1, pY + 1),
                // Top
                world.GetTile(pX, pY - 1),
                world.GetTile(pX - 1, pY - 1),
                world.GetTile(pX + 1, pY - 1),
                // Down
                world.GetTile(pX, pY),
                world.GetTile(pX - 1, pY),
                world.GetTile(pX + 1, pY),
            };

            //for (int i = 0; i < AwesomeGame.PlayerTiles.Count; i++)
            //{
            //    walls.Add(AwesomeGame.PlayerTiles[i]);
            //    AwesomeGame.PlayerTiles[i].Position = AwesomeGame.Players[i].Position;
            //}

            foreach (var tile in walls)
            {
                if (tile == null || tile.type == TileType.GROUND)
                {
                    continue;
                }

                FloatRect tileRect = new FloatRect(tile.Position, new Vector2f(Tile.TILE_SIZE, Tile.TILE_SIZE));
                DebugRender.AddRectangle(tileRect, Color.Yellow);

                if (playerRect.Intersects(tileRect))
                {
                    Vector2f offset = new Vector2f(playerRect.Left - tileRect.Left, playerRect.Top - tileRect.Top);
                    offset.X /= Math.Abs(offset.X);
                    offset.Y /= Math.Abs(offset.Y);

                    float speed = Math.Abs(movement.X);

                    // Left walls
                    if (offset.X > 0)
                    {
                        // Sends the player one tile away
                        Position   = new Vector2f(tileRect.Left + tileRect.Width + playerRect.Width * 0.5f, Position.Y);
                        movement.X = 0;
                        velocity.X = 0;
                    }
                    // Right walls
                    else if (offset.X < 0)
                    {
                        Position   = new Vector2f(tileRect.Left - playerRect.Width * 0.5f, Position.Y);
                        movement.X = 0;
                        velocity.X = 0;
                        //Position = new Vector2f(Position.X - 2, Position.Y);
                    }
                    // Top walls
                    else if (offset.Y > 0)
                    {
                        Position   = new Vector2f(Position.X, tileRect.Top + tileRect.Height + playerRect.Height * 0.5f);
                        movement.Y = 0;
                        velocity.Y = 0;
                        //Position = new Vector2f(Position.X, Position.Y + 2);
                    }
                    // Down walls
                    else if (offset.Y < 0)
                    {
                        Position   = new Vector2f(Position.X, tileRect.Top - playerRect.Height * 0.5f);
                        movement.Y = 0;
                        velocity.Y = 0;
                        //Position = new Vector2f(Position.X, Position.Y - 2);
                    }

                    OnWallCollided();
                }
            }
        }