bounce and paddle stuff

main
Evan Hemsley 2020-07-15 13:08:58 -07:00
parent 24ffd2c9ba
commit a86f8fd8a2
21 changed files with 222 additions and 51 deletions

View File

@ -0,0 +1,6 @@
using Encompass;
namespace PongFE.Components
{
public struct CanBeTrackedComponent : IComponent { }
}

View File

@ -0,0 +1,15 @@
using Encompass;
using PongFE.Enums;
namespace PongFE.Components
{
public struct ComputerControlComponent : IComponent
{
public PlayerIndex PlayerIndex { get; }
public ComputerControlComponent(PlayerIndex playerIndex)
{
PlayerIndex = playerIndex;
}
}
}

View File

@ -1,13 +1,8 @@
using Encompass; using Encompass;
using PongFE.Enums;
namespace PongFE.Components namespace PongFE.Components
{ {
public enum PlayerIndex
{
One,
Two
}
public struct PlayerInputComponent : IComponent public struct PlayerInputComponent : IComponent
{ {
public PlayerIndex PlayerIndex { get; } public PlayerIndex PlayerIndex { get; }

View File

@ -1,3 +1,4 @@
using System.Numerics;
using Encompass; using Encompass;
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Graphics;
@ -6,11 +7,13 @@ namespace PongFE.Components
public struct Texture2DComponent : IComponent, IDrawableComponent public struct Texture2DComponent : IComponent, IDrawableComponent
{ {
public Texture2D Texture { get; } public Texture2D Texture { get; }
public Vector2 Scale { get; }
public int Layer { get; } public int Layer { get; }
public Texture2DComponent(Texture2D texture, int layer) public Texture2DComponent(Texture2D texture, int layer, Vector2 scale)
{ {
Texture = texture; Texture = texture;
Scale = scale;
Layer = layer; Layer = layer;
} }
} }

View File

@ -1,6 +1,7 @@
using System.Numerics; using System.Numerics;
using Encompass; using Encompass;
using PongFE.Components; using PongFE.Components;
using PongFE.Enums;
using PongFE.Messages; using PongFE.Messages;
namespace PongFE.Engines namespace PongFE.Engines

View File

@ -1,5 +1,6 @@
using Encompass; using Encompass;
using PongFE.Components; using PongFE.Components;
using PongFE.Enums;
using PongFE.Messages; using PongFE.Messages;
namespace PongFE.Engines namespace PongFE.Engines

View File

@ -0,0 +1,46 @@
using Encompass;
using PongFE.Components;
using PongFE.Enums;
using PongFE.Messages;
namespace PongFE.Engines
{
[Reads(
typeof(ComputerControlComponent),
typeof(PositionComponent),
typeof(CanBeTrackedComponent)
)]
[Sends(typeof(PaddleMoveMessage))]
public class ComputerControlEngine : Engine
{
public override void Update(double dt)
{
foreach (ref readonly var entity in ReadEntities<ComputerControlComponent>())
{
if (HasComponent<PositionComponent>(entity))
{
ref readonly var trackerPositionComponent = ref GetComponent<PositionComponent>(entity);
if (SomeComponent<CanBeTrackedComponent>())
{
ref readonly var trackedEntity = ref ReadEntity<CanBeTrackedComponent>();
if (HasComponent<PositionComponent>(trackedEntity))
{
ref readonly var trackedPositionComponent = ref GetComponent<PositionComponent>(trackedEntity);
if (trackerPositionComponent.Position.Y - trackedPositionComponent.Position.Y > 20)
{
SendMessage(new PaddleMoveMessage(entity, PaddleMoveDirection.Up));
}
else if (trackerPositionComponent.Position.Y - trackedPositionComponent.Position.Y < -20)
{
SendMessage(new PaddleMoveMessage(entity, PaddleMoveDirection.Down));
}
}
}
}
}
}
}
}

View File

@ -1,6 +1,7 @@
using Encompass; using Encompass;
using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Input;
using PongFE.Components; using PongFE.Components;
using PongFE.Enums;
using PongFE.Messages; using PongFE.Messages;
namespace PongFE.Engines namespace PongFE.Engines

View File

@ -4,6 +4,7 @@ using Encompass;
using MoonTools.Bonk; using MoonTools.Bonk;
using MoonTools.Structs; using MoonTools.Structs;
using PongFE.Components; using PongFE.Components;
using PongFE.Enums;
using PongFE.Messages; using PongFE.Messages;
namespace PongFE.Engines namespace PongFE.Engines

View File

@ -1,5 +1,6 @@
using Encompass; using Encompass;
using PongFE.Components; using PongFE.Components;
using PongFE.Enums;
using PongFE.Messages; using PongFE.Messages;
namespace PongFE.Engines namespace PongFE.Engines

View File

@ -7,11 +7,11 @@ namespace PongFE.Spawners
{ {
public class BallSpawner : Spawner<BallSpawnMessage> public class BallSpawner : Spawner<BallSpawnMessage>
{ {
private Texture2D BallTexture { get; } private Texture2D WhitePixel { get; }
public BallSpawner(Texture2D ballTexture) public BallSpawner(Texture2D whitePixel)
{ {
BallTexture = ballTexture; WhitePixel = whitePixel;
} }
protected override void Spawn(BallSpawnMessage message) protected override void Spawn(BallSpawnMessage message)
@ -20,9 +20,10 @@ namespace PongFE.Spawners
AddComponent(ball, new PositionComponent(message.Position)); AddComponent(ball, new PositionComponent(message.Position));
AddComponent(ball, new VelocityComponent(message.Velocity)); AddComponent(ball, new VelocityComponent(message.Velocity));
AddComponent(ball, new CollisionComponent(new MoonTools.Bonk.Rectangle(0, 0, 16, 16))); AddComponent(ball, new CollisionComponent(new MoonTools.Bonk.Rectangle(0, 0, 16, 16)));
AddComponent(ball, new Texture2DComponent(BallTexture, 0)); AddComponent(ball, new Texture2DComponent(WhitePixel, 0, new System.Numerics.Vector2(message.Width, message.Height)));
AddComponent(ball, new CanBeBouncedComponent()); AddComponent(ball, new CanBeBouncedComponent());
AddComponent(ball, new BounceResponseComponent()); AddComponent(ball, new BounceResponseComponent());
AddComponent(ball, new CanBeTrackedComponent());
} }
} }
} }

View File

@ -0,0 +1,36 @@
using Encompass;
using Microsoft.Xna.Framework.Graphics;
using PongFE.Components;
using PongFE.Enums;
using PongFE.Messages;
namespace PongFE.Spawners
{
public class PaddleSpawner : Spawner<PaddleSpawnMessage>
{
private Texture2D WhitePixel { get; }
public PaddleSpawner(Texture2D whitePixel)
{
WhitePixel = whitePixel;
}
protected override void Spawn(PaddleSpawnMessage message)
{
var paddle = CreateEntity();
if (message.PaddleControl == PaddleControl.Player)
{
AddComponent(paddle, new PlayerInputComponent(message.PlayerIndex));
}
else
{
AddComponent(paddle, new ComputerControlComponent(message.PlayerIndex));
}
AddComponent(paddle, new PaddleMoveSpeedComponent(400));
AddComponent(paddle, new PositionComponent(message.Position));
AddComponent(paddle, new CollisionComponent(new MoonTools.Bonk.Rectangle(0, 0, message.Width, message.Height)));
AddComponent(paddle, new CanCauseBounceComponent());
AddComponent(paddle, new Texture2DComponent(WhitePixel, 0, new System.Numerics.Vector2(message.Width, message.Height)));
}
}
}

26
PongFE/Enums/Enums.cs Normal file
View File

@ -0,0 +1,26 @@
namespace PongFE.Enums
{
public enum PlayerIndex
{
One,
Two
}
public enum HitOrientation
{
Horizontal,
Vertical
}
public enum PaddleMoveDirection
{
Up,
Down
}
public enum PaddleControl
{
Player,
Computer
}
}

View File

@ -0,0 +1,12 @@
using MoonTools.Structs;
namespace PongFE.Extensions
{
public static class Vector2Extensions
{
public static Microsoft.Xna.Framework.Vector2 ToXNAVector(this System.Numerics.Vector2 vector)
{
return new Microsoft.Xna.Framework.Vector2(vector.X, vector.Y);
}
}
}

View File

@ -8,11 +8,15 @@ namespace PongFE.Messages
{ {
public Position2D Position { get; } public Position2D Position { get; }
public Vector2 Velocity { get; } public Vector2 Velocity { get; }
public int Width { get; }
public int Height { get; }
public BallSpawnMessage(Position2D position, Vector2 velocity) public BallSpawnMessage(Position2D position, Vector2 velocity, int width, int height)
{ {
Position = position; Position = position;
Velocity = velocity; Velocity = velocity;
Width = width;
Height = height;
} }
} }
} }

