diff --git a/Engine b/Engine index 5fcf63c..d60537f 160000 --- a/Engine +++ b/Engine @@ -1 +1 @@ -Subproject commit 5fcf63c5a70f031fb7a808723fb8a25adb19ab39 +Subproject commit d60537f9406ae3615c7cdcd6f986cf368fa77cfb diff --git a/Game/Behaviours/MovementBehaviour.cs b/Game/Behaviours/MovementBehaviour.cs new file mode 100644 index 0000000..a04888b --- /dev/null +++ b/Game/Behaviours/MovementBehaviour.cs @@ -0,0 +1,56 @@ +using System; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Input; +using Syntriax.Engine.Core; +using Syntriax.Engine.Input; + +namespace Pong.Behaviours; + +public class MovementBehaviour(Keys Up, Keys Down, float Speed) : BehaviourOverride +{ + private Keys Up { get; } = Up; + private Keys Down { get; } = Down; + public float Speed { get; set; } = Speed; + + private bool isUpPressed = false; + private bool isDownPressed = false; + + private IKeyboardInputs inputs = null!; + + protected override void OnUpdate(GameTime time) + { + if (isUpPressed && isDownPressed) + return; + + if (isUpPressed) + GameObject.Transform.Position = GameObject.Transform.Position + Vector2.UnitY * (float)time.ElapsedGameTime.TotalSeconds * Speed; + else if (isDownPressed) + GameObject.Transform.Position = GameObject.Transform.Position + -Vector2.UnitY * (float)time.ElapsedGameTime.TotalSeconds * Speed; + } + + protected override void OnInitialize() + { + if (!BehaviourController.TryGetBehaviour(out inputs)) + throw new Exception($"{nameof(IKeyboardInputs)} is missing on ${GameObject.Name}."); + + inputs.RegisterOnPress(Up, OnUpPressed); + inputs.RegisterOnRelease(Up, OnUpReleased); + + inputs.RegisterOnPress(Down, OnDownPressed); + inputs.RegisterOnRelease(Down, OnDownReleased); + } + + protected override void OnFinalize() + { + inputs.UnregisterOnPress(Up, OnUpPressed); + inputs.UnregisterOnRelease(Up, OnUpReleased); + + inputs.UnregisterOnPress(Down, OnDownPressed); + inputs.UnregisterOnRelease(Down, OnDownReleased); + } + + private void OnUpPressed(IKeyboardInputs inputs, Keys keys) => isUpPressed = true; + private void OnUpReleased(IKeyboardInputs inputs, Keys keys) => isUpPressed = false; + private void OnDownPressed(IKeyboardInputs inputs, Keys keys) => isDownPressed = true; + private void OnDownReleased(IKeyboardInputs inputs, Keys keys) => isDownPressed = false; +} diff --git a/Game/Game.csproj b/Game/Game.csproj index 112ea9e..ada7328 100644 --- a/Game/Game.csproj +++ b/Game/Game.csproj @@ -24,6 +24,7 @@ + diff --git a/Game/Game1.cs b/Game/Game1.cs index a564d67..aff900a 100644 --- a/Game/Game1.cs +++ b/Game/Game1.cs @@ -1,7 +1,11 @@ using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; +using Pong.Behaviours; using Syntriax.Engine.Core; +using Syntriax.Engine.Core.Abstract; +using Syntriax.Engine.Core.Behaviours; +using Syntriax.Engine.Input; namespace Pong; @@ -21,7 +25,9 @@ public class Game1 : Game protected override void Initialize() { gameManager = new(); + // TODO: Add your initialization logic here + gameManager.Initialize(); base.Initialize(); } @@ -30,6 +36,25 @@ public class Game1 : Game { _spriteBatch = new SpriteBatch(GraphicsDevice); + Sprite sprite = new Sprite() { Texture2D = Content.Load("Sprites/Pixel") }; + { + IGameObject gameObject = gameManager.InstantiateGameObject(); + gameObject.Name = "Left"; + gameObject.Transform.Position = new Vector2(Window.ClientBounds.Width * .1f, Window.ClientBounds.Height * .5f); + gameObject.Transform.Scale = new Vector2(Window.ClientBounds.Width * .02f, Window.ClientBounds.Height * .15f); + gameObject.BehaviourController.AddBehaviour(); + gameObject.BehaviourController.AddBehaviour(Keys.S, Keys.W, 200f); + gameObject.BehaviourController.AddBehaviour().Assign(sprite); + } + { + IGameObject gameObject = gameManager.InstantiateGameObject(); + gameObject.Name = "Right"; + gameObject.Transform.Position = new Vector2(Window.ClientBounds.Width * .9f, Window.ClientBounds.Height * .5f); + gameObject.Transform.Scale = new Vector2(Window.ClientBounds.Width * .02f, Window.ClientBounds.Height * .15f); + gameObject.BehaviourController.AddBehaviour(); + gameObject.BehaviourController.AddBehaviour(Keys.Down, Keys.Up, 200f); + gameObject.BehaviourController.AddBehaviour().Assign(sprite); + } // TODO: use this.Content to load your game content here } @@ -37,17 +62,28 @@ public class Game1 : Game { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) Exit(); + if (Keyboard.GetState().IsKeyDown(Keys.F)) + { + _graphics.PreferMultiSampling = false; + _graphics.PreferredBackBufferWidth = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width; + _graphics.PreferredBackBufferHeight = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height; + _graphics.IsFullScreen = true; + _graphics.ApplyChanges(); + } // TODO: Add your update logic here + gameManager.Update(gameTime); base.Update(gameTime); } protected override void Draw(GameTime gameTime) { - GraphicsDevice.Clear(Color.CornflowerBlue); + GraphicsDevice.Clear(new Color() { R = 32, G = 32, B = 32 }); // TODO: Add your drawing code here + gameManager.PreDraw(gameTime); + gameManager.Draw(_spriteBatch); base.Draw(gameTime); }