-
Notifications
You must be signed in to change notification settings - Fork 2
/
Map.go
325 lines (276 loc) · 6.01 KB
/
Map.go
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package main
import (
"container/list"
"log"
)
type FieldObject int
var globalBombCount uint64 = 0
var playerDied = false
const bombStates = 3
/*
FieldObject "Enum"
*/
const (
FieldObjectNull FieldObject = 0
FieldObjectBomb FieldObject = 1
FieldObjectWeakWall FieldObject = 2
FieldObjectSolidWall FieldObject = 3
FieldObjectItemUpgrade FieldObject = 4
FieldObjectItemDowngrade FieldObject = 5
FieldObjectItemBoost FieldObject = 6
FieldObjectItemSlow FieldObject = 7
FieldObjectItemGhost FieldObject = 8
FieldObjectExplosion FieldObject = 9
FieldObjectBombState1 FieldObject = 10
FieldObjectBombState2 FieldObject = 11
FieldObjectPortal FieldObject = 12
FieldObjectPoison FieldObject = 13
)
/*
Represents the Gamemap through a two dimensional Array of Fields.
*/
type Map struct {
Fields [][]Field
}
/*
Initialises the Map.
Fills all Array-Positions with empty Field and then fills it with CreateMapFromImage.
*/
func NewMap(size int) Map {
m := Map{Fields: make([][]Field, size)}
for i := 0; i < len(m.Fields); i++ {
m.Fields[i] = make([]Field, size)
for j := 0; j < len(m.Fields[i]); j++ {
m.Fields[i][j] = NewField()
}
}
if err := CreateMapFromImage(m, "images/testMap.png"); err != nil {
log.Fatal(err)
}
return m
}
/*
Fills the Map with empty Field
*/
func (m *Map) clear() {
for i := 0; i < len(m.Fields); i++ {
for j := 0; j < len(m.Fields[0]); j++ {
m.Fields[i][j] = NewField()
}
}
}
type Field struct {
Contains []FieldType
Player *list.List
}
func NewField() Field {
return Field{
Contains: make([]FieldType, 2),
Player: list.New(),
}
}
/*
Adds a Bomb-Object to the Contains-Array of the Field.
*/
func (f *Field) addBomb(b *Bomb) {
log.Println("added bomb.")
if f.Contains[0] != nil {
f.Contains[1] = b
} else {
f.Contains[0] = b
}
//MapChanged()
}
/*
Adds a Wall-Object to the Contains-Array of the Field.
*/
func (f *Field) addWall(w *Wall) {
if f.Contains[0] != nil {
f.Contains[1] = w
} else {
f.Contains[0] = w
}
}
/*
Adds a Item-Object to the Contains-Array of the Field.
*/
func (f *Field) addItem(i *Item) {
if f.Contains[0] != nil {
f.Contains[1] = i
} else {
f.Contains[0] = i
}
}
/*
Adds a Portal-Object to the Contains-Array of the Field.
*/
func (m *Map) addPortal(p *Portal) {
m.Fields[p.portalOne.x][p.portalOne.y].Contains[1] = p
m.Fields[p.portalTwo.x][p.portalTwo.y].Contains[1] = p
}
/*
Casts an Explosion on a Field. The Explosion kills al Player on the Field and destroys destructible Walls.
*/
func (f *Field) explosion() bool {
killAllPlayersOnField(f.Player)
for i := 0; i < 2; i++ {
if f.Contains[i] != nil {
if f.Contains[i].isDestructible() {
f.Contains[i] = nil
} else {
return true
}
}
}
return false
}
/*
Adds a Explosion-Object to the Contains-Array of the Field.
*/
func (f *Field) addExplosion(e *Explosion) {
if f.Contains[0] != nil {
f.Contains[1] = e
} else {
f.Contains[0] = e
}
}
/*
Adds a Poison-Object to the Contains-Array of the Field.
*/
func (f *Field) addPoison(p *Poison) {
if f.Contains[0] != nil {
f.Contains[1] = p
} else {
f.Contains[0] = p
}
}
type eventFunction func(i interface{})
/*
Represents a FieldType.
*/
type FieldType interface {
isAccessible() bool
startEvent(f eventFunction)
isDestructible() bool
getType() FieldObject
}
type Poison struct {
}
func newPoison() Poison {
return Poison{}
}
func (p *Poison) isAccessible() bool {
return true
}
func (p *Poison) startEvent(f eventFunction) {
}
func (p *Poison) isDestructible() bool {
return false
}
func (p *Poison) getType() FieldObject {
return FieldObjectPoison
}
type Explosion struct {
ExpFields []Position
}
func newExplosion() Explosion {
return Explosion{
ExpFields: make([]Position, 0),
}
}
func (e *Explosion) isAccessible() bool {
return true
}
func (e *Explosion) startEvent(f eventFunction) {
}
func (e *Explosion) isDestructible() bool {
return false
}
func (e *Explosion) getType() FieldObject {
return FieldObjectExplosion
}
type Item struct {
Type FieldObject
}
func NewItem(t FieldObject) Item {
return Item{Type: t}
}
func (i *Item) isAccessible() bool {
return true
}
func (i *Item) startEvent(f eventFunction) {
}
func (i *Item) isDestructible() bool {
return false
}
func (i *Item) getType() FieldObject {
return i.Type
}
type Portal struct {
iFeelUsed bool
portalOne Position
portalTwo Position
}
func NewPortal(portalOne Position, portalTwo Position) Portal {
return Portal{
iFeelUsed: false,
portalOne: portalOne,
portalTwo: portalTwo,
}
}
func (p *Portal) isAccessible() bool {
return true
}
func (p *Portal) startEvent(f eventFunction) {
}
func (p *Portal) isDestructible() bool {
return false
}
func (p *Portal) getType() FieldObject {
return FieldObjectPortal
}
type Wall struct {
Destructible bool
}
func NewWall(destructible bool) *Wall {
return &Wall{Destructible: destructible}
}
func (w *Wall) isAccessible() bool {
return false
}
func (w *Wall) startEvent(f eventFunction) {
}
func (w *Wall) isDestructible() bool {
return w.Destructible
}
func (w *Wall) getType() FieldObject {
if w.isDestructible() {
return FieldObjectWeakWall
} else {
return FieldObjectSolidWall
}
}
/*
Represents the Fields of the Map without Players, only FieldObjects.
*/
func BuildAbstractGameMap() [][][]FieldObject {
//Create map to send
newAbstractMap := make([][][]FieldObject, len(GameMap.Fields))
//testToSend := make([][]int, len(GameMap.Fields))
for i, _ := range GameMap.Fields {
newAbstractMap[i] = make([][]FieldObject, len(GameMap.Fields[i]))
//testToSend[i] = make([]int, len(GameMap.Fields[i]))
for j, _ := range GameMap.Fields[i] {
newAbstractMap[i][j] = make([]FieldObject, len(GameMap.Fields[i][j].Contains))
if GameMap.Fields[i][j].Player.Front() != nil {
//testToSend[i][j] = 1
}
for k, _ := range GameMap.Fields[i][j].Contains {
if GameMap.Fields[i][j].Contains[k] != nil {
newAbstractMap[i][j][k] = GameMap.Fields[i][j].Contains[k].getType()
}
}
}
}
return newAbstractMap
}