public void Update(Entity entity, Map map)
        {
            var pos = entity.Position;

            var bounds = entity.GeometryBoundingBox;

            // if entity is moving diagonally, divide vector by sqrt 2
            if (entity.Velocity.X != 0 && entity.Velocity.Y != 0)
                entity.Velocity /= MathUtils.SqrtTwo;

            entity.Position += entity.Velocity * Game.DeltaTime.AsSeconds();

            // see if entity is colliding with an unpassable tile
            for (var x = 0; x < map.Size.X; x++)
            {
                for (var y = 0; y < map.Size.Y; y++)
                {
                    var t = map.Layer1Cells[x, y];

                    if (t.IsEmpty()) continue;

                    _tileBounds.Left = x * map.CellSize;
                    _tileBounds.Top = y * map.CellSize;

                    FloatRect area;
                    if (!bounds.Intersects(_tileBounds, out area)) continue;

                    // vertical collision
                    if (area.Width > area.Height && Math.Abs(area.Height - bounds.Height) > 0.001f)
                    {
                        // top
                        entity.Position = area.Contains(area.Left, bounds.Top) ? new Vector2f(pos.X, pos.Y + area.Height) : new Vector2f(pos.X, pos.Y - area.Height);
                    }

                    // horizontal collision
                    else if (area.Width < area.Height || Math.Abs(area.Height - bounds.Height) < 0.001f)
                    {
                        // right
                        entity.Position = area.Contains(bounds.Left + bounds.Width - 0.0001f, area.Top + 1) ? new Vector2f(pos.X - area.Width, pos.Y) : new Vector2f(pos.X + area.Width, pos.Y);
                    }
                }
            }

            // set velocity back to 0 for next frame
            entity.Velocity = new Vector2f(0, 0);
        }
Beispiel #2
0
        public Game()
        {
            View = new View
            {
                Size = new Vector2f(320, 240),
                Center = new Vector2f(320, 240)
            };
            _player = new Entity(EntityType.Player);
            _frameClock = new Clock();

            Map = new Map("testmap", 40, 30);

            for (var x = 0; x < Map.Size.X; x++)
            {
                for (var y = 0; y < Map.Size.Y; y++)
                {
                    Map.AddTile(TileType.Stonefloor, x, y, 0);
                }
            }

            Map.AmbientLightColor = new Color(70, 70, 70, 255);
            _player.Position = new Vector2f(20 * 32, 15 * 32);
            Map.AddEntity(_player);

            _playerLight = new PointLight(Color.White, 1.0f, 128, _player.Position);

            _mouseLight = new PointLight(Color.Red, 1.0f, 128, (Vector2f) InputHandler.MousePosition);

            Map.AddLight(_mouseLight);
            Map.AddLight(_playerLight);

            Map.AddTile(TileType.StonewallNorth, 22, 18, 1);
            Map.AddTile(TileType.StonewallNorth, 22, 14, 1);
            Map.AddTile(TileType.StonewallNorth, 18, 18, 1);
            Map.AddTile(TileType.StonewallNorth, 18, 14, 1);
            Map.AddTile(TileType.StonewallNorth, 20, 12, 1);
            Map.AddTile(TileType.StonewallNorth, 16, 16, 1);
            Map.AddTile(TileType.StonewallNorth, 20, 20, 1);
            Map.AddTile(TileType.StonewallNorth, 24, 16, 1);
        }
Beispiel #3
0
        public static Map Load(string filename)
        {
            var br = new BinaryReader(
                File.Open(filename, FileMode.Open));

            var version = br.ReadInt16();

            var name = br.ReadString();

            var size = new Vector2i(br.ReadInt32(), br.ReadInt32());

            var cellSize = br.ReadInt32();

            var entityCount = br.ReadInt32();
            var lightCount = br.ReadInt32();

            var map = new Map(name, size, cellSize);

            for (var i = 0; i < size.X; i++)
            {
                for (var j = 0; j < size.Y; j++)
                {
                    map.Layer0Cells[i, j] = MapCell.Read(br);
                    map.Layer0Cells[i, j].SetPosition(i * cellSize, j * cellSize);

                    map.Layer1Cells[i, j] = MapCell.Read(br);
                    map.Layer1Cells[i, j].SetPosition(i * cellSize, j * cellSize);

                    map.Layer2Cells[i, j] = MapCell.Read(br);
                    map.Layer2Cells[i, j].SetPosition(i * cellSize, j * cellSize);
                }
            }

            for (var i = 0; i < entityCount; i++)
                map.AddEntity(Entity.Read(br));

            map.AmbientLightColor = new Color(br.ReadByte(), br.ReadByte(), br.ReadByte());

            for (var i = 0; i < lightCount; i++)
                map.AddLight(PointLight.Read(br));

            br.Close();

            var sb = new StringBuilder();
            sb.AppendLine("Loaded map " + name);
            sb.AppendLine("\tVersion: " + version);
            sb.AppendLine("\tName: " + name);
            sb.AppendLine("\tSize: (" + size.X + ", " + size.Y + ")");
            sb.AppendLine("\tCell Size: " + cellSize);
            sb.AppendLine("\tEntity Count: " + entityCount);
            sb.AppendLine("\tLight Count: " + lightCount);

            Console.WriteLine(sb);

            return map;
        }
Beispiel #4
0
 public void Update(RenderTarget target, Map map)
 {
     Type.Input.Update(this);
     Type.Graphics.Update(this, target);
     Type.Physics.Update(this, map);
 }