feat: Movement Box Boundaries

This commit is contained in:
Syntriax 2023-11-27 17:06:18 +03:00
parent 1c248c0e97
commit 74077cd051
2 changed files with 8 additions and 4 deletions

View File

@ -6,10 +6,12 @@ using Syntriax.Engine.Input;
namespace Pong.Behaviours;
public class MovementBoxBehaviour(Keys Up, Keys Down, float Speed) : BehaviourOverride
public class MovementBoxBehaviour(Keys Up, Keys Down, float High, float Low, float Speed) : BehaviourOverride
{
private Keys Up { get; } = Up;
private Keys Down { get; } = Down;
public float High { get; } = High;
public float Low { get; } = Low;
public float Speed { get; set; } = Speed;
private bool isUpPressed = false;
@ -26,6 +28,8 @@ public class MovementBoxBehaviour(Keys Up, Keys Down, float Speed) : BehaviourOv
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;
GameObject.Transform.Position = new Vector2(GameObject.Transform.Position.X, MathF.Max(MathF.Min(GameObject.Transform.Position.Y, High), Low));
}
protected override void OnInitialize()

View File

@ -1,4 +1,4 @@
using System;
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
@ -59,7 +59,7 @@ public class Game1 : Game
gameObjectLeft.Transform.Position = new Vector2(-350f, 0f);
gameObjectLeft.Transform.Scale = new Vector2(10f, 40f);
gameObjectLeft.BehaviourController.AddBehaviour<KeyboardInputsBehaviour>();
gameObjectLeft.BehaviourController.AddBehaviour<MovementBoxBehaviour>(Keys.W, Keys.S, 400f);
gameObjectLeft.BehaviourController.AddBehaviour<MovementBoxBehaviour>(Keys.W, Keys.S, 268f, -268f, 400f);
gameObjectLeft.BehaviourController.AddBehaviour<DisplayableSpriteBehaviour>().Assign(spriteBox);
IGameObject gameObjectRight = gameManager.InstantiateGameObject<GameObject>();
@ -67,7 +67,7 @@ public class Game1 : Game
gameObjectRight.Transform.Position = new Vector2(350f, 0f);
gameObjectRight.Transform.Scale = new Vector2(10f, 40f);
gameObjectRight.BehaviourController.AddBehaviour<KeyboardInputsBehaviour>();
gameObjectRight.BehaviourController.AddBehaviour<MovementBoxBehaviour>(Keys.Up, Keys.Down, 400f);
gameObjectRight.BehaviourController.AddBehaviour<MovementBoxBehaviour>(Keys.Up, Keys.Down, 268f, -268f, 400f);
gameObjectRight.BehaviourController.AddBehaviour<DisplayableSpriteBehaviour>().Assign(spriteBox);
// TODO: use this.Content to load your game content here
}