Skip to content

Commit 831c722

Browse files
committed
Remove context arg requirement from System init
1 parent c90a49d commit 831c722

File tree

5 files changed

+53
-26
lines changed

5 files changed

+53
-26
lines changed

Sources/GateEngine/ECS/Base/ECSContext.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -264,13 +264,13 @@ extension ECSContext {
264264
// }
265265
for system in self.systems {
266266
self.performance?.beginStatForSystem(system)
267-
await system.willUpdate(input: input, withTimePassed: deltaTime)
267+
await system.willUpdate(input: input, withTimePassed: deltaTime, context: self)
268268
self.performance?.endCurrentStatistic()
269269
}
270270
for system in self.platformSystems {
271271
// guard type(of: system).phase == .postDeferred else { continue }
272272
self.performance?.beginStatForSystem(system)
273-
await system.willUpdate(input: input, withTimePassed: deltaTime)
273+
await system.willUpdate(input: input, withTimePassed: deltaTime, context: self)
274274
self.performance?.endCurrentStatistic()
275275
}
276276

@@ -314,7 +314,7 @@ extension ECSContext {
314314

315315
for system in self.renderingSystems {
316316
self.performance?.beginStatForSystem(system)
317-
system.willRender(into: view, withTimePassed: deltaTime)
317+
system.willRender(into: view, withTimePassed: deltaTime, context: self)
318318
self.performance?.endCurrentStatistic()
319319
}
320320

@@ -443,38 +443,38 @@ public extension ECSContext {
443443
}
444444
@discardableResult
445445
func insertSystem<T: System>(_ system: T.Type) -> T {
446-
let system = system.init(context: self)
446+
let system = system.init()
447447
self.insertSystem(system)
448448
return system
449449
}
450450
@discardableResult
451451
func insertSystem<T: RenderingSystem>(_ system: T.Type) -> T {
452-
let system = system.init(context: self)
452+
let system = system.init()
453453
self.insertSystem(system)
454454
return system
455455
}
456456
@discardableResult
457457
internal func insertSystem<T: PlatformSystem>(_ system: T.Type) -> T {
458-
let system = system.init(context: self)
458+
let system = system.init()
459459
self.insertSystem(system)
460460
return system
461461
}
462462
func removeSystem(_ system: System) {
463463
if let index = self._systems.firstIndex(where: { $0 === system }) {
464464
let system = self._systems.remove(at: index)
465-
system.teardown(context: self)
465+
system._teardown(context: self)
466466
}
467467
}
468468
func removeSystem(_ system: RenderingSystem) {
469469
if let index = self._renderingSystems.firstIndex(where: { $0 === system }) {
470470
let system = self._renderingSystems.remove(at: index)
471-
system.teardown(context: self)
471+
system._teardown(context: self)
472472
}
473473
}
474474
internal func removeSystem(_ system: PlatformSystem) {
475475
if let index = self._platformSystems.firstIndex(where: { $0 === system }) {
476476
let system = self._platformSystems.remove(at: index)
477-
system.teardown(context: self)
477+
system._teardown(context: self)
478478
}
479479
}
480480
@discardableResult

Sources/GateEngine/ECS/Base/PlatformSystem.swift

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,30 @@ import GameMath
1212
private var didSetup = false
1313
private(set) lazy var backgroundTask = BackgroundTask(system: self)
1414

15-
required init(context: ECSContext) {
16-
self.context = context
15+
@usableFromInline
16+
internal weak var _context: ECSContext! = nil
17+
@inlinable
18+
public var context: ECSContext {
19+
return _context.unsafelyUnwrapped
1720
}
1821

19-
public let context: ECSContext
22+
public required init() { }
2023

21-
internal final func willUpdate(input: HID, withTimePassed deltaTime: Float) async {
24+
internal final func willUpdate(input: HID, withTimePassed deltaTime: Float, context: ECSContext) async {
2225
if didSetup == false {
26+
self._context = context
2327
didSetup = true
2428
await setup(context: context, input: input)
2529
}
2630
if await shouldUpdate(context: context, input: input, withTimePassed: deltaTime) {
2731
await update(context: context, input: input, withTimePassed: deltaTime)
2832
}
2933
}
34+
internal func _teardown(context: ECSContext) {
35+
self.teardown(context: context)
36+
self._context = nil
37+
self.didSetup = false
38+
}
3039

3140
/**
3241
Called once when the entity is removed from the game.

Sources/GateEngine/ECS/Base/RenderingSystem.swift

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,30 @@
1414
return Game.shared
1515
}
1616

17-
public let context: ECSContext
18-
19-
required public init(context: ECSContext) {
20-
self.context = context
17+
@usableFromInline
18+
internal weak var _context: ECSContext! = nil
19+
@inlinable
20+
public var context: ECSContext {
21+
return _context.unsafelyUnwrapped
2122
}
23+
24+
public required init() { }
2225

23-
internal final func willRender(into view: GameView, withTimePassed deltaTime: Float) {
26+
internal final func willRender(into view: GameView, withTimePassed deltaTime: Float, context: ECSContext) {
2427
if didSetup == false {
28+
self._context = context
2529
didSetup = true
2630
setup(context: context)
2731
}
2832
if shouldRender(context: context, into: view, withTimePassed: deltaTime) {
2933
render(context: context, into: view, withTimePassed: deltaTime)
3034
}
3135
}
36+
internal func _teardown(context: ECSContext) {
37+
self.teardown(context: context)
38+
didSetup = false
39+
self._context = nil
40+
}
3241

3342
/**
3443
Called once when this ``RenderingSystem`` is inserted into the game instance.

Sources/GateEngine/ECS/Base/System.swift

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,30 @@ import GameMath
1111
private var didSetup = false
1212
public private(set) lazy var backgroundTask = BackgroundTask(system: self)
1313

14-
required public init(context: ECSContext) {
15-
self.context = context
14+
@usableFromInline
15+
internal weak var _context: ECSContext! = nil
16+
@inlinable
17+
public var context: ECSContext {
18+
return _context.unsafelyUnwrapped
1619
}
17-
18-
public let context: ECSContext
19-
20-
internal final func willUpdate(input: HID, withTimePassed deltaTime: Float) async {
20+
21+
public required init() { }
22+
23+
internal final func willUpdate(input: HID, withTimePassed deltaTime: Float, context: ECSContext) async {
2124
if didSetup == false {
25+
_context = context
2226
didSetup = true
2327
await setup(for: context, input: input)
2428
}
2529
if await shouldUpdate(context: context, input: input, withTimePassed: deltaTime) {
2630
await update(context: context, input: input, withTimePassed: deltaTime)
2731
}
2832
}
33+
internal func _teardown(context: ECSContext) {
34+
self.teardown(context: context)
35+
self._context = nil
36+
self.didSetup = false
37+
}
2938

3039
/**
3140
Called once when the entity is removed from the game.

Sources/GateEngine/ECS/StandardRenderingSystem.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ public final class StandardRenderingSystem: RenderingSystem {
1111
internal var verticalResolution: Float? = nil
1212
internal lazy var renderTarget: RenderTarget = RenderTarget()
1313

14-
public convenience init(verticalResolution: UInt, context: ECSContext) {
15-
self.init(context: context)
14+
public convenience init(verticalResolution: UInt) {
15+
self.init()
1616
self.verticalResolution = Float(verticalResolution)
1717
}
1818

0 commit comments

Comments
 (0)