-
Notifications
You must be signed in to change notification settings - Fork 1
/
moveEntity.c
60 lines (57 loc) · 2.97 KB
/
moveEntity.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
/* Section of program moves a given entity within a two-dimensional array
The check for obstruction has yet to be written
First written 8/04/15.
Move function requires entity symbol to be already loaded into array
*/
#include "moveEntity.h"
#include "main.h"
int move(int roomWidth, int roomHeight, char entityArray[MAP_SIZE][MAP_SIZE], char mapArray[MAP_SIZE][MAP_SIZE], int direction, int entityIndex, entity entityInfo[MAX_ENTITIES]) {
/*int valid;
valid = entityCheck(x, y, entityArray, entityInfo);
if (valid == FALSE) {*/
int result;
int posx = entityInfo[entityIndex].entityx;
int posy = entityInfo[entityIndex].entityy;
assert(direction == (UP) || direction == (DOWN) || direction == (LEFT) || direction == (RIGHT));
if (direction == UP && entityCheck(posx,posy-1,entityArray,entityInfo) == FALSE && posy-1 > -1) {
entityArray[posx][posy-1] = entityInfo[entityIndex].entitySymbol;
entityArray[posx][posy] = NO_ENTITY;
entityInfo[entityIndex].entityy = entityInfo[entityIndex].entityy-1;
result = TRUE;
} else if (direction == DOWN && entityCheck(posx,posy+1,entityArray,entityInfo) == FALSE && posy+1 < roomHeight) {
entityArray[posx][posy+1] = entityInfo[entityIndex].entitySymbol;
entityArray[posx][posy] = NO_ENTITY;
entityInfo[entityIndex].entityy = entityInfo[entityIndex].entityy +1;
result = TRUE;
} else if (direction == RIGHT && entityCheck(posx+1,posy,entityArray,entityInfo) == FALSE && posx+1 < roomWidth) {
entityArray[posx+1][posy] = entityInfo[entityIndex].entitySymbol;
entityArray[posx][posy] = NO_ENTITY;
entityInfo[entityIndex].entityx = entityInfo[entityIndex].entityx +1;
result = TRUE;
} else if (direction == LEFT && entityCheck(posx-1,posy,entityArray,entityInfo) == FALSE && posx-1 > -1) {
entityArray[posx-1][posy] = entityInfo[entityIndex].entitySymbol;
entityArray[posx][posy] = NO_ENTITY;
entityInfo[entityIndex].entityx = entityInfo[entityIndex].entityx -1;
result = TRUE;
}else {
result = FALSE; //returns to calling function where if it is called from player movement returns an error
}
return result;
}
/*Old obstructiocheck, only checked if space was empty, did not return what was in the location
int obstructionCheck(char entityArray, char mapArray, int direction, int entityx, int entityy) {
//returns TRUE if move is valid, else returns FALSE if move is obstructed by tile or entity
if (direction == UP && mapArray[&entityx][&entityy+1] == EMPTY_SPACE) {
return TRUE;
} else if (direction == DOWN && mapArray[&entityx][&entityy-1] == EMPTY_SPACE){
return TRUE;
} else if (direction == RIGHT && mapArray[&entityx+1][&entityy] == EMPTY_SPACE){
return TRUE;
} else if (direction == LEFT && mapArray[&entityx-1][&entityy] == EMPTY_SPACE){
return TRUE;
} else {
return FALSE;
}
return EXIT_SUCCESS;
}
*/