Tets1231
This commit is contained in:
parent
c66eec61ac
commit
84ecc68320
|
@ -3,43 +3,49 @@ using Microsoft.Xna.Framework;
|
||||||
using Microsoft.Xna.Framework.Input;
|
using Microsoft.Xna.Framework.Input;
|
||||||
using Syntriax.Engine.Core;
|
using Syntriax.Engine.Core;
|
||||||
using Syntriax.Engine.Input;
|
using Syntriax.Engine.Input;
|
||||||
|
using Syntriax.Engine.Physics2D.Abstract;
|
||||||
|
|
||||||
namespace Pong.Behaviours;
|
namespace Pong.Behaviours;
|
||||||
|
|
||||||
public class MovementBallBehaviour(Vector2 StartDirection, PlayAreaBehaviour PlayAreaBehaviour, float Speed) : BehaviourOverride
|
public class MovementBallBehaviour(Vector2 StartDirection, float Speed) : BehaviourOverride
|
||||||
{
|
{
|
||||||
public Vector2 StartDirection { get; private set; } = StartDirection;
|
public Vector2 StartDirection { get; private set; } = Vector2.Normalize(StartDirection);
|
||||||
public PlayAreaBehaviour PlayAreaBehaviour { get; } = PlayAreaBehaviour;
|
|
||||||
public float Speed { get; set; } = Speed;
|
public float Speed { get; set; } = Speed;
|
||||||
|
|
||||||
protected override void OnInitialize() => StartDirection.Normalize();
|
protected override void OnFirstActiveFrame(GameTime time)
|
||||||
|
|
||||||
protected override void OnUpdate(GameTime time)
|
|
||||||
{
|
{
|
||||||
GameObject.Transform.Position += StartDirection * (time.ElapsedGameTime.Nanoseconds * .001f) * Speed;
|
if (!BehaviourController.TryGetBehaviour(out IRigidBody2D? rigidBody))
|
||||||
|
throw new Exception($"Where's my {nameof(IRigidBody2D)}????");
|
||||||
|
|
||||||
float absY = MathF.Abs(GameObject.Transform.Position.Y);
|
rigidBody.Velocity = StartDirection * Speed;
|
||||||
float differenceY = absY - PlayAreaBehaviour.PlayArea.Y * 0.5f;
|
|
||||||
if (differenceY > 0f)
|
|
||||||
{
|
|
||||||
if (GameObject.Transform.Position.Y > 0f)
|
|
||||||
GameObject.Transform.Position -= Vector2.UnitY * differenceY * 2f;
|
|
||||||
else
|
|
||||||
GameObject.Transform.Position += Vector2.UnitY * differenceY * 2f;
|
|
||||||
|
|
||||||
StartDirection = new(StartDirection.X, -StartDirection.Y);
|
|
||||||
}
|
|
||||||
|
|
||||||
float absX = MathF.Abs(GameObject.Transform.Position.X);
|
|
||||||
float differenceX = absX - PlayAreaBehaviour.PlayArea.X * 0.5f;
|
|
||||||
if (differenceX > 0f)
|
|
||||||
{
|
|
||||||
if (GameObject.Transform.Position.X > 0f)
|
|
||||||
GameObject.Transform.Position -= Vector2.UnitX * differenceX * 2f;
|
|
||||||
else
|
|
||||||
GameObject.Transform.Position += Vector2.UnitX * differenceX * 2f;
|
|
||||||
|
|
||||||
StartDirection = new(-StartDirection.X, StartDirection.Y);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// protected override void OnUpdate(GameTime time)
|
||||||
|
// {
|
||||||
|
// GameObject.Transform.Position += StartDirection * (time.ElapsedGameTime.Nanoseconds * .001f) * Speed;
|
||||||
|
|
||||||
|
// float absY = MathF.Abs(GameObject.Transform.Position.Y);
|
||||||
|
// float differenceY = absY - PlayAreaBehaviour.PlayArea.Y * 0.5f;
|
||||||
|
// if (differenceY > 0f)
|
||||||
|
// {
|
||||||
|
// if (GameObject.Transform.Position.Y > 0f)
|
||||||
|
// GameObject.Transform.Position -= Vector2.UnitY * differenceY * 2f;
|
||||||
|
// else
|
||||||
|
// GameObject.Transform.Position += Vector2.UnitY * differenceY * 2f;
|
||||||
|
|
||||||
|
// StartDirection = new(StartDirection.X, -StartDirection.Y);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// float absX = MathF.Abs(GameObject.Transform.Position.X);
|
||||||
|
// float differenceX = absX - PlayAreaBehaviour.PlayArea.X * 0.5f;
|
||||||
|
// if (differenceX > 0f)
|
||||||
|
// {
|
||||||
|
// if (GameObject.Transform.Position.X > 0f)
|
||||||
|
// GameObject.Transform.Position -= Vector2.UnitX * differenceX * 2f;
|
||||||
|
// else
|
||||||
|
// GameObject.Transform.Position += Vector2.UnitX * differenceX * 2f;
|
||||||
|
|
||||||
|
// StartDirection = new(-StartDirection.X, StartDirection.Y);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|
|
@ -45,7 +45,7 @@ public class Game1 : Game
|
||||||
engine = new PhysicsEngine2D();
|
engine = new PhysicsEngine2D();
|
||||||
_spriteBatch = new SpriteBatch(GraphicsDevice);
|
_spriteBatch = new SpriteBatch(GraphicsDevice);
|
||||||
|
|
||||||
// Sprite spriteBox = new Sprite() { Texture2D = Content.Load<Texture2D>("Sprites/Pixel") };
|
Sprite spriteBox = new Sprite() { Texture2D = Content.Load<Texture2D>("Sprites/Pixel") };
|
||||||
Sprite spriteBall = new Sprite() { Texture2D = Content.Load<Texture2D>("Sprites/Circle") };
|
Sprite spriteBall = new Sprite() { Texture2D = Content.Load<Texture2D>("Sprites/Circle") };
|
||||||
|
|
||||||
IGameObject gameObjectCamera = gameManager.InstantiateGameObject<GameObject>();
|
IGameObject gameObjectCamera = gameManager.InstantiateGameObject<GameObject>();
|
||||||
|
@ -55,52 +55,58 @@ public class Game1 : Game
|
||||||
gameManager.Camera = gameObjectCamera.BehaviourController.AddBehaviour<CameraBehaviour>();
|
gameManager.Camera = gameObjectCamera.BehaviourController.AddBehaviour<CameraBehaviour>();
|
||||||
gameManager.Camera.Viewport = GraphicsDevice.Viewport;
|
gameManager.Camera.Viewport = GraphicsDevice.Viewport;
|
||||||
|
|
||||||
// IGameObject gameObjectPlayArea = gameManager.InstantiateGameObject<GameObject>();
|
|
||||||
// PlayAreaBehaviour playAreaBehaviour = gameObjectPlayArea.BehaviourController.AddBehaviour<PlayAreaBehaviour>();
|
|
||||||
// playAreaBehaviour.PlayArea = new Vector2(_graphics.PreferredBackBufferWidth, _graphics.PreferredBackBufferHeight);
|
|
||||||
|
|
||||||
IGameObject gameObjectBall = gameManager.InstantiateGameObject<GameObject>();
|
IGameObject gameObjectBall = gameManager.InstantiateGameObject<GameObject>();
|
||||||
gameObjectBall.Name = "Ball";
|
gameObjectBall.Name = "Ball";
|
||||||
gameObjectBall.Transform.Position = Vector2.Zero;
|
gameObjectBall.Transform.Position = Vector2.Zero;
|
||||||
gameObjectBall.Transform.Scale = new Vector2(1f / 51.2f, 1f / 51.2f);
|
gameObjectBall.Transform.Scale = new Vector2(1f / 51.2f, 1f / 51.2f);
|
||||||
engine.AddRigidBody(gameObjectBall.BehaviourController.AddBehaviour<RigidBody2D>());
|
engine.AddRigidBody(gameObjectBall.BehaviourController.AddBehaviour<RigidBody2D>());
|
||||||
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;
|
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;
|
||||||
// gameObjectBall.BehaviourController.AddBehaviour<MovementBallBehaviour>(new Vector2(.1f, .1f), playAreaBehaviour, 100f);
|
gameObjectBall.BehaviourController.AddBehaviour<MovementBallBehaviour>(new Vector2(.1f, .1f), 500f);
|
||||||
gameObjectBall.BehaviourController.AddBehaviour<DisplayableSpriteBehaviour>().Assign(spriteBall);
|
gameObjectBall.BehaviourController.AddBehaviour<DisplayableSpriteBehaviour>().Assign(spriteBall);
|
||||||
IGameObject gameObjectBall2 = gameManager.InstantiateGameObject<GameObject>();
|
|
||||||
gameObjectBall2.Name = "Ball2";
|
|
||||||
gameObjectBall2.Transform.Position = Vector2.UnitY * -50f;
|
|
||||||
gameObjectBall2.Transform.Scale = new Vector2(1f / 51.2f, 1f / 51.2f);
|
|
||||||
RigidBody2D rigidBody = gameObjectBall2.BehaviourController.AddBehaviour<RigidBody2D>();
|
|
||||||
rigidBody.Velocity = Vector2.UnitY * 100f;
|
|
||||||
engine.AddRigidBody(rigidBody);
|
|
||||||
gameObjectBall2.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;
|
|
||||||
// gameObjectBall2.BehaviourController.AddBehaviour<MovementBallBehaviour>(new Vector2(.1f, .1f), playAreaBehaviour, 100f);
|
|
||||||
gameObjectBall2.BehaviourController.AddBehaviour<DisplayableSpriteBehaviour>().Assign(spriteBall);
|
|
||||||
|
|
||||||
IGameObject gameObjectBall3 = gameManager.InstantiateGameObject<GameObject>();
|
IGameObject gameObjectLeft = gameManager.InstantiateGameObject<GameObject>();
|
||||||
gameObjectBall3.Name = "Ball";
|
gameObjectLeft.Name = "Left";
|
||||||
gameObjectBall3.Transform.Position = Vector2.UnitY * -120f + Vector2.UnitX * 10f;
|
gameObjectLeft.Transform.Position = new Vector2(-452, 0f);
|
||||||
gameObjectBall3.Transform.Scale = new Vector2(1f / 51.2f, 1f / 51.2f);
|
gameObjectLeft.Transform.Scale = new Vector2(10f, 40f);
|
||||||
engine.AddRigidBody(gameObjectBall3.BehaviourController.AddBehaviour<RigidBody2D>());
|
gameObjectLeft.BehaviourController.AddBehaviour<KeyboardInputsBehaviour>();
|
||||||
gameObjectBall3.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;
|
gameObjectLeft.BehaviourController.AddBehaviour<MovementBoxBehaviour>(Keys.W, Keys.S, 268f, -268f, 400f);
|
||||||
// gameObjectBall3.BehaviourController.AddBehaviour<MovementBallBehaviour>(new Vector2(.1f, .1f), playAreaBehaviour, 100f);
|
gameObjectLeft.BehaviourController.AddBehaviour<DisplayableSpriteBehaviour>().Assign(spriteBox);
|
||||||
gameObjectBall3.BehaviourController.AddBehaviour<DisplayableSpriteBehaviour>().Assign(spriteBall);
|
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)]);
|
||||||
// IGameObject gameObjectLeft = gameManager.InstantiateGameObject<GameObject>();
|
engine.AddRigidBody(gameObjectLeft.BehaviourController.AddBehaviour<RigidBody2D>());
|
||||||
// gameObjectLeft.Name = "Left";
|
|
||||||
// gameObjectLeft.Transform.Position = new Vector2(-452, 0f);
|
IGameObject gameObjectRight = gameManager.InstantiateGameObject<GameObject>();
|
||||||
// gameObjectLeft.Transform.Scale = new Vector2(10f, 40f);
|
gameObjectRight.Name = "Right";
|
||||||
// gameObjectLeft.BehaviourController.AddBehaviour<KeyboardInputsBehaviour>();
|
gameObjectRight.Transform.Position = new Vector2(452, 0f);
|
||||||
// gameObjectLeft.BehaviourController.AddBehaviour<MovementBoxBehaviour>(Keys.W, Keys.S, 268f, -268f, 400f);
|
gameObjectRight.Transform.Scale = new Vector2(10f, 40f);
|
||||||
// gameObjectLeft.BehaviourController.AddBehaviour<DisplayableSpriteBehaviour>().Assign(spriteBox);
|
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);
|
||||||
|
goPlayAreaTop.Transform.Scale = new Vector2(1024f, 40f);
|
||||||
|
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));
|
||||||
|
goPlayAreaBottom.Transform.Scale = new Vector2(1024f, 40f);
|
||||||
|
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);
|
||||||
|
goPlayAreaRight.Transform.Scale = new Vector2(40f, 576f);
|
||||||
|
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);
|
||||||
|
goPlayAreaLeft.Transform.Scale = new Vector2(40f, 576f);
|
||||||
|
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>());
|
||||||
|
|
||||||
// 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);
|
|
||||||
// TODO: use this.Content to load your game content here
|
// TODO: use this.Content to load your game content here
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -63,9 +63,9 @@ public class PhysicsEngine2D : IPhysicsEngine2D
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (colliders[colliderIX].BehaviourController.TryGetBehaviour(out IRigidBody2D? rigidX))
|
if (colliders[colliderIX].BehaviourController.TryGetBehaviour(out IRigidBody2D? rigidX))
|
||||||
rigidX.Velocity = -normal * rigidX.Velocity.Length();
|
rigidX.Velocity = Vector2.Reflect(rigidX.Velocity, normal);
|
||||||
if (colliders[colliderIY].BehaviourController.TryGetBehaviour(out IRigidBody2D? rigidY))
|
if (colliders[colliderIY].BehaviourController.TryGetBehaviour(out IRigidBody2D? rigidY))
|
||||||
rigidY.Velocity = normal * rigidY.Velocity.Length();
|
rigidY.Velocity = Vector2.Reflect(rigidY.Velocity, normal);
|
||||||
|
|
||||||
// Console.WriteLine($"Collision");
|
// Console.WriteLine($"Collision");
|
||||||
break;
|
break;
|
||||||
|
|
Loading…
Reference in New Issue