-
Notifications
You must be signed in to change notification settings - Fork 1
/
PlayerMonsterContactEvent.swift
46 lines (37 loc) · 1.32 KB
/
PlayerMonsterContactEvent.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
//
// PlayerMonsterContactEvent.swift
// star-dash
//
// Created by Jason Qiu on 16/3/24.
//
import Foundation
class PlayerMonsterContactEvent: Event {
var entityId: EntityId
let timestamp: Date
let monsterId: EntityId
init(from playerId: EntityId, on monsterId: EntityId) {
self.timestamp = Date.now
self.entityId = playerId
self.monsterId = monsterId
}
func execute(on target: EventModifiable) {
guard let positionSystem = target.system(ofType: PositionSystem.self),
let physicsSystem = target.system(ofType: PhysicsSystem.self) else {
return
}
guard let playerPosition = positionSystem.getPosition(of: entityId),
let monsterPosition = positionSystem.getPosition(of: monsterId) else {
return
}
guard let playerSize = physicsSystem.getSize(of: entityId),
let monsterSize = physicsSystem.getSize(of: monsterId) else {
return
}
let isPlayerAbove = playerPosition.y - (playerSize.height / 2) >= monsterPosition.y + (monsterSize.height / 2)
if isPlayerAbove {
target.add(event: PlayerAttackMonsterEvent(on: monsterId))
} else {
target.add(event: MonsterAttackPlayerEvent(from: monsterId, on: entityId))
}
}
}