-
Notifications
You must be signed in to change notification settings - Fork 0
/
Character.c
215 lines (187 loc) · 5.78 KB
/
Character.c
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
#include <stdio.h>
#include <math.h>
#include <graphics.h>
#include "Global.h"
#include "Bloc.h"
#define SPEED 1
Coordinates position;
DIRECTION orientation;
float U_SIZE;
/*
* Function: initCharacterPosition
* --------------------
* Initalize character position to the maze' starting point
*
* startPoint: coordinates of the maze' starting point.
*
* returns: void
*/
void initCharacterPosition(Coordinates startPoint) {
orientation = NORTH;
position = startPoint;
}
/*
* Function: DrawCharacter
* --------------------
* Draw the character thanks to its position and
* face the orientation of the image according to the last movements made
*
* returns: void
*/
void drawCharacter() {
setcolor(rgb(0, 255, 0));
switch (orientation) {
case NORTH:
rectangle(position.x - U_SIZE, position.y - U_SIZE, position.x + U_SIZE, position.y + U_SIZE);
break;
case SOUTH:
rectangle(position.x - U_SIZE, position.y - U_SIZE, position.x + U_SIZE, position.y + U_SIZE);
break;
case EAST:
rectangle(position.x - U_SIZE, position.y - U_SIZE, position.x + U_SIZE, position.y + U_SIZE);
break;
case WEST:
rectangle(position.x - U_SIZE, position.y - U_SIZE, position.x + U_SIZE, position.y + U_SIZE);
break;
}
}
/*
* Function: MoveCharacter
* --------------------
* Change character coordinates if the movement is allowed
*
* keyPressed: an integer representing the key pressed.
* maze: the current maze we are playing in.
*
* returns: void
*/
void moveCharacter(Maze maze) {
Coordinates temp = position;
int direction = NONE;
if (keypressed(KB_UP))
direction = NORTH;
else if (keypressed(KB_LEFT))
direction = WEST;
else if (keypressed(KB_DOWN))
direction = SOUTH;
else if (keypressed(KB_RIGHT))
direction = EAST;
switch (direction) {
case NORTH:
orientation = NORTH;
temp.y -= SPEED;
break;
case SOUTH:
orientation = SOUTH;
temp.y += SPEED;
break;
case EAST:
orientation = EAST;
temp.x += SPEED;
break;
case WEST:
orientation = WEST;
temp.x -= SPEED;
break;
}
/* If the movement is possible (not blocked by wall or extremities) we update character position. */
if (direction && isMovementAllowed(temp, maze)) position = temp;
}
/*
* Function: isMovementAllowed
* --------------------
* Tell if the movement is allowed by checking if the temp
* position isn't in conflict with any block/wall
*
* temp: character's temporary coordinates after the movement.
* maze: the current maze we are playing in.
*
* returns: True if the movement is allowed and false otherwise.
*/
bool isMovementAllowed(Coordinates temp, Maze maze) {
const float BLOC_WIDTH = getBlocSize();
const int BLOC_FULL_WIDTH = BLOC_WIDTH * 2;
/* Check that we are inside the maze/window. */
if (temp.x - U_SIZE < 0 || temp.y - U_SIZE < 0 || temp.x + U_SIZE > getmaxx() || temp.y + U_SIZE > getmaxy())
return FALSE;
int col = (int)(round(temp.x / (float)(BLOC_FULL_WIDTH)) - 1);
int row = (int)(round(temp.y / (float)(BLOC_FULL_WIDTH)) - 1);
switch (orientation) {
/* Trying to go up. */
case NORTH:
if (maze.schema[row][col] || (temp.x % BLOC_FULL_WIDTH && !fmod(temp.x, BLOC_WIDTH) && maze.schema[row][col + 1]))
return FALSE;
else if (temp.y < (BLOC_FULL_WIDTH * (row + 1)) - U_SIZE) {
if (maze.schema[row - 1][col])
return FALSE;
else if (temp.x < (BLOC_FULL_WIDTH * (col + 1)) - U_SIZE)
if (maze.schema[row - 1][col - 1])
return FALSE;
else if (temp.x > (BLOC_FULL_WIDTH * (col + 1)) + U_SIZE)
if (maze.schema[row - 1][col + 1])
return FALSE;
}
break;
/* Trying to go down. */
case SOUTH:
if (maze.schema[row][col] || (temp.x % BLOC_FULL_WIDTH && !fmod(temp.x, BLOC_WIDTH) && maze.schema[row][col - 1]))
return FALSE;
else if (temp.y > (BLOC_FULL_WIDTH * (row + 1)) + U_SIZE){
if (maze.schema[row + 1][col])
return FALSE;
else if (temp.x < (BLOC_FULL_WIDTH * (col + 1)) - U_SIZE && maze.schema[row + 1][col - 1])
return FALSE;
else if (temp.x > (BLOC_FULL_WIDTH * (col + 1)) + U_SIZE && maze.schema[row + 1][col + 1])
return FALSE;
}
break;
/* Trying to go right. */
case EAST:
if (maze.schema[row][col] || (temp.y % BLOC_FULL_WIDTH && !fmod(temp.y, BLOC_WIDTH) && maze.schema[row + 1][col]))
return FALSE;
else if (temp.x > (BLOC_FULL_WIDTH * (col + 1)) + U_SIZE) {
if (maze.schema[row][col + 1])
return FALSE;
else if (temp.y < (BLOC_FULL_WIDTH * (row + 1)) - U_SIZE && maze.schema[row - 1][col + 1])
return FALSE;
else if (temp.y > (BLOC_FULL_WIDTH * (row + 1)) + U_SIZE && maze.schema[row + 1][col + 1])
return FALSE;
}
break;
/* Trying to go left. */
case WEST:
if (maze.schema[row][col] || (temp.y % BLOC_FULL_WIDTH && !fmod(temp.y, BLOC_WIDTH) && maze.schema[row - 1][col]))
return FALSE;
else if (temp.x < (BLOC_FULL_WIDTH * (col + 1)) - U_SIZE) {
if (maze.schema[row][col - 1])
return FALSE;
else if (temp.y < (BLOC_FULL_WIDTH * (row + 1)) - U_SIZE && maze.schema[row - 1][col - 1])
return FALSE;
else if (temp.y >(BLOC_FULL_WIDTH * (row + 1)) + U_SIZE && maze.schema[row + 1][col - 1])
return FALSE;
}
break;
}
return TRUE;
}
/*
* Function: getCharacterPosition
* --------------------
* Get character's current position
*
* returns: character's coordinates
*/
Coordinates getCharacterPosition() {
return position;
}
/*
* Function: setCharacterSize
* --------------------
* set character size
*
* returns: void
*/
void setCharacterSize(float userSize) {
/* Round to 1 decimal */
U_SIZE = floorf(userSize * 10) / 10;
}