diff --git a/Source/Demos/Platformer/Components/Player.cs b/Source/Demos/Platformer/Components/Player.cs index f89bfc1b..90582b9e 100644 --- a/Source/Demos/Platformer/Components/Player.cs +++ b/Source/Demos/Platformer/Components/Player.cs @@ -10,7 +10,8 @@ namespace Platformer.Components public enum State { Idle, - Attacking, + Kicking, + Punching, Jumping, Falling, Walking, @@ -22,5 +23,7 @@ namespace Platformer.Components { public Facing Facing { get; set; } = Facing.Right; public State State { get; set; } + public bool IsAttacking => State == State.Kicking || State == State.Punching; + public bool CanJump => State == State.Idle || State == State.Walking; } } \ No newline at end of file diff --git a/Source/Demos/Platformer/Content/hero.png b/Source/Demos/Platformer/Content/hero.png index 118b0610..d3815449 100644 Binary files a/Source/Demos/Platformer/Content/hero.png and b/Source/Demos/Platformer/Content/hero.png differ diff --git a/Source/Demos/Platformer/EntityFactory.cs b/Source/Demos/Platformer/EntityFactory.cs index 961eea8f..24ccd2fe 100644 --- a/Source/Demos/Platformer/EntityFactory.cs +++ b/Source/Demos/Platformer/EntityFactory.cs @@ -34,8 +34,9 @@ namespace Platformer animationFactory.Add("jump", new SpriteSheetAnimationData(new[] { 10, 12 }, frameDuration: 1.0f, isLooping: false)); animationFactory.Add("fall", new SpriteSheetAnimationData(new[] { 13, 14 }, frameDuration: 1.0f, isLooping: false)); animationFactory.Add("swim", new SpriteSheetAnimationData(new[] { 18, 19, 20, 21, 22, 23 })); - animationFactory.Add("kick", new SpriteSheetAnimationData(new[] { 15 }, frameDuration: 0.5f, isLooping: false)); - animationFactory.Add("cool", new SpriteSheetAnimationData(new[] { 17 }, frameDuration: 0.5f, isLooping: false)); + animationFactory.Add("kick", new SpriteSheetAnimationData(new[] { 15 }, frameDuration: 0.3f, isLooping: false)); + animationFactory.Add("punch", new SpriteSheetAnimationData(new[] { 26 }, frameDuration: 0.3f, isLooping: false)); + animationFactory.Add("cool", new SpriteSheetAnimationData(new[] { 17 }, frameDuration: 0.3f, isLooping: false)); entity.Attach(new AnimatedSprite(animationFactory, "idle")); entity.Attach(new Transform2(position, 0, Vector2.One * 4)); entity.Attach(new Body { Position = position, Size = new Vector2(32, 64), BodyType = BodyType.Dynamic }); diff --git a/Source/Demos/Platformer/GameMain.cs b/Source/Demos/Platformer/GameMain.cs index 3d412b42..72576415 100644 --- a/Source/Demos/Platformer/GameMain.cs +++ b/Source/Demos/Platformer/GameMain.cs @@ -1,6 +1,8 @@ using Autofac; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; +using MonoGame.Extended; +using MonoGame.Extended.Entities; using MonoGame.Extended.Tiled; using MonoGame.Extended.Tiled.Renderers; using Platformer.Systems; @@ -12,6 +14,8 @@ namespace Platformer private TiledMap _map; private TiledMapRenderer _renderer; private EntityFactory _entityFactory; + private OrthographicCamera _camera; + private Entity _playerEntity; public GameMain() { @@ -19,7 +23,10 @@ namespace Platformer protected override void RegisterDependencies(ContainerBuilder builder) { + _camera = new OrthographicCamera(GraphicsDevice); + builder.RegisterInstance(new SpriteBatch(GraphicsDevice)); + builder.RegisterInstance(_camera); builder.RegisterType(); builder.RegisterType(); builder.RegisterType(); @@ -28,7 +35,7 @@ namespace Platformer protected override void LoadContent() { _entityFactory = new EntityFactory(EntityComponentSystem.EntityManager, Content); - _entityFactory.CreatePlayer(new Vector2(100, 240)); + _playerEntity = _entityFactory.CreatePlayer(new Vector2(100, 240)); // TOOD: Load maps and collision data more nicely :) _map = Content.Load("test-map"); @@ -63,13 +70,16 @@ namespace Platformer // Exit(); _renderer.Update(gameTime); + //_camera.LookAt(_playerEntity.Get().Position); base.Update(gameTime); } protected override void Draw(GameTime gameTime) { - _renderer.Draw(); + GraphicsDevice.Clear(Color.Black); + + _renderer.Draw(_camera.GetViewMatrix()); base.Draw(gameTime); } diff --git a/Source/Demos/Platformer/Systems/PlayerSystem.cs b/Source/Demos/Platformer/Systems/PlayerSystem.cs index 4cb27365..ee7f3783 100644 --- a/Source/Demos/Platformer/Systems/PlayerSystem.cs +++ b/Source/Demos/Platformer/Systems/PlayerSystem.cs @@ -23,20 +23,16 @@ namespace Platformer.Systems var body = entity.Get(); var keyboardState = KeyboardExtended.GetState(); - if (keyboardState.WasKeyJustUp(Keys.Up)) + if (player.CanJump) { - if (player.State != State.Jumping && player.State != State.Falling && player.State != State.Attacking) + if(keyboardState.WasKeyJustUp(Keys.Up)) body.Velocity.Y -= 550 + Math.Abs(body.Velocity.X) * 0.4f; - player.State = State.Jumping; - } - - if (keyboardState.WasKeyJustUp(Keys.Z)) - { - if(player.State == State.Idle) + if (keyboardState.WasKeyJustUp(Keys.Z)) + { body.Velocity.Y -= 550 + Math.Abs(body.Velocity.X) * 0.4f; - - player.State = State.Attacking; + player.State = player.State == State.Idle ? State.Punching : State.Kicking; + } } if (keyboardState.IsKeyDown(Keys.Right)) @@ -51,18 +47,18 @@ namespace Platformer.Systems player.Facing = Facing.Left; } - if (body.Velocity.Y < 0) - player.State = State.Jumping; - - if (body.Velocity.Y > 0) - player.State = State.Falling; - - if (player.State != State.Attacking) + if (!player.IsAttacking) { if (body.Velocity.X > 0 || body.Velocity.X < 0) player.State = State.Walking; + + if (body.Velocity.Y < 0) + player.State = State.Jumping; + + if (body.Velocity.Y > 0) + player.State = State.Falling; - if (body.Velocity.EqualsWithTolerence(Vector2.Zero, 5f)) + if (body.Velocity.EqualsWithTolerence(Vector2.Zero, 5)) player.State = State.Idle; } @@ -84,9 +80,12 @@ namespace Platformer.Systems case State.Idle: sprite.Play("idle"); break; - case State.Attacking: + case State.Kicking: sprite.Play("kick", () => player.State = State.Idle); break; + case State.Punching: + sprite.Play("punch", () => player.State = State.Idle); + break; case State.Cool: sprite.Play("cool"); break; diff --git a/Source/Demos/Platformer/Systems/RenderSystem.cs b/Source/Demos/Platformer/Systems/RenderSystem.cs index dc5f1eda..25683bb9 100644 --- a/Source/Demos/Platformer/Systems/RenderSystem.cs +++ b/Source/Demos/Platformer/Systems/RenderSystem.cs @@ -12,15 +12,17 @@ namespace Platformer.Systems public class RenderSystem : EntityProcessingSystem { private readonly SpriteBatch _spriteBatch; + private readonly OrthographicCamera _camera; - public RenderSystem(SpriteBatch spriteBatch) + public RenderSystem(SpriteBatch spriteBatch, OrthographicCamera camera) { _spriteBatch = spriteBatch; + _camera = camera; } protected override void Begin(GameTime gameTime) { - _spriteBatch.Begin(samplerState: SamplerState.PointClamp); + _spriteBatch.Begin(samplerState: SamplerState.PointClamp, transformMatrix: _camera.GetViewMatrix()); } protected override void Process(GameTime gameTime, Entity entity) diff --git a/Source/MonoGame.Extended/OrthographicCamera.cs b/Source/MonoGame.Extended/OrthographicCamera.cs index 9105fab1..2cb2ee71 100644 --- a/Source/MonoGame.Extended/OrthographicCamera.cs +++ b/Source/MonoGame.Extended/OrthographicCamera.cs @@ -35,7 +35,7 @@ namespace MonoGame.Extended public float Zoom { - get { return _zoom; } + get => _zoom; set { if ((value < MinimumZoom) || (value > MaximumZoom)) @@ -47,7 +47,7 @@ namespace MonoGame.Extended public float MinimumZoom { - get { return _minimumZoom; } + get => _minimumZoom; set { if (value < 0) @@ -62,7 +62,7 @@ namespace MonoGame.Extended public float MaximumZoom { - get { return _maximumZoom; } + get => _maximumZoom; set { if (value < 0)