/
PhysicsModule.swift
52 lines (43 loc) · 1.92 KB
/
PhysicsModule.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
51
52
import CoreGraphics
import SDPhysicsEngine
class PhysicsModule: SyncModule {
let entityManager: EntitySyncInterface
init(entityManager: EntitySyncInterface) {
self.entityManager = entityManager
}
func sync(entity: Entity, from object: SDObject) {
guard let physicsComponent = entityManager.component(ofType: PhysicsComponent.self, of: entity.id),
let body = object.physicsBody else {
return
}
physicsComponent.mass = body.mass
physicsComponent.velocity = body.velocity
// physicsComponent.force = body.force
physicsComponent.affectedByGravity = body.affectedByGravity
physicsComponent.categoryBitMask = body.categoryBitMask
physicsComponent.contactTestMask = body.contactTestMask
physicsComponent.collisionBitMask = body.collisionBitMask
}
func sync(object: SDObject, from entity: Entity) {
guard let physicsComponent = entityManager.component(ofType: PhysicsComponent.self, of: entity.id),
let body = object.physicsBody else {
return
}
body.mass = physicsComponent.mass
body.velocity = physicsComponent.velocity
// body.force = physicsComponent.force
body.affectedByGravity = physicsComponent.affectedByGravity
body.categoryBitMask = body.categoryBitMask
body.contactTestMask = body.contactTestMask
body.collisionBitMask = body.collisionBitMask
}
func create(for object: SDObject, from entity: Entity) {
guard let physicsComponent = entityManager.component(ofType: PhysicsComponent.self, of: entity.id) else {
return
}
object.physicsBody = createRectanglePhysicsBody(physicsComponent: physicsComponent)
}
private func createRectanglePhysicsBody(physicsComponent: PhysicsComponent) -> SDPhysicsBody {
SDPhysicsBody(rectangleOf: CGSize(width: 50, height: 50))
}
}