Skip to content

Commit

Permalink
changed DefaultSlap sample to not rely on code generation
Browse files Browse the repository at this point in the history
  • Loading branch information
Doraku committed Apr 5, 2021
1 parent bb04750 commit 2fb8204
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 19 deletions.
18 changes: 14 additions & 4 deletions source/Sample/DefaultSlap/System/AISystem.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
using System;
using System.Threading;
using DefaultEcs;
using DefaultEcs.System;
using DefaultEcs.Threading;
using DefaultSlap.Component;
using Microsoft.Xna.Framework;

namespace DefaultSlap.System
{
public sealed partial class AISystem : AEntitySetSystem<float>
[With(typeof(TargetPosition), typeof(PositionFloat), typeof(Speed))]
public sealed class AISystem : AEntitySetSystem<float>
{
private readonly ThreadLocal<Random> _random = new(() => new Random());

[Update]
private void Update(float elapsedTime, ref TargetPosition target, ref PositionFloat position, in Speed speed)
public AISystem(World world, IParallelRunner runner)
: base(world, runner)
{ }

protected override void Update(float state, in Entity entity)
{
ref TargetPosition target = ref entity.Get<TargetPosition>();
ref PositionFloat position = ref entity.Get<PositionFloat>();
Speed speed = entity.Get<Speed>();

Vector2 offset = new Vector2(target.Value.X, target.Value.Y) - position.Value;
if (target.Value == Point.Zero
|| offset.Length() < 10f)
Expand All @@ -23,7 +33,7 @@ private void Update(float elapsedTime, ref TargetPosition target, ref PositionFl

offset.Normalize();

position.Value += offset * elapsedTime * speed.Value;
position.Value += offset * state * speed.Value;
}
}
}
22 changes: 15 additions & 7 deletions source/Sample/DefaultSlap/System/DrawSystem.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
using DefaultEcs.System;
using DefaultEcs;
using DefaultEcs.System;
using DefaultSlap.Component;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace DefaultSlap.System
{
public sealed partial class DrawSystem : AEntitySetSystem<float>
[With(typeof(Position), typeof(DrawInfo))]
public sealed class DrawSystem : AEntitySetSystem<float>
{
[ConstructorParameter]
private readonly SpriteBatch _batch;

[ConstructorParameter]
private readonly Texture2D _square;

public DrawSystem(World world, SpriteBatch batch, Texture2D square)
: base(world)
{
_batch = batch;
_square = square;
}

protected override void PreUpdate(float state) => _batch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);

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

_batch.Draw(_square, new Rectangle(position.Value - (drawInfo.Size / new Point(2)), drawInfo.Size), null, drawInfo.Color, 0, Vector2.Zero, SpriteEffects.None, drawInfo.ZIndex ?? 0f);
}

Expand Down
17 changes: 13 additions & 4 deletions source/Sample/DefaultSlap/System/PlayerSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,32 @@

namespace DefaultSlap.System
{
internal sealed partial class PlayerSystem : AEntitySetSystem<float>
[With(typeof(PlayerState), typeof(Position), typeof(DrawInfo))]
internal sealed class PlayerSystem : AEntitySetSystem<float>
{
[ConstructorParameter]
private readonly GameWindow _window;

private MouseState _mouseState;
private bool _isSlaping;

public PlayerSystem(World world, GameWindow window)
: base(world)
{
_window = window;
}

protected override void PreUpdate(float state)
{
_mouseState = Mouse.GetState(_window);
_isSlaping = _mouseState.LeftButton == ButtonState.Pressed;
}

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

position.Value = _mouseState.Position;

if (_isSlaping
Expand Down
17 changes: 13 additions & 4 deletions source/Sample/DefaultSlap/System/PositionSystem.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
using DefaultEcs.System;
using DefaultEcs;
using DefaultEcs.System;
using DefaultEcs.Threading;
using DefaultSlap.Component;
using Microsoft.Xna.Framework;

namespace DefaultSlap.System
{
public sealed partial class PositionSystem : AEntitySetSystem<float>
[With(typeof(PositionFloat), typeof(Position))]
public sealed class PositionSystem : AEntitySetSystem<float>
{
[Update]
private static void Update(in PositionFloat positionFloat, ref Position position)
public PositionSystem(World world, IParallelRunner runner)
: base(world, runner)
{ }

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

position.Value = new Point((int)positionFloat.Value.X, (int)positionFloat.Value.Y);
}
}
Expand Down

0 comments on commit 2fb8204

Please sign in to comment.