示例#1
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);
        }
        public void Update(Entity entity, RenderTarget target)
        {
            var velocity = entity.Velocity;

            // south
            if (velocity.X == 0 && velocity.Y > 0)
            {
                _currentAnimation = _south;
            }
            // east
            if (velocity.X > 0 && velocity.Y == 0)
            {
                _currentAnimation = _east;
            }
            // north
            if (velocity.X == 0 && velocity.Y < 0)
            {
                _currentAnimation = _north;
            }
            // west
            if (velocity.X < 0 && velocity.Y == 0)
            {
                _currentAnimation = _west;
            }
            // _northwest
            if (velocity.X < 0 && velocity.Y < 0)
            {
                _currentAnimation = _northwest;
            }
            // northeast
            if (velocity.X > 0 && velocity.Y < 0)
            {
                _currentAnimation = _northeast;
            }
            // southwest
            if (velocity.X < 0 && velocity.Y > 0)
            {
                _currentAnimation = _southwest;
            }
            // southeast
            if (velocity.X > 0 && velocity.Y > 0)
            {
                _currentAnimation = _southeast;
            }

            _sprite.Animation = _currentAnimation;
            _sprite.Pause = false;

            // if not moving, stop animation
            if (velocity.X == 0 && velocity.Y == 0)
            {
                _sprite.Stop();
                _footstepMusic.Stop();
            }
            else
            {
                if (_footstepMusic.Status == SoundStatus.Stopped)
                {
                    _footstepMusic.Play();
                }
            }

            _sprite.Update(Game.DeltaTime);

            _sprite.Position = entity.Position;

            var spriteBounds = _sprite.GlobalBounds;
            var geoBounds = new FloatRect(spriteBounds.Left + 3, spriteBounds.Top + spriteBounds.Height - 3, spriteBounds.Width - 6, 3);

            entity.EntityBoundingBox = spriteBounds;
            entity.GeometryBoundingBox = geoBounds;

            _entityBoundingBoxOutline.Position = new Vector2f(spriteBounds.Left, spriteBounds.Top);
            _entityBoundingBoxOutline.Size = new Vector2f(spriteBounds.Width, spriteBounds.Height);

            _geometryBoundingBoxOutline.Position = new Vector2f(geoBounds.Left, geoBounds.Top);
            _geometryBoundingBoxOutline.Size = new Vector2f(geoBounds.Width, geoBounds.Height);

            target.Draw(_sprite);

            if (!State.ShowEntityBoundingBoxes) return;

            target.Draw(_entityBoundingBoxOutline);
            target.Draw(_geometryBoundingBoxOutline);
        }