/
PositionSystem.swift
50 lines (40 loc) · 1.4 KB
/
PositionSystem.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//
// PositionSystem.swift
// star-dash
//
// Created by Ho Jun Hao on 13/3/24.
//
import Foundation
class PositionSystem: System {
var isActive: Bool
var dispatcher: EventModifiable?
var entityManager: EntityManager
init(_ entityManager: EntityManager, dispatcher: EventModifiable? = nil) {
self.isActive = true
self.entityManager = entityManager
self.dispatcher = dispatcher
}
func move(entityId: EntityId, to newPosition: CGPoint) {
guard let positionComponent = getPositionComponent(of: entityId) else {
return
}
positionComponent.setPosition(position: newPosition)
}
func rotate(entityId: EntityId, to newRotation: Float) {
guard let positionComponent = getPositionComponent(of: entityId) else {
return
}
positionComponent.setRotation(rotation: newRotation)
}
func sync(entityPositionMap: [EntityId: CGPoint], entityRotationMap: [EntityId: Float]) {
for (entityId, newPosition) in entityPositionMap {
move(entityId: entityId, to: newPosition)
}
for (entityId, newRotation) in entityRotationMap {
rotate(entityId: entityId, to: newRotation)
}
}
private func getPositionComponent(of entityId: EntityId) -> PositionComponent? {
entityManager.component(ofType: PositionComponent.self, of: entityId)
}
}