-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
- Loading branch information
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2022 CodeWriter Packages | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,50 @@ | ||
# Morpeh.Events | ||
Events for Morpeh ECS | ||
# Morpeh.Events [![Github license](https://img.shields.io/github/license/codewriter-packages/Morpeh.Events.svg?style=flat-square)](#) [![Unity 2020.1](https://img.shields.io/badge/Unity-2020.1+-2296F3.svg?style=flat-square)](#) ![GitHub package.json version](https://img.shields.io/github/package-json/v/codewriter-packages/Morpeh.Events?style=flat-square) | ||
_Events for Morpeh ECS_ | ||
|
||
## How to use? | ||
|
||
```csharp | ||
using System; | ||
using Scellecs.Morpeh; | ||
using Scellecs.Morpeh.Systems; | ||
|
||
[Serializable] | ||
public struct DamageRequestedEvent : IEventData { | ||
public EntityId targetEntityId; | ||
} | ||
|
||
[Serializable] | ||
public struct DamagedEvent : IEventData { | ||
public EntityId targetEntityId; | ||
} | ||
|
||
public class DamageSystem : UpdateSystem { | ||
private Event<DamageRequestedEvent> damageRequestedEvent; | ||
private Event<DamagedEvent> damagedEvent; | ||
|
||
public override void OnAwake() { | ||
damageRequestedEvent = World.GetEvent<DamageRequestedEvent>(); | ||
damagedEvent = World.GetEvent<DamagedEvent>(); | ||
} | ||
|
||
public override void OnUpdate(float deltaTime) { | ||
if (!damageRequestedEvent.IsPublished) { | ||
return; | ||
} | ||
|
||
foreach (var evt in damageRequestedEvent.BatchedChanges) { | ||
ApplyDamage(evt.targetEntityId); | ||
|
||
damagedEvent.NextFrame(new DamagedEvent { | ||
targetEntityId = evt.targetEntityId, | ||
}); | ||
} | ||
} | ||
|
||
private void ApplyDamage(EntityId target) { } | ||
} | ||
``` | ||
|
||
## License | ||
|
||
Morpeh.Events is [MIT licensed](./LICENSE.md). |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
using System; | ||
using JetBrains.Annotations; | ||
using Scellecs.Morpeh.Collections; | ||
|
||
namespace Scellecs.Morpeh | ||
{ | ||
public class Event<TData> : IEventInternal where TData : struct, IEventData | ||
{ | ||
private readonly EventRegistry _registry; | ||
|
||
[PublicAPI] public FastList<TData> BatchedChanges { get; } = new FastList<TData>(); | ||
[PublicAPI] public FastList<TData> ScheduledChanges { get; } = new FastList<TData>(); | ||
|
||
[PublicAPI] public bool IsPublished { get; private set; } | ||
[PublicAPI] public bool IsScheduled { get; private set; } | ||
|
||
internal event Action<FastList<TData>> Callback; | ||
|
||
internal Event(EventRegistry registry) | ||
{ | ||
_registry = registry; | ||
} | ||
|
||
[PublicAPI] | ||
public void NextFrame(TData data) | ||
{ | ||
ScheduledChanges.Add(data); | ||
|
||
if (!IsPublished && !IsScheduled) | ||
{ | ||
_registry.DispatchedEvents.Add(this); | ||
} | ||
|
||
IsScheduled = true; | ||
} | ||
|
||
[PublicAPI] | ||
public IDisposable Subscribe(Action<FastList<TData>> callback) | ||
{ | ||
return new Subscription(this, callback); | ||
} | ||
|
||
internal class Subscription : IDisposable | ||
{ | ||
private readonly Event<TData> _owner; | ||
private readonly Action<FastList<TData>> _callback; | ||
|
||
public Subscription(Event<TData> owner, Action<FastList<TData>> callback) | ||
{ | ||
_owner = owner; | ||
_callback = callback; | ||
|
||
_owner.Callback += _callback; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
_owner.Callback -= _callback; | ||
} | ||
} | ||
|
||
public void OnFrameEnd() | ||
{ | ||
if (IsPublished) | ||
{ | ||
Callback?.Invoke(BatchedChanges); | ||
|
||
IsPublished = false; | ||
BatchedChanges.Clear(); | ||
} | ||
|
||
if (IsScheduled) | ||
{ | ||
IsPublished = true; | ||
IsScheduled = false; | ||
BatchedChanges.AddListRange(ScheduledChanges); | ||
ScheduledChanges.Clear(); | ||
|
||
_registry.DispatchedEvents.Add(this); | ||
} | ||
} | ||
} | ||
|
||
public interface IEventInternal | ||
{ | ||
void OnFrameEnd(); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using System.Threading; | ||
|
||
namespace Scellecs.Morpeh | ||
{ | ||
internal class EventIdentifier<TData> where TData : struct, IEventData | ||
{ | ||
public static int identifier; | ||
|
||
static EventIdentifier() | ||
{ | ||
identifier = CommonEventIdentifier.GetID(); | ||
} | ||
} | ||
|
||
internal class CommonEventIdentifier | ||
{ | ||
private static int _counter; | ||
|
||
public static int GetID() | ||
{ | ||
return Interlocked.Increment(ref _counter); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using Scellecs.Morpeh.Collections; | ||
|
||
namespace Scellecs.Morpeh | ||
{ | ||
internal class EventRegistry | ||
{ | ||
internal readonly IntHashMap<IEventInternal> RegisteredEvents = new IntHashMap<IEventInternal>(); | ||
|
||
internal FastList<IEventInternal> DispatchedEvents = new FastList<IEventInternal>(); | ||
internal FastList<IEventInternal> ExecutingEvents = new FastList<IEventInternal>(); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using JetBrains.Annotations; | ||
using Scellecs.Morpeh.Collections; | ||
|
||
namespace Scellecs.Morpeh | ||
{ | ||
public static class EventWorldExtensions | ||
{ | ||
private static readonly IntHashMap<EventRegistry> Registries = new IntHashMap<EventRegistry>(); | ||
|
||
[PublicAPI] | ||
public static Event<TData> GetEvent<TData>(this World world) | ||
where TData : struct, IEventData | ||
{ | ||
var worldIdentifier = world.identifier; | ||
var eventIdentifier = EventIdentifier<TData>.identifier; | ||
|
||
if (!Registries.TryGetValue(worldIdentifier, out var registry)) | ||
{ | ||
registry = new EventRegistry(); | ||
|
||
var eventSystemGroup = world.CreateSystemsGroup(); | ||
eventSystemGroup.AddSystem(new ProcessEventsSystem(registry)); | ||
world.AddSystemsGroup(9999, eventSystemGroup); | ||
|
||
Registries.Add(worldIdentifier, registry, out _); | ||
} | ||
|
||
if (!registry.RegisteredEvents.TryGetValue(eventIdentifier, out var registeredEvent)) | ||
{ | ||
registeredEvent = new Event<TData>(registry); | ||
|
||
registry.RegisteredEvents.Add(eventIdentifier, registeredEvent, out _); | ||
} | ||
|
||
return (Event<TData>) registeredEvent; | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace Scellecs.Morpeh | ||
{ | ||
public interface IEventData | ||
{ | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using Scellecs.Morpeh.Collections; | ||
|
||
namespace Scellecs.Morpeh | ||
{ | ||
internal sealed class ProcessEventsSystem : ICleanupSystem | ||
{ | ||
private readonly EventRegistry _registry; | ||
|
||
public World World { get; set; } | ||
|
||
public ProcessEventsSystem(EventRegistry registry) | ||
{ | ||
_registry = registry; | ||
} | ||
|
||
public void OnAwake() | ||
{ | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
} | ||
|
||
public void OnUpdate(float deltaTime) | ||
{ | ||
var list = _registry.DispatchedEvents; | ||
if (list.length == 0) | ||
{ | ||
return; | ||
} | ||
|
||
_registry.DispatchedEvents = _registry.ExecutingEvents; | ||
_registry.ExecutingEvents = list; | ||
|
||
foreach (var evt in list) | ||
{ | ||
evt.OnFrameEnd(); | ||
} | ||
|
||
list.Clear(); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"reference": "Scellecs.Morpeh" | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"name": "com.codewriter.morpeh.events", | ||
"displayName": "Morpeh.Events", | ||
"description": "Events for Morpeh ECS", | ||
"version": "1.0.0", | ||
"unity": "2020.1", | ||
"license": "MIT", | ||
"author": "CodeWriter (https://github.com/orgs/codewriter-packages)", | ||
"dependencies": {} | ||
} |