Skip to content

Commit ca1a53c

Browse files
authored
Merge pull request #148 from rrbox/feat/events
Feat/events
2 parents 719e6bd + f51bc88 commit ca1a53c

19 files changed

+206
-157
lines changed

Sources/ECS/Event/CommandsEvent/CommandsEvent+Buffer.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,21 @@
66
//
77

88
extension AnyMap<EventStorage> {
9-
mutating func setUpCommandsEventQueue<T: CommandsEventProtocol>(eventOfType: T.Type) {
10-
push(CommandsEventQueue<T>())
9+
func commandsEventReceiver<T: CommandsEventProtocol>(eventOfType type: T.Type) -> CommandsEventReceiver<T>? {
10+
valueRef(ofType: CommandsEventReceiver<T>.self)?.body
1111
}
1212

13-
func commandsEventQueue<T: CommandsEventProtocol>(eventOfType: T.Type) -> CommandsEventQueue<T>? {
14-
valueRef(ofType: CommandsEventQueue<T>.self)?.body
13+
mutating func registerCommandsEventReceiver<T: CommandsEventProtocol>(eventType: T.Type) {
14+
push(CommandsEventReceiver<T>())
1515
}
1616

1717
func commandsEventWriter<T>(eventOfType type: T.Type) -> CommandsEventWriter<T>? {
1818
valueRef(ofType: CommandsEventWriter<T>.self)?.body
1919
}
2020

2121
mutating func registerCommandsEventWriter<T: CommandsEventProtocol>(eventType: T.Type) {
22-
let eventQueue = valueRef(ofType: CommandsEventQueue<T>.self)!.body
23-
push(CommandsEventWriter<T>(eventQueue: eventQueue))
22+
let receiver = valueRef(ofType: CommandsEventReceiver<T>.self)!.body
23+
push(CommandsEventWriter<T>(receiver: receiver))
2424
}
2525

2626
func commandsEventResponder<T: CommandsEventProtocol>(eventOfType type: T.Type) -> EventResponder<T>? {

Sources/ECS/Event/CommandsEvent/CommandsEventQueue.swift

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//
2+
// CommandsEventReceiver.swift
3+
// ECS_Swift
4+
//
5+
// Created by rrbox on 2025/07/06.
6+
//
7+
8+
final class CommandsEventReceiver<T: CommandsEventProtocol>: AnyEventReceiver, EventStorageElement {
9+
var eventBuffer = [T]()
10+
11+
override func receive(worldStorage: WorldStorageRef) {
12+
let events = eventBuffer
13+
eventBuffer = []
14+
guard !events.isEmpty else { return }
15+
worldStorage.eventStorage.push(EventReader(events: events))
16+
if let systems = worldStorage.eventStorage.commandsEventResponder(eventOfType: T.self)!.systems[.update] {
17+
for system in systems {
18+
system.execute(worldStorage)
19+
}
20+
}
21+
for schedule in worldStorage.stateStorage.currentEventSchedulesWhichAssociatedStates() {
22+
guard let systems = worldStorage.eventStorage.commandsEventResponder(eventOfType: T.self)!.systems[schedule] else { continue }
23+
for system in systems {
24+
system.execute(worldStorage)
25+
}
26+
}
27+
worldStorage.eventStorage.pop(EventReader<T>.self)
28+
}
29+
}

Sources/ECS/Event/CommandsEvent/CommandsEventWriter.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
//
77

88
final class CommandsEventWriter<T: CommandsEventProtocol>: SystemParameter, EventStorageElement {
9-
unowned let eventQueue: CommandsEventQueue<T>
9+
unowned let receiver: CommandsEventReceiver<T>
1010

11-
init(eventQueue: CommandsEventQueue<T>) {
12-
self.eventQueue = eventQueue
11+
init(receiver: CommandsEventReceiver<T>) {
12+
self.receiver = receiver
1313
}
1414

1515
public func send(value: T) {
16-
self.eventQueue.eventQueue.append(value)
16+
receiver.eventBuffer.append(value)
1717
}
1818

1919
public static func register(to worldStorage: WorldStorageRef) {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//
2+
// AnyEventReceiver.swift
3+
// ECS_Swift
4+
//
5+
// Created by rrbox on 2025/07/06.
6+
//
7+
8+
class AnyEventReceiver {
9+
func receive(worldStorage: WorldStorageRef) {}
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//
2+
// EventProtocol.swift
3+
// ECS_Swift
4+
//
5+
// Created by rrbox on 2025/07/06.
6+
//
7+
8+
public protocol EventProtocol {
9+
10+
}

Sources/ECS/Event/EventStreaming/EventQueue.swift

Lines changed: 0 additions & 11 deletions
This file was deleted.

Sources/ECS/Event/EventStreaming/EventReader.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
//
77

88
final public class EventReader<T>: SystemParameter, EventStorageElement {
9-
public let value: T
9+
public let events: [T]
1010

11-
init(value: T) {
12-
self.value = value
11+
init(events: [T]) {
12+
self.events = events
1313
}
1414

1515
public static func register(to worldStorage: WorldStorageRef) {
Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,29 @@
11
//
2-
// Event.swift
3-
//
2+
// EventReceiver.swift
3+
// ECS_Swift
44
//
5-
// Created by rrbox on 2023/08/14.
5+
// Created by rrbox on 2025/07/06.
66
//
77

8-
public protocol EventProtocol {
9-
10-
}
11-
12-
class AnyEvent {
13-
func runEventReceiver(worldStorage: WorldStorageRef) {
14-
15-
}
16-
}
17-
18-
final class Event<T: EventProtocol>: AnyEvent {
19-
let value: T
20-
21-
init(value: T) {
22-
self.value = value
23-
}
24-
25-
override func runEventReceiver(worldStorage: WorldStorageRef) {
26-
worldStorage.eventStorage.push(EventReader(value: self.value))
8+
final class EventReceiver<T: EventProtocol>: AnyEventReceiver, EventStorageElement {
9+
var eventBuffer = [T]()
2710

11+
override func receive(worldStorage: WorldStorageRef) {
12+
let events = eventBuffer
13+
eventBuffer = []
14+
guard !events.isEmpty else { return }
15+
worldStorage.eventStorage.push(EventReader(events: events))
2816
if let systems = worldStorage.eventStorage.eventResponder(eventOfType: T.self)!.systems[.update] {
2917
for system in systems {
3018
system.execute(worldStorage)
3119
}
3220
}
33-
3421
for schedule in worldStorage.stateStorage.currentEventSchedulesWhichAssociatedStates() {
3522
guard let systems = worldStorage.eventStorage.eventResponder(eventOfType: T.self)!.systems[schedule] else { continue }
3623
for system in systems {
3724
system.execute(worldStorage)
3825
}
3926
}
40-
4127
worldStorage.eventStorage.pop(EventReader<T>.self)
4228
}
4329
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//
2+
// EventReceivers.swift
3+
// ECS_Swift
4+
//
5+
// Created by rrbox on 2025/07/06.
6+
//
7+
8+
final class EventReceivers: EventStorageElement {
9+
var eventReceivers = [AnyEventReceiver]()
10+
}

0 commit comments

Comments
 (0)