ball destroy and respawn

main
Evan Hemsley 2020-07-15 14:00:55 -07:00
parent a86f8fd8a2
commit 12e08e284c
11 changed files with 161 additions and 11 deletions

View File

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

View File

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

View File

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

View File

@ -0,0 +1,14 @@
using Encompass;
namespace PongFE.Components
{
public struct SpawnBallAfterDestroyComponent : IComponent
{
public float Seconds { get; }
public SpawnBallAfterDestroyComponent(float seconds)
{
Seconds = seconds;
}
}
}

View File

@ -7,10 +7,15 @@ namespace PongFE.Engines
{
[Reads(
typeof(CanCauseBounceComponent),
typeof(CanBeBouncedComponent)
typeof(CanBeBouncedComponent),
typeof(CanDestroyComponent),
typeof(CanBeDestroyedComponent)
)]
[Receives(typeof(CollisionMessage))]
[Sends(typeof(BounceMessage))]
[Sends(
typeof(BounceMessage),
typeof(DestroyMessage)
)]
public class CollisionEngine : Engine
{
public override void Update(double dt)
@ -19,6 +24,9 @@ namespace PongFE.Engines
{
CheckBounce(message.EntityA, message.EntityB, message.HitOrientation);
CheckBounce(message.EntityB, message.EntityA, message.HitOrientation);
CheckDestroy(message.EntityA, message.EntityB);
CheckDestroy(message.EntityB, message.EntityA);
}
}
@ -32,5 +40,16 @@ namespace PongFE.Engines
}
}
}
private void CheckDestroy(Entity a, Entity b)
{
if (HasComponent<CanDestroyComponent>(a))
{
if (HasComponent<CanBeDestroyedComponent>(b))
{
SendMessage(new DestroyMessage(b));
}
}
}
}
}

View File

@ -0,0 +1,35 @@
using Encompass;
using PongFE.Components;
using PongFE.Messages;
namespace PongFE.Engines
{
[Reads(typeof(SpawnBallAfterDestroyComponent))]
[Receives(typeof(DestroyMessage))]
[Sends(typeof(BallSpawnMessage))]
public class DestroyEngine : Engine
{
public override void Update(double dt)
{
foreach (ref readonly var message in ReadMessages<DestroyMessage>())
{
if (HasComponent<SpawnBallAfterDestroyComponent>(message.Entity))
{
ref readonly var respawnComponent = ref GetComponent<SpawnBallAfterDestroyComponent>(message.Entity);
SendMessage(
new BallSpawnMessage(
new MoonTools.Structs.Position2D(640, 360),
new System.Numerics.Vector2(-200, 100),
16,
16
),
respawnComponent.Seconds
);
}
Destroy(message.Entity);
}
}
}
}

View File

@ -24,6 +24,8 @@ namespace PongFE.Spawners
AddComponent(ball, new CanBeBouncedComponent());
AddComponent(ball, new BounceResponseComponent());
AddComponent(ball, new CanBeTrackedComponent());
AddComponent(ball, new CanBeDestroyedComponent());
AddComponent(ball, new SpawnBallAfterDestroyComponent(0.5f));
}
}
}

View File

@ -0,0 +1,18 @@
using Encompass;
using PongFE.Components;
using PongFE.Messages;
namespace PongFE.Spawners
{
public class GoalBoundarySpawner : Spawner<GoalBoundarySpawnMessage>
{
protected override void Spawn(GoalBoundarySpawnMessage message)
{
var entity = CreateEntity();
AddComponent(entity, new PositionComponent(message.Position));
AddComponent(entity, new CollisionComponent(new MoonTools.Bonk.Rectangle(0, 0, message.Width, message.Height)));
AddComponent(entity, new CanDestroyComponent());
}
}
}

View File

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

View File

@ -0,0 +1,19 @@
using Encompass;
using MoonTools.Structs;
namespace PongFE.Messages
{
public struct GoalBoundarySpawnMessage : IMessage
{
public Position2D Position { get; }
public int Width { get; }
public int Height { get; }
public GoalBoundarySpawnMessage(Position2D position, int width, int height)
{
Position = position;
Width = width;
Height = height;
}
}
}

View File

@ -45,12 +45,14 @@ namespace PongFE
WorldBuilder.AddEngine(new MotionEngine());
WorldBuilder.AddEngine(new CollisionEngine());
WorldBuilder.AddEngine(new BounceEngine());
WorldBuilder.AddEngine(new DestroyEngine());
WorldBuilder.AddEngine(new UpdatePositionEngine());
WorldBuilder.AddEngine(new UpdateVelocityEngine());
WorldBuilder.AddEngine(new ComputerControlEngine());
WorldBuilder.AddEngine(new BallSpawner(WhitePixel));
WorldBuilder.AddEngine(new BoundarySpawner());
WorldBuilder.AddEngine(new GoalBoundarySpawner());
WorldBuilder.AddEngine(new PaddleSpawner(WhitePixel));
WorldBuilder.AddOrderedRenderer(new Texture2DRenderer(SpriteBatch));
@ -93,15 +95,6 @@ namespace PongFE
)
);
// right boundary
WorldBuilder.SendMessage(
new BoundarySpawnMessage(
new MoonTools.Structs.Position2D(1280, 0),
6,
720
)
);
// bottom boundary
WorldBuilder.SendMessage(
new BoundarySpawnMessage(
@ -111,6 +104,24 @@ namespace PongFE
)
);
// right boundary
WorldBuilder.SendMessage(
new GoalBoundarySpawnMessage(
new MoonTools.Structs.Position2D(1280, 0),
6,
720
)
);
// left boundary
WorldBuilder.SendMessage(
new GoalBoundarySpawnMessage(
new MoonTools.Structs.Position2D(-6, 0),
6,
720
)
);
World = WorldBuilder.Build();
}