Skip to content

Commit

Permalink
Remove dynamic events
Browse files Browse the repository at this point in the history
  • Loading branch information
rettoph committed Dec 18, 2019
1 parent cb01e34 commit 8c6b3ea
Show file tree
Hide file tree
Showing 29 changed files with 192 additions and 105 deletions.
8 changes: 0 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,2 @@
# Galactic Fighters
A game inspired by Void Hunters from FunOrb

## Custom Events
These are events triggered by Guppy but specific to this game. Events can be bound to via the `Creatable.Events` attribute.

| Project | Class | Event | Arg | Description |
| ------- | ----- | ----- | --- | ----------- |
| | | | | |
| `VoidHuntersRevived.Library` | `FarseerEntity` | `controller:chainged` | `IController` | Invoked when the SetController method is called. |
7 changes: 4 additions & 3 deletions VoidHuntersRevived.Client.Library/ClientServiceLoader.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Guppy.Attributes;
using Guppy.Collections;
using Guppy.Extensions.DependencyInjection;
using Guppy.Factories;
using Guppy.Interfaces;
using Guppy.Loaders;
Expand All @@ -21,9 +22,9 @@ public void ConfigureServices(IServiceCollection services)
{
services.AddTransient<SpriteManager>();
services.AddScoped<ServerShadow>();
services.AddScoped<DebugOverlay>(p => p.GetRequiredService<InitializableFactory<DebugOverlay>>().Build<DebugOverlay>());
services.AddScoped<Sensor>(p => p.GetRequiredService<EntityCollection>().Create<Sensor>("entity:sensor"));
services.AddScoped<TrailManager>(p => p.GetRequiredService<EntityCollection>().Create<TrailManager>("entity:trail-manager"));
services.AddScoped<DebugOverlay, InitializableFactory<DebugOverlay>>();
services.AddScoped<Sensor>("entity:sensor");
services.AddScoped<TrailManager>("entity:trail-manager");
}

public void ConfigureProvider(IServiceProvider provider)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,14 @@ protected override void Initialize()

_box = new BoundingBox(new Vector3(this.driven.Bounds.Left, this.driven.Bounds.Top, 0), new Vector3(this.driven.Bounds.Right, this.driven.Bounds.Bottom, 0));

this.driven.Events.TryAdd<GameTime>("cleaned", this.HandleChunkCleaned);
this.driven.OnCleaned += this.HandleChunkCleaned;
}

protected override void Dispose()
{
base.Dispose();

this.driven.OnCleaned -= this.HandleChunkCleaned;
}
#endregion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected override void PreInitialize()
_shadow = this.driven.CreateBody(_server.World);

this.driven.Actions.TryAdd("update:vitals", this.HandleUpdateVitalsAction);
this.driven.Events.TryAdd<Controller>("controller:changed", this.HandleControllerChanged);
this.driven.OnControllerChanged += this.HandleControllerChanged;
}

protected override void Initialize()
Expand All @@ -64,6 +64,8 @@ protected override void Dispose()
base.Dispose();

_shadow.Dispose(withFixtures: true);

this.driven.OnControllerChanged -= this.HandleControllerChanged;
}
#endregion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,10 @@ protected override void Initialize()
// _camera.ZoomLerp = 0.005f;

// Setup local user events
_pointer.Events.TryAdd<Int32>("scrolled", this.HandlePointerScrolled);
_pointer.Events.TryAdd<Pointer.Button>("pressed", this.HandlePointerButtonPressed);
_pointer.Events.TryAdd<Pointer.Button>("released", this.HandlePointerButtonReleased);
_pointer.OnScrolled += this.HandlePointerScrolled;
_pointer.OnPressed += this.HandlePointerButtonPressed;
_pointer.OnReleased += this.HandlePointerButtonReleased;


