feat: Implemented Basic Mechanics

This commit is contained in:
Syntriax 2024-01-28 15:45:39 +03:00
parent b3af693678
commit 45df32dabb
1 changed files with 53 additions and 42 deletions

View File

@ -23,7 +23,7 @@ public class Game1 : Game
public static GameManager gameManager = null!;
public static Sprite spriteBox = null!;
private MonoGameCameraBehaviour cameraBehaviour = null!;
private PongScoreboard pongScoreboard;
public Game1()
{
@ -57,6 +57,7 @@ public class Game1 : Game
{
_spriteBatch = new SpriteBatch(GraphicsDevice);
_shapeBatch = new ShapeBatch(GraphicsDevice, Content);
SpriteFont spriteFont = Content.Load<SpriteFont>("UbuntuMono");
// spriteBox = new Sprite() { Texture2D = Content.Load<Texture2D>("Sprites/Pixel") };
// Sprite spriteBall = new Sprite() { Texture2D = Content.Load<Texture2D>("Sprites/Circle") };
@ -69,13 +70,21 @@ public class Game1 : Game
cameraBehaviour.Viewport = GraphicsDevice.Viewport;
gameManager.Camera = cameraBehaviour;
////////////////////////////////////////////////////////////////////////////////////
IGameObject gameObjectPongManager = gameManager.InstantiateGameObject<GameObject>();
gameObjectPongManager.Name = "Pong Game Manager";
pongScoreboard = gameObjectPongManager.BehaviourController.AddBehaviour<PongScoreboard>(5);
////////////////////////////////////////////////////////////////////////////////////
IGameObject gameObjectBall = gameManager.InstantiateGameObject<GameObject>();
gameObjectBall.Name = "Ball";
gameObjectBall.Transform.Position = new Vector2D(0, 0f);
gameObjectBall.Transform.Scale = new Vector2D(10f, 10f);
gameObjectBall.BehaviourController.AddBehaviour<MovementBallBehaviour>(Vector2D.One, 500f);
gameObjectBall.BehaviourController.AddBehaviour<CircleBehaviour>(new Circle(Vector2D.Zero, 1f));
engine.AddRigidBody(gameObjectBall.BehaviourController.AddBehaviour<RigidBody2D>());
MovementBallBehaviour movementBallBehaviour = gameObjectBall.BehaviourController.AddBehaviour<MovementBallBehaviour>();
RigidBody2D rigidBodyBall = gameObjectBall.BehaviourController.AddBehaviour<RigidBody2D>();
engine.AddRigidBody(rigidBodyBall);
////////////////////////////////////////////////////////////////////////////////////
@ -123,6 +132,7 @@ public class Game1 : Game
gameObjectWallRight.Name = "WallRight";
gameObjectWallRight.Transform.Position = new Vector2D(522f, 0f);
gameObjectWallRight.Transform.Scale = new Vector2D(20f, 318f);
gameObjectWallRight.BehaviourController.AddBehaviour<ScoreBoundary>((Action)pongScoreboard.ScoreToLeft);
gameObjectWallRight.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box);
RigidBody2D rigidBodyWallRight = gameObjectWallRight.BehaviourController.AddBehaviour<RigidBody2D>();
rigidBodyWallRight.IsStatic = true;
@ -132,10 +142,49 @@ public class Game1 : Game
gameObjectWallLeft.Name = "WallLeft";
gameObjectWallLeft.Transform.Position = new Vector2D(-522f, 0f);
gameObjectWallLeft.Transform.Scale = new Vector2D(20f, 318f);
gameObjectWallLeft.BehaviourController.AddBehaviour<ScoreBoundary>((Action)pongScoreboard.ScoreToRight);
gameObjectWallLeft.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box);
RigidBody2D rigidBodyWallLeft = gameObjectWallLeft.BehaviourController.AddBehaviour<RigidBody2D>();
rigidBodyWallLeft.IsStatic = true;
engine.AddRigidBody(rigidBodyWallLeft);
////////////////////////////////////////////////////////////////////////////////////
///
///
IGameObject gameObjectLeftScoreText = gameManager.InstantiateGameObject<GameObject>();
gameObjectLeftScoreText.Name = "Score Left";
gameObjectLeftScoreText.Transform.Position = new Vector2D(-250f, 250f);
gameObjectLeftScoreText.Transform.Scale = Vector2D.One * .25f;
TextBehaviour textBehaviourLeft = gameObjectLeftScoreText.BehaviourController.AddBehaviour<TextBehaviour>();
textBehaviourLeft.Font = spriteFont;
textBehaviourLeft.Text = "0";
IGameObject gameObjectRightScoreText = gameManager.InstantiateGameObject<GameObject>();
gameObjectRightScoreText.Name = "Score Right";
gameObjectRightScoreText.Transform.Position = new Vector2D(250f, 250f);
gameObjectRightScoreText.Transform.Scale = Vector2D.One * .25f;
TextBehaviour textBehaviourRight = gameObjectRightScoreText.BehaviourController.AddBehaviour<TextBehaviour>();
textBehaviourRight.Font = spriteFont;
textBehaviourRight.Text = "0";
pongScoreboard.OnScored += () =>
{
gameObjectBall.Transform.Position = Vector2D.Zero;
textBehaviourLeft.Text = pongScoreboard.ScoreLeft.ToString();
textBehaviourRight.Text = pongScoreboard.ScoreRight.ToString();
};
pongScoreboard.OnFinished += () =>
{
rigidBodyBall.BehaviourController.GameObject.Transform.Position = Vector2D.Zero;
rigidBodyBall.Velocity = Vector2D.Zero;
};
pongScoreboard.OnReset += () =>
{
gameObjectBall.StateEnable.Enabled = true;
rigidBodyBall.Velocity = Vector2D.One * movementBallBehaviour.Speed;
textBehaviourLeft.Text = pongScoreboard.ScoreLeft.ToString();
textBehaviourRight.Text = pongScoreboard.ScoreRight.ToString();
};
}
protected override void Update(GameTime gameTime)
@ -170,56 +219,18 @@ public class Game1 : Game
if (Keyboard.GetState().IsKeyDown(Keys.E))
cameraBehaviour.BehaviourController.GameObject.Transform.Rotation -= gameTime.ElapsedGameTime.Nanoseconds * 0.000025f;
if (Keyboard.GetState().IsKeyDown(Keys.N))
{
seconds = 70f;
while (physicsTimer + 0.01f < seconds)
{
physicsTimer += 0.01f;
engine.Step(.01f);
}
}
if (Keyboard.GetState().IsKeyDown(Keys.M))
{
seconds = 0f;
while (physicsTimer - 0.01f > seconds)
{
physicsTimer -= 0.01f;
engine.Step(-.01f);
}
}
if (Keyboard.GetState().IsKeyDown(Keys.Space))
{
seconds += gameTime.ElapsedGameTime.Milliseconds * .005f;
while (physicsTimer + 0.01f < seconds)
{
Console.WriteLine($"Physics Timer: {physicsTimer}");
physicsTimer += 0.01f;
engine.Step(.01f);
}
}
if (Keyboard.GetState().IsKeyDown(Keys.B))
{
seconds -= gameTime.ElapsedGameTime.Milliseconds * .005f;
while (physicsTimer - 0.01f > seconds)
{
Console.WriteLine($"Physics Timer: {physicsTimer}");
physicsTimer -= 0.01f;
engine.Step(-.01f);
}
}
pongScoreboard.Reset();
gameManager.Update(gameTime.ToEngineTime());
while (physicsTimer + 0.01f < gameTime.TotalGameTime.TotalMilliseconds * .001f)//seconds)
{
// Console.WriteLine($"Physics Timer: {physicsTimer}");
physicsTimer += 0.01f;
engine.Step(.01f);
}
base.Update(gameTime);
}
static float physicsTimer = 0f;
static float seconds = 0f;
protected override void Draw(GameTime gameTime)
{