Engine-Pong/Game/Game1.cs

120 lines
4.8 KiB
C#

using System;
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;
public class Game1 : Game
{
private GraphicsDeviceManager _graphics;
private SpriteBatch _spriteBatch;
private GameManager gameManager = null!;
private GameObject gameObjectLeft;
private GameObject gameObjectRight;
public Game1()
{
_graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
}
protected override void Initialize()
{
gameManager = new();
// TODO: Add your initialization logic here
gameManager.Initialize();
base.Initialize();
}
protected override void LoadContent()
{
_spriteBatch = new SpriteBatch(GraphicsDevice);
Texture2D texture2D = Content.Load<Texture2D>("Sprites/Pixel");
Sprite spriteRight = new Sprite() { Texture2D = texture2D, Color = Color.Gold };
Sprite spriteLeft = new Sprite() { Texture2D = texture2D, Color = Color.AliceBlue };
gameObjectLeft = gameManager.InstantiateGameObject<GameObject>();
gameObjectLeft.Name = "Left";
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.BehaviourController.AddBehaviour<KeyboardInputsBehaviour>();
gameObjectLeft.BehaviourController.AddBehaviour<MovementBehaviour>(Keys.S, Keys.W, 200f);
gameObjectLeft.BehaviourController.AddBehaviour<DrawableSpriteBehaviour>().Assign(spriteLeft);
gameObjectRight = gameManager.InstantiateGameObject<GameObject>();
gameObjectRight.Name = "Right";
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.BehaviourController.AddBehaviour<KeyboardInputsBehaviour>();
gameObjectRight.BehaviourController.AddBehaviour<MovementBehaviour>(Keys.Down, Keys.Up, 200f);
gameObjectRight.BehaviourController.AddBehaviour<DrawableSpriteBehaviour>().Assign(spriteRight);
// TODO: use this.Content to load your game content here
}
protected override void Update(GameTime gameTime)
{
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();
}
if (Keyboard.GetState().IsKeyDown(Keys.H))
{
gameManager.RemoveGameObject(gameObjectLeft);
gameManager.RemoveGameObject(gameObjectRight);
IBehaviourController lbc = gameObjectLeft.BehaviourController;
IBehaviourController rbc = gameObjectRight.BehaviourController;
Console.WriteLine($"LeftBehaviourController: {lbc.GameObject.Name}");
Console.WriteLine($"RightBehaviourController: {rbc.GameObject.Name}");
Console.WriteLine($"gameObjectLeft.Assign(rbc): {gameObjectLeft.Assign(rbc)}");
Console.WriteLine($"gameObjectRight.Assign(lbc): {gameObjectRight.Assign(lbc)}");
Console.WriteLine($"lbc.Assign(gameObjectRight): {lbc.Assign(gameObjectRight)}");
Console.WriteLine($"rbc.Assign(gameObjectLeft): {rbc.Assign(gameObjectLeft)}");
gameManager.RegisterGameObject(gameObjectLeft);
gameManager.RegisterGameObject(gameObjectRight);
Console.WriteLine($"LeftBehaviourController: {lbc.GameObject.Name}");
Console.WriteLine($"RightBehaviourController: {rbc.GameObject.Name}");
Console.WriteLine($"//////////////////////////////////////////////////");
}
// TODO: Add your update logic here
gameManager.Update(gameTime);
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
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);
}
}