-
Notifications
You must be signed in to change notification settings - Fork 1
/
PhysicsModule.swift
50 lines (41 loc) · 1.89 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
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
}
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
}
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)
object.physicsBody?.restitution = physicsComponent.restitution
object.physicsBody?.isDynamic = physicsComponent.isDynamic
object.physicsBody?.affectedByGravity = physicsComponent.affectedByGravity
object.physicsBody?.categoryBitMask = physicsComponent.categoryBitMask
object.physicsBody?.contactTestMask = physicsComponent.contactTestMask
object.physicsBody?.collisionBitMask = physicsComponent.collisionBitMask
}
private func createRectanglePhysicsBody(physicsComponent: PhysicsComponent) -> SDPhysicsBody {
SDPhysicsBody(rectangleOf: CGSize(width: 50, height: 50))
}
}