View File

@ -1,4 +1,5 @@
using Encompass; using Encompass;
using PongFE.Enums;
namespace PongFE.Messages namespace PongFE.Messages
{ {

View File

@ -1,13 +1,8 @@
using Encompass; using Encompass;
using PongFE.Enums;
namespace PongFE.Messages namespace PongFE.Messages
{ {
public enum HitOrientation
{
Horizontal,
Vertical
}
public struct CollisionMessage : IMessage public struct CollisionMessage : IMessage
{ {
public Entity EntityA { get; } public Entity EntityA { get; }

View File

@ -1,13 +1,8 @@
using Encompass; using Encompass;
using PongFE.Enums;
namespace PongFE.Messages namespace PongFE.Messages
{ {
public enum PaddleMoveDirection
{
Up,
Down
}
public struct PaddleMoveMessage : IMessage, IHasEntity public struct PaddleMoveMessage : IMessage, IHasEntity
{ {
public Entity Entity { get; } public Entity Entity { get; }

View File

@ -0,0 +1,30 @@
using Encompass;
using MoonTools.Structs;
using PongFE.Enums;
namespace PongFE.Messages
{
public struct PaddleSpawnMessage : IMessage
{
public Position2D Position { get; }
public PlayerIndex PlayerIndex { get; }
public PaddleControl PaddleControl { get; }
public int Width { get; }
public int Height { get; }
public PaddleSpawnMessage(
Position2D position,
PlayerIndex playerIndex,
PaddleControl paddleControl,
int width,
int height
)
{
Position = position;
PlayerIndex = playerIndex;
PaddleControl = paddleControl;
Width = width;
Height = height;
}
}
}

View File

@ -1,8 +1,8 @@
using Encompass; 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.Engines; using PongFE.Engines;
using PongFE.Enums;
using PongFE.Messages; using PongFE.Messages;
using PongFE.Renderers; using PongFE.Renderers;
using PongFE.Spawners; using PongFE.Spawners;
@ -18,8 +18,6 @@ namespace PongFE
SpriteBatch SpriteBatch { get; set; } SpriteBatch SpriteBatch { get; set; }
Texture2D WhitePixel { get; set; } Texture2D WhitePixel { get; set; }
RenderTarget2D PaddleTexture { get; set; }
RenderTarget2D BallTexture { get; set; }
public PongFEGame() public PongFEGame()
{ {
@ -41,19 +39,6 @@ namespace PongFE
WhitePixel = new Texture2D(GraphicsDevice, 1, 1); WhitePixel = new Texture2D(GraphicsDevice, 1, 1);
WhitePixel.SetData(new Color[] { Color.White }); WhitePixel.SetData(new Color[] { Color.White });
PaddleTexture = new RenderTarget2D(GraphicsDevice, 20, 80);
GraphicsDevice.SetRenderTarget(PaddleTexture);
SpriteBatch.Begin();
SpriteBatch.Draw(WhitePixel, new Rectangle(0, 0, 20, 80), Color.White);
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);
WorldBuilder.AddEngine(new InputEngine()); WorldBuilder.AddEngine(new InputEngine());
WorldBuilder.AddEngine(new PaddleMovementEngine()); WorldBuilder.AddEngine(new PaddleMovementEngine());
WorldBuilder.AddEngine(new VelocityEngine()); WorldBuilder.AddEngine(new VelocityEngine());
@ -62,24 +47,40 @@ namespace PongFE
WorldBuilder.AddEngine(new BounceEngine()); WorldBuilder.AddEngine(new BounceEngine());
WorldBuilder.AddEngine(new UpdatePositionEngine()); WorldBuilder.AddEngine(new UpdatePositionEngine());
WorldBuilder.AddEngine(new UpdateVelocityEngine()); WorldBuilder.AddEngine(new UpdateVelocityEngine());
WorldBuilder.AddEngine(new ComputerControlEngine());
WorldBuilder.AddEngine(new BallSpawner(BallTexture)); WorldBuilder.AddEngine(new BallSpawner(WhitePixel));
WorldBuilder.AddEngine(new BoundarySpawner()); WorldBuilder.AddEngine(new BoundarySpawner());
WorldBuilder.AddEngine(new PaddleSpawner(WhitePixel));
WorldBuilder.AddOrderedRenderer(new Texture2DRenderer(SpriteBatch)); WorldBuilder.AddOrderedRenderer(new Texture2DRenderer(SpriteBatch));
var paddle = WorldBuilder.CreateEntity(); WorldBuilder.SendMessage(
WorldBuilder.SetComponent(paddle, new PlayerInputComponent(PongFE.Components.PlayerIndex.One)); new PaddleSpawnMessage(
WorldBuilder.SetComponent(paddle, new PaddleMoveSpeedComponent(400)); new MoonTools.Structs.Position2D(5, 5),
WorldBuilder.SetComponent(paddle, new PositionComponent(new MoonTools.Structs.Position2D(5, 5))); Enums.PlayerIndex.One,
WorldBuilder.SetComponent(paddle, new CollisionComponent(new MoonTools.Bonk.Rectangle(0, 0, 20, 80))); PaddleControl.Player,
WorldBuilder.SetComponent(paddle, new CanCauseBounceComponent()); 20,
WorldBuilder.SetComponent(paddle, new Texture2DComponent(PaddleTexture, 0)); 80
)
);
WorldBuilder.SendMessage(
new PaddleSpawnMessage(
new MoonTools.Structs.Position2D(1255, 5),
Enums.PlayerIndex.Two,
PaddleControl.Computer,
20,
80
)
);
WorldBuilder.SendMessage( WorldBuilder.SendMessage(
new BallSpawnMessage( new BallSpawnMessage(
new MoonTools.Structs.Position2D(640, 360), new MoonTools.Structs.Position2D(640, 360),
new System.Numerics.Vector2(-200, -50) new System.Numerics.Vector2(-300, 200),
16,
16
) )
); );

View File

@ -21,12 +21,12 @@ namespace PongFE.Renderers
_spriteBatch.Draw( _spriteBatch.Draw(
textureComponent.Texture, textureComponent.Texture,
positionComponent.Position.Truncated().ToXNAVector(), positionComponent.Position.ToXNAVector(),
null, null,
Color.White, Color.White,
0, 0,
Vector2.Zero, Vector2.Zero,
Vector2.One, textureComponent.Scale.ToXNAVector(),
SpriteEffects.None, SpriteEffects.None,
0 0
); );