_debug.AddLine(gt => "\nCurrent Player");
_debug.AddLine(gt => $" Position => X: {this.driven?.Ship?.Bridge?.Position.X.ToString("#,##0.000")} Y: {this.driven?.Ship?.Bridge?.Position.Y.ToString("#,##00.000")}, R: {this.driven?.Ship?.Bridge?.Rotation.ToString("#,##00.000")}");
Expand All @@ -86,6 +87,15 @@ protected override void Initialize()
};
}
}

protected override void Dispose()
{
base.Dispose();

_pointer.OnScrolled -= this.HandlePointerScrolled;
_pointer.OnPressed -= this.HandlePointerButtonPressed;
_pointer.OnReleased -= this.HandlePointerButtonReleased;
}
#endregion

#region Frame Methods
Expand Down Expand Up @@ -223,7 +233,7 @@ private void TryReleaseTractorBeam()
#endregion

#region Event Handlers
private void HandlePointerScrolled(object sender, Int32 arg)
private void HandlePointerScrolled(object sender, Single arg)
{ // Zoom in the camera
_camera.ZoomTo((Single)Math.Pow(1.5, (Single)arg / 120));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,16 @@ protected override void Initialize()
{
base.Initialize();

this.driven.Events.TryAdd<ShipPart.ChainUpdate>("chain:updated", this.HandleChainUpdated);
this.driven.OnChainUpdated += this.HandleChainUpdated;
}

protected override void Dispose()
{
base.Dispose();

_fixtures.Clear();

this.driven.OnChainUpdated -= this.HandleChainUpdated;
}
#endregion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ protected override void PostInitialize()

_shadow = _server[this.driven];

this.driven.MaleConnectionNode.Events.TryAdd<ConnectionNode>("detached", (s, n) =>
// TODO: deregister event handler somewhere
this.driven.MaleConnectionNode.OnDetached += (s, n) =>
{
// Update the server shadow's Body's world position
this.driven.SetWorldTransform(n.Parent.Root, _shadow);
});
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,14 @@ protected override void PostInitialize()
{
base.PostInitialize();

this.driven.Events.TryAdd<ChainUpdate>("chain:updated", this.HandleChainUpdated);
this.driven.OnChainUpdated += this.HandleChainUpdated;
}

protected override void Dispose()
{
base.Dispose();

this.driven.OnChainUpdated -= this.HandleChainUpdated;
}
#endregion

Expand Down
5 changes: 3 additions & 2 deletions VoidHuntersRevived.Library/Entities/Controllers/Chunk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ public override bool Add(FarseerEntity entity)
{
_added.Enqueue(entity);
this.GetSurrounding().ForEach(c => c.Dirty = true);
entity.Events.TryAdd<Boolean>("dirty:changed", this.HandleComponentDirtyChanged);
entity.OnDirtyChanged += this.HandleComponentDirtyChanged;

return true;
}
}
Expand All @@ -115,7 +116,7 @@ public override bool Remove(FarseerEntity entity)
if(base.Remove(entity))
{
this.GetSurrounding().ForEach(c => c.Dirty = true);
entity.Events.TryRemove<Boolean>("dirty:changed", this.HandleComponentDirtyChanged);
entity.OnDirtyChanged -= this.HandleComponentDirtyChanged;

return true;
}
Expand Down
8 changes: 5 additions & 3 deletions VoidHuntersRevived.Library/Entities/Controllers/Controller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public abstract class Controller : Entity
public Boolean Locked { get; protected set; }
#endregion

#region Events
public event EventHandler<GameTime> OnCleaned;
#endregion

#region Lifecycle Methods
protected override void Create(IServiceProvider provider)
{
Expand All @@ -44,8 +48,6 @@ protected override void Create(IServiceProvider provider)
_components = new HashSet<FarseerEntity>();

this.Locked = false;

this.Events.Register<GameTime>("cleaned");
}

