-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayfield.cpp
108 lines (93 loc) · 3.41 KB
/
playfield.cpp
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
#include "playfield.h"
/*
* Spielfeld Design
* # = Solider Block
* x = zerstörbarer Block
* ' ' = Pfad auf dem sich der Spieler bewegen kann
*/
char playfieldDesign[PLAYFIELD_SIZE_Y][PLAYFIELD_SIZE_X] =
{
{'#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#'},
{'#',' ',' ','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','#'},
{'#',' ','#','x','#','x','#','x','#','x','#','x','#','x','#','x','#','x','#'},
{'#','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','#'},
{'#','x','#','x','#','x','#','x','#','x','#','x','#','x','#','x','#','x','#'},
{'#','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','#'},
{'#','x','#','x','#','x','#','x','#','x','#','x','#','x','#','x','#','x','#'},
{'#','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','#'},
{'#','x','#','x','#','x','#','x','#','x','#','x','#','x','#','x','#','x','#'},
{'#','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','#'},
{'#','x','#','x','#','x','#','x','#','x','#','x','#','x','#','x','#','x','#'},
{'#','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','#'},
{'#','x','#','x','#','x','#','x','#','x','#','x','#','x','#','x','#',' ','#'},
{'#','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x',' ',' ','#'},
{'#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#'}
};
/*
* Erstellt das Spielfeld
*/
Playfield::Playfield()
{
//Startposition bestimmen
int startpos_x = 0;
int startpos_y = 0;
int x=startpos_x;
int y=startpos_y;
//Jeder Block aus dem Designe herstellen und in playFieldblocks abspeichern
for(int counter_y = 0; counter_y<PLAYFIELD_SIZE_Y; counter_y++)
{
for(int counter_x = 0; counter_x<PLAYFIELD_SIZE_X; counter_x++)
{
s_blockbehavoir blockmode;
char block = playfieldDesign[counter_y][counter_x];
switch(block)
{
case (' '): blockmode = MODE_PATH; break;
case ('#'): blockmode = MODE_SOLID; break;
case ('x'): blockmode = MODE_DESTROYABLE ;break;
}
playfieldBlocks[counter_y][counter_x] = new Block(x,y,blockmode);
x += BLOCK_SIZE_X;
}
x=startpos_x;
y+= BLOCK_SIZE_Y;
}
}
/*
* Zeichnet das Spielfeld auf die Szene
*/
void Playfield::Draw(QGraphicsScene * scene)
{
int counter_y = 0;
int counter_x = 0;
for(counter_y = 0; counter_y<PLAYFIELD_SIZE_Y; counter_y++)
{
for( counter_x = 0; counter_x<PLAYFIELD_SIZE_X; counter_x++)
{
if(playfieldBlocks[counter_y][counter_x]!=NULL)
scene->addItem(playfieldBlocks[counter_y][counter_x]); //Block auf Szene zeichnen
}
}
}
/*
* Anhand der Koordinate den Block bestimmen
* Es kann ausgewählt werden ob der aktuelle Block oder ein nachbarblock zurückgegeben werden soll
*/
Block * Playfield::getBlock(int PosX, int PosY, Nextblock dir)
{
//Blockkoordinate berechen
int x = PosX / BLOCK_SIZE_X;
int y = PosY / BLOCK_SIZE_Y;
//Nachbarblock bestimmen
if(x>=0 && x)
if(dir == UP) y -= 1;
if(dir == DOWN) y += 1;
if(dir == RIGHT)x += 1;
if(dir == LEFT) x -= 1;
//Maximalwerte abfangen
if(x<0 || y<0)
return NULL;
if(x>=PLAYFIELD_SIZE_X || y>=PLAYFIELD_SIZE_Y)
return NULL;
return playfieldBlocks[y][x];
}