refactor: Shortened Code

This commit is contained in:
Syntriax 2024-01-30 12:59:41 +03:00
parent fe4618abd3
commit 4cb9c7dc8d
3 changed files with 28 additions and 45 deletions

2
Engine

@ -1 +1 @@
Subproject commit 07666359f26d4761914aac05d73fdbe0020b0b6a Subproject commit 3817ebebfe3c3d328219656033f2518da6a59350

View File

@ -68,6 +68,9 @@ public class MonoGameCameraBehaviour : BehaviourOverride
set => Transform.Rotation = value; set => Transform.Rotation = value;
} }
protected override void OnFirstActiveFrame()
=> Viewport = GamePong.graphics.GraphicsDevice.Viewport;
protected override void OnPreDraw() protected override void OnPreDraw()
{ {
MatrixTransform = MatrixTransform =

View File

@ -59,10 +59,9 @@ public class GamePong : Game
shapeBatch = new ShapeBatch(GraphicsDevice, Content); shapeBatch = new ShapeBatch(GraphicsDevice, Content);
SpriteFont spriteFont = Content.Load<SpriteFont>("UbuntuMono"); SpriteFont spriteFont = Content.Load<SpriteFont>("UbuntuMono");
IGameObject gameObjectCamera = gameManager.InstantiateGameObject<GameObject>(); ////////////////////////////////////////////////////////////////////////////////////
gameObjectCamera.Name = "Camera";
gameObjectCamera.Transform.Position = Vector2D.Zero;
IGameObject gameObjectCamera = gameManager.InstantiateGameObject<GameObject>().SetGameObject("Camera"); ;
gameObjectCamera.BehaviourController.AddBehaviour<CameraController>(); gameObjectCamera.BehaviourController.AddBehaviour<CameraController>();
cameraBehaviour = gameObjectCamera.BehaviourController.AddBehaviour<MonoGameCameraBehaviour>(); cameraBehaviour = gameObjectCamera.BehaviourController.AddBehaviour<MonoGameCameraBehaviour>();
@ -70,16 +69,14 @@ public class GamePong : Game
//////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////
IGameObject gameObjectPongManager = gameManager.InstantiateGameObject<GameObject>(); IGameObject gameObjectPongManager = gameManager.InstantiateGameObject<GameObject>().SetGameObject("Pong Game Manager");
gameObjectPongManager.Name = "Pong Game Manager";
pongManager = gameObjectPongManager.BehaviourController.AddBehaviour<PongManager>(5); pongManager = gameObjectPongManager.BehaviourController.AddBehaviour<PongManager>(5);
//////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////
IGameObject gameObjectBall = gameManager.InstantiateGameObject<GameObject>(); IGameObject gameObjectBall = gameManager.InstantiateGameObject<GameObject>().SetGameObject("Ball");
gameObjectBall.Name = "Ball"; gameObjectBall.Transform.SetTransform(position: new Vector2D(0, 0f), scale: new Vector2D(10f, 10f));
gameObjectBall.Transform.Position = new Vector2D(0, 0f);
gameObjectBall.Transform.Scale = new Vector2D(10f, 10f);
gameObjectBall.BehaviourController.AddBehaviour<CircleBehaviour>(new Circle(Vector2D.Zero, 1f)); gameObjectBall.BehaviourController.AddBehaviour<CircleBehaviour>(new Circle(Vector2D.Zero, 1f));
PongBall ballBehaviour = gameObjectBall.BehaviourController.AddBehaviour<PongBall>(); PongBall ballBehaviour = gameObjectBall.BehaviourController.AddBehaviour<PongBall>();
RigidBody2D rigidBodyBall = gameObjectBall.BehaviourController.AddBehaviour<RigidBody2D>(); RigidBody2D rigidBodyBall = gameObjectBall.BehaviourController.AddBehaviour<RigidBody2D>();
@ -87,20 +84,17 @@ public class GamePong : Game
//////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////
IGameObject gameObjectLeftPaddle = gameManager.InstantiateGameObject<GameObject>(); IGameObject gameObjectLeftPaddle = gameManager.InstantiateGameObject<GameObject>().SetGameObject("Left Paddle");
gameObjectLeftPaddle.Name = "Left Paddle"; gameObjectLeftPaddle.Transform.SetTransform(position: new Vector2D(-468f, 0f), scale: new Vector2D(15f, 60f));
gameObjectLeftPaddle.Transform.Position = new Vector2D(-468f, 0f);
gameObjectLeftPaddle.Transform.Scale = new Vector2D(15f, 60f);
gameObjectLeftPaddle.BehaviourController.AddBehaviour<MovementBoxBehaviour>(Keys.W, Keys.S, 228f, -228f, 400f); gameObjectLeftPaddle.BehaviourController.AddBehaviour<MovementBoxBehaviour>(Keys.W, Keys.S, 228f, -228f, 400f);
gameObjectLeftPaddle.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box); gameObjectLeftPaddle.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box);
RigidBody2D rigidBodyLeftPaddle = gameObjectLeftPaddle.BehaviourController.AddBehaviour<RigidBody2D>(); RigidBody2D rigidBodyLeftPaddle = gameObjectLeftPaddle.BehaviourController.AddBehaviour<RigidBody2D>();
rigidBodyLeftPaddle.IsStatic = true; rigidBodyLeftPaddle.IsStatic = true;
engine.AddRigidBody(rigidBodyLeftPaddle); engine.AddRigidBody(rigidBodyLeftPaddle);
IGameObject gameObjectRightPaddle = gameManager.InstantiateGameObject<GameObject>(); IGameObject gameObjectRightPaddle = gameManager.InstantiateGameObject<GameObject>().SetGameObject("Right Paddle");
gameObjectRightPaddle.Name = "Right Paddle"; gameObjectRightPaddle.Transform.SetTransform(position: new Vector2D(468f, 0f), scale: new Vector2D(15f, 60f));
gameObjectRightPaddle.Transform.Position = new Vector2D(468f, 0f);
gameObjectRightPaddle.Transform.Scale = new Vector2D(15f, 60f);
gameObjectRightPaddle.BehaviourController.AddBehaviour<MovementBoxBehaviour>(Keys.Up, Keys.Down, 228f, -228f, 400f); gameObjectRightPaddle.BehaviourController.AddBehaviour<MovementBoxBehaviour>(Keys.Up, Keys.Down, 228f, -228f, 400f);
gameObjectRightPaddle.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box); gameObjectRightPaddle.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box);
RigidBody2D rigidBodyRightPaddle = gameObjectRightPaddle.BehaviourController.AddBehaviour<RigidBody2D>(); RigidBody2D rigidBodyRightPaddle = gameObjectRightPaddle.BehaviourController.AddBehaviour<RigidBody2D>();
@ -109,38 +103,30 @@ public class GamePong : Game
//////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////
IGameObject gameObjectWallTop = gameManager.InstantiateGameObject<GameObject>(); IGameObject gameObjectWallTop = gameManager.InstantiateGameObject<GameObject>().SetGameObject("Wall Top");
gameObjectWallTop.Name = "WallTop"; gameObjectWallTop.Transform.SetTransform(position: new Vector2D(0f, 308f), scale: new Vector2D(552f, 20f));
gameObjectWallTop.Transform.Position = new Vector2D(0f, 308f);
gameObjectWallTop.Transform.Scale = new Vector2D(552f, 20f);
gameObjectWallTop.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box); gameObjectWallTop.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box);
RigidBody2D rigidBodyWallTop = gameObjectWallTop.BehaviourController.AddBehaviour<RigidBody2D>(); RigidBody2D rigidBodyWallTop = gameObjectWallTop.BehaviourController.AddBehaviour<RigidBody2D>();
rigidBodyWallTop.IsStatic = true; rigidBodyWallTop.IsStatic = true;
engine.AddRigidBody(rigidBodyWallTop); engine.AddRigidBody(rigidBodyWallTop);
IGameObject gameObjectWallBottom = gameManager.InstantiateGameObject<GameObject>(); IGameObject gameObjectWallBottom = gameManager.InstantiateGameObject<GameObject>().SetGameObject("Wall Bottom");
gameObjectWallBottom.Name = "WallBottom"; gameObjectWallBottom.Transform.SetTransform(position: new Vector2D(0f, -308f), scale: new Vector2D(552f, 20f));
gameObjectWallBottom.Transform.Position = new Vector2D(0f, -308f);
gameObjectWallBottom.Transform.Scale = new Vector2D(552f, 20f);
gameObjectWallBottom.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box); gameObjectWallBottom.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box);
RigidBody2D rigidBodyWallBottom = gameObjectWallBottom.BehaviourController.AddBehaviour<RigidBody2D>(); RigidBody2D rigidBodyWallBottom = gameObjectWallBottom.BehaviourController.AddBehaviour<RigidBody2D>();
rigidBodyWallBottom.IsStatic = true; rigidBodyWallBottom.IsStatic = true;
engine.AddRigidBody(rigidBodyWallBottom); engine.AddRigidBody(rigidBodyWallBottom);
IGameObject gameObjectWallRight = gameManager.InstantiateGameObject<GameObject>(); IGameObject gameObjectWallRight = gameManager.InstantiateGameObject<GameObject>().SetGameObject("Wall Right");
gameObjectWallRight.Name = "WallRight"; gameObjectWallRight.Transform.SetTransform(position: new Vector2D(532f, 0f), scale: new Vector2D(20f, 328f));
gameObjectWallRight.Transform.Position = new Vector2D(532f, 0f);
gameObjectWallRight.Transform.Scale = new Vector2D(20f, 328f);
gameObjectWallRight.BehaviourController.AddBehaviour<ScoreBoundary>((Action)pongManager.ScoreToLeft); gameObjectWallRight.BehaviourController.AddBehaviour<ScoreBoundary>((Action)pongManager.ScoreToLeft);
gameObjectWallRight.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box); gameObjectWallRight.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box);
RigidBody2D rigidBodyWallRight = gameObjectWallRight.BehaviourController.AddBehaviour<RigidBody2D>(); RigidBody2D rigidBodyWallRight = gameObjectWallRight.BehaviourController.AddBehaviour<RigidBody2D>();
rigidBodyWallRight.IsStatic = true; rigidBodyWallRight.IsStatic = true;
engine.AddRigidBody(rigidBodyWallRight); engine.AddRigidBody(rigidBodyWallRight);
IGameObject gameObjectWallLeft = gameManager.InstantiateGameObject<GameObject>(); IGameObject gameObjectWallLeft = gameManager.InstantiateGameObject<GameObject>().SetGameObject("Wall Left");
gameObjectWallLeft.Name = "WallLeft"; gameObjectWallLeft.Transform.SetTransform(position: new Vector2D(-532f, 0f), scale: new Vector2D(20f, 328f));
gameObjectWallLeft.Transform.Position = new Vector2D(-532f, 0f);
gameObjectWallLeft.Transform.Scale = new Vector2D(20f, 328f);
gameObjectWallLeft.BehaviourController.AddBehaviour<ScoreBoundary>((Action)pongManager.ScoreToRight); gameObjectWallLeft.BehaviourController.AddBehaviour<ScoreBoundary>((Action)pongManager.ScoreToRight);
gameObjectWallLeft.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box); gameObjectWallLeft.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box);
RigidBody2D rigidBodyWallLeft = gameObjectWallLeft.BehaviourController.AddBehaviour<RigidBody2D>(); RigidBody2D rigidBodyWallLeft = gameObjectWallLeft.BehaviourController.AddBehaviour<RigidBody2D>();
@ -149,17 +135,14 @@ public class GamePong : Game
//////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////
IGameObject gameObjectLeftScoreText = gameManager.InstantiateGameObject<GameObject>(); IGameObject gameObjectLeftScoreText = gameManager.InstantiateGameObject<GameObject>().SetGameObject("Score Left");
gameObjectLeftScoreText.Name = "Score Left"; gameObjectLeftScoreText.Transform.SetTransform(position: new Vector2D(-250f, 250f), scale: Vector2D.One * .25f);
gameObjectLeftScoreText.Transform.Position = new Vector2D(-250f, 250f);
gameObjectLeftScoreText.Transform.Scale = Vector2D.One * .25f;
PongTextBehaviour textBehaviourLeft = gameObjectLeftScoreText.BehaviourController.AddBehaviour<PongTextBehaviour>(true); PongTextBehaviour textBehaviourLeft = gameObjectLeftScoreText.BehaviourController.AddBehaviour<PongTextBehaviour>(true);
textBehaviourLeft.Font = spriteFont; textBehaviourLeft.Font = spriteFont;
textBehaviourLeft.Text = "0"; textBehaviourLeft.Text = "0";
IGameObject gameObjectRightScoreText = gameManager.InstantiateGameObject<GameObject>();
gameObjectRightScoreText.Name = "Score Right"; IGameObject gameObjectRightScoreText = gameManager.InstantiateGameObject<GameObject>().SetGameObject("Score Right");
gameObjectRightScoreText.Transform.Position = new Vector2D(250f, 250f); gameObjectRightScoreText.Transform.SetTransform(position: new Vector2D(250f, 250f), scale: Vector2D.One * .25f);
gameObjectRightScoreText.Transform.Scale = Vector2D.One * .25f;
PongTextBehaviour textBehaviourRight = gameObjectRightScoreText.BehaviourController.AddBehaviour<PongTextBehaviour>(false); PongTextBehaviour textBehaviourRight = gameObjectRightScoreText.BehaviourController.AddBehaviour<PongTextBehaviour>(false);
textBehaviourRight.Font = spriteFont; textBehaviourRight.Font = spriteFont;
textBehaviourRight.Text = "0"; textBehaviourRight.Text = "0";
@ -184,9 +167,6 @@ public class GamePong : Game
{ {
GraphicsDevice.Clear(new Color() { R = 32, G = 32, B = 32 }); GraphicsDevice.Clear(new Color() { R = 32, G = 32, B = 32 });
// gameManager.Camera.Position = gameObjectBall.Transform.Position;
// Console.WriteLine($"Pos: {gameManager.Camera.Position}");
// TODO: Add your drawing code here // TODO: Add your drawing code here
gameManager.PreDraw(); gameManager.PreDraw();