-
Notifications
You must be signed in to change notification settings - Fork 1
/
SpriteModule.swift
64 lines (49 loc) · 1.88 KB
/
SpriteModule.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
53
54
55
56
57
58
59
60
61
62
63
64
import CoreGraphics
import SDPhysicsEngine
class SpriteModule: SyncModule {
let entityManager: EntitySyncInterface
init(entityManager: EntitySyncInterface) {
self.entityManager = entityManager
}
func sync(entity: Entity, from object: SDObject) {
}
func sync(object: SDObject, from entity: Entity) {
guard let spriteComponent = entityManager.component(ofType: SpriteComponent.self, of: entity.id),
let spriteObject = object as? SDSpriteObject else {
return
}
if spriteComponent.textureAtlas == nil && spriteObject.activeTexture != nil {
spriteObject.cancelTexture()
return
}
guard let textureAtlas = spriteComponent.textureAtlas else {
return // no texture to run
}
if let activeTexture = spriteObject.activeTexture,
activeTexture == textureAtlas {
return // correct texture is already running
}
spriteObject.cancelTexture()
spriteObject.runTexture(named: textureAtlas)
}
func create(for object: SDObject, from entity: Entity) {
}
}
extension SpriteModule: CreationModule {
func createObject(from entity: Entity) -> SDObject? {
var newObject = SDObject()
if let spriteComponent = entityManager.component(ofType: SpriteComponent.self, of: entity.id) {
let spriteObject = SDSpriteObject(imageNamed: spriteComponent.image)
if let size = spriteComponent.size {
spriteObject.size = size
}
newObject = spriteObject
}
guard let positionComponent = entityManager.component(ofType: PositionComponent.self, of: entity.id) else {
return nil
}
newObject.position = positionComponent.position
newObject.rotation = CGFloat(positionComponent.rotation)
return newObject
}
}