scoring stuff

main
Evan Hemsley 2020-07-16 14:39:36 -07:00
parent dccab83a00
commit 0f6282b049
16 changed files with 124 additions and 45 deletions

View File

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

View File

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

View File

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

View File

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

View File

@ -0,0 +1,14 @@
using Encompass;
namespace PongFE.Components
{
public struct ScoreComponent : IComponent
{
public int Score { get; }
public ScoreComponent(int score)
{
Score = score;
}
}
}

View File

@ -47,7 +47,7 @@ namespace PongFE.Engines
{
if (HasComponent<CanBeDestroyedComponent>(b))
{
SendMessage(new DestroyMessage(b));
SendMessage(new DestroyMessage(b, a));
}
}
}

View File

@ -4,9 +4,15 @@ using PongFE.Messages;
namespace PongFE.Engines
{
[Reads(typeof(SpawnBallAfterDestroyComponent))]
[Reads(
typeof(SpawnBallAfterDestroyComponent),
typeof(IncreaseScoreAfterDestroyComponent)
)]
[Receives(typeof(DestroyMessage))]
[Sends(typeof(BallSpawnMessage))]
[Sends(
typeof(BallSpawnMessage),
typeof(ScoreMessage)
)]
public class DestroyEngine : Engine
{
public override void Update(double dt)
@ -28,6 +34,11 @@ namespace PongFE.Engines
);
}
if (HasComponent<IncreaseScoreAfterDestroyComponent>(message.Entity))
{
SendMessage(new ScoreMessage(message.DestroyedBy));
}
Destroy(message.Entity);
}
}

View File

@ -6,7 +6,7 @@ using PongFE.Messages;
namespace PongFE.Engines
{
[Reads(typeof(PlayerInputComponent))]
[Reads(typeof(PlayerInputComponent), typeof(PlayerComponent))]
[Sends(typeof(PaddleMoveMessage))]
public class InputEngine : Engine
{
@ -17,8 +17,10 @@ namespace PongFE.Engines
foreach (ref readonly var playerInputEntity in ReadEntities<PlayerInputComponent>())
{
ref readonly var playerInputComponent = ref GetComponent<PlayerInputComponent>(playerInputEntity);
if (playerInputComponent.PlayerIndex == PlayerIndex.One)
if (HasComponent<PlayerComponent>(playerInputEntity))
{
ref readonly var playerComponent = ref GetComponent<PlayerComponent>(playerInputEntity);
if (playerComponent.PlayerIndex == PlayerIndex.One)
{
if (keyboardState.IsKeyDown(Keys.Down))
{
@ -42,4 +44,5 @@ namespace PongFE.Engines
}
}
}
}
}

View File

@ -0,0 +1,24 @@
using Encompass;
using PongFE.Components;
using PongFE.Messages;
namespace PongFE.Engines
{
[Reads(typeof(ScoreComponent))]
[Receives(typeof(ScoreMessage))]
[Writes(typeof(ScoreComponent))]
public class ScoreEngine : Engine
{
public override void Update(double dt)
{
foreach (ref readonly var scoreMessage in ReadMessages<ScoreMessage>())
{
if (HasComponent<ScoreComponent>(scoreMessage.Entity))
{
ref readonly var scoreComponent = ref GetComponent<ScoreComponent>(scoreMessage.Entity);
SetComponent(scoreMessage.Entity, new ScoreComponent(scoreComponent.Score + 1));
}
}
}
}
}

View File

@ -32,6 +32,7 @@ namespace PongFE.Spawners
AddComponent(ball, new CanBeTrackedComponent());
AddComponent(ball, new CanBeDestroyedComponent());
AddComponent(ball, new SpawnBallAfterDestroyComponent(0.5f));
AddComponent(ball, new IncreaseScoreAfterDestroyComponent());
}
}
}

View File

@ -13,6 +13,7 @@ namespace PongFE.Spawners
AddComponent(entity, new PositionComponent(message.Position));
AddComponent(entity, new CollisionComponent(new MoonTools.Bonk.Rectangle(0, 0, message.Width, message.Height)));
AddComponent(entity, new CanDestroyComponent());
AddComponent(entity, new ScoreComponent(0));
}
}
}

View File

@ -20,12 +20,13 @@ namespace PongFE.Spawners
var paddle = CreateEntity();
if (message.PaddleControl == PaddleControl.Player)
{
AddComponent(paddle, new PlayerInputComponent(message.PlayerIndex));
AddComponent(paddle, new PlayerInputComponent());
}
else
{
AddComponent(paddle, new ComputerControlComponent(message.PlayerIndex));
AddComponent(paddle, new ComputerControlComponent());
}
AddComponent(paddle, new PlayerComponent(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)));

View File

@ -5,10 +5,12 @@ namespace PongFE.Messages
public struct DestroyMessage : IMessage
{
public Entity Entity { get; }
public Entity DestroyedBy { get; }
public DestroyMessage(Entity entity)
public DestroyMessage(Entity entity, Entity destroyedBy)
{
Entity = entity;
DestroyedBy = destroyedBy;
}
}
}

View File

@ -1,5 +1,6 @@
using Encompass;
using MoonTools.Structs;
using PongFE.Enums;
namespace PongFE.Messages
{

View File

@ -0,0 +1,14 @@
using Encompass;
namespace PongFE.Messages
{
public struct ScoreMessage : IMessage
{
public Entity Entity { get; }
public ScoreMessage(Entity entity)
{
Entity = entity;
}
}
}

View File

@ -46,6 +46,7 @@ namespace PongFE
WorldBuilder.AddEngine(new CollisionEngine());
WorldBuilder.AddEngine(new BounceEngine());
WorldBuilder.AddEngine(new DestroyEngine());
WorldBuilder.AddEngine(new ScoreEngine());
WorldBuilder.AddEngine(new UpdatePositionEngine());
WorldBuilder.AddEngine(new UpdateVelocityEngine());
WorldBuilder.AddEngine(new ComputerControlEngine());