Example #1
0
        public override void UpdateNPC()
        {
            updateMovement();

            if (UIManager.Over == null && UIManager.Drag == null)
            {
                Vector2i mousePos = Mouse.GetPosition(Program.Window);
                Tile     tile     = world.GetTileByWorldPos(mousePos);

                DebugRender.AddRectangle(mousePos.X + world.XShift * Tile.TILE_SIZE, mousePos.Y + world.YShift * Tile.TILE_SIZE, Tile.TILE_SIZE, Tile.TILE_SIZE, Color.Green);
                if (mousePos.X + world.XShift * Tile.TILE_SIZE >= Program.Game.World.MIN_XShift && mousePos.X + world.XShift * Tile.TILE_SIZE <= Program.Game.World.MAX_XShift + 1500)
                {
                    if (Mouse.IsButtonPressed(Mouse.Button.Left))
                    {
                        int i = (int)(mousePos.X / Tile.TILE_SIZE) + (int)world.XShift;
                        int j = (int)(mousePos.Y / Tile.TILE_SIZE) + (int)world.YShift;
                        world.SetTile(TileType.NONE, i, j);
                    }

                    if (Mouse.IsButtonPressed(Mouse.Button.Right))
                    {
                        int i = (int)(mousePos.X / Tile.TILE_SIZE) + (int)world.XShift;
                        int j = (int)(mousePos.Y / Tile.TILE_SIZE) + (int)world.YShift;
                        world.SetTile(TileType.GROUND, i, j);
                    }
                }
            }
        }
Example #2
0
        public override void UpdateNPC()
        {
            updateMovement();

            if (UIManager.Over == null && UIManager.Drag == null)
            {
                var mousePos = Mouse.GetPosition(Program.Window);
                var tile     = world.GetTileByWorldPos(mousePos);
                if (tile != null)
                {
                    FloatRect tileRect = tile.GetFloatRect();
                    DebugRender.AddRectangle(tileRect, Color.Green);

                    if (Mouse.IsButtonPressed(Mouse.Button.Left))
                    {
                        int i = (int)(mousePos.X / Tile.TILE_SIZE);
                        int j = (int)(mousePos.Y / Tile.TILE_SIZE);
                        world.SetTile(TileType.NONE, i, j);
                    }
                }

                if (Mouse.IsButtonPressed(Mouse.Button.Right))
                {
                    int i = (int)(mousePos.X / Tile.TILE_SIZE);
                    int j = (int)(mousePos.Y / Tile.TILE_SIZE);
                    world.SetTile(TileType.GROUND, i, j);
                }
            }
        }
Example #3
0
        bool updateWallCollision(int i, int j, int iOffset, ref Vector2f stepPos, FloatRect stepRect)
        {
            var dirType = iOffset > 0 ? DirectionType.Right : DirectionType.Left;

            Tile[] walls = new Tile[]
            {
                world.GetTile(i + iOffset, j - 1),
                world.GetTile(i + iOffset, j - 2),
                world.GetTile(i + iOffset, j - 3),
            };

            bool isWallCollided = false;

            foreach (Tile t in walls)
            {
                if (t == null)
                {
                    continue;
                }

                FloatRect tileRect = new FloatRect(t.Position, new Vector2f(Tile.TILE_SIZE, Tile.TILE_SIZE));

                DebugRender.AddRectangle(tileRect, Color.Yellow);

                if (updateCollision(stepRect, tileRect, dirType, ref stepPos))
                {
                    isWallCollided = true;
                }
            }

            return(isWallCollided);
        }
Example #4
0
        private void updatePhysics()
        {
            velocity.X *= 0.99f;
            velocity.Y += 0.55f;

            var   offset = velocity + movement;
            float dist   = MathHelper.GetDistance(offset);

            int countStep = 1;

            //float stepSize = ;
            if (dist > (float)Tile.TILE_SIZE / 2)
            {
                countStep = (int)(dist / (Tile.TILE_SIZE / 2));
            }

            Vector2f  nextPos  = Position + offset;
            Vector2f  stepPos  = Position - rect.Origin;
            FloatRect stepRect = new FloatRect(stepPos, rect.Size);
            Vector2f  stepVec  = (nextPos - Position) / countStep;

            for (int step = 0; step < countStep; step++)
            {
                bool isBreakStep = false;

                stepPos += stepVec;
                stepRect = new FloatRect(stepPos, rect.Size);

                DebugRender.AddRectangle(stepRect, Color.Blue);

                int  i    = (int)((stepPos.X + rect.Size.X / 2) / Tile.TILE_SIZE);
                int  j    = (int)((stepPos.Y + rect.Size.Y) / Tile.TILE_SIZE);
                Tile tile = world.GetTile(i, j);

                if (tile != null)
                {
                    FloatRect tileRect = new FloatRect(tile.Position, new Vector2f(Tile.TILE_SIZE, Tile.TILE_SIZE));

                    DebugRender.AddRectangle(tileRect, Color.Red);

                    if (updateCollision(stepRect, tileRect, DirectionType.Down, ref stepPos))
                    {
                        velocity.Y  = 0;
                        isFly       = false;
                        isBreakStep = true;
                    }
                    else
                    {
                        isFly = true;
                    }
                }
                else
                {
                    isFly = true;
                }

                if (updateWallCollision(i, j, -1, ref stepPos, stepRect) || updateWallCollision(i, j, 1, ref stepPos, stepRect))
                {
                    OnWallCollided();
                    isBreakStep = true;
                }

                if (isBreakStep)
                {
                    break;
                }
            }

            Position = stepPos + rect.Origin;
        }