early paddle and ball structure
parent
d7b4b6e4fd
commit
b607c5a2e9
|
@ -0,0 +1,14 @@
|
||||||
|
using Encompass;
|
||||||
|
|
||||||
|
namespace PongFE.Components
|
||||||
|
{
|
||||||
|
public struct PaddleMoveSpeedComponent : IComponent
|
||||||
|
{
|
||||||
|
public float Speed { get; }
|
||||||
|
|
||||||
|
public PaddleMoveSpeedComponent(float speed)
|
||||||
|
{
|
||||||
|
Speed = speed;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,20 @@
|
||||||
|
using Encompass;
|
||||||
|
|
||||||
|
namespace PongFE.Components
|
||||||
|
{
|
||||||
|
public enum PlayerIndex
|
||||||
|
{
|
||||||
|
One,
|
||||||
|
Two
|
||||||
|
}
|
||||||
|
|
||||||
|
public struct PlayerInputComponent : IComponent
|
||||||
|
{
|
||||||
|
public PlayerIndex PlayerIndex { get; }
|
||||||
|
|
||||||
|
public PlayerInputComponent(PlayerIndex playerIndex)
|
||||||
|
{
|
||||||
|
PlayerIndex = playerIndex;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,44 @@
|
||||||
|
using Encompass;
|
||||||
|
using Microsoft.Xna.Framework.Input;
|
||||||
|
using PongFE.Components;
|
||||||
|
using PongFE.Messages;
|
||||||
|
|
||||||
|
namespace PongFE.Engines
|
||||||
|
{
|
||||||
|
[Reads(typeof(PlayerInputComponent))]
|
||||||
|
[Sends(typeof(PaddleMoveMessage))]
|
||||||
|
public class InputEngine : Engine
|
||||||
|
{
|
||||||
|
public override void Update(double dt)
|
||||||
|
{
|
||||||
|
var keyboardState = Keyboard.GetState();
|
||||||
|
|
||||||
|
foreach (ref readonly var playerInputEntity in ReadEntities<PlayerInputComponent>())
|
||||||
|
{
|
||||||
|
ref readonly var playerInputComponent = ref GetComponent<PlayerInputComponent>(playerInputEntity);
|
||||||
|
|
||||||
|
if (playerInputComponent.PlayerIndex == PlayerIndex.One)
|
||||||
|
{
|
||||||
|
if (keyboardState.IsKeyDown(Keys.Down))
|
||||||
|
{
|
||||||
|
SendMessage(
|
||||||
|
new PaddleMoveMessage(
|
||||||
|
playerInputEntity,
|
||||||
|
PaddleMoveDirection.Down
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
else if (keyboardState.IsKeyDown(Keys.Up))
|
||||||
|
{
|
||||||
|
SendMessage(
|
||||||
|
new PaddleMoveMessage(
|
||||||
|
playerInputEntity,
|
||||||
|
PaddleMoveDirection.Up
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,25 @@
|
||||||
|
using Encompass;
|
||||||
|
using PongFE.Components;
|
||||||
|
using PongFE.Messages;
|
||||||
|
|
||||||
|
namespace PongFE.Engines
|
||||||
|
{
|
||||||
|
[Reads(typeof(PositionComponent))]
|
||||||
|
[Receives(typeof(MotionMessage))]
|
||||||
|
[Writes(typeof(PositionComponent))]
|
||||||
|
public class MotionEngine : Engine
|
||||||
|
{
|
||||||
|
public override void Update(double dt)
|
||||||
|
{
|
||||||
|
foreach (ref readonly var motionMessage in ReadMessages<MotionMessage>())
|
||||||
|
{
|
||||||
|
if (HasComponent<PositionComponent>(motionMessage.Entity))
|
||||||
|
{
|
||||||
|
ref readonly var positionComponent = ref GetComponent<PositionComponent>(motionMessage.Entity);
|
||||||
|
var newPosition = positionComponent.Position + motionMessage.Movement;
|
||||||
|
SetComponent(motionMessage.Entity, new PositionComponent(newPosition));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,30 @@
|
||||||
|
using Encompass;
|
||||||
|
using PongFE.Components;
|
||||||
|
using PongFE.Messages;
|
||||||
|
|
||||||
|
namespace PongFE.Engines
|
||||||
|
{
|
||||||
|
[Reads(typeof(PaddleMoveSpeedComponent))]
|
||||||
|
[Receives(typeof(PaddleMoveMessage))]
|
||||||
|
[Sends(typeof(MotionMessage))]
|
||||||
|
public class PaddleMovementEngine : Engine
|
||||||
|
{
|
||||||
|
public override void Update(double dt)
|
||||||
|
{
|
||||||
|
foreach (ref readonly var message in ReadMessages<PaddleMoveMessage>())
|
||||||
|
{
|
||||||
|
if (HasComponent<PaddleMoveSpeedComponent>(message.Entity))
|
||||||
|
{
|
||||||
|
var directionMultiplier = message.PaddleMoveDirection == PaddleMoveDirection.Down ? 1 : -1;
|
||||||
|
ref readonly var paddleMoveSpeedComponent = ref GetComponent<PaddleMoveSpeedComponent>(message.Entity);
|
||||||
|
SendMessage(
|
||||||
|
new MotionMessage(
|
||||||
|
message.Entity,
|
||||||
|
new System.Numerics.Vector2(0, paddleMoveSpeedComponent.Speed * directionMultiplier * (float)dt)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,24 @@
|
||||||
|
using Encompass;
|
||||||
|
using Microsoft.Xna.Framework.Graphics;
|
||||||
|
using PongFE.Components;
|
||||||
|
using PongFE.Messages;
|
||||||
|
|
||||||
|
namespace PongFE.Spawners
|
||||||
|
{
|
||||||
|
public class BallSpawner : Spawner<BallSpawnMessage>
|
||||||
|
{
|
||||||
|
private Texture2D BallTexture { get; }
|
||||||
|
|
||||||
|
public BallSpawner(Texture2D ballTexture)
|
||||||
|
{
|
||||||
|
BallTexture = ballTexture;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void Spawn(BallSpawnMessage message)
|
||||||
|
{
|
||||||
|
var ball = CreateEntity();
|
||||||
|
AddComponent(ball, new PositionComponent(new MoonTools.Structs.Position2D(640, 360)));
|
||||||
|
AddComponent(ball, new Texture2DComponent(BallTexture, 0));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,15 @@
|
||||||
|
using Encompass;
|
||||||
|
using MoonTools.Structs;
|
||||||
|
|
||||||
|
namespace PongFE.Messages
|
||||||
|
{
|
||||||
|
public struct BallSpawnMessage : IMessage
|
||||||
|
{
|
||||||
|
public Position2D Position { get; }
|
||||||
|
|
||||||
|
public BallSpawnMessage(Position2D position)
|
||||||
|
{
|
||||||
|
Position = position;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,17 @@
|
||||||
|
using System.Numerics;
|
||||||
|
using Encompass;
|
||||||
|
|
||||||
|
namespace PongFE.Messages
|
||||||
|
{
|
||||||
|
public struct MotionMessage : IMessage, IHasEntity
|
||||||
|
{
|
||||||
|
public Entity Entity { get; }
|
||||||
|
public Vector2 Movement { get; }
|
||||||
|
|
||||||
|
public MotionMessage(Entity entity, Vector2 movement)
|
||||||
|
{
|
||||||
|
Entity = entity;
|
||||||
|
Movement = movement;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,22 @@
|
||||||
|
using Encompass;
|
||||||
|
|
||||||
|
namespace PongFE.Messages
|
||||||
|
{
|
||||||
|
public enum PaddleMoveDirection
|
||||||
|
{
|
||||||
|
Up,
|
||||||
|
Down
|
||||||
|
}
|
||||||
|
|
||||||
|
public struct PaddleMoveMessage : IMessage, IHasEntity
|
||||||
|
{
|
||||||
|
public Entity Entity { get; }
|
||||||
|
public PaddleMoveDirection PaddleMoveDirection { get; }
|
||||||
|
|
||||||
|
public PaddleMoveMessage(Entity entity, PaddleMoveDirection paddleMoveDirection)
|
||||||
|
{
|
||||||
|
Entity = entity;
|
||||||
|
PaddleMoveDirection = paddleMoveDirection;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,7 +2,10 @@ using Encompass;
|
||||||
using Microsoft.Xna.Framework;
|
using Microsoft.Xna.Framework;
|
||||||
using Microsoft.Xna.Framework.Graphics;
|
using Microsoft.Xna.Framework.Graphics;
|
||||||
using PongFE.Components;
|
using PongFE.Components;
|
||||||
|
using PongFE.Engines;
|
||||||
|
using PongFE.Messages;
|
||||||
using PongFE.Renderers;
|
using PongFE.Renderers;
|
||||||
|
using PongFE.Spawners;
|
||||||
|
|
||||||
namespace PongFE
|
namespace PongFE
|
||||||
{
|
{
|
||||||
|
@ -16,6 +19,7 @@ namespace PongFE
|
||||||
SpriteBatch SpriteBatch { get; set; }
|
SpriteBatch SpriteBatch { get; set; }
|
||||||
Texture2D WhitePixel { get; set; }
|
Texture2D WhitePixel { get; set; }
|
||||||
RenderTarget2D PaddleTexture { get; set; }
|
RenderTarget2D PaddleTexture { get; set; }
|
||||||
|
RenderTarget2D BallTexture { get; set; }
|
||||||
|
|
||||||
public PongFEGame()
|
public PongFEGame()
|
||||||
{
|
{
|
||||||
|
@ -27,6 +31,7 @@ namespace PongFE
|
||||||
|
|
||||||
Window.AllowUserResizing = true;
|
Window.AllowUserResizing = true;
|
||||||
IsMouseVisible = true;
|
IsMouseVisible = true;
|
||||||
|
IsFixedTimeStep = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void LoadContent()
|
protected override void LoadContent()
|
||||||
|
@ -41,14 +46,28 @@ namespace PongFE
|
||||||
SpriteBatch.Begin();
|
SpriteBatch.Begin();
|
||||||
SpriteBatch.Draw(WhitePixel, new Rectangle(0, 0, 20, 80), Color.White);
|
SpriteBatch.Draw(WhitePixel, new Rectangle(0, 0, 20, 80), Color.White);
|
||||||
SpriteBatch.End();
|
SpriteBatch.End();
|
||||||
|
|
||||||
|
BallTexture = new RenderTarget2D(GraphicsDevice, 16, 16);
|
||||||
|
GraphicsDevice.SetRenderTarget(BallTexture);
|
||||||
|
SpriteBatch.Begin();
|
||||||
|
SpriteBatch.Draw(WhitePixel, new Rectangle(0, 0, 16, 16), Color.White);
|
||||||
|
SpriteBatch.End();
|
||||||
GraphicsDevice.SetRenderTarget(null);
|
GraphicsDevice.SetRenderTarget(null);
|
||||||
|
|
||||||
|
WorldBuilder.AddEngine(new InputEngine());
|
||||||
|
WorldBuilder.AddEngine(new PaddleMovementEngine());
|
||||||
|
WorldBuilder.AddEngine(new MotionEngine());
|
||||||
|
WorldBuilder.AddEngine(new BallSpawner(BallTexture));
|
||||||
WorldBuilder.AddOrderedRenderer(new Texture2DRenderer(SpriteBatch));
|
WorldBuilder.AddOrderedRenderer(new Texture2DRenderer(SpriteBatch));
|
||||||
|
|
||||||
var paddle = WorldBuilder.CreateEntity();
|
var paddle = WorldBuilder.CreateEntity();
|
||||||
|
WorldBuilder.SetComponent(paddle, new PlayerInputComponent(PongFE.Components.PlayerIndex.One));
|
||||||
|
WorldBuilder.SetComponent(paddle, new PaddleMoveSpeedComponent(400));
|
||||||
WorldBuilder.SetComponent(paddle, new PositionComponent(new MoonTools.Structs.Position2D(5, 5)));
|
WorldBuilder.SetComponent(paddle, new PositionComponent(new MoonTools.Structs.Position2D(5, 5)));
|
||||||
WorldBuilder.SetComponent(paddle, new Texture2DComponent(PaddleTexture, 0));
|
WorldBuilder.SetComponent(paddle, new Texture2DComponent(PaddleTexture, 0));
|
||||||
|
|
||||||
|
WorldBuilder.SendMessage(new BallSpawnMessage(new MoonTools.Structs.Position2D(640, 360)));
|
||||||
|
|
||||||
World = WorldBuilder.Build();
|
World = WorldBuilder.Build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -59,6 +78,7 @@ namespace PongFE
|
||||||
|
|
||||||
protected override void Update(GameTime gameTime)
|
protected override void Update(GameTime gameTime)
|
||||||
{
|
{
|
||||||
|
System.Console.WriteLine(1 / gameTime.ElapsedGameTime.TotalSeconds);
|
||||||
World.Update(gameTime.ElapsedGameTime.TotalSeconds);
|
World.Update(gameTime.ElapsedGameTime.TotalSeconds);
|
||||||
|
|
||||||
base.Update(gameTime);
|
base.Update(gameTime);
|
||||||
|
|
Loading…
Reference in New Issue