public override void Dispose()
Expand Down Expand Up @@ -103,7 +105,7 @@ protected void TryClean(GameTime gameTime)
{ // Clean the chunk if dirty
this.Clean(gameTime);

this.Events.TryInvoke<GameTime>(this, "cleaned", gameTime);
this.OnCleaned?.Invoke(this, gameTime);

this.Dirty = false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public override Boolean Add(FarseerEntity entity)
{
if (base.Add(entity))
{
entity.Events.TryAdd<Creatable>("disposing", this.HandleComponentDisposing);
entity.OnDisposing += this.HandleComponentDisposing;

return true;
}
Expand All @@ -38,7 +38,7 @@ public override Boolean Remove(FarseerEntity entity)
{
if (base.Remove(entity))
{
entity.Events.TryRemove<Creatable>("disposing", this.HandleComponentDisposing);
entity.OnDisposing -= this.HandleComponentDisposing;

return true;
}
Expand All @@ -48,9 +48,9 @@ public override Boolean Remove(FarseerEntity entity)
#endregion

#region Event Handlers
protected virtual void HandleComponentDisposing(object sender, Creatable arg)
protected virtual void HandleComponentDisposing(object sender, EventArgs arg)
{
var entity = arg as FarseerEntity;
var entity = sender as FarseerEntity;
// Auto remove the item from the current controller
this.Remove(entity);
entity.SetController(_annex);
Expand Down
9 changes: 5 additions & 4 deletions VoidHuntersRevived.Library/Entities/FarseerEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ public class FarseerEntity : NetworkEntity
public virtual Boolean IsMoving { get => this.Body.LinearVelocity.Length() > Single.Epsilon && Math.Abs(this.Body.AngularVelocity) > Single.Epsilon; }
#endregion

#region Events
public event EventHandler<Controller> OnControllerChanged;
#endregion

#region Lifecycle Methods
protected override void Create(IServiceProvider provider)
{
Expand All @@ -56,9 +60,6 @@ protected override void Create(IServiceProvider provider)

this.world = provider.GetRequiredService<World>();
this.chunks = provider.GetRequiredService<ChunkCollection>();

// Create internal events
this.Events.Register<Controller>("controller:changed");
}

protected override void PreInitialize()
Expand Down Expand Up @@ -134,7 +135,7 @@ internal void SetController(Controller controller)
this.Controller = controller;
this.Controller?.SetupBody(this, this.Body);

this.Events.TryInvoke<Controller>(this, "controller:changed", this.Controller);
this.OnControllerChanged?.Invoke(this, this.Controller);
}
}
#endregion
Expand Down
18 changes: 13 additions & 5 deletions VoidHuntersRevived.Library/Entities/NetworkEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ public class NetworkEntity : Entity
protected Group group { get; private set; }
#endregion

#region Events
public event EventHandler<Boolean> OnDirtyChanged;
public event EventHandler<GameTime> OnCleaned;
#endregion

#region Lifecycle Methods
protected override void Create(IServiceProvider provider)
{
Expand All @@ -38,9 +43,6 @@ protected override void Create(IServiceProvider provider)

this.entities = provider.GetRequiredService<EntityCollection>();
this.group = provider.GetRequiredService<NetworkScene>().Group;

this.Events.Register<Boolean>("dirty:changed");
this.Events.Register<GameTime>("clean");
}

protected override void PreInitialize()
Expand All @@ -66,8 +68,9 @@ protected override void Update(GameTime gameTime)

if (this.Dirty)
{ // If the curent entity is dirty...
this.Events.TryInvoke<GameTime>(this, "clean", gameTime);
this.Clean(gameTime);
this.SetDirty(false);
this.OnCleaned?.Invoke(this, gameTime);
}
}
#endregion
Expand All @@ -79,9 +82,14 @@ public void SetDirty(Boolean value)
{
this.Dirty = value;

this.Events.TryInvoke<Boolean>(this, "dirty:changed", this.Dirty);
this.OnDirtyChanged?.Invoke(this, this.Dirty);
}
}

protected virtual void Clean(GameTime gameTime)
{

}
#endregion

#region Network Methods
Expand Down
Loading

0 comments on commit 8c6b3ea

Please sign in to comment.