Skip to content

Commit 00962ef

Browse files
committed
initial
1 parent d77f12b commit 00962ef

22 files changed

Lines changed: 1081 additions & 2 deletions

.editorconfig

Lines changed: 708 additions & 0 deletions
Large diffs are not rendered by default.

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 CodeWriter Packages
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

LICENSE.md.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,50 @@
1-
# Morpeh.Events
2-
Events for Morpeh ECS
1+
# 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)
2+
_Events for Morpeh ECS_
3+
4+
## How to use?
5+
6+
```csharp
7+
using System;
8+
using Scellecs.Morpeh;
9+
using Scellecs.Morpeh.Systems;
10+
11+
[Serializable]
12+
public struct DamageRequestedEvent : IEventData {
13+
public EntityId targetEntityId;
14+
}
15+
16+
[Serializable]
17+
public struct DamagedEvent : IEventData {
18+
public EntityId targetEntityId;
19+
}
20+
21+
public class DamageSystem : UpdateSystem {
22+
private Event<DamageRequestedEvent> damageRequestedEvent;
23+
private Event<DamagedEvent> damagedEvent;
24+
25+
public override void OnAwake() {
26+
damageRequestedEvent = World.GetEvent<DamageRequestedEvent>();
27+
damagedEvent = World.GetEvent<DamagedEvent>();
28+
}
29+
30+
public override void OnUpdate(float deltaTime) {
31+
if (!damageRequestedEvent.IsPublished) {
32+
return;
33+
}
34+
35+
foreach (var evt in damageRequestedEvent.BatchedChanges) {
36+
ApplyDamage(evt.targetEntityId);
37+
38+
damagedEvent.NextFrame(new DamagedEvent {
39+
targetEntityId = evt.targetEntityId,
40+
});
41+
}
42+
}
43+
44+
private void ApplyDamage(EntityId target) { }
45+
}
46+
```
47+
48+
## License
49+
50+
Morpeh.Events is [MIT licensed](./LICENSE.md).

README.md.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Scellecs.Morpeh.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Scellecs.Morpeh/Event.cs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
using System;
2+
using JetBrains.Annotations;
3+
using Scellecs.Morpeh.Collections;
4+
5+
namespace Scellecs.Morpeh
6+
{
7+
public class Event<TData> : IEventInternal where TData : struct, IEventData
8+
{
9+
private readonly EventRegistry _registry;
10+
11+
[PublicAPI] public FastList<TData> BatchedChanges { get; } = new FastList<TData>();
12+
[PublicAPI] public FastList<TData> ScheduledChanges { get; } = new FastList<TData>();
13+
14+
[PublicAPI] public bool IsPublished { get; private set; }
15+
[PublicAPI] public bool IsScheduled { get; private set; }
16+
17+
internal event Action<FastList<TData>> Callback;
18+
19+
internal Event(EventRegistry registry)
20+
{
21+
_registry = registry;
22+
}
23+
24+
[PublicAPI]
25+
public void NextFrame(TData data)
26+
{
27+
ScheduledChanges.Add(data);
28+
29+
if (!IsPublished && !IsScheduled)
30+
{
31+
_registry.DispatchedEvents.Add(this);
32+
}
33+
34+
IsScheduled = true;
35+
}
36+
37+
[PublicAPI]
38+
public IDisposable Subscribe(Action<FastList<TData>> callback)
39+
{
40+
return new Subscription(this, callback);
41+
}
42+
43+
internal class Subscription : IDisposable
44+
{
45+
private readonly Event<TData> _owner;
46+
private readonly Action<FastList<TData>> _callback;
47+
48+
public Subscription(Event<TData> owner, Action<FastList<TData>> callback)
49+
{
50+
_owner = owner;
51+
_callback = callback;
52+
53+
_owner.Callback += _callback;
54+
}
55+
56+
public void Dispose()
57+
{
58+
_owner.Callback -= _callback;
59+
}
60+
}
61+
62+
public void OnFrameEnd()
63+
{
64+
if (IsPublished)
65+
{
66+
Callback?.Invoke(BatchedChanges);
67+
68+
IsPublished = false;
69+
BatchedChanges.Clear();
70+
}
71+
72+
if (IsScheduled)
73+
{
74+
IsPublished = true;
75+
IsScheduled = false;
76+
BatchedChanges.AddListRange(ScheduledChanges);
77+
ScheduledChanges.Clear();
78+
79+
_registry.DispatchedEvents.Add(this);
80+
}
81+
}
82+
}
83+
84+
public interface IEventInternal
85+
{
86+
void OnFrameEnd();
87+
}
88+
}

Scellecs.Morpeh/Event.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Scellecs.Morpeh/EventIdentifier.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System.Threading;
2+
3+
namespace Scellecs.Morpeh
4+
{
5+
internal class EventIdentifier<TData> where TData : struct, IEventData
6+
{
7+
public static int identifier;
8+
9+
static EventIdentifier()
10+
{
11+
identifier = CommonEventIdentifier.GetID();
12+
}
13+
}
14+
15+
internal class CommonEventIdentifier
16+
{
17+
private static int _counter;
18+
19+
public static int GetID()
20+
{
21+
return Interlocked.Increment(ref _counter);
22+
}
23+
}
24+
}

Scellecs.Morpeh/EventIdentifier.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)