This repository has been archived by the owner on Mar 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBomb.swift
122 lines (101 loc) · 4.2 KB
/
Bomb.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//
// Bomb.swift
// Bomberman
//
// Created by Wolfgang Schreurs on 30/04/16.
//
//
import Foundation
class Bomb: Entity {
var range: Int = 2
let player: PlayerIndex
// MARK: - Initialization
init(forGame game: Game, player: PlayerIndex, configComponent: ConfigComponent, gridPosition: Point) {
self.player = player
super.init(forGame: game, configComponent: configComponent, gridPosition: gridPosition)
if let visualComponent = component(ofType: VisualComponent.self) {
visualComponent.spriteNode.zPosition = EntityLayer.bomb.rawValue
if let physicsBody = visualComponent.spriteNode.physicsBody {
physicsBody.categoryBitMask = EntityCategory.Prop
physicsBody.contactTestBitMask = EntityCategory.Nothing
physicsBody.collisionBitMask = EntityCategory.Nothing
}
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// MARK: - Public
override func destroy() {
super.destroy()
}
func explodeAtGridPosition(_ gridPosition: Point) throws {
var validPositions: [(Point, Direction)] = [(gridPosition, .none)]
// west
for x in (gridPosition.x - self.range ..< gridPosition.x).reversed() {
let testGridPosition = Point(x: x, y: gridPosition.y)
if let tile = self.game?.tileAtGridPosition(testGridPosition) {
if tile.tileType == TileType.destructableBlock &&
x == gridPosition.x - 1 {
validPositions.append((testGridPosition, .left))
tile.destroy()
}
break
} else {
validPositions.append((testGridPosition, .left))
}
}
// east
for x in (gridPosition.x + 1 ... gridPosition.x + self.range) {
let testGridPosition = Point(x: x, y: gridPosition.y)
if let tile = self.game?.tileAtGridPosition(testGridPosition) {
if tile.tileType == TileType.destructableBlock &&
x == gridPosition.x + 1 {
validPositions.append((testGridPosition, .right))
tile.destroy()
}
break
} else {
validPositions.append((testGridPosition, .right))
}
}
// north
for y in (gridPosition.y + 1 ... gridPosition.y + self.range) {
let testGridPosition = Point(x: gridPosition.x, y: y)
if let tile = self.game?.tileAtGridPosition(testGridPosition) {
if tile.tileType == TileType.destructableBlock &&
y == gridPosition.y + 1 {
validPositions.append((testGridPosition, .up))
tile.destroy()
}
break
} else {
validPositions.append((testGridPosition, .up))
}
}
// south
for y in (gridPosition.y - self.range ..< gridPosition.y).reversed() {
let testGridPosition = Point(x: gridPosition.x, y: y)
if let tile = self.game?.tileAtGridPosition(testGridPosition) {
if tile.tileType == TileType.destructableBlock &&
y == gridPosition.y - 1 {
validPositions.append((testGridPosition, .down))
tile.destroy()
}
break
} else {
validPositions.append((testGridPosition, .down))
}
}
for validPosition in validPositions {
if let bomb = self.game?.bombAtGridPosition(validPosition.0) {
bomb.destroy()
}
let propLoader = PropLoader(forGame: self.game!)
if let explosion = try propLoader.explosionWithGridPosition(validPosition.0,
direction: validPosition.1) {
self.game?.addEntity(explosion)
}
}
}
}