Skip to content

Commit

Permalink
changed DefaultBrick sample to use fully static system (no reflection…
Browse files Browse the repository at this point in the history
…/code generation)
  • Loading branch information
Doraku committed Apr 5, 2021
1 parent 417f772 commit bb04750
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 30 deletions.
1 change: 0 additions & 1 deletion source/Sample/DefaultBrick/DefaultBrick.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="DefaultEcs.Analyzer" Version="0.15.0" PrivateAssets="all" />
<PackageReference Include="MonoGame.Framework.WindowsDX" Version="3.8.0.1641" />
</ItemGroup>

Expand Down
5 changes: 2 additions & 3 deletions source/Sample/DefaultBrick/DefaultGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ public DefaultGame()
new PositionSystem(_world, _runner),
new DrawSystem(_batch, _square, _world));

_world.Subscribe(this);
_world.Subscribe<BrickBrokenMessage>(On);
_world.Subscribe<BarBounceMessage>(On);

Level1.CreatePlayer(_world);
}
Expand All @@ -72,10 +73,8 @@ public DefaultGame()

#region Callbacks

[Subscribe]
private void On(in BrickBrokenMessage _) => _breakSound.Play();

[Subscribe]
private void On(in BarBounceMessage _) => _bounceSound.Play();

#endregion
Expand Down
13 changes: 9 additions & 4 deletions source/Sample/DefaultBrick/System/BallBoundSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@

namespace DefaultBrick.System
{
[With(typeof(Ball))]
public sealed partial class BallBoundSystem : AEntitySetSystem<float>
public sealed class BallBoundSystem : AEntitySetSystem<float>
{
[Update(true)]
private void Update(in Entity entity, ref Position position, ref Velocity velocity)
public BallBoundSystem(World world)
: base(world.GetEntities().With<Ball>().With<Position>().With<Velocity>().AsSet(), true)
{ }

protected override void Update(float state, in Entity entity)
{
ref Position position = ref entity.Get<Position>();
ref Velocity velocity = ref entity.Get<Velocity>();

if (position.Value.X < 0)
{
position.Value.X *= -1;
Expand Down
15 changes: 11 additions & 4 deletions source/Sample/DefaultBrick/System/BallToBarSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,28 @@

namespace DefaultBrick.System
{
public sealed partial class BallToBarSystem : AEntitySetSystem<float>
public sealed class BallToBarSystem : AEntitySetSystem<float>
{
[ConstructorParameter]
private readonly GameWindow _window;

private MouseState _state;

public BallToBarSystem(World world, GameWindow window)
: base(world.GetEntities().With<BallStart>().With<DrawInfo>().AsSet(), true)
{
_window = window;
}

protected override void PreUpdate(float state)
{
_state = Mouse.GetState(_window);
}

[Update(true)]
private void Update(in Entity entity, in BallStart ballStart, ref DrawInfo drawInfo)
protected override void Update(float state, in Entity entity)
{
ref BallStart ballStart = ref entity.Get<BallStart>();
DrawInfo drawInfo = entity.Get<DrawInfo>();

int offset = ballStart.OffSet;

drawInfo.Destination.X = MathHelper.Clamp(_state.X - 50 + offset, offset, _window.ClientBounds.Width - 100 + offset);
Expand Down
3 changes: 1 addition & 2 deletions source/Sample/DefaultBrick/System/CollisionSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@

namespace DefaultBrick.System
{
[With(typeof(Solid), typeof(DrawInfo))]
public sealed class CollisionSystem : AEntitySetSystem<float>
{
private readonly World _world;
private readonly EntitySet _balls;

public CollisionSystem(World world)
: base(world, true)
: base(world.GetEntities().With<Solid>().With<DrawInfo>().AsSet(), true)
{
_world = world;
_balls = _world.GetEntities().With<Ball>().With<Position>().With<Velocity>().AsSet();
Expand Down
7 changes: 3 additions & 4 deletions source/Sample/DefaultBrick/System/GameSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,15 @@ public GameSystem(World world)
_random = new Random();
_world = world;

_world.Subscribe(this);
_world.Subscribe<BallDroppedMessage>(On);
_world.Subscribe<BrickBrokenMessage>(On);
_world.Subscribe<NewBrickMessage>(On);
}

[Subscribe]
private void On(in BallDroppedMessage _) => --_ballCount;

[Subscribe]
private void On(in BrickBrokenMessage _) => --_brickCount;

[Subscribe]
private void On(in NewBrickMessage _) => ++_brickCount;

public bool IsEnabled { get; set; } = true;
Expand Down
15 changes: 10 additions & 5 deletions source/Sample/DefaultBrick/System/PlayerSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,27 @@

namespace DefaultBrick.System
{
[With(typeof(PlayerInput))]
public sealed partial class PlayerSystem : AEntitySetSystem<float>
public sealed class PlayerSystem : AEntitySetSystem<float>
{
[ConstructorParameter]
private readonly GameWindow _window;

private MouseState _state;

public PlayerSystem(World world, GameWindow window)
: base(world.GetEntities().With<PlayerInput>().With<DrawInfo>().AsSet())
{
_window = window;
}

protected override void PreUpdate(float state)
{
_state = Mouse.GetState(_window);
}

[Update]
private void Update(ref DrawInfo drawInfo)
protected override void Update(float state, in Entity entity)
{
ref DrawInfo drawInfo = ref entity.Get<DrawInfo>();

drawInfo.Destination.X = MathHelper.Clamp(_state.X - (drawInfo.Destination.Width / 2), 0, _window.ClientBounds.Width - drawInfo.Destination.Width);
}
}
Expand Down
14 changes: 11 additions & 3 deletions source/Sample/DefaultBrick/System/PositionSystem.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
using DefaultBrick.Component;
using DefaultEcs;
using DefaultEcs.System;
using DefaultEcs.Threading;

namespace DefaultBrick.System
{
public sealed partial class PositionSystem : AEntitySetSystem<float>
public sealed class PositionSystem : AEntitySetSystem<float>
{
[Update]
private static void Update(in Position position, ref DrawInfo drawInfo)
public PositionSystem(World world, IParallelRunner runner)
: base(world.GetEntities().With<Position>().With<DrawInfo>().AsSet(), runner)
{ }

protected override void Update(float state, in Entity entity)
{
Position position = entity.Get<Position>();
ref DrawInfo drawInfo = ref entity.Get<DrawInfo>();

drawInfo.Destination.X = (int)position.Value.X;
drawInfo.Destination.Y = (int)position.Value.Y;
}
Expand Down
16 changes: 12 additions & 4 deletions source/Sample/DefaultBrick/System/VelocitySystem.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
using DefaultBrick.Component;
using DefaultEcs;
using DefaultEcs.System;
using DefaultEcs.Threading;
using Microsoft.Xna.Framework;

namespace DefaultBrick.System
{
public sealed partial class VelocitySystem : AEntitySetSystem<float>
public sealed class VelocitySystem : AEntitySetSystem<float>
{
[Update]
private static void Update(float elapsedTime, ref Velocity velocity, ref Position position)
public VelocitySystem(World world, IParallelRunner runner)
: base(world.GetEntities().With<Velocity>().With<Position>().AsSet(), runner)
{ }

protected override void Update(float state, in Entity entity)
{
Vector2 offset = velocity.Value * elapsedTime;
ref Velocity velocity = ref entity.Get<Velocity>();
ref Position position = ref entity.Get<Position>();

Vector2 offset = velocity.Value * state;

position.Value.X += offset.X;
position.Value.Y += offset.Y;
Expand Down

0 comments on commit bb04750

Please sign in to comment.