diff --git a/KavTest/Components/AngularVelocityComponent.cs b/KavTest/Components/AngularVelocityComponent.cs new file mode 100644 index 0000000..d7bf1e5 --- /dev/null +++ b/KavTest/Components/AngularVelocityComponent.cs @@ -0,0 +1,15 @@ +using Encompass; +using Microsoft.Xna.Framework; + +namespace KavTest.Components +{ + public struct AngularVelocityComponent : IComponent + { + public Vector3 AngularVelocity { get; } + + public AngularVelocityComponent(Vector3 angularVelocity) + { + AngularVelocity = angularVelocity; + } + } +} diff --git a/KavTest/Engines/RotationEngine.cs b/KavTest/Engines/RotationEngine.cs new file mode 100644 index 0000000..fdeb567 --- /dev/null +++ b/KavTest/Engines/RotationEngine.cs @@ -0,0 +1,29 @@ +using Encompass; +using KavTest.Components; +using Microsoft.Xna.Framework; + +namespace KavTest.Engines +{ + [Reads(typeof(TransformComponent), typeof(AngularVelocityComponent))] + [Writes(typeof(TransformComponent))] + public class RotationEngine : Engine + { + public override void Update(double dt) + { + foreach (var entity in ReadEntities()) + { + if (HasComponent(entity)) + { + ref readonly var angularVelocityComponent = ref GetComponent(entity); + ref readonly var transformComponent = ref GetComponent(entity); + + var angularVelocity = angularVelocityComponent.AngularVelocity * (float)dt; + var transform = transformComponent.Transform; + + var newTransform = Matrix.Transform(transform, Quaternion.CreateFromYawPitchRoll(angularVelocity.X, angularVelocity.Y, angularVelocity.Z)); + SetComponent(entity, new TransformComponent(newTransform)); + } + } + } + } +} diff --git a/KavTest/Engines/Spawners/RustyBallSpawner.cs b/KavTest/Engines/Spawners/RustyBallSpawner.cs index b1d16c5..35e700c 100644 --- a/KavTest/Engines/Spawners/RustyBallSpawner.cs +++ b/KavTest/Engines/Spawners/RustyBallSpawner.cs @@ -20,6 +20,7 @@ namespace KavTest.Spawners AddComponent(entity, new TransformComponent(message.Transform)); AddComponent(entity, new ModelComponent(RustyBallModel)); + AddComponent(entity, new AngularVelocityComponent(new Microsoft.Xna.Framework.Vector3(5, -2, 1))); } } } diff --git a/KavTest/KavTestGame.cs b/KavTest/KavTestGame.cs index 3ccaa55..d3c86a9 100644 --- a/KavTest/KavTestGame.cs +++ b/KavTest/KavTestGame.cs @@ -1,6 +1,7 @@ using System.IO; using Encompass; using KavTest.Components; +using KavTest.Engines; using KavTest.Messages; using KavTest.Renderers; using KavTest.Spawners; @@ -35,6 +36,7 @@ namespace KavTest Smuggler.Importer.ImportGLB(GraphicsDevice, File.OpenRead("Content/cube.glb")) ); + WorldBuilder.AddEngine(new RotationEngine()); WorldBuilder.AddEngine(new RustyBallSpawner(rustyBallModel)); WorldBuilder.AddEngine(new LightBulbSpawner()); WorldBuilder.AddGeneralRenderer(new SceneRenderer(GraphicsDevice), 0);