-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.c
178 lines (163 loc) · 4.33 KB
/
game.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
#include "myLib.h"
#include <stdio.h>
#include "text.h"
#include "heart.h"
//Draw all paths in the passed in array
void drawPaths(PATH *paths, int numPaths, short pathColor) {
for(int i = 0; i < numPaths; i++) {
drawRect(paths[i].y,paths[i].x,paths[i].h,paths[i].w,pathColor);
}
}
//Establish all bounds in the passed in array
void drawBounds(BOUND *bounds, int numBounds) {
for(int i = 0; i < numBounds; i++) {
if (bounds[i].vis == 1)
drawRect(bounds[i].y,bounds[i].x,bounds[i].h,bounds[i].w,BOAR);
}
}
//Returns a char representing which direction movement is allowed based off of collision.
unsigned char getMovementData(int x, int y, int size, BOUND bounds[], int numBounds) {
//Numbers necessary to turn on 1st, 2nd, 3rd, and 4th bit of a byte
short u = 1;
short d = 2;
short r = 4;
short l = 8;
//Check all bounds for edge collisions, and turn of a bit if there is such a violation
for(int i = 0; i < numBounds; i++) {
BOUND b = bounds[i];
if(y == b.y + b.h && x + size >= b.x && x <= b.x + b.w)
u = 0;
if(y + size == b.y && x + size >= b.x && x <= b.x + b.w)
d = 0;
if(x + size == b.x && y + size >= b.y && y <= b.y + b.h)
r = 0;
if(x == b.x + b.w && y + size >= b.y && y <= b.y + b.h)
l = 0;
}
//Return result char
unsigned char ret = u | d | r | l;
return ret;
}
//Updates health
void drawHealth(int l) {
drawRect(146,210,14,30, BLACK);
drawImage3(149, 228, 10, 10, heart);
char liveString[10];
sprintf(liveString, "%d", l);
drawString(150, 213, liveString, RED);
}
int enemyLogic(ENEMY enemies[], int numEnemies, PLAYER p, u16 pathColor) {
//////Enemy logic//////
//
// Return 1 if player is hit.
//
// ~~~ v - VERTICAL PATROLLER ~~~
// Moves up and down the screen
// METADATA
// [0]: Initial direction
// [1]: Pixels per direction
// [2]: Used to count down, must = [1] to start
// [3]: Speed
//
// ~~~ h - HORIZONTAL PATROLLER ~~~
// Moves up and down the screen
// METADATA
// [0]: Initial direction
// [1]: Pixels per direction
// [2]: Used to count down, must = [1] to start
// [3]: Speed
//
// ~~~ g - GROWER ~~~
// Moves up and down the screen
// METADATA
// [0]: Initial direction
// [1]: Pixels to expand
// [2]: Used to count down, must = [1] to start
// [3]: Speed out of 10
// [4]: Flip-flop, used to slow down motion - don't touch
int ret = 0;
for(int i = 0; i < numEnemies; i++) {
//Alias
ENEMY e = enemies[i];
//Vertical patroller type
if (e.type == 'v') {
e.meta[2]-=e.meta[3]; //count down
if(e.meta[2] <= 0) { //reached 0
e.meta[2] = e.meta[1]; //reset timer
e.meta[0] = -e.meta[0]; //change direction
}
//Draw old rectangle to cover trail
drawRect(e.y,e.x,e.size,e.size,pathColor);
//Decide which direction to go and move
if(e.meta[0] > 0) {
e.y-=e.meta[3];
} else {
e.y+=e.meta[3];
}
//Draw enemy
drawRect(e.y,e.x,e.size,e.size,e.color);
//Horizontal patroller type
} else if (e.type == 'h') {
e.meta[2]-=e.meta[3];
if(e.meta[2] <= 0) {
e.meta[2] = e.meta[1];
e.meta[0] = -e.meta[0];
}
//Draw old rectangle to cover trail
drawRect(e.y,e.x,e.size,e.size,pathColor);
//Decide which direction to go and move
if(e.meta[0] > 0) {
e.x-=e.meta[3];
} else {
e.x+=e.meta[3];
}
//Draw enemy
drawRect(e.y,e.x,e.size,e.size,e.color);
//Grower type
} else if (e.type == 'g') {
//Frames to pause between growth
int buffer = 10 - e.meta[3];
if(e.meta[4] == buffer)
e.meta[2]-=1;
if(e.meta[2] <= 0) {
e.meta[2] = e.meta[1];
e.meta[0] = -e.meta[0];
}
//Draw old rectangle to cover trail
drawRect(e.y,e.x,e.size,e.size,pathColor);
//Decide which direction to go and move
if(e.meta[0] > 0 && e.meta[4] == buffer) {
e.size-=2;
e.x+=1;
e.y+=1;
e.meta[4] = 0;
} else if (e.meta[0] < 0 && e.meta[4] == buffer) {
e.size+=2;
e.x-=1;
e.y-=1;
e.meta[4] = 0;
} else {
e.meta[4]++;
}
//Draw enemy
drawRect(e.y,e.x,e.size,e.size,e.color);
}
//Insert updated enemy
enemies[i] = e;
//Check for collision
if(p.x <= e.x && p.x + p.size >= e.x && p.y <= e.y + e.size && p.y + p.size >= e.y) {
ret = 1;
}
if(p.x + p.size >= e.x && p.x <= e.x + e.size && p.y <= e.y + e.size && p.y + p.size >= e.y) {
ret = 1;
}
}
return ret;
}
//Teleports player p to a location and updates the old player to prevent improper residue
void teleport(PLAYER* p, PLAYER* op, int x, int y) {
p->x = x;
p->y = y;
op->x = x;
op->y = y;
}