![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Game For ECE 2035
Dependencies: mbed wave_player 4DGL-uLCD-SE MMA8452
main.cpp@18:760dd68e939e, 2021-12-03 (annotated)
- Committer:
- nasiromar
- Date:
- Fri Dec 03 19:12:50 2021 +0000
- Revision:
- 18:760dd68e939e
- Parent:
- 16:06a88c0110ff
New Update;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rconnorlawson | 0:35660d7952f7 | 1 | // Project includes |
rconnorlawson | 0:35660d7952f7 | 2 | #include "globals.h" |
rconnorlawson | 0:35660d7952f7 | 3 | #include "hardware.h" |
rconnorlawson | 0:35660d7952f7 | 4 | #include "map.h" |
rconnorlawson | 0:35660d7952f7 | 5 | #include "graphics.h" |
rconnorlawson | 0:35660d7952f7 | 6 | #include "speech.h" |
nasiromar | 6:c9695079521d | 7 | #include "spells.h" |
nasiromar | 6:c9695079521d | 8 | #include "actions.h" |
nasiromar | 9:cbb9cfb1f6c5 | 9 | #include "SDFileSystem.h" |
rconnorlawson | 0:35660d7952f7 | 10 | |
rconnorlawson | 0:35660d7952f7 | 11 | // Functions in this file |
rconnorlawson | 0:35660d7952f7 | 12 | int get_action (GameInputs inputs); |
rconnorlawson | 0:35660d7952f7 | 13 | int update_game (int action); |
rconnorlawson | 0:35660d7952f7 | 14 | void draw_game (int init); |
rconnorlawson | 0:35660d7952f7 | 15 | void init_main_map (); |
rconnorlawson | 0:35660d7952f7 | 16 | int main (); |
rconnorlawson | 0:35660d7952f7 | 17 | |
nasiromar | 14:7225da81314a | 18 | FILE *wave_file; |
nasiromar | 9:cbb9cfb1f6c5 | 19 | |
nasiromar | 10:e18685911e84 | 20 | |
nasiromar | 9:cbb9cfb1f6c5 | 21 | |
nasiromar | 10:e18685911e84 | 22 | |
nasiromar | 10:e18685911e84 | 23 | |
rconnorlawson | 0:35660d7952f7 | 24 | /** |
rconnorlawson | 0:35660d7952f7 | 25 | * The main game state. Must include Player locations and previous locations for |
rconnorlawson | 0:35660d7952f7 | 26 | * drawing to work properly. Other items can be added as needed. |
rconnorlawson | 0:35660d7952f7 | 27 | */ |
rconnorlawson | 0:35660d7952f7 | 28 | struct { |
rconnorlawson | 0:35660d7952f7 | 29 | int x,y; // Current locations |
rconnorlawson | 0:35660d7952f7 | 30 | int px, py; // Previous locations |
rconnorlawson | 0:35660d7952f7 | 31 | int has_key; |
nasiromar | 6:c9695079521d | 32 | int spell; |
nasiromar | 16:06a88c0110ff | 33 | int god,god_walk; |
nasiromar | 8:fcc333a8f9e1 | 34 | int won; |
nasiromar | 9:cbb9cfb1f6c5 | 35 | int HP,MP; |
nasiromar | 11:6cd02a8539d1 | 36 | int talked; |
nasiromar | 14:7225da81314a | 37 | int gobs_slain,eye_slain; |
nasiromar | 14:7225da81314a | 38 | int money; |
rashidi1saeed | 2:1ca97843a334 | 39 | // You can add other properties for the player here |
rconnorlawson | 0:35660d7952f7 | 40 | } Player; |
rconnorlawson | 0:35660d7952f7 | 41 | |
nasiromar | 15:05592aaa468c | 42 | int i = 3; |
nasiromar | 9:cbb9cfb1f6c5 | 43 | |
rconnorlawson | 0:35660d7952f7 | 44 | /** |
rconnorlawson | 0:35660d7952f7 | 45 | * Given the game inputs, determine what kind of update needs to happen. |
rconnorlawson | 0:35660d7952f7 | 46 | * Possbile return values are defined below. |
rconnorlawson | 0:35660d7952f7 | 47 | */ |
rconnorlawson | 0:35660d7952f7 | 48 | #define NO_ACTION 0 |
rconnorlawson | 0:35660d7952f7 | 49 | #define ACTION_BUTTON 1 |
rconnorlawson | 0:35660d7952f7 | 50 | #define MENU_BUTTON 2 |
rconnorlawson | 0:35660d7952f7 | 51 | #define GO_LEFT 3 |
rconnorlawson | 0:35660d7952f7 | 52 | #define GO_RIGHT 4 |
rconnorlawson | 0:35660d7952f7 | 53 | #define GO_UP 5 |
rconnorlawson | 0:35660d7952f7 | 54 | #define GO_DOWN 6 |
nasiromar | 6:c9695079521d | 55 | #define GOD_MODE 7 |
nasiromar | 6:c9695079521d | 56 | |
nasiromar | 6:c9695079521d | 57 | #define TILT .25 |
rconnorlawson | 0:35660d7952f7 | 58 | int get_action(GameInputs inputs) |
rconnorlawson | 0:35660d7952f7 | 59 | { |
nasiromar | 6:c9695079521d | 60 | |
nasiromar | 6:c9695079521d | 61 | if(inputs.ay > TILT) { //Go in north direction if it is not blocked |
nasiromar | 6:c9695079521d | 62 | return GO_UP; |
nasiromar | 6:c9695079521d | 63 | } |
nasiromar | 6:c9695079521d | 64 | if(inputs.ay < TILT *-1) { //Go in south direction if it is not blocked |
nasiromar | 6:c9695079521d | 65 | return GO_DOWN; |
nasiromar | 6:c9695079521d | 66 | } |
nasiromar | 6:c9695079521d | 67 | if(inputs.ax > TILT) { //Go in north direction if it is not blocked |
nasiromar | 6:c9695079521d | 68 | return GO_RIGHT; |
nasiromar | 6:c9695079521d | 69 | } |
nasiromar | 6:c9695079521d | 70 | if(inputs.ax < TILT *-1) { //Go in north direction if it is not blocked |
nasiromar | 6:c9695079521d | 71 | return GO_LEFT; |
nasiromar | 6:c9695079521d | 72 | } |
nasiromar | 6:c9695079521d | 73 | if(!inputs.b1) { |
nasiromar | 6:c9695079521d | 74 | wait(.5); |
nasiromar | 6:c9695079521d | 75 | return ACTION_BUTTON; |
nasiromar | 6:c9695079521d | 76 | } |
nasiromar | 6:c9695079521d | 77 | if(!inputs.b2) { |
nasiromar | 6:c9695079521d | 78 | wait(.5); |
nasiromar | 6:c9695079521d | 79 | return MENU_BUTTON; |
nasiromar | 6:c9695079521d | 80 | } |
nasiromar | 6:c9695079521d | 81 | if (!inputs.b3) { |
nasiromar | 6:c9695079521d | 82 | wait(.5); |
nasiromar | 6:c9695079521d | 83 | return GOD_MODE; |
nasiromar | 6:c9695079521d | 84 | } else { |
nasiromar | 6:c9695079521d | 85 | return NO_ACTION; |
nasiromar | 6:c9695079521d | 86 | } |
rconnorlawson | 0:35660d7952f7 | 87 | } |
rconnorlawson | 0:35660d7952f7 | 88 | |
nasiromar | 6:c9695079521d | 89 | |
rconnorlawson | 0:35660d7952f7 | 90 | /** |
rconnorlawson | 0:35660d7952f7 | 91 | * Update the game state based on the user action. For example, if the user |
rconnorlawson | 0:35660d7952f7 | 92 | * requests GO_UP, then this function should determine if that is possible by |
rconnorlawson | 0:35660d7952f7 | 93 | * consulting the map, and update the Player position accordingly. |
nasiromar | 6:c9695079521d | 94 | * |
rconnorlawson | 0:35660d7952f7 | 95 | * Return values are defined below. FULL_DRAW indicates that for this frame, |
rconnorlawson | 0:35660d7952f7 | 96 | * draw_game should not optimize drawing and should draw every tile, even if |
rconnorlawson | 0:35660d7952f7 | 97 | * the player has not moved. |
rconnorlawson | 0:35660d7952f7 | 98 | */ |
nasiromar | 9:cbb9cfb1f6c5 | 99 | // Game Life and Magic Points |
nasiromar | 6:c9695079521d | 100 | |
nasiromar | 6:c9695079521d | 101 | |
nasiromar | 14:7225da81314a | 102 | int purchase; |
nasiromar | 14:7225da81314a | 103 | bool on_off = false; |
nasiromar | 10:e18685911e84 | 104 | |
nasiromar | 10:e18685911e84 | 105 | |
rconnorlawson | 0:35660d7952f7 | 106 | #define NO_RESULT 0 |
rconnorlawson | 0:35660d7952f7 | 107 | #define GAME_OVER 1 |
rconnorlawson | 0:35660d7952f7 | 108 | #define FULL_DRAW 2 |
rconnorlawson | 0:35660d7952f7 | 109 | int update_game(int action) |
rconnorlawson | 0:35660d7952f7 | 110 | { |
nasiromar | 15:05592aaa468c | 111 | walk_npc(); |
nasiromar | 15:05592aaa468c | 112 | |
nasiromar | 15:05592aaa468c | 113 | |
nasiromar | 15:05592aaa468c | 114 | // Save player previous location before updating |
rconnorlawson | 0:35660d7952f7 | 115 | Player.px = Player.x; |
rconnorlawson | 0:35660d7952f7 | 116 | Player.py = Player.y; |
nasiromar | 6:c9695079521d | 117 | |
rconnorlawson | 0:35660d7952f7 | 118 | // Do different things based on the each action. |
rconnorlawson | 0:35660d7952f7 | 119 | // You can define functions like "go_up()" that get called for each case. |
nasiromar | 6:c9695079521d | 120 | |
nasiromar | 6:c9695079521d | 121 | // Need to check if Claue has changed as well if whats in front of me is able to be walked into |
nasiromar | 6:c9695079521d | 122 | switch(action) { |
nasiromar | 6:c9695079521d | 123 | case GO_UP: { |
nasiromar | 6:c9695079521d | 124 | MapItem* north = get_north(Player.x,Player.y); |
nasiromar | 6:c9695079521d | 125 | if (north->walkable && north == NULL || north->type == (PORTAL || PLANT) && Player.y != 0 || Player.god ) { |
nasiromar | 6:c9695079521d | 126 | Player.y--; |
nasiromar | 10:e18685911e84 | 127 | |
nasiromar | 6:c9695079521d | 128 | } |
nasiromar | 16:06a88c0110ff | 129 | if (north->walkable && north == NULL && Player.god_walk && Player.god ) { |
nasiromar | 16:06a88c0110ff | 130 | Player.y-=3; |
nasiromar | 16:06a88c0110ff | 131 | |
nasiromar | 16:06a88c0110ff | 132 | } |
nasiromar | 6:c9695079521d | 133 | break; |
nasiromar | 6:c9695079521d | 134 | } |
nasiromar | 6:c9695079521d | 135 | case GO_RIGHT: { |
nasiromar | 6:c9695079521d | 136 | MapItem* east = get_east(Player.x,Player.y); |
nasiromar | 6:c9695079521d | 137 | if (east->walkable && east == NULL || east->type == (PORTAL || PLANT) && Player.x != 0 || Player.god ) { |
nasiromar | 6:c9695079521d | 138 | Player.x++; |
nasiromar | 6:c9695079521d | 139 | } |
nasiromar | 16:06a88c0110ff | 140 | if (east->walkable && east == NULL && Player.god_walk && Player.god ) { |
nasiromar | 16:06a88c0110ff | 141 | Player.x+=3; |
nasiromar | 16:06a88c0110ff | 142 | } |
nasiromar | 6:c9695079521d | 143 | break; |
nasiromar | 6:c9695079521d | 144 | } |
nasiromar | 6:c9695079521d | 145 | case GO_DOWN: { |
nasiromar | 6:c9695079521d | 146 | MapItem* south = get_south(Player.x,Player.y); |
nasiromar | 6:c9695079521d | 147 | if (south->walkable && south == NULL || south->type ==(PORTAL || PLANT) && Player.y != 0 || Player.god ) { |
nasiromar | 6:c9695079521d | 148 | Player.y++; |
nasiromar | 6:c9695079521d | 149 | } |
nasiromar | 16:06a88c0110ff | 150 | if (south->walkable && south == NULL && Player.god_walk && Player.god ) { |
nasiromar | 16:06a88c0110ff | 151 | Player.y+=3; |
nasiromar | 16:06a88c0110ff | 152 | |
nasiromar | 16:06a88c0110ff | 153 | } |
nasiromar | 6:c9695079521d | 154 | break; |
nasiromar | 6:c9695079521d | 155 | } |
nasiromar | 6:c9695079521d | 156 | case GO_LEFT: { |
nasiromar | 6:c9695079521d | 157 | MapItem* west = get_west(Player.x,Player.y); |
nasiromar | 6:c9695079521d | 158 | if (west->walkable && west == NULL || west->type == (PORTAL || PLANT) && Player.x != 0 || Player.god ) { |
nasiromar | 6:c9695079521d | 159 | Player.x--; |
nasiromar | 6:c9695079521d | 160 | } |
nasiromar | 16:06a88c0110ff | 161 | |
nasiromar | 16:06a88c0110ff | 162 | if (west->walkable && west == NULL && Player.god_walk && Player.god ) { |
nasiromar | 16:06a88c0110ff | 163 | Player.x-=3; |
nasiromar | 16:06a88c0110ff | 164 | } |
nasiromar | 6:c9695079521d | 165 | break; |
nasiromar | 6:c9695079521d | 166 | } |
nasiromar | 6:c9695079521d | 167 | case ACTION_BUTTON: { |
nasiromar | 6:c9695079521d | 168 | MapItem* west = get_west(Player.x,Player.y); |
nasiromar | 6:c9695079521d | 169 | MapItem* south = get_south(Player.x,Player.y); |
nasiromar | 6:c9695079521d | 170 | MapItem* north = get_north(Player.x,Player.y); |
nasiromar | 6:c9695079521d | 171 | MapItem* east = get_east(Player.x,Player.y); |
nasiromar | 6:c9695079521d | 172 | MapItem* curr = get_here(Player.x,Player.y); |
nasiromar | 9:cbb9cfb1f6c5 | 173 | |
nasiromar | 16:06a88c0110ff | 174 | if(east->type == NPC||west->type == NPC||north->type == NPC||south->type == NPC){ |
nasiromar | 16:06a88c0110ff | 175 | if( Player.has_key == 1) |
nasiromar | 16:06a88c0110ff | 176 | { |
nasiromar | 7:862062ffca62 | 177 | npc_speech2(); |
nasiromar | 16:06a88c0110ff | 178 | } |
nasiromar | 16:06a88c0110ff | 179 | else |
nasiromar | 16:06a88c0110ff | 180 | npc_speech1(); |
nasiromar | 9:cbb9cfb1f6c5 | 181 | } |
nasiromar | 9:cbb9cfb1f6c5 | 182 | |
nasiromar | 16:06a88c0110ff | 183 | |
nasiromar | 16:06a88c0110ff | 184 | |
nasiromar | 16:06a88c0110ff | 185 | if(east->type == NPCT||west->type == NPCT||north->type == NPCT||south->type == NPCT ) { |
nasiromar | 16:06a88c0110ff | 186 | if( Player.has_key == 1) |
nasiromar | 16:06a88c0110ff | 187 | { |
nasiromar | 16:06a88c0110ff | 188 | npc2_speech2(); |
nasiromar | 16:06a88c0110ff | 189 | } |
nasiromar | 16:06a88c0110ff | 190 | else |
nasiromar | 16:06a88c0110ff | 191 | npc2_speech1(); |
nasiromar | 9:cbb9cfb1f6c5 | 192 | } |
nasiromar | 16:06a88c0110ff | 193 | |
nasiromar | 9:cbb9cfb1f6c5 | 194 | |
nasiromar | 9:cbb9cfb1f6c5 | 195 | if(north->type == PORTAL||south->type == PORTAL||east->type == PORTAL||west->type == PORTAL) { |
nasiromar | 15:05592aaa468c | 196 | set_active_map(1); |
nasiromar | 7:862062ffca62 | 197 | Player.x = Player.y = 7; |
nasiromar | 9:cbb9cfb1f6c5 | 198 | } |
nasiromar | 16:06a88c0110ff | 199 | if(north->type == KEY||south->type == KEY||east->type == KEY||west->type == KEY) { |
nasiromar | 16:06a88c0110ff | 200 | pick_key(); |
nasiromar | 16:06a88c0110ff | 201 | map_erase(26,13); |
nasiromar | 16:06a88c0110ff | 202 | } |
nasiromar | 15:05592aaa468c | 203 | |
nasiromar | 16:06a88c0110ff | 204 | |
nasiromar | 16:06a88c0110ff | 205 | if(Player.HP<50){ |
nasiromar | 16:06a88c0110ff | 206 | heal_up(); |
nasiromar | 16:06a88c0110ff | 207 | } |
nasiromar | 16:06a88c0110ff | 208 | if (Player.MP<50){ |
nasiromar | 16:06a88c0110ff | 209 | mag_up(); |
nasiromar | 16:06a88c0110ff | 210 | } |
nasiromar | 16:06a88c0110ff | 211 | |
nasiromar | 15:05592aaa468c | 212 | |
nasiromar | 14:7225da81314a | 213 | if(east->type == ENEMY1||west->type == ENEMY1||north->type == ENEMY1||south->type == ENEMY1) { |
nasiromar | 14:7225da81314a | 214 | Player.HP-=2; |
nasiromar | 14:7225da81314a | 215 | fire_dmg(); |
nasiromar | 14:7225da81314a | 216 | gob_dmg(); |
nasiromar | 14:7225da81314a | 217 | Player.HP-=10; |
nasiromar | 14:7225da81314a | 218 | Player.x--; |
nasiromar | 14:7225da81314a | 219 | Player.y--; |
nasiromar | 14:7225da81314a | 220 | Player.gobs_slain += 1; |
nasiromar | 13:798a4dd14c7e | 221 | map_delete(10,10); |
nasiromar | 11:6cd02a8539d1 | 222 | } |
nasiromar | 15:05592aaa468c | 223 | |
nasiromar | 14:7225da81314a | 224 | if(east->type == ENEMY2||west->type == ENEMY2||north->type == ENEMY2||south->type == ENEMY2) { |
nasiromar | 14:7225da81314a | 225 | Player.HP-=12; |
nasiromar | 14:7225da81314a | 226 | gob_dmg(); |
nasiromar | 14:7225da81314a | 227 | Player.x--; |
nasiromar | 14:7225da81314a | 228 | Player.y--; |
nasiromar | 13:798a4dd14c7e | 229 | map_delete(5,7); |
nasiromar | 11:6cd02a8539d1 | 230 | } |
nasiromar | 15:05592aaa468c | 231 | |
nasiromar | 14:7225da81314a | 232 | if(east->type == ENEMY||west->type == ENEMY||north->type == ENEMY||south->type == ENEMY) { |
nasiromar | 13:798a4dd14c7e | 233 | Player.HP-=10; |
nasiromar | 14:7225da81314a | 234 | eye_dmg(); |
nasiromar | 14:7225da81314a | 235 | Player.eye_slain += 1; |
nasiromar | 13:798a4dd14c7e | 236 | Player.x--; |
nasiromar | 13:798a4dd14c7e | 237 | Player.y--; |
nasiromar | 14:7225da81314a | 238 | map_delete(15,15); |
nasiromar | 14:7225da81314a | 239 | } |
nasiromar | 15:05592aaa468c | 240 | |
nasiromar | 14:7225da81314a | 241 | if(east->type == CHEST||west->type == CHEST||north->type == CHEST||south->type == CHEST && Player.MP < 100) { |
nasiromar | 14:7225da81314a | 242 | potion(); |
nasiromar | 14:7225da81314a | 243 | Player.MP += 5; |
nasiromar | 14:7225da81314a | 244 | } |
nasiromar | 15:05592aaa468c | 245 | |
nasiromar | 14:7225da81314a | 246 | if(east->type == CHESTT||west->type == CHESTT||north->type == CHESTT||south->type == CHESTT) { |
nasiromar | 14:7225da81314a | 247 | money_chest(); |
nasiromar | 14:7225da81314a | 248 | Player.money += 2; |
nasiromar | 14:7225da81314a | 249 | //wave_file = fopen("/sd/rupee-collect.wav","r"); |
nasiromar | 14:7225da81314a | 250 | //waver.play(wave_file); |
nasiromar | 14:7225da81314a | 251 | //fclose(wave_file); |
nasiromar | 13:798a4dd14c7e | 252 | } |
nasiromar | 15:05592aaa468c | 253 | |
nasiromar | 14:7225da81314a | 254 | if(east->type == VILL||west->type == VILL||north->type == VILL||south->type == VILL) { |
nasiromar | 14:7225da81314a | 255 | merch_speech1(); |
nasiromar | 14:7225da81314a | 256 | init_store(); |
nasiromar | 14:7225da81314a | 257 | purchase = store(); |
nasiromar | 15:05592aaa468c | 258 | |
nasiromar | 15:05592aaa468c | 259 | if (purchase == POT && Player.money >= 4) { |
nasiromar | 14:7225da81314a | 260 | poti(); |
nasiromar | 14:7225da81314a | 261 | Player.money -= 4; |
nasiromar | 14:7225da81314a | 262 | Player.MP += 8; |
nasiromar | 15:05592aaa468c | 263 | } |
nasiromar | 15:05592aaa468c | 264 | |
nasiromar | 15:05592aaa468c | 265 | if (purchase == ELX && Player.money >= 5) { |
nasiromar | 14:7225da81314a | 266 | elx(); |
nasiromar | 14:7225da81314a | 267 | Player.money -= 5; |
nasiromar | 14:7225da81314a | 268 | Player.HP += 10; |
nasiromar | 15:05592aaa468c | 269 | } |
nasiromar | 15:05592aaa468c | 270 | |
nasiromar | 15:05592aaa468c | 271 | if (purchase == FRT && Player.money >= 2) { |
nasiromar | 14:7225da81314a | 272 | frt(); |
nasiromar | 14:7225da81314a | 273 | Player.money -= 2; |
nasiromar | 14:7225da81314a | 274 | Player.HP += 6; |
nasiromar | 15:05592aaa468c | 275 | } |
nasiromar | 15:05592aaa468c | 276 | if (Player.money <= 1) { |
nasiromar | 14:7225da81314a | 277 | cantbuy(); |
nasiromar | 15:05592aaa468c | 278 | } |
nasiromar | 9:cbb9cfb1f6c5 | 279 | |
nasiromar | 15:05592aaa468c | 280 | merch_speech2(); |
nasiromar | 9:cbb9cfb1f6c5 | 281 | } |
rconnorlawson | 0:35660d7952f7 | 282 | |
nasiromar | 15:05592aaa468c | 283 | |
nasiromar | 15:05592aaa468c | 284 | if(north->type == DRAGON||south->type == DRAGON||east->type == DRAGON||west->type == DRAGON && i >0) { |
nasiromar | 15:05592aaa468c | 285 | |
nasiromar | 15:05592aaa468c | 286 | if ( Player.spell == WATER && Player.gobs_slain == 1 && Player.eye_slain == 1) { |
nasiromar | 15:05592aaa468c | 287 | drg(); |
nasiromar | 15:05592aaa468c | 288 | i--; |
nasiromar | 6:c9695079521d | 289 | |
nasiromar | 15:05592aaa468c | 290 | } else |
nasiromar | 15:05592aaa468c | 291 | Player.HP-=50; |
nasiromar | 15:05592aaa468c | 292 | Player.x--; |
nasiromar | 15:05592aaa468c | 293 | Player.y--; |
nasiromar | 15:05592aaa468c | 294 | |
nasiromar | 15:05592aaa468c | 295 | |
nasiromar | 15:05592aaa468c | 296 | if (i <=0) { |
nasiromar | 15:05592aaa468c | 297 | slay_dragon(); |
nasiromar | 15:05592aaa468c | 298 | map_erase(15,9); |
nasiromar | 15:05592aaa468c | 299 | Player.has_key = 1; |
nasiromar | 15:05592aaa468c | 300 | |
nasiromar | 16:06a88c0110ff | 301 | |
nasiromar | 11:6cd02a8539d1 | 302 | |
nasiromar | 15:05592aaa468c | 303 | } |
nasiromar | 15:05592aaa468c | 304 | } |
nasiromar | 15:05592aaa468c | 305 | |
nasiromar | 15:05592aaa468c | 306 | |
nasiromar | 15:05592aaa468c | 307 | |
rconnorlawson | 0:35660d7952f7 | 308 | |
nasiromar | 15:05592aaa468c | 309 | if(north->type == PORTAl||south->type == PORTAl||east->type == PORTAl||west->type == PORTAl) { |
nasiromar | 15:05592aaa468c | 310 | set_active_map(0); |
nasiromar | 16:06a88c0110ff | 311 | add_key(26,13); |
nasiromar | 15:05592aaa468c | 312 | Player.x=20; |
nasiromar | 15:05592aaa468c | 313 | Player.y = 36; |
nasiromar | 15:05592aaa468c | 314 | } |
nasiromar | 15:05592aaa468c | 315 | |
nasiromar | 15:05592aaa468c | 316 | if((north->type == DOOR||south->type == DOOR||east->type == DOOR||west->type == DOOR)&&Player.has_key == 1) { |
nasiromar | 15:05592aaa468c | 317 | Player.won = door_open(); |
nasiromar | 15:05592aaa468c | 318 | } |
nasiromar | 15:05592aaa468c | 319 | |
nasiromar | 15:05592aaa468c | 320 | if((north->type == DOOR||south->type == DOOR||east->type == DOOR||west->type == DOOR)&&Player.has_key == 0) { |
nasiromar | 15:05592aaa468c | 321 | door_locked(); |
nasiromar | 15:05592aaa468c | 322 | } |
nasiromar | 15:05592aaa468c | 323 | |
nasiromar | 15:05592aaa468c | 324 | if((north->type == PLANT||south->type == PLANT||east->type == PLANT||west->type == PLANT||curr->type == PLANT)&& |
nasiromar | 15:05592aaa468c | 325 | (north->data == FRUIT||south->data == FRUIT||east->data == FRUIT||west->data == FRUIT||curr->data == FRUIT)) { |
nasiromar | 15:05592aaa468c | 326 | if(Player.HP < 100) { |
nasiromar | 15:05592aaa468c | 327 | fruit(); |
nasiromar | 15:05592aaa468c | 328 | Player.HP += 5; |
nasiromar | 16:06a88c0110ff | 329 | |
nasiromar | 15:05592aaa468c | 330 | } else |
nasiromar | 15:05592aaa468c | 331 | no_fruit(); |
nasiromar | 10:e18685911e84 | 332 | |
nasiromar | 10:e18685911e84 | 333 | |
nasiromar | 10:e18685911e84 | 334 | |
nasiromar | 10:e18685911e84 | 335 | |
nasiromar | 10:e18685911e84 | 336 | |
nasiromar | 10:e18685911e84 | 337 | } |
nasiromar | 15:05592aaa468c | 338 | } |
nasiromar | 15:05592aaa468c | 339 | break; |
nasiromar | 15:05592aaa468c | 340 | case MENU_BUTTON: { |
nasiromar | 15:05592aaa468c | 341 | init_spells(); |
nasiromar | 10:e18685911e84 | 342 | |
nasiromar | 15:05592aaa468c | 343 | Player.spell = spell(); |
nasiromar | 15:05592aaa468c | 344 | Player.MP -= 10; |
nasiromar | 15:05592aaa468c | 345 | uLCD.locate(15,15); |
nasiromar | 15:05592aaa468c | 346 | uLCD.printf("S:%1i",Player.spell); |
nasiromar | 10:e18685911e84 | 347 | } |
nasiromar | 15:05592aaa468c | 348 | break; |
nasiromar | 15:05592aaa468c | 349 | case GOD_MODE: { |
nasiromar | 15:05592aaa468c | 350 | |
nasiromar | 15:05592aaa468c | 351 | Player.god = !Player.god; |
nasiromar | 15:05592aaa468c | 352 | if (Player.god) { |
nasiromar | 15:05592aaa468c | 353 | god_modeOn(); |
nasiromar | 16:06a88c0110ff | 354 | Player.god_walk = !Player.god_walk; |
nasiromar | 15:05592aaa468c | 355 | } else |
nasiromar | 15:05592aaa468c | 356 | god_modeOff(); |
nasiromar | 16:06a88c0110ff | 357 | Player.god_walk = Player.god_walk; |
nasiromar | 10:e18685911e84 | 358 | |
nasiromar | 15:05592aaa468c | 359 | } |
nasiromar | 15:05592aaa468c | 360 | break; |
nasiromar | 15:05592aaa468c | 361 | |
nasiromar | 15:05592aaa468c | 362 | default: |
nasiromar | 15:05592aaa468c | 363 | |
nasiromar | 15:05592aaa468c | 364 | break; |
nasiromar | 15:05592aaa468c | 365 | } |
nasiromar | 15:05592aaa468c | 366 | |
nasiromar | 15:05592aaa468c | 367 | |
nasiromar | 15:05592aaa468c | 368 | return NO_RESULT; |
nasiromar | 15:05592aaa468c | 369 | |
nasiromar | 15:05592aaa468c | 370 | |
nasiromar | 10:e18685911e84 | 371 | } |
nasiromar | 10:e18685911e84 | 372 | |
nasiromar | 10:e18685911e84 | 373 | /** |
nasiromar | 10:e18685911e84 | 374 | * Entry point for frame drawing. This should be called once per iteration of |
nasiromar | 10:e18685911e84 | 375 | * the game loop. This draws all tiles on the screen, followed by the status |
nasiromar | 10:e18685911e84 | 376 | * bars. Unless init is nonzero, this function will optimize drawing by only |
nasiromar | 10:e18685911e84 | 377 | * drawing tiles that have changed from the previous frame. |
nasiromar | 10:e18685911e84 | 378 | */ |
nasiromar | 15:05592aaa468c | 379 | |
nasiromar | 14:7225da81314a | 380 | |
nasiromar | 15:05592aaa468c | 381 | |
nasiromar | 10:e18685911e84 | 382 | void draw_game(int init) |
nasiromar | 10:e18685911e84 | 383 | { |
nasiromar | 10:e18685911e84 | 384 | // Draw game border first |
nasiromar | 10:e18685911e84 | 385 | if(init) draw_border(); |
nasiromar | 10:e18685911e84 | 386 | |
nasiromar | 10:e18685911e84 | 387 | // Iterate over all visible map tiles |
nasiromar | 10:e18685911e84 | 388 | for (int i = -5; i <= 5; i++) { // Iterate over columns of tiles |
nasiromar | 10:e18685911e84 | 389 | for (int j = -4; j <= 4; j++) { // Iterate over one column of tiles |
nasiromar | 10:e18685911e84 | 390 | // Here, we have a given (i,j) |
nasiromar | 10:e18685911e84 | 391 | |
nasiromar | 10:e18685911e84 | 392 | // Compute the current map (x,y) of this tile |
nasiromar | 10:e18685911e84 | 393 | int x = i + Player.x; |
nasiromar | 10:e18685911e84 | 394 | int y = j + Player.y; |
nasiromar | 9:cbb9cfb1f6c5 | 395 | |
nasiromar | 10:e18685911e84 | 396 | // Compute the previous map (px, py) of this tile |
nasiromar | 10:e18685911e84 | 397 | int px = i + Player.px; |
nasiromar | 10:e18685911e84 | 398 | int py = j + Player.py; |
nasiromar | 10:e18685911e84 | 399 | |
nasiromar | 10:e18685911e84 | 400 | // Compute u,v coordinates for drawing |
nasiromar | 10:e18685911e84 | 401 | int u = (i+5)*11 + 3; |
nasiromar | 10:e18685911e84 | 402 | int v = (j+4)*11 + 15; |
nasiromar | 10:e18685911e84 | 403 | |
nasiromar | 10:e18685911e84 | 404 | // Figure out what to draw |
nasiromar | 10:e18685911e84 | 405 | DrawFunc draw = NULL; |
nasiromar | 10:e18685911e84 | 406 | if (init && i == 0 && j == 0) { // Only draw the player on init |
nasiromar | 10:e18685911e84 | 407 | draw_player(u, v, Player.has_key); |
nasiromar | 10:e18685911e84 | 408 | continue; |
nasiromar | 10:e18685911e84 | 409 | } else if (x >= 0 && y >= 0 && x < map_width() && y < map_height()) { // Current (i,j) in the map |
nasiromar | 10:e18685911e84 | 410 | MapItem* curr_item = get_here(x, y); |
nasiromar | 10:e18685911e84 | 411 | MapItem* prev_item = get_here(px, py); |
nasiromar | 10:e18685911e84 | 412 | if (init || curr_item != prev_item) { // Only draw if they're different |
nasiromar | 10:e18685911e84 | 413 | if (curr_item) { // There's something here! Draw it |
nasiromar | 10:e18685911e84 | 414 | draw = curr_item->draw; |
nasiromar | 10:e18685911e84 | 415 | } else { // There used to be something, but now there isn't |
nasiromar | 10:e18685911e84 | 416 | draw = draw_nothing; |
nasiromar | 10:e18685911e84 | 417 | } |
nasiromar | 10:e18685911e84 | 418 | } |
nasiromar | 10:e18685911e84 | 419 | } else if (init) { // If doing a full draw, but we're out of bounds, draw the walls. |
nasiromar | 10:e18685911e84 | 420 | draw = draw_wall; |
nasiromar | 9:cbb9cfb1f6c5 | 421 | } |
nasiromar | 9:cbb9cfb1f6c5 | 422 | |
nasiromar | 10:e18685911e84 | 423 | // Actually draw the tile |
nasiromar | 10:e18685911e84 | 424 | if (draw) draw(u, v); |
nasiromar | 10:e18685911e84 | 425 | } |
rconnorlawson | 0:35660d7952f7 | 426 | } |
rconnorlawson | 0:35660d7952f7 | 427 | |
nasiromar | 10:e18685911e84 | 428 | // Draw status bars |
nasiromar | 14:7225da81314a | 429 | draw_upper_status(Player.x,Player.y,Player.money); |
nasiromar | 10:e18685911e84 | 430 | draw_lower_status(Player.HP,Player.MP); |
nasiromar | 10:e18685911e84 | 431 | } |
nasiromar | 9:cbb9cfb1f6c5 | 432 | |
nasiromar | 9:cbb9cfb1f6c5 | 433 | |
nasiromar | 10:e18685911e84 | 434 | /** |
nasiromar | 10:e18685911e84 | 435 | * Initialize the main world map. Add walls around the edges, interior chambers, |
nasiromar | 10:e18685911e84 | 436 | * and plants in the background so you can see motion. Note: using the similar |
nasiromar | 10:e18685911e84 | 437 | * procedure you can init the secondary map(s). |
nasiromar | 10:e18685911e84 | 438 | */ |
nasiromar | 10:e18685911e84 | 439 | void init_main_map() |
nasiromar | 10:e18685911e84 | 440 | { |
nasiromar | 10:e18685911e84 | 441 | // "Random" plants |
nasiromar | 10:e18685911e84 | 442 | Map* map = set_active_map(0); |
nasiromar | 11:6cd02a8539d1 | 443 | for(int i = map_width() + 9; i < map_area(); i += 39) { |
nasiromar | 10:e18685911e84 | 444 | add_plant(i % map_width(), i / map_width()); |
nasiromar | 10:e18685911e84 | 445 | } |
nasiromar | 10:e18685911e84 | 446 | pc.printf("plants\r\n"); |
nasiromar | 9:cbb9cfb1f6c5 | 447 | |
nasiromar | 10:e18685911e84 | 448 | pc.printf("Adding walls!\r\n"); |
nasiromar | 10:e18685911e84 | 449 | add_wall(0, 0, HORIZONTAL, map_width()); |
nasiromar | 10:e18685911e84 | 450 | add_wall(0, map_height()-1, HORIZONTAL, map_width()); |
nasiromar | 10:e18685911e84 | 451 | add_wall(0, 0, VERTICAL, map_height()); |
nasiromar | 10:e18685911e84 | 452 | add_wall(map_width()-1, 0, VERTICAL, map_height()); |
nasiromar | 10:e18685911e84 | 453 | pc.printf("Walls done!\r\n"); |
nasiromar | 10:e18685911e84 | 454 | |
nasiromar | 10:e18685911e84 | 455 | |
nasiromar | 10:e18685911e84 | 456 | |
nasiromar | 10:e18685911e84 | 457 | for(int i = 7; i < 13; i++) { |
nasiromar | 10:e18685911e84 | 458 | for (int j = 23; j <31; j++) { |
nasiromar | 10:e18685911e84 | 459 | add_kindom(j,i); |
nasiromar | 9:cbb9cfb1f6c5 | 460 | } |
nasiromar | 9:cbb9cfb1f6c5 | 461 | } |
rconnorlawson | 0:35660d7952f7 | 462 | |
nasiromar | 14:7225da81314a | 463 | add_chest2(20,20); |
nasiromar | 14:7225da81314a | 464 | add_chest2(5,8); |
nasiromar | 14:7225da81314a | 465 | add_chest2(33,20); |
nasiromar | 14:7225da81314a | 466 | add_chest2(14,16); |
nasiromar | 14:7225da81314a | 467 | add_chest2(40,15); |
nasiromar | 10:e18685911e84 | 468 | |
nasiromar | 15:05592aaa468c | 469 | add_npc(10,5); |
nasiromar | 15:05592aaa468c | 470 | |
nasiromar | 11:6cd02a8539d1 | 471 | add_npc2(30,30); |
nasiromar | 15:05592aaa468c | 472 | |
nasiromar | 15:05592aaa468c | 473 | add_npc2(11,11); |
nasiromar | 15:05592aaa468c | 474 | |
nasiromar | 14:7225da81314a | 475 | add_npc2(20,9); |
nasiromar | 15:05592aaa468c | 476 | |
nasiromar | 13:798a4dd14c7e | 477 | add_merch(12,16); |
nasiromar | 15:05592aaa468c | 478 | |
nasiromar | 15:05592aaa468c | 479 | for(int i = 11; i <= 13; i++) { |
nasiromar | 14:7225da81314a | 480 | add_store(i,15); |
nasiromar | 15:05592aaa468c | 481 | } |
nasiromar | 15:05592aaa468c | 482 | |
nasiromar | 14:7225da81314a | 483 | add_fire(27,13); |
nasiromar | 14:7225da81314a | 484 | add_fire(25,13); |
nasiromar | 15:05592aaa468c | 485 | |
nasiromar | 10:e18685911e84 | 486 | add_door(26,12); |
nasiromar | 10:e18685911e84 | 487 | |
nasiromar | 10:e18685911e84 | 488 | add_portal(20,35); |
nasiromar | 10:e18685911e84 | 489 | |
nasiromar | 10:e18685911e84 | 490 | print_map(); |
nasiromar | 10:e18685911e84 | 491 | } |
nasiromar | 10:e18685911e84 | 492 | |
nasiromar | 10:e18685911e84 | 493 | /** |
nasiromar | 10:e18685911e84 | 494 | * Program entry point! This is where it all begins. |
nasiromar | 10:e18685911e84 | 495 | * This function orchestrates all the parts of the game. Most of your |
nasiromar | 10:e18685911e84 | 496 | * implementation should be elsewhere - this holds the game loop, and should |
nasiromar | 10:e18685911e84 | 497 | * read like a road map for the rest of the code. |
nasiromar | 10:e18685911e84 | 498 | */ |
nasiromar | 10:e18685911e84 | 499 | int main() |
nasiromar | 10:e18685911e84 | 500 | { |
nasiromar | 10:e18685911e84 | 501 | |
nasiromar | 10:e18685911e84 | 502 | Player.HP = 100; |
nasiromar | 10:e18685911e84 | 503 | Player.MP = 100; |
nasiromar | 10:e18685911e84 | 504 | |
nasiromar | 10:e18685911e84 | 505 | // MapItem* north = get_north(Player.x,Player.y); |
rconnorlawson | 0:35660d7952f7 | 506 | |
nasiromar | 6:c9695079521d | 507 | |
nasiromar | 10:e18685911e84 | 508 | // First things first: initialize hardware |
nasiromar | 10:e18685911e84 | 509 | ASSERT_P(hardware_init() == ERROR_NONE, "Hardware init failed!"); |
nasiromar | 9:cbb9cfb1f6c5 | 510 | |
nasiromar | 10:e18685911e84 | 511 | // Initialize the maps |
nasiromar | 10:e18685911e84 | 512 | maps_init(); |
nasiromar | 10:e18685911e84 | 513 | init_main_map(); |
nasiromar | 11:6cd02a8539d1 | 514 | init_dung(); |
nasiromar | 15:05592aaa468c | 515 | |
nasiromar | 15:05592aaa468c | 516 | |
nasiromar | 10:e18685911e84 | 517 | |
nasiromar | 10:e18685911e84 | 518 | // Initialize game state |
nasiromar | 10:e18685911e84 | 519 | set_active_map(0); |
nasiromar | 10:e18685911e84 | 520 | Player.x = Player.y = 5; |
nasiromar | 14:7225da81314a | 521 | Player.money = 0; |
nasiromar | 12:116a4cc85b16 | 522 | Player.spell = NULL; |
nasiromar | 15:05592aaa468c | 523 | |
nasiromar | 10:e18685911e84 | 524 | |
nasiromar | 10:e18685911e84 | 525 | draw_start(); |
nasiromar | 10:e18685911e84 | 526 | |
nasiromar | 12:116a4cc85b16 | 527 | //wave_file = fopen("/sd/godfather.wav","r"); |
nasiromar | 12:116a4cc85b16 | 528 | //waver.play(wave_file); |
nasiromar | 12:116a4cc85b16 | 529 | //fclose(wave_file); |
nasiromar | 10:e18685911e84 | 530 | |
nasiromar | 10:e18685911e84 | 531 | while(1) { |
nasiromar | 10:e18685911e84 | 532 | |
nasiromar | 10:e18685911e84 | 533 | Timer t; |
nasiromar | 10:e18685911e84 | 534 | t.start(); |
nasiromar | 10:e18685911e84 | 535 | GameInputs inp = read_inputs(); |
nasiromar | 10:e18685911e84 | 536 | int action = get_action(inp); |
nasiromar | 10:e18685911e84 | 537 | |
nasiromar | 10:e18685911e84 | 538 | |
nasiromar | 10:e18685911e84 | 539 | if( action == ACTION_BUTTON) { |
nasiromar | 10:e18685911e84 | 540 | |
nasiromar | 10:e18685911e84 | 541 | break; |
nasiromar | 9:cbb9cfb1f6c5 | 542 | } |
nasiromar | 10:e18685911e84 | 543 | t.stop(); |
nasiromar | 10:e18685911e84 | 544 | int dt = t.read_ms(); |
nasiromar | 10:e18685911e84 | 545 | if (dt < 100) wait_ms(100-dt); |
nasiromar | 9:cbb9cfb1f6c5 | 546 | |
nasiromar | 9:cbb9cfb1f6c5 | 547 | } |
nasiromar | 9:cbb9cfb1f6c5 | 548 | |
nasiromar | 6:c9695079521d | 549 | |
nasiromar | 10:e18685911e84 | 550 | // Initial drawing |
nasiromar | 10:e18685911e84 | 551 | draw_game(true); |
nasiromar | 10:e18685911e84 | 552 | // Main game loop |
nasiromar | 10:e18685911e84 | 553 | while(1) { |
nasiromar | 10:e18685911e84 | 554 | // Timer to measure game update speed |
nasiromar | 10:e18685911e84 | 555 | Timer t; |
nasiromar | 10:e18685911e84 | 556 | t.start(); |
nasiromar | 6:c9695079521d | 557 | |
nasiromar | 10:e18685911e84 | 558 | // Actually do the game update: |
nasiromar | 10:e18685911e84 | 559 | // 1. Read inputs |
nasiromar | 10:e18685911e84 | 560 | GameInputs input = read_inputs(); |
nasiromar | 10:e18685911e84 | 561 | // 2. Determine action (get_action) |
nasiromar | 10:e18685911e84 | 562 | int action = get_action(input); |
nasiromar | 10:e18685911e84 | 563 | // 3. Update game (update_game) |
nasiromar | 10:e18685911e84 | 564 | update_game(action); |
nasiromar | 10:e18685911e84 | 565 | // 3b. Check for game over |
nasiromar | 16:06a88c0110ff | 566 | if(Player.HP <= 0) { |
nasiromar | 10:e18685911e84 | 567 | draw_gameover(); |
nasiromar | 10:e18685911e84 | 568 | break; |
nasiromar | 10:e18685911e84 | 569 | } |
nasiromar | 16:06a88c0110ff | 570 | if(Player.won == 1 ){ |
nasiromar | 16:06a88c0110ff | 571 | draw_win(); |
nasiromar | 16:06a88c0110ff | 572 | break; |
nasiromar | 16:06a88c0110ff | 573 | } |
nasiromar | 10:e18685911e84 | 574 | // 4. Draw frame (draw_game) |
nasiromar | 10:e18685911e84 | 575 | draw_game(true); |
nasiromar | 9:cbb9cfb1f6c5 | 576 | |
nasiromar | 10:e18685911e84 | 577 | // 5. Frame delay |
nasiromar | 10:e18685911e84 | 578 | t.stop(); |
nasiromar | 10:e18685911e84 | 579 | int dt = t.read_ms(); |
nasiromar | 10:e18685911e84 | 580 | if (dt < 100) wait_ms(100 - dt); |
nasiromar | 10:e18685911e84 | 581 | } |
nasiromar | 10:e18685911e84 | 582 | // |
nasiromar | 9:cbb9cfb1f6c5 | 583 | |
nasiromar | 9:cbb9cfb1f6c5 | 584 | |
nasiromar | 10:e18685911e84 | 585 | } |