Engine-Pong/Game/Game1.cs

91 lines
3.4 KiB
C#
Raw Normal View History

2023-11-23 23:59:03 +03:00
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Pong.Behaviours;
2023-11-23 23:59:03 +03:00
using Syntriax.Engine.Core;
using Syntriax.Engine.Core.Abstract;
using Syntriax.Engine.Core.Behaviours;
using Syntriax.Engine.Input;
2023-11-23 23:59:03 +03:00
namespace Pong;
public class Game1 : Game
{
private GraphicsDeviceManager _graphics;
private SpriteBatch _spriteBatch;
private GameManager gameManager = null!;
public Game1()
{
_graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
}
protected override void Initialize()
{
gameManager = new();
2023-11-23 23:59:03 +03:00
// TODO: Add your initialization logic here
gameManager.Initialize();
2023-11-23 23:59:03 +03:00
base.Initialize();
}
protected override void LoadContent()
{
_spriteBatch = new SpriteBatch(GraphicsDevice);
Sprite sprite = new Sprite() { Texture2D = Content.Load<Texture2D>("Sprites/Pixel") };
{
IGameObject gameObject = gameManager.InstantiateGameObject<GameObject>();
gameObject.Name = "Left";
gameObject.Transform.Position = new Vector2(Window.ClientBounds.Width * .1f, Window.ClientBounds.Height * .5f);
gameObject.Transform.Scale = new Vector2(Window.ClientBounds.Width * .02f, Window.ClientBounds.Height * .15f);
gameObject.BehaviourController.AddBehaviour<KeyboardInputsBehaviour>();
gameObject.BehaviourController.AddBehaviour<MovementBehaviour>(Keys.S, Keys.W, 200f);
gameObject.BehaviourController.AddBehaviour<DrawableSpriteBehaviour>().Assign(sprite);
}
{
IGameObject gameObject = gameManager.InstantiateGameObject<GameObject>();
gameObject.Name = "Right";
gameObject.Transform.Position = new Vector2(Window.ClientBounds.Width * .9f, Window.ClientBounds.Height * .5f);
gameObject.Transform.Scale = new Vector2(Window.ClientBounds.Width * .02f, Window.ClientBounds.Height * .15f);
gameObject.BehaviourController.AddBehaviour<KeyboardInputsBehaviour>();
gameObject.BehaviourController.AddBehaviour<MovementBehaviour>(Keys.Down, Keys.Up, 200f);
gameObject.BehaviourController.AddBehaviour<DrawableSpriteBehaviour>().Assign(sprite);
}
2023-11-23 23:59:03 +03:00
// 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();
}
2023-11-23 23:59:03 +03:00
// TODO: Add your update logic here
gameManager.Update(gameTime);
2023-11-23 23:59:03 +03:00
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(new Color() { R = 32, G = 32, B = 32 });
2023-11-23 23:59:03 +03:00
// TODO: Add your drawing code here
gameManager.PreDraw(gameTime);
gameManager.Draw(_spriteBatch);
2023-11-23 23:59:03 +03:00
base.Draw(gameTime);
}
}