feat: Added Player Objects w/ Movement Behaviour
This commit is contained in:
		
							
								
								
									
										2
									
								
								Engine
									
									
									
									
									
								
							
							
								
								
								
								
								
							
						
						
									
										2
									
								
								Engine
									
									
									
									
									
								
							 Submodule Engine updated: 5fcf63c5a7...d60537f940
									
								
							
							
								
								
									
										56
									
								
								Game/Behaviours/MovementBehaviour.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										56
									
								
								Game/Behaviours/MovementBehaviour.cs
									
									
									
									
									
										Normal file
									
								
							@@ -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;
 | 
			
		||||
}
 | 
			
		||||
@@ -24,6 +24,7 @@
 | 
			
		||||
  </ItemGroup>
 | 
			
		||||
  <ItemGroup>
 | 
			
		||||
    <ProjectReference Include="..\Engine\Engine.Core\Engine.Core.csproj" />
 | 
			
		||||
    <ProjectReference Include="..\Engine\Engine.Input\Engine.Input.csproj" />
 | 
			
		||||
  </ItemGroup>
 | 
			
		||||
  <Target Name="RestoreDotnetTools" BeforeTargets="Restore">
 | 
			
		||||
    <Message Text="Restoring dotnet tools" Importance="High" />
 | 
			
		||||
 
 | 
			
		||||
@@ -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<Texture2D>("Sprites/Pixel") };
 | 
			
		||||
        {
 | 
			
		||||
            IGameObject gameObject = gameManager.InstantiateGameObject<GameObject>();
 | 
			
		||||
            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<KeyboardInputsBehaviour>();
 | 
			
		||||
            gameObject.BehaviourController.AddBehaviour<MovementBehaviour>(Keys.S, Keys.W, 200f);
 | 
			
		||||
            gameObject.BehaviourController.AddBehaviour<DrawableSpriteBehaviour>().Assign(sprite);
 | 
			
		||||
        }
 | 
			
		||||
        {
 | 
			
		||||
            IGameObject gameObject = gameManager.InstantiateGameObject<GameObject>();
 | 
			
		||||
            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<KeyboardInputsBehaviour>();
 | 
			
		||||
            gameObject.BehaviourController.AddBehaviour<MovementBehaviour>(Keys.Down, Keys.Up, 200f);
 | 
			
		||||
            gameObject.BehaviourController.AddBehaviour<DrawableSpriteBehaviour>().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);
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user