Lots of Stuff That I Can't Break Into Smaller Commits

This commit is contained in:
Syntriax 2024-01-24 19:25:31 +03:00
parent 6e6475f8bf
commit bf825fd961
6 changed files with 67 additions and 46 deletions

2
Engine

@ -1 +1 @@
Subproject commit 3428fcc6ca759805c26f742f9b2ca820953c3ba3
Subproject commit ed15238dcdc094453c697a8f83d9308f6702dc21

View File

@ -0,0 +1,29 @@
using Microsoft.Xna.Framework;
using Apos.Shapes;
using Syntriax.Engine.Core;
namespace Pong.Behaviours;
public class RotatableBehaviour : BehaviourOverride
{
private KeyboardInputsBehaviour? inputs = null;
protected override void OnFirstActiveFrame()
{
if (BehaviourController.TryGetBehaviour(out inputs))
inputs.BehaviourController.AddBehaviour<KeyboardInputsBehaviour>();
}
protected override void OnUpdate()
{
if (inputs is null)
return;
if (inputs.IsPressed(Microsoft.Xna.Framework.Input.Keys.NumPad4))
Transform.Rotation += Time.Elapsed.Nanoseconds * 0.0025f;
if (inputs.IsPressed(Microsoft.Xna.Framework.Input.Keys.NumPad6))
Transform.Rotation -= Time.Elapsed.Nanoseconds * 0.0025f;
}
}

View File

@ -1,19 +1,17 @@
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Apos.Shapes;
using Syntriax.Engine.Core;
using Syntriax.Engine.Physics2D.Primitives;
using Syntriax.Engine.Core.Abstract;
using Syntriax.Engine.Physics2D.Abstract;
namespace Pong.Behaviours;
public class ShapeAABBBehaviour : BehaviourOverride, IDisplayableShape
{
private readonly List<Vector2D> vectors = [];
private ShapeBehaviour? shapeBehaviour = null;
private Shape transformedShape = new([]);
private IShapeCollider2D? shapeCollider = null;
private readonly static Vector2D screenScale = Vector2D.Down + Vector2D.Right;
public ShapeAABBBehaviour() { }
@ -26,16 +24,17 @@ public class ShapeAABBBehaviour : BehaviourOverride, IDisplayableShape
protected override void OnFirstActiveFrame()
{
BehaviourController.TryGetBehaviour(out shapeBehaviour);
BehaviourController.TryGetBehaviour(out shapeCollider);
}
public void Draw(ShapeBatch shapeBatch)
{
if (shapeBehaviour is null)
if (shapeCollider is null)
return;
shapeBehaviour.Shape.TransformShape(Transform, vectors);
AABB aabb = AABB.FromVectors(vectors);
AABB aabb = AABB.FromVectors(shapeCollider.ShapeWorld);
shapeBatch.BorderCircle(aabb.Center.ToDisplayVector2(), 7.5f, Color.Beige);
shapeBatch.DrawRectangle(aabb.Center.Scale(screenScale).Subtract(aabb.SizeHalf).ToVector2(), aabb.Size.ToVector2(), Color.Transparent, Color.Blue);
}

View File

@ -1,49 +1,31 @@
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Apos.Shapes;
using Syntriax.Engine.Core;
using Syntriax.Engine.Core.Abstract;
using Syntriax.Engine.Physics2D.Primitives;
namespace Pong.Behaviours;
public class ShapeBehaviour : BehaviourOverride, IDisplayableShape
public class ShapeBehaviour : Syntriax.Engine.Physics2D.Collider2DShapeBehaviour, IDisplayableShape
{
private readonly List<Vector2D> vectors = [];
private readonly static Vector2D screenScale = Vector2D.Down + Vector2D.Right;
public ShapeBehaviour(Shape Shape) { this.ShapeLocal = Shape; }
public ShapeBehaviour(Shape Shape, float Thickness) { this.ShapeLocal = Shape; this.Thickness = Thickness; }
public ShapeBehaviour(Shape Shape, Color color) { this.ShapeLocal = Shape; Color = color; }
public ShapeBehaviour(Shape Shape, Color color, float Thickness) { this.ShapeLocal = Shape; this.Thickness = Thickness; Color = color; }
public ShapeBehaviour(Shape Shape) { this.Shape = Shape; }
public ShapeBehaviour(Shape Shape, float Thickness) { this.Shape = Shape; this.Thickness = Thickness; }
public ShapeBehaviour(Shape Shape, Color color) { this.Shape = Shape; Color = color; }
public ShapeBehaviour(Shape Shape, Color color, float Thickness) { this.Shape = Shape; this.Thickness = Thickness; Color = color; }
public Shape Shape { get; } = default!;
public Color Color { get; set; } = Color.White;
public float Thickness { get; set; } = .5f;
public void Draw(ShapeBatch shapeBatch)
{
Shape.TransformShape(GameObject.Transform, vectors);
for (int i = 0; i < vectors.Count; i++)
vectors[i] = vectors[i].Scale(screenScale);
Recalculate();
int count = ShapeWorld.Vertices.Count;
for (int i = 0; i < vectors.Count - 1; i++)
shapeBatch.DrawLine(vectors[i].ToVector2(), vectors[i + 1].ToVector2(), Thickness, Color, Color);
shapeBatch.DrawLine(vectors[0].ToVector2(), vectors[^1].ToVector2(), Thickness, Color, Color);
}
}
public static class ShapeTransform
{
public static void TransformShape(this Shape shape, ITransform transform, List<Vector2D> vectors)
{
vectors.Clear();
int count = shape.Vertices.Count;
for (int i = 0; i < count; i++)
vectors.Add(transform.TransformVector2D(shape[i]));
shapeBatch.BorderCircle(Transform.Position.ToDisplayVector2(), 5f, Color.DarkRed);
for (int i = 0; i < count - 1; i++)
shapeBatch.DrawLine(ShapeWorld[i].ToDisplayVector2(), ShapeWorld[i + 1].ToDisplayVector2(), Thickness, Color, Color);
shapeBatch.DrawLine(ShapeWorld[0].ToDisplayVector2(), ShapeWorld[^1].ToDisplayVector2(), Thickness, Color, Color);
}
}

View File

@ -6,10 +6,14 @@ namespace Pong;
public static class EngineConverter
{
public readonly static Vector2D screenScale = Vector2D.Down + Vector2D.Right;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static EngineTime ToEngineTime(this GameTime gameTime) => new(gameTime.TotalGameTime, gameTime.ElapsedGameTime);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2D ToVector2D(this Vector2 vector) => new(vector.X, vector.Y);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 ToVector2(this Vector2D vector) => new(vector.X, vector.Y);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 ToDisplayVector2(this Vector2D vector) => vector.Scale(screenScale).ToVector2();
}

View File

@ -66,14 +66,26 @@ public class Game1 : Game
gameManager.Camera = cameraBehaviour;
gameObjectDiamond = gameManager.InstantiateGameObject<GameObject>();
IGameObject gameObjectDiamond = gameManager.InstantiateGameObject<GameObject>();
gameObjectDiamond.Name = "Diamond";
gameObjectDiamond.Transform.Position = new Vector2D(0f, 0f);
gameObjectDiamond.Transform.Scale = new Vector2D(100f, 100f);
gameObjectDiamond.BehaviourController.AddBehaviour<KeyboardInputsBehaviour>();
gameObjectDiamond.BehaviourController.AddBehaviour<MovementBoxBehaviour>(Keys.W, Keys.S, 268f, -268f, 400f);
gameObjectDiamond.BehaviourController.AddBehaviour<RotatableBehaviour>();
gameObjectDiamond.BehaviourController.AddBehaviour<ShapeBehaviour>(new Shape([Vector2D.Up, Vector2D.One, Vector2D.Right, Vector2D.Down, Vector2D.Zero, Vector2D.Left]));
gameObjectDiamond.BehaviourController.AddBehaviour<ShapeAABBBehaviour>();
IGameObject gameObjectShape = gameManager.InstantiateGameObject<GameObject>();
gameObjectShape.Name = "Shape";
gameObjectShape.Transform.Position = new Vector2D(250f, 0f);
gameObjectShape.Transform.Scale = new Vector2D(100f, 100f);
gameObjectShape.BehaviourController.AddBehaviour<KeyboardInputsBehaviour>();
gameObjectShape.BehaviourController.AddBehaviour<MovementBoxBehaviour>(Keys.W, Keys.S, 268f, -268f, 400f);
gameObjectShape.BehaviourController.AddBehaviour<RotatableBehaviour>();
gameObjectShape.BehaviourController.AddBehaviour<ShapeBehaviour>(new Shape([Vector2D.Up, Vector2D.One, Vector2D.Right, Vector2D.Down, Vector2D.Zero, Vector2D.Left]));
// gameObjectShape.BehaviourController.AddBehaviour<ShapeAABBBehaviour>();
}
protected override void Update(GameTime gameTime)
@ -107,10 +119,6 @@ public class Game1 : Game
cameraBehaviour.BehaviourController.GameObject.Transform.Rotation += gameTime.ElapsedGameTime.Nanoseconds * 0.000025f;
if (Keyboard.GetState().IsKeyDown(Keys.E))
cameraBehaviour.BehaviourController.GameObject.Transform.Rotation -= gameTime.ElapsedGameTime.Nanoseconds * 0.000025f;
if (Keyboard.GetState().IsKeyDown(Keys.NumPad4))
gameObjectDiamond.Transform.Rotation += gameTime.ElapsedGameTime.Nanoseconds * 0.0025f;
if (Keyboard.GetState().IsKeyDown(Keys.NumPad6))
gameObjectDiamond.Transform.Rotation -= gameTime.ElapsedGameTime.Nanoseconds * 0.0025f;
if (Keyboard.GetState().IsKeyDown(Keys.N))
{
@ -163,7 +171,6 @@ public class Game1 : Game
}
static float physicsTimer = 0f;
static float seconds = 0f;
private GameObject gameObjectDiamond;
protected override void Draw(GameTime gameTime)
{