feat: Basic Ball Movement

This commit is contained in:
Syntriax 2023-11-27 12:00:25 +03:00
parent 49566e6981
commit e134192187
3 changed files with 24 additions and 3 deletions

View File

@ -0,0 +1,20 @@
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Syntriax.Engine.Core;
using Syntriax.Engine.Input;
namespace Pong.Behaviours;
public class MovementBallBehaviour(Vector2 StartDirection, float Speed) : BehaviourOverride
{
public Vector2 StartDirection { get; } = StartDirection;
public float Speed { get; set; } = Speed;
protected override void OnInitialize() => StartDirection.Normalize();
protected override void OnUpdate(GameTime time)
{
GameObject.Transform.Position += StartDirection * (time.ElapsedGameTime.Nanoseconds * .001f) * Speed;
}
}

View File

@ -6,7 +6,7 @@ using Syntriax.Engine.Input;
namespace Pong.Behaviours; namespace Pong.Behaviours;
public class MovementBehaviour(Keys Up, Keys Down, float Speed) : BehaviourOverride public class MovementBoxBehaviour(Keys Up, Keys Down, float Speed) : BehaviourOverride
{ {
private Keys Up { get; } = Up; private Keys Up { get; } = Up;
private Keys Down { get; } = Down; private Keys Down { get; } = Down;

View File

@ -44,6 +44,7 @@ public class Game1 : Game
gameObjectBall.Name = "Ball"; gameObjectBall.Name = "Ball";
gameObjectBall.Transform.Position = new Vector2(Window.ClientBounds.Width * .5f, Window.ClientBounds.Height * .5f); gameObjectBall.Transform.Position = new Vector2(Window.ClientBounds.Width * .5f, Window.ClientBounds.Height * .5f);
gameObjectBall.Transform.Scale = new Vector2(.025f, .025f); gameObjectBall.Transform.Scale = new Vector2(.025f, .025f);
gameObjectBall.BehaviourController.AddBehaviour<MovementBallBehaviour>(new Vector2(.1f, .1f), 100f);
gameObjectBall.BehaviourController.AddBehaviour<DisplayableSpriteBehaviour>().Assign(spriteBall); gameObjectBall.BehaviourController.AddBehaviour<DisplayableSpriteBehaviour>().Assign(spriteBall);
IGameObject gameObjectLeft = gameManager.InstantiateGameObject<GameObject>(); IGameObject gameObjectLeft = gameManager.InstantiateGameObject<GameObject>();
@ -51,7 +52,7 @@ public class Game1 : Game
gameObjectLeft.Transform.Position = new Vector2(Window.ClientBounds.Width * .1f, Window.ClientBounds.Height * .5f); gameObjectLeft.Transform.Position = new Vector2(Window.ClientBounds.Width * .1f, Window.ClientBounds.Height * .5f);
gameObjectLeft.Transform.Scale = new Vector2(Window.ClientBounds.Width * .02f, Window.ClientBounds.Height * .15f); gameObjectLeft.Transform.Scale = new Vector2(Window.ClientBounds.Width * .02f, Window.ClientBounds.Height * .15f);
gameObjectLeft.BehaviourController.AddBehaviour<KeyboardInputsBehaviour>(); gameObjectLeft.BehaviourController.AddBehaviour<KeyboardInputsBehaviour>();
gameObjectLeft.BehaviourController.AddBehaviour<MovementBehaviour>(Keys.S, Keys.W, 200f); gameObjectLeft.BehaviourController.AddBehaviour<MovementBoxBehaviour>(Keys.S, Keys.W, 200f);
gameObjectLeft.BehaviourController.AddBehaviour<DisplayableSpriteBehaviour>().Assign(spriteBox); gameObjectLeft.BehaviourController.AddBehaviour<DisplayableSpriteBehaviour>().Assign(spriteBox);
IGameObject gameObjectRight = gameManager.InstantiateGameObject<GameObject>(); IGameObject gameObjectRight = gameManager.InstantiateGameObject<GameObject>();
@ -59,7 +60,7 @@ public class Game1 : Game
gameObjectRight.Transform.Position = new Vector2(Window.ClientBounds.Width * .9f, Window.ClientBounds.Height * .5f); gameObjectRight.Transform.Position = new Vector2(Window.ClientBounds.Width * .9f, Window.ClientBounds.Height * .5f);
gameObjectRight.Transform.Scale = new Vector2(Window.ClientBounds.Width * .02f, Window.ClientBounds.Height * .15f); gameObjectRight.Transform.Scale = new Vector2(Window.ClientBounds.Width * .02f, Window.ClientBounds.Height * .15f);
gameObjectRight.BehaviourController.AddBehaviour<KeyboardInputsBehaviour>(); gameObjectRight.BehaviourController.AddBehaviour<KeyboardInputsBehaviour>();
gameObjectRight.BehaviourController.AddBehaviour<MovementBehaviour>(Keys.Down, Keys.Up, 200f); gameObjectRight.BehaviourController.AddBehaviour<MovementBoxBehaviour>(Keys.Down, Keys.Up, 200f);
gameObjectRight.BehaviourController.AddBehaviour<DisplayableSpriteBehaviour>().Assign(spriteBox); gameObjectRight.BehaviourController.AddBehaviour<DisplayableSpriteBehaviour>().Assign(spriteBox);
// TODO: use this.Content to load your game content here // TODO: use this.Content to load your game content here
} }