2023-11-27 17:06:33 +03:00
using System ;
2023-11-24 17:48:50 +03:00
using Microsoft.Xna.Framework ;
2023-11-23 23:59:03 +03:00
using Microsoft.Xna.Framework.Graphics ;
using Microsoft.Xna.Framework.Input ;
2023-11-24 15:50:23 +03:00
using Pong.Behaviours ;
2023-11-23 23:59:03 +03:00
using Syntriax.Engine.Core ;
2023-11-24 15:50:23 +03:00
using Syntriax.Engine.Core.Abstract ;
2023-11-27 10:38:02 +03:00
using Syntriax.Engine.Graphics.TwoDimensional ;
2023-11-24 15:50:23 +03:00
using Syntriax.Engine.Input ;
2023-12-01 17:42:07 +03:00
using Syntriax.Engine.Physics2D ;
2023-11-23 23:59:03 +03:00
namespace Pong ;
public class Game1 : Game
{
2023-11-27 17:41:21 +03:00
private GraphicsDeviceManager _graphics = null ! ;
2023-12-01 17:42:07 +03:00
private PhysicsEngine2D engine ;
2023-11-27 17:41:21 +03:00
private SpriteBatch _spriteBatch = null ! ;
2023-12-04 17:48:22 +03:00
public static GameManager gameManager = null ! ;
public static Sprite spriteBox = null ! ;
2023-11-23 23:59:03 +03:00
public Game1 ( )
{
2023-11-27 17:06:33 +03:00
_graphics = new GraphicsDeviceManager ( this )
{
PreferredBackBufferWidth = 1024 ,
PreferredBackBufferHeight = 576
} ;
2023-11-27 17:41:21 +03:00
2023-11-23 23:59:03 +03:00
Content . RootDirectory = "Content" ;
IsMouseVisible = true ;
}
protected override void Initialize ( )
{
gameManager = new ( ) ;
2023-11-24 15:50:23 +03:00
2023-11-23 23:59:03 +03:00
// TODO: Add your initialization logic here
2023-11-24 15:50:23 +03:00
gameManager . Initialize ( ) ;
2023-11-23 23:59:03 +03:00
base . Initialize ( ) ;
}
protected override void LoadContent ( )
{
2023-12-01 17:42:07 +03:00
engine = new PhysicsEngine2D ( ) ;
2023-11-30 10:48:57 +03:00
_spriteBatch = new SpriteBatch ( GraphicsDevice ) ;
2023-12-04 17:48:22 +03:00
spriteBox = new Sprite ( ) { Texture2D = Content . Load < Texture2D > ( "Sprites/Pixel" ) } ;
2023-11-27 11:49:05 +03:00
Sprite spriteBall = new Sprite ( ) { Texture2D = Content . Load < Texture2D > ( "Sprites/Circle" ) } ;
2023-11-27 13:46:21 +03:00
IGameObject gameObjectCamera = gameManager . InstantiateGameObject < GameObject > ( ) ;
gameObjectCamera . Name = "Camera" ;
2023-11-27 15:14:26 +03:00
gameObjectCamera . Transform . Position = Vector2 . Zero ;
2023-11-27 13:46:21 +03:00
gameManager . Camera = gameObjectCamera . BehaviourController . AddBehaviour < CameraBehaviour > ( ) ;
gameManager . Camera . Viewport = GraphicsDevice . Viewport ;
2023-11-27 11:49:05 +03:00
IGameObject gameObjectBall = gameManager . InstantiateGameObject < GameObject > ( ) ;
gameObjectBall . Name = "Ball" ;
2023-11-27 15:14:26 +03:00
gameObjectBall . Transform . Position = Vector2 . Zero ;
gameObjectBall . Transform . Scale = new Vector2 ( 1f / 51.2f , 1f / 51.2f ) ;
2023-12-01 17:42:07 +03:00
engine . AddRigidBody ( gameObjectBall . BehaviourController . AddBehaviour < RigidBody2D > ( ) ) ;
2023-12-04 13:35:31 +03:00
gameObjectBall . BehaviourController . AddBehaviour < Collider2DBehaviour > ( ( System . Collections . Generic . IList < Vector2 > ) [ new Vector2 ( 1 , 1 ) * 5f , new Vector2 ( - 1 , 1 ) * 5f , new Vector2 ( 1 , - 1 ) * 5f , new Vector2 ( - 1 , - 1 ) * 5f ] ) . OffsetScale = Vector2 . One * 51.2f ;
2023-12-04 14:06:52 +03:00
gameObjectBall . BehaviourController . AddBehaviour < MovementBallBehaviour > ( new Vector2 ( . 1f , . 1f ) , 500f ) ;
2023-11-27 11:49:05 +03:00
gameObjectBall . BehaviourController . AddBehaviour < DisplayableSpriteBehaviour > ( ) . Assign ( spriteBall ) ;
2023-12-04 14:06:52 +03:00
IGameObject gameObjectLeft = gameManager . InstantiateGameObject < GameObject > ( ) ;
gameObjectLeft . Name = "Left" ;
gameObjectLeft . Transform . Position = new Vector2 ( - 452 , 0f ) ;
gameObjectLeft . Transform . Scale = new Vector2 ( 10f , 40f ) ;
gameObjectLeft . BehaviourController . AddBehaviour < KeyboardInputsBehaviour > ( ) ;
gameObjectLeft . BehaviourController . AddBehaviour < MovementBoxBehaviour > ( Keys . W , Keys . S , 268f , - 268f , 400f ) ;
gameObjectLeft . BehaviourController . AddBehaviour < DisplayableSpriteBehaviour > ( ) . Assign ( spriteBox ) ;
gameObjectLeft . BehaviourController . AddBehaviour < Collider2DBehaviour > ( ( System . Collections . Generic . IList < Vector2 > ) [ new Vector2 ( . 5f , . 5f ) , new Vector2 ( - . 5f , . 5f ) , new Vector2 ( . 5f , - . 5f ) , new Vector2 ( - . 5f , - . 5f ) ] ) ;
engine . AddRigidBody ( gameObjectLeft . BehaviourController . AddBehaviour < RigidBody2D > ( ) ) ;
IGameObject gameObjectRight = gameManager . InstantiateGameObject < GameObject > ( ) ;
gameObjectRight . Name = "Right" ;
gameObjectRight . Transform . Position = new Vector2 ( 452 , 0f ) ;
gameObjectRight . Transform . Scale = new Vector2 ( 10f , 40f ) ;
gameObjectRight . BehaviourController . AddBehaviour < KeyboardInputsBehaviour > ( ) ;
gameObjectRight . BehaviourController . AddBehaviour < MovementBoxBehaviour > ( Keys . Up , Keys . Down , 268f , - 268f , 400f ) ;
gameObjectRight . BehaviourController . AddBehaviour < DisplayableSpriteBehaviour > ( ) . Assign ( spriteBox ) ;
gameObjectRight . BehaviourController . AddBehaviour < Collider2DBehaviour > ( ( System . Collections . Generic . IList < Vector2 > ) [ new Vector2 ( . 5f , . 5f ) , new Vector2 ( - . 5f , . 5f ) , new Vector2 ( . 5f , - . 5f ) , new Vector2 ( - . 5f , - . 5f ) ] ) ;
engine . AddRigidBody ( gameObjectRight . BehaviourController . AddBehaviour < RigidBody2D > ( ) ) ;
IGameObject goPlayAreaTop = gameManager . InstantiateGameObject < GameObject > ( ) ;
goPlayAreaTop . Transform . Position = new Vector2 ( 0f , 288f + 20f ) ;
2023-12-04 17:48:22 +03:00
goPlayAreaTop . Transform . Scale = new Vector2 ( 10240f , 40f ) ;
goPlayAreaTop . BehaviourController . AddBehaviour < DisplayableSpriteBehaviour > ( ) . Assign ( spriteBox ) ;
2023-12-04 14:06:52 +03:00
goPlayAreaTop . BehaviourController . AddBehaviour < Collider2DBehaviour > ( ( System . Collections . Generic . IList < Vector2 > ) [ new Vector2 ( . 5f , . 5f ) , new Vector2 ( - . 5f , . 5f ) , new Vector2 ( . 5f , - . 5f ) , new Vector2 ( - . 5f , - . 5f ) ] ) ;
engine . AddRigidBody ( goPlayAreaTop . BehaviourController . AddBehaviour < RigidBody2D > ( ) ) ;
IGameObject goPlayAreaBottom = gameManager . InstantiateGameObject < GameObject > ( ) ;
goPlayAreaBottom . Transform . Position = new Vector2 ( 0f , - ( 288f + 20f ) ) ;
2023-12-04 17:48:22 +03:00
goPlayAreaBottom . Transform . Scale = new Vector2 ( 10240f , 40f ) ;
goPlayAreaBottom . BehaviourController . AddBehaviour < DisplayableSpriteBehaviour > ( ) . Assign ( spriteBox ) ;
2023-12-04 14:06:52 +03:00
goPlayAreaBottom . BehaviourController . AddBehaviour < Collider2DBehaviour > ( ( System . Collections . Generic . IList < Vector2 > ) [ new Vector2 ( . 5f , . 5f ) , new Vector2 ( - . 5f , . 5f ) , new Vector2 ( . 5f , - . 5f ) , new Vector2 ( - . 5f , - . 5f ) ] ) ;
engine . AddRigidBody ( goPlayAreaBottom . BehaviourController . AddBehaviour < RigidBody2D > ( ) ) ;
IGameObject goPlayAreaRight = gameManager . InstantiateGameObject < GameObject > ( ) ;
goPlayAreaRight . Transform . Position = new Vector2 ( 512f + 20f , 0f ) ;
2023-12-04 17:48:22 +03:00
goPlayAreaRight . Transform . Scale = new Vector2 ( 40f , 5760f ) ;
goPlayAreaRight . BehaviourController . AddBehaviour < DisplayableSpriteBehaviour > ( ) . Assign ( spriteBox ) ;
2023-12-04 14:06:52 +03:00
goPlayAreaRight . BehaviourController . AddBehaviour < Collider2DBehaviour > ( ( System . Collections . Generic . IList < Vector2 > ) [ new Vector2 ( . 5f , . 5f ) , new Vector2 ( - . 5f , . 5f ) , new Vector2 ( . 5f , - . 5f ) , new Vector2 ( - . 5f , - . 5f ) ] ) ;
engine . AddRigidBody ( goPlayAreaRight . BehaviourController . AddBehaviour < RigidBody2D > ( ) ) ;
IGameObject goPlayAreaLeft = gameManager . InstantiateGameObject < GameObject > ( ) ;
goPlayAreaLeft . Transform . Position = new Vector2 ( - ( 512f + 20f ) , 0f ) ;
2023-12-04 17:48:22 +03:00
goPlayAreaLeft . Transform . Scale = new Vector2 ( 40f , 5760f ) ;
goPlayAreaLeft . BehaviourController . AddBehaviour < DisplayableSpriteBehaviour > ( ) . Assign ( spriteBox ) ;
2023-12-04 14:06:52 +03:00
goPlayAreaLeft . BehaviourController . AddBehaviour < Collider2DBehaviour > ( ( System . Collections . Generic . IList < Vector2 > ) [ new Vector2 ( . 5f , . 5f ) , new Vector2 ( - . 5f , . 5f ) , new Vector2 ( . 5f , - . 5f ) , new Vector2 ( - . 5f , - . 5f ) ] ) ;
engine . AddRigidBody ( goPlayAreaLeft . BehaviourController . AddBehaviour < RigidBody2D > ( ) ) ;
2023-12-05 14:47:17 +03:00
// IGameObject goPlayAreaCenter = gameManager.InstantiateGameObject<GameObject>();
// goPlayAreaCenter.Transform.Position = new Vector2(100f, 100f);
// goPlayAreaCenter.Transform.Scale = new Vector2(40f, 40f);
// // goPlayAreaCenter.BehaviourController.AddBehaviour<DisplayableSpriteBehaviour>().Assign(spriteBox);
// goPlayAreaCenter.BehaviourController.AddBehaviour<Collider2DBehaviour>((System.Collections.Generic.IList<Vector2>)[new Vector2(.5f, .5f), new Vector2(-.5f, .5f), new Vector2(.5f, -.5f), new Vector2(-.5f, -.5f)]);
// engine.AddRigidBody(goPlayAreaCenter.BehaviourController.AddBehaviour<RigidBody2D>());
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 ( ) ;
2023-11-24 17:48:50 +03:00
2023-11-24 15:50:23 +03:00
if ( Keyboard . GetState ( ) . IsKeyDown ( Keys . F ) )
{
2023-11-27 15:14:26 +03:00
if ( _graphics . IsFullScreen )
return ;
2023-11-24 15:50:23 +03:00
_graphics . PreferMultiSampling = false ;
2023-11-27 13:46:21 +03:00
_graphics . PreferredBackBufferWidth = GraphicsDevice . Adapter . CurrentDisplayMode . Width ;
_graphics . PreferredBackBufferHeight = GraphicsDevice . Adapter . CurrentDisplayMode . Height ;
2023-11-24 15:50:23 +03:00
_graphics . IsFullScreen = true ;
_graphics . ApplyChanges ( ) ;
2023-11-27 15:14:26 +03:00
float previousScreenSize = MathF . Sqrt ( MathF . Pow ( gameManager . Camera . Viewport . Width , 2f ) + MathF . Pow ( gameManager . Camera . Viewport . Height , 2f ) ) ;
float currentScreenSize = MathF . Sqrt ( MathF . Pow ( GraphicsDevice . Viewport . Width , 2f ) + MathF . Pow ( GraphicsDevice . Viewport . Height , 2f ) ) ;
gameManager . Camera . Zoom / = previousScreenSize / currentScreenSize ;
2023-11-27 13:46:21 +03:00
gameManager . Camera . Viewport = GraphicsDevice . Viewport ;
2023-11-24 15:50:23 +03:00
}
2023-11-27 15:14:26 +03:00
2023-11-27 13:46:21 +03:00
if ( Keyboard . GetState ( ) . IsKeyDown ( Keys . U ) )
2023-11-27 15:14:26 +03:00
gameManager . Camera . Zoom + = gameTime . ElapsedGameTime . Nanoseconds * 0.00025f ;
if ( Keyboard . GetState ( ) . IsKeyDown ( Keys . J ) )
gameManager . Camera . Zoom - = gameTime . ElapsedGameTime . Nanoseconds * 0.00025f ;
2023-11-27 13:46:21 +03:00
if ( Keyboard . GetState ( ) . IsKeyDown ( Keys . Q ) )
gameManager . Camera . Rotation + = gameTime . ElapsedGameTime . Nanoseconds * 0.000025f ;
if ( Keyboard . GetState ( ) . IsKeyDown ( Keys . E ) )
gameManager . Camera . Rotation - = gameTime . ElapsedGameTime . Nanoseconds * 0.000025f ;
2023-11-23 23:59:03 +03:00
2023-12-04 17:48:22 +03:00
if ( Keyboard . GetState ( ) . IsKeyDown ( Keys . Space ) )
seconds + = gameTime . ElapsedGameTime . Milliseconds * . 0005f ;
2023-12-05 14:47:17 +03:00
if ( Keyboard . GetState ( ) . IsKeyDown ( Keys . B ) )
{
seconds - = gameTime . ElapsedGameTime . Milliseconds * . 0005f ;
while ( physicsTimer - 0.01f > seconds )
{
Console . WriteLine ( $"Physics Timer: {physicsTimer}" ) ;
physicsTimer - = 0.01f ;
engine . Step ( - . 01f ) ;
}
}
2023-12-04 17:48:22 +03:00
2023-12-04 23:55:43 +03:00
while ( physicsTimer + 0.01f < seconds )
2023-12-01 17:42:07 +03:00
{
2023-12-04 17:48:22 +03:00
Console . WriteLine ( $"Physics Timer: {physicsTimer}" ) ;
2023-12-01 17:42:07 +03:00
physicsTimer + = 0.01f ;
engine . Step ( . 01f ) ;
}
2023-11-24 15:50:23 +03:00
gameManager . Update ( gameTime ) ;
2023-11-23 23:59:03 +03:00
base . Update ( gameTime ) ;
}
2023-12-01 17:42:07 +03:00
static float physicsTimer = 0f ;
2023-12-04 17:48:22 +03:00
static float seconds = 0f ;
2023-11-23 23:59:03 +03:00
protected override void Draw ( GameTime gameTime )
{
2023-11-24 15:50:23 +03:00
GraphicsDevice . Clear ( new Color ( ) { R = 32 , G = 32 , B = 32 } ) ;
2023-11-23 23:59:03 +03:00
// TODO: Add your drawing code here
2023-11-24 15:50:23 +03:00
gameManager . PreDraw ( gameTime ) ;
2023-11-27 13:46:21 +03:00
gameManager . Camera . Update ( ) ;
2023-11-24 15:50:23 +03:00
gameManager . Draw ( _spriteBatch ) ;
2023-11-23 23:59:03 +03:00
base . Draw ( gameTime ) ;
}
}