Game For ECE 2035
Dependencies: mbed wave_player 4DGL-uLCD-SE MMA8452
main.cpp@14:7225da81314a, 2021-12-03 (annotated)
- Committer:
- nasiromar
- Date:
- Fri Dec 03 08:45:49 2021 +0000
- Revision:
- 14:7225da81314a
- Parent:
- 13:798a4dd14c7e
- Child:
- 15:05592aaa468c
NasLand Almost Done
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 | 6:c9695079521d | 33 | int god; |
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 | 9:cbb9cfb1f6c5 | 42 | |
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 | 11:6cd02a8539d1 | 111 | //walk_goblin(0); |
nasiromar | 14:7225da81314a | 112 | |
nasiromar | 10:e18685911e84 | 113 | |
nasiromar | 10:e18685911e84 | 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 | 6:c9695079521d | 129 | break; |
nasiromar | 6:c9695079521d | 130 | } |
nasiromar | 6:c9695079521d | 131 | case GO_RIGHT: { |
nasiromar | 6:c9695079521d | 132 | MapItem* east = get_east(Player.x,Player.y); |
nasiromar | 6:c9695079521d | 133 | if (east->walkable && east == NULL || east->type == (PORTAL || PLANT) && Player.x != 0 || Player.god ) { |
nasiromar | 6:c9695079521d | 134 | Player.x++; |
nasiromar | 6:c9695079521d | 135 | } |
nasiromar | 6:c9695079521d | 136 | break; |
nasiromar | 6:c9695079521d | 137 | } |
nasiromar | 6:c9695079521d | 138 | case GO_DOWN: { |
nasiromar | 6:c9695079521d | 139 | MapItem* south = get_south(Player.x,Player.y); |
nasiromar | 6:c9695079521d | 140 | if (south->walkable && south == NULL || south->type ==(PORTAL || PLANT) && Player.y != 0 || Player.god ) { |
nasiromar | 6:c9695079521d | 141 | Player.y++; |
nasiromar | 6:c9695079521d | 142 | } |
nasiromar | 6:c9695079521d | 143 | break; |
nasiromar | 6:c9695079521d | 144 | } |
nasiromar | 6:c9695079521d | 145 | case GO_LEFT: { |
nasiromar | 6:c9695079521d | 146 | MapItem* west = get_west(Player.x,Player.y); |
nasiromar | 6:c9695079521d | 147 | if (west->walkable && west == NULL || west->type == (PORTAL || PLANT) && Player.x != 0 || Player.god ) { |
nasiromar | 6:c9695079521d | 148 | Player.x--; |
nasiromar | 6:c9695079521d | 149 | } |
nasiromar | 6:c9695079521d | 150 | break; |
nasiromar | 6:c9695079521d | 151 | } |
nasiromar | 6:c9695079521d | 152 | case ACTION_BUTTON: { |
nasiromar | 6:c9695079521d | 153 | MapItem* west = get_west(Player.x,Player.y); |
nasiromar | 6:c9695079521d | 154 | MapItem* south = get_south(Player.x,Player.y); |
nasiromar | 6:c9695079521d | 155 | MapItem* north = get_north(Player.x,Player.y); |
nasiromar | 6:c9695079521d | 156 | MapItem* east = get_east(Player.x,Player.y); |
nasiromar | 6:c9695079521d | 157 | MapItem* curr = get_here(Player.x,Player.y); |
nasiromar | 9:cbb9cfb1f6c5 | 158 | |
nasiromar | 9:cbb9cfb1f6c5 | 159 | if((east->type == NPC||west->type == NPC||north->type == NPC||south->type == NPC)&& Player.has_key == 1) { |
nasiromar | 7:862062ffca62 | 160 | npc_speech2(); |
nasiromar | 9:cbb9cfb1f6c5 | 161 | } |
nasiromar | 9:cbb9cfb1f6c5 | 162 | |
nasiromar | 9:cbb9cfb1f6c5 | 163 | if(east->type == NPC||west->type == NPC||north->type == NPC||south->type == NPC && Player.has_key == 0 ) { |
nasiromar | 6:c9695079521d | 164 | npc_speech1(); |
nasiromar | 11:6cd02a8539d1 | 165 | Player.talked = 1; |
nasiromar | 9:cbb9cfb1f6c5 | 166 | } |
nasiromar | 14:7225da81314a | 167 | |
nasiromar | 14:7225da81314a | 168 | if(east->type == NPCT||west->type == NPCT||north->type == NPCT||south->type == NPCT && Player.has_key == 0 ) { |
nasiromar | 14:7225da81314a | 169 | npc2_speech(); |
nasiromar | 14:7225da81314a | 170 | Player.talked = 1; |
nasiromar | 14:7225da81314a | 171 | } |
nasiromar | 9:cbb9cfb1f6c5 | 172 | |
nasiromar | 9:cbb9cfb1f6c5 | 173 | if(north->type == PORTAL||south->type == PORTAL||east->type == PORTAL||west->type == PORTAL) { |
nasiromar | 11:6cd02a8539d1 | 174 | set_active_map(1); |
nasiromar | 7:862062ffca62 | 175 | Player.x = Player.y = 7; |
nasiromar | 9:cbb9cfb1f6c5 | 176 | } |
nasiromar | 11:6cd02a8539d1 | 177 | |
nasiromar | 14:7225da81314a | 178 | |
nasiromar | 14:7225da81314a | 179 | |
nasiromar | 14:7225da81314a | 180 | if(east->type == ENEMY1||west->type == ENEMY1||north->type == ENEMY1||south->type == ENEMY1) { |
nasiromar | 14:7225da81314a | 181 | Player.HP-=2; |
nasiromar | 14:7225da81314a | 182 | fire_dmg(); |
nasiromar | 14:7225da81314a | 183 | gob_dmg(); |
nasiromar | 14:7225da81314a | 184 | Player.HP-=10; |
nasiromar | 14:7225da81314a | 185 | Player.x--; |
nasiromar | 14:7225da81314a | 186 | Player.y--; |
nasiromar | 14:7225da81314a | 187 | Player.gobs_slain += 1; |
nasiromar | 13:798a4dd14c7e | 188 | map_delete(10,10); |
nasiromar | 11:6cd02a8539d1 | 189 | } |
nasiromar | 11:6cd02a8539d1 | 190 | |
nasiromar | 14:7225da81314a | 191 | if(east->type == ENEMY2||west->type == ENEMY2||north->type == ENEMY2||south->type == ENEMY2) { |
nasiromar | 14:7225da81314a | 192 | Player.HP-=12; |
nasiromar | 14:7225da81314a | 193 | gob_dmg(); |
nasiromar | 14:7225da81314a | 194 | Player.x--; |
nasiromar | 14:7225da81314a | 195 | Player.y--; |
nasiromar | 13:798a4dd14c7e | 196 | map_delete(5,7); |
nasiromar | 11:6cd02a8539d1 | 197 | } |
nasiromar | 11:6cd02a8539d1 | 198 | |
nasiromar | 14:7225da81314a | 199 | if(east->type == ENEMY||west->type == ENEMY||north->type == ENEMY||south->type == ENEMY) { |
nasiromar | 13:798a4dd14c7e | 200 | Player.HP-=10; |
nasiromar | 14:7225da81314a | 201 | eye_dmg(); |
nasiromar | 14:7225da81314a | 202 | Player.eye_slain += 1; |
nasiromar | 13:798a4dd14c7e | 203 | Player.x--; |
nasiromar | 13:798a4dd14c7e | 204 | Player.y--; |
nasiromar | 14:7225da81314a | 205 | map_delete(15,15); |
nasiromar | 14:7225da81314a | 206 | } |
nasiromar | 14:7225da81314a | 207 | |
nasiromar | 14:7225da81314a | 208 | if(east->type == CHEST||west->type == CHEST||north->type == CHEST||south->type == CHEST && Player.MP < 100) { |
nasiromar | 14:7225da81314a | 209 | potion(); |
nasiromar | 14:7225da81314a | 210 | Player.MP += 5; |
nasiromar | 14:7225da81314a | 211 | } |
nasiromar | 14:7225da81314a | 212 | |
nasiromar | 14:7225da81314a | 213 | if(east->type == CHESTT||west->type == CHESTT||north->type == CHESTT||south->type == CHESTT) { |
nasiromar | 14:7225da81314a | 214 | money_chest(); |
nasiromar | 14:7225da81314a | 215 | Player.money += 2; |
nasiromar | 14:7225da81314a | 216 | //wave_file = fopen("/sd/rupee-collect.wav","r"); |
nasiromar | 14:7225da81314a | 217 | //waver.play(wave_file); |
nasiromar | 14:7225da81314a | 218 | //fclose(wave_file); |
nasiromar | 13:798a4dd14c7e | 219 | } |
nasiromar | 13:798a4dd14c7e | 220 | |
nasiromar | 14:7225da81314a | 221 | if(east->type == VILL||west->type == VILL||north->type == VILL||south->type == VILL) { |
nasiromar | 14:7225da81314a | 222 | merch_speech1(); |
nasiromar | 14:7225da81314a | 223 | init_store(); |
nasiromar | 14:7225da81314a | 224 | purchase = store(); |
nasiromar | 14:7225da81314a | 225 | |
nasiromar | 14:7225da81314a | 226 | if (purchase == POT && Player.money >= 4){ |
nasiromar | 14:7225da81314a | 227 | poti(); |
nasiromar | 14:7225da81314a | 228 | Player.money -= 4; |
nasiromar | 14:7225da81314a | 229 | Player.MP += 8; |
nasiromar | 14:7225da81314a | 230 | } |
nasiromar | 14:7225da81314a | 231 | |
nasiromar | 14:7225da81314a | 232 | if (purchase == ELX && Player.money >= 5){ |
nasiromar | 14:7225da81314a | 233 | elx(); |
nasiromar | 14:7225da81314a | 234 | Player.money -= 5; |
nasiromar | 14:7225da81314a | 235 | Player.HP += 10; |
nasiromar | 14:7225da81314a | 236 | } |
nasiromar | 14:7225da81314a | 237 | |
nasiromar | 14:7225da81314a | 238 | if (purchase == FRT && Player.money >= 2){ |
nasiromar | 14:7225da81314a | 239 | frt(); |
nasiromar | 14:7225da81314a | 240 | Player.money -= 2; |
nasiromar | 14:7225da81314a | 241 | Player.HP += 6; |
nasiromar | 14:7225da81314a | 242 | } |
nasiromar | 14:7225da81314a | 243 | if (Player.money <= 1){ |
nasiromar | 14:7225da81314a | 244 | cantbuy(); |
nasiromar | 14:7225da81314a | 245 | } |
nasiromar | 14:7225da81314a | 246 | |
nasiromar | 14:7225da81314a | 247 | merch_speech2(); |
nasiromar | 11:6cd02a8539d1 | 248 | } |
nasiromar | 11:6cd02a8539d1 | 249 | |
nasiromar | 9:cbb9cfb1f6c5 | 250 | |
nasiromar | 14:7225da81314a | 251 | if(north->type == DRAGON||south->type == DRAGON||east->type == DRAGON||west->type == DRAGON) { |
nasiromar | 14:7225da81314a | 252 | if ( Player.spell == WATER){ |
nasiromar | 14:7225da81314a | 253 | |
nasiromar | 7:862062ffca62 | 254 | slay_dragon(); |
nasiromar | 7:862062ffca62 | 255 | Player.has_key = 1; |
nasiromar | 14:7225da81314a | 256 | } |
nasiromar | 14:7225da81314a | 257 | |
nasiromar | 13:798a4dd14c7e | 258 | //walk_goblin(0); |
nasiromar | 9:cbb9cfb1f6c5 | 259 | } |
rconnorlawson | 0:35660d7952f7 | 260 | |
nasiromar | 9:cbb9cfb1f6c5 | 261 | if(north->type == PORTAl||south->type == PORTAl||east->type == PORTAl||west->type == PORTAl) { |
nasiromar | 11:6cd02a8539d1 | 262 | set_active_map(0); |
nasiromar | 11:6cd02a8539d1 | 263 | Player.x=20; |
nasiromar | 11:6cd02a8539d1 | 264 | Player.y = 36; |
nasiromar | 9:cbb9cfb1f6c5 | 265 | } |
nasiromar | 6:c9695079521d | 266 | |
nasiromar | 9:cbb9cfb1f6c5 | 267 | if((north->type == DOOR||south->type == DOOR||east->type == DOOR||west->type == DOOR)&&Player.has_key == 1) { |
nasiromar | 9:cbb9cfb1f6c5 | 268 | Player.won = door_open(); |
rconnorlawson | 0:35660d7952f7 | 269 | } |
nasiromar | 11:6cd02a8539d1 | 270 | |
nasiromar | 11:6cd02a8539d1 | 271 | if((north->type == DOOR||south->type == DOOR||east->type == DOOR||west->type == DOOR)&&Player.has_key == 0) { |
nasiromar | 11:6cd02a8539d1 | 272 | door_locked(); |
nasiromar | 11:6cd02a8539d1 | 273 | } |
rconnorlawson | 0:35660d7952f7 | 274 | |
nasiromar | 9:cbb9cfb1f6c5 | 275 | if((north->type == PLANT||south->type == PLANT||east->type == PLANT||west->type == PLANT||curr->type == PLANT)&& |
nasiromar | 10:e18685911e84 | 276 | (north->data == FRUIT||south->data == FRUIT||east->data == FRUIT||west->data == FRUIT||curr->data == FRUIT)) { |
nasiromar | 9:cbb9cfb1f6c5 | 277 | if(Player.HP < 100) { |
nasiromar | 9:cbb9cfb1f6c5 | 278 | fruit(); |
nasiromar | 9:cbb9cfb1f6c5 | 279 | Player.HP += 5; |
nasiromar | 9:cbb9cfb1f6c5 | 280 | north->data = NULL; |
nasiromar | 9:cbb9cfb1f6c5 | 281 | south->data = NULL; |
nasiromar | 9:cbb9cfb1f6c5 | 282 | east->data = NULL; |
nasiromar | 9:cbb9cfb1f6c5 | 283 | west->data = NULL; |
nasiromar | 9:cbb9cfb1f6c5 | 284 | curr->data = NULL; |
nasiromar | 10:e18685911e84 | 285 | } else |
nasiromar | 9:cbb9cfb1f6c5 | 286 | no_fruit(); |
nasiromar | 13:798a4dd14c7e | 287 | |
nasiromar | 10:e18685911e84 | 288 | |
nasiromar | 10:e18685911e84 | 289 | |
nasiromar | 10:e18685911e84 | 290 | |
nasiromar | 10:e18685911e84 | 291 | |
nasiromar | 9:cbb9cfb1f6c5 | 292 | } |
nasiromar | 10:e18685911e84 | 293 | } |
nasiromar | 10:e18685911e84 | 294 | break; |
nasiromar | 10:e18685911e84 | 295 | case MENU_BUTTON: { |
nasiromar | 10:e18685911e84 | 296 | init_spells(); |
nasiromar | 14:7225da81314a | 297 | |
nasiromar | 14:7225da81314a | 298 | Player.spell = spell(); |
nasiromar | 11:6cd02a8539d1 | 299 | Player.MP -= 10; |
nasiromar | 14:7225da81314a | 300 | uLCD.locate(15,15); |
nasiromar | 14:7225da81314a | 301 | uLCD.printf("S:%1i",Player.spell); |
nasiromar | 10:e18685911e84 | 302 | } |
nasiromar | 10:e18685911e84 | 303 | break; |
nasiromar | 10:e18685911e84 | 304 | case GOD_MODE: { |
nasiromar | 10:e18685911e84 | 305 | |
nasiromar | 10:e18685911e84 | 306 | Player.god = !Player.god; |
nasiromar | 10:e18685911e84 | 307 | if (Player.god) { |
nasiromar | 10:e18685911e84 | 308 | god_modeOn(); |
nasiromar | 10:e18685911e84 | 309 | } else |
nasiromar | 10:e18685911e84 | 310 | god_modeOff(); |
nasiromar | 10:e18685911e84 | 311 | |
nasiromar | 10:e18685911e84 | 312 | } |
nasiromar | 10:e18685911e84 | 313 | break; |
nasiromar | 10:e18685911e84 | 314 | |
nasiromar | 10:e18685911e84 | 315 | default: |
nasiromar | 10:e18685911e84 | 316 | |
nasiromar | 9:cbb9cfb1f6c5 | 317 | break; |
nasiromar | 10:e18685911e84 | 318 | } |
nasiromar | 10:e18685911e84 | 319 | |
nasiromar | 10:e18685911e84 | 320 | |
nasiromar | 10:e18685911e84 | 321 | return NO_RESULT; |
nasiromar | 10:e18685911e84 | 322 | |
nasiromar | 10:e18685911e84 | 323 | |
nasiromar | 10:e18685911e84 | 324 | } |
nasiromar | 10:e18685911e84 | 325 | |
nasiromar | 10:e18685911e84 | 326 | /** |
nasiromar | 10:e18685911e84 | 327 | * Entry point for frame drawing. This should be called once per iteration of |
nasiromar | 10:e18685911e84 | 328 | * the game loop. This draws all tiles on the screen, followed by the status |
nasiromar | 10:e18685911e84 | 329 | * bars. Unless init is nonzero, this function will optimize drawing by only |
nasiromar | 10:e18685911e84 | 330 | * drawing tiles that have changed from the previous frame. |
nasiromar | 10:e18685911e84 | 331 | */ |
nasiromar | 14:7225da81314a | 332 | |
nasiromar | 14:7225da81314a | 333 | |
nasiromar | 14:7225da81314a | 334 | |
nasiromar | 10:e18685911e84 | 335 | void draw_game(int init) |
nasiromar | 10:e18685911e84 | 336 | { |
nasiromar | 10:e18685911e84 | 337 | // Draw game border first |
nasiromar | 10:e18685911e84 | 338 | if(init) draw_border(); |
nasiromar | 10:e18685911e84 | 339 | |
nasiromar | 10:e18685911e84 | 340 | // Iterate over all visible map tiles |
nasiromar | 10:e18685911e84 | 341 | for (int i = -5; i <= 5; i++) { // Iterate over columns of tiles |
nasiromar | 10:e18685911e84 | 342 | for (int j = -4; j <= 4; j++) { // Iterate over one column of tiles |
nasiromar | 10:e18685911e84 | 343 | // Here, we have a given (i,j) |
nasiromar | 10:e18685911e84 | 344 | |
nasiromar | 10:e18685911e84 | 345 | // Compute the current map (x,y) of this tile |
nasiromar | 10:e18685911e84 | 346 | int x = i + Player.x; |
nasiromar | 10:e18685911e84 | 347 | int y = j + Player.y; |
nasiromar | 9:cbb9cfb1f6c5 | 348 | |
nasiromar | 10:e18685911e84 | 349 | // Compute the previous map (px, py) of this tile |
nasiromar | 10:e18685911e84 | 350 | int px = i + Player.px; |
nasiromar | 10:e18685911e84 | 351 | int py = j + Player.py; |
nasiromar | 10:e18685911e84 | 352 | |
nasiromar | 10:e18685911e84 | 353 | // Compute u,v coordinates for drawing |
nasiromar | 10:e18685911e84 | 354 | int u = (i+5)*11 + 3; |
nasiromar | 10:e18685911e84 | 355 | int v = (j+4)*11 + 15; |
nasiromar | 10:e18685911e84 | 356 | |
nasiromar | 10:e18685911e84 | 357 | // Figure out what to draw |
nasiromar | 10:e18685911e84 | 358 | DrawFunc draw = NULL; |
nasiromar | 10:e18685911e84 | 359 | if (init && i == 0 && j == 0) { // Only draw the player on init |
nasiromar | 10:e18685911e84 | 360 | draw_player(u, v, Player.has_key); |
nasiromar | 10:e18685911e84 | 361 | continue; |
nasiromar | 10:e18685911e84 | 362 | } else if (x >= 0 && y >= 0 && x < map_width() && y < map_height()) { // Current (i,j) in the map |
nasiromar | 10:e18685911e84 | 363 | MapItem* curr_item = get_here(x, y); |
nasiromar | 10:e18685911e84 | 364 | MapItem* prev_item = get_here(px, py); |
nasiromar | 10:e18685911e84 | 365 | if (init || curr_item != prev_item) { // Only draw if they're different |
nasiromar | 10:e18685911e84 | 366 | if (curr_item) { // There's something here! Draw it |
nasiromar | 10:e18685911e84 | 367 | draw = curr_item->draw; |
nasiromar | 10:e18685911e84 | 368 | } else { // There used to be something, but now there isn't |
nasiromar | 10:e18685911e84 | 369 | draw = draw_nothing; |
nasiromar | 10:e18685911e84 | 370 | } |
nasiromar | 10:e18685911e84 | 371 | } |
nasiromar | 10:e18685911e84 | 372 | } else if (init) { // If doing a full draw, but we're out of bounds, draw the walls. |
nasiromar | 10:e18685911e84 | 373 | draw = draw_wall; |
nasiromar | 9:cbb9cfb1f6c5 | 374 | } |
nasiromar | 9:cbb9cfb1f6c5 | 375 | |
nasiromar | 10:e18685911e84 | 376 | // Actually draw the tile |
nasiromar | 10:e18685911e84 | 377 | if (draw) draw(u, v); |
nasiromar | 10:e18685911e84 | 378 | } |
rconnorlawson | 0:35660d7952f7 | 379 | } |
rconnorlawson | 0:35660d7952f7 | 380 | |
nasiromar | 10:e18685911e84 | 381 | // Draw status bars |
nasiromar | 14:7225da81314a | 382 | draw_upper_status(Player.x,Player.y,Player.money); |
nasiromar | 10:e18685911e84 | 383 | draw_lower_status(Player.HP,Player.MP); |
nasiromar | 10:e18685911e84 | 384 | } |
nasiromar | 9:cbb9cfb1f6c5 | 385 | |
nasiromar | 9:cbb9cfb1f6c5 | 386 | |
nasiromar | 10:e18685911e84 | 387 | /** |
nasiromar | 10:e18685911e84 | 388 | * Initialize the main world map. Add walls around the edges, interior chambers, |
nasiromar | 10:e18685911e84 | 389 | * and plants in the background so you can see motion. Note: using the similar |
nasiromar | 10:e18685911e84 | 390 | * procedure you can init the secondary map(s). |
nasiromar | 10:e18685911e84 | 391 | */ |
nasiromar | 10:e18685911e84 | 392 | void init_main_map() |
nasiromar | 10:e18685911e84 | 393 | { |
nasiromar | 10:e18685911e84 | 394 | // "Random" plants |
nasiromar | 10:e18685911e84 | 395 | Map* map = set_active_map(0); |
nasiromar | 11:6cd02a8539d1 | 396 | for(int i = map_width() + 9; i < map_area(); i += 39) { |
nasiromar | 10:e18685911e84 | 397 | add_plant(i % map_width(), i / map_width()); |
nasiromar | 10:e18685911e84 | 398 | } |
nasiromar | 10:e18685911e84 | 399 | pc.printf("plants\r\n"); |
nasiromar | 9:cbb9cfb1f6c5 | 400 | |
nasiromar | 10:e18685911e84 | 401 | pc.printf("Adding walls!\r\n"); |
nasiromar | 10:e18685911e84 | 402 | add_wall(0, 0, HORIZONTAL, map_width()); |
nasiromar | 10:e18685911e84 | 403 | add_wall(0, map_height()-1, HORIZONTAL, map_width()); |
nasiromar | 10:e18685911e84 | 404 | add_wall(0, 0, VERTICAL, map_height()); |
nasiromar | 10:e18685911e84 | 405 | add_wall(map_width()-1, 0, VERTICAL, map_height()); |
nasiromar | 10:e18685911e84 | 406 | pc.printf("Walls done!\r\n"); |
nasiromar | 10:e18685911e84 | 407 | |
nasiromar | 10:e18685911e84 | 408 | |
nasiromar | 10:e18685911e84 | 409 | |
nasiromar | 10:e18685911e84 | 410 | for(int i = 7; i < 13; i++) { |
nasiromar | 10:e18685911e84 | 411 | for (int j = 23; j <31; j++) { |
nasiromar | 10:e18685911e84 | 412 | add_kindom(j,i); |
nasiromar | 9:cbb9cfb1f6c5 | 413 | } |
nasiromar | 9:cbb9cfb1f6c5 | 414 | } |
rconnorlawson | 0:35660d7952f7 | 415 | |
nasiromar | 14:7225da81314a | 416 | add_chest2(20,20); |
nasiromar | 14:7225da81314a | 417 | add_chest2(5,8); |
nasiromar | 14:7225da81314a | 418 | add_chest2(33,20); |
nasiromar | 14:7225da81314a | 419 | add_chest2(14,16); |
nasiromar | 14:7225da81314a | 420 | add_chest2(40,15); |
nasiromar | 10:e18685911e84 | 421 | |
nasiromar | 11:6cd02a8539d1 | 422 | add_npc(9,9); |
nasiromar | 11:6cd02a8539d1 | 423 | |
nasiromar | 11:6cd02a8539d1 | 424 | add_npc2(30,30); |
nasiromar | 11:6cd02a8539d1 | 425 | |
nasiromar | 14:7225da81314a | 426 | add_npc2(10,10); |
nasiromar | 14:7225da81314a | 427 | |
nasiromar | 14:7225da81314a | 428 | add_npc2(20,9); |
nasiromar | 14:7225da81314a | 429 | |
nasiromar | 13:798a4dd14c7e | 430 | add_merch(12,16); |
nasiromar | 11:6cd02a8539d1 | 431 | |
nasiromar | 14:7225da81314a | 432 | for(int i = 11; i <= 13; i++){ |
nasiromar | 14:7225da81314a | 433 | add_store(i,15); |
nasiromar | 14:7225da81314a | 434 | } |
nasiromar | 14:7225da81314a | 435 | |
nasiromar | 14:7225da81314a | 436 | add_fire(27,13); |
nasiromar | 14:7225da81314a | 437 | add_fire(25,13); |
nasiromar | 14:7225da81314a | 438 | |
nasiromar | 10:e18685911e84 | 439 | add_door(26,12); |
nasiromar | 10:e18685911e84 | 440 | |
nasiromar | 10:e18685911e84 | 441 | add_portal(20,35); |
nasiromar | 10:e18685911e84 | 442 | |
nasiromar | 10:e18685911e84 | 443 | print_map(); |
nasiromar | 10:e18685911e84 | 444 | } |
nasiromar | 10:e18685911e84 | 445 | |
nasiromar | 10:e18685911e84 | 446 | /** |
nasiromar | 10:e18685911e84 | 447 | * Program entry point! This is where it all begins. |
nasiromar | 10:e18685911e84 | 448 | * This function orchestrates all the parts of the game. Most of your |
nasiromar | 10:e18685911e84 | 449 | * implementation should be elsewhere - this holds the game loop, and should |
nasiromar | 10:e18685911e84 | 450 | * read like a road map for the rest of the code. |
nasiromar | 10:e18685911e84 | 451 | */ |
nasiromar | 10:e18685911e84 | 452 | int main() |
nasiromar | 10:e18685911e84 | 453 | { |
nasiromar | 10:e18685911e84 | 454 | |
nasiromar | 10:e18685911e84 | 455 | Player.HP = 100; |
nasiromar | 10:e18685911e84 | 456 | Player.MP = 100; |
nasiromar | 10:e18685911e84 | 457 | |
nasiromar | 10:e18685911e84 | 458 | // MapItem* north = get_north(Player.x,Player.y); |
rconnorlawson | 0:35660d7952f7 | 459 | |
nasiromar | 6:c9695079521d | 460 | |
nasiromar | 10:e18685911e84 | 461 | // First things first: initialize hardware |
nasiromar | 10:e18685911e84 | 462 | ASSERT_P(hardware_init() == ERROR_NONE, "Hardware init failed!"); |
nasiromar | 9:cbb9cfb1f6c5 | 463 | |
nasiromar | 10:e18685911e84 | 464 | // Initialize the maps |
nasiromar | 10:e18685911e84 | 465 | maps_init(); |
nasiromar | 10:e18685911e84 | 466 | init_main_map(); |
nasiromar | 11:6cd02a8539d1 | 467 | init_dung(); |
nasiromar | 11:6cd02a8539d1 | 468 | |
nasiromar | 12:116a4cc85b16 | 469 | |
nasiromar | 10:e18685911e84 | 470 | |
nasiromar | 10:e18685911e84 | 471 | // Initialize game state |
nasiromar | 10:e18685911e84 | 472 | set_active_map(0); |
nasiromar | 10:e18685911e84 | 473 | Player.x = Player.y = 5; |
nasiromar | 14:7225da81314a | 474 | Player.money = 0; |
nasiromar | 12:116a4cc85b16 | 475 | Player.spell = NULL; |
nasiromar | 14:7225da81314a | 476 | |
nasiromar | 10:e18685911e84 | 477 | |
nasiromar | 10:e18685911e84 | 478 | draw_start(); |
nasiromar | 10:e18685911e84 | 479 | |
nasiromar | 12:116a4cc85b16 | 480 | //wave_file = fopen("/sd/godfather.wav","r"); |
nasiromar | 12:116a4cc85b16 | 481 | //waver.play(wave_file); |
nasiromar | 12:116a4cc85b16 | 482 | //fclose(wave_file); |
nasiromar | 10:e18685911e84 | 483 | |
nasiromar | 10:e18685911e84 | 484 | while(1) { |
nasiromar | 10:e18685911e84 | 485 | |
nasiromar | 10:e18685911e84 | 486 | Timer t; |
nasiromar | 10:e18685911e84 | 487 | t.start(); |
nasiromar | 10:e18685911e84 | 488 | GameInputs inp = read_inputs(); |
nasiromar | 10:e18685911e84 | 489 | int action = get_action(inp); |
nasiromar | 10:e18685911e84 | 490 | |
nasiromar | 10:e18685911e84 | 491 | |
nasiromar | 10:e18685911e84 | 492 | if( action == ACTION_BUTTON) { |
nasiromar | 10:e18685911e84 | 493 | |
nasiromar | 10:e18685911e84 | 494 | break; |
nasiromar | 9:cbb9cfb1f6c5 | 495 | } |
nasiromar | 10:e18685911e84 | 496 | t.stop(); |
nasiromar | 10:e18685911e84 | 497 | int dt = t.read_ms(); |
nasiromar | 10:e18685911e84 | 498 | if (dt < 100) wait_ms(100-dt); |
nasiromar | 9:cbb9cfb1f6c5 | 499 | |
nasiromar | 9:cbb9cfb1f6c5 | 500 | } |
nasiromar | 9:cbb9cfb1f6c5 | 501 | |
nasiromar | 6:c9695079521d | 502 | |
nasiromar | 10:e18685911e84 | 503 | // Initial drawing |
nasiromar | 10:e18685911e84 | 504 | draw_game(true); |
nasiromar | 10:e18685911e84 | 505 | // Main game loop |
nasiromar | 10:e18685911e84 | 506 | while(1) { |
nasiromar | 10:e18685911e84 | 507 | // Timer to measure game update speed |
nasiromar | 10:e18685911e84 | 508 | Timer t; |
nasiromar | 10:e18685911e84 | 509 | t.start(); |
nasiromar | 6:c9695079521d | 510 | |
nasiromar | 10:e18685911e84 | 511 | // Actually do the game update: |
nasiromar | 10:e18685911e84 | 512 | // 1. Read inputs |
nasiromar | 10:e18685911e84 | 513 | GameInputs input = read_inputs(); |
nasiromar | 10:e18685911e84 | 514 | // 2. Determine action (get_action) |
nasiromar | 10:e18685911e84 | 515 | int action = get_action(input); |
nasiromar | 10:e18685911e84 | 516 | // 3. Update game (update_game) |
nasiromar | 10:e18685911e84 | 517 | update_game(action); |
nasiromar | 10:e18685911e84 | 518 | // 3b. Check for game over |
nasiromar | 14:7225da81314a | 519 | if(Player.won == 1 || Player.HP <= 0) { |
nasiromar | 10:e18685911e84 | 520 | draw_gameover(); |
nasiromar | 10:e18685911e84 | 521 | break; |
nasiromar | 10:e18685911e84 | 522 | } |
nasiromar | 10:e18685911e84 | 523 | // 4. Draw frame (draw_game) |
nasiromar | 10:e18685911e84 | 524 | draw_game(true); |
nasiromar | 9:cbb9cfb1f6c5 | 525 | |
nasiromar | 10:e18685911e84 | 526 | // 5. Frame delay |
nasiromar | 10:e18685911e84 | 527 | t.stop(); |
nasiromar | 10:e18685911e84 | 528 | int dt = t.read_ms(); |
nasiromar | 10:e18685911e84 | 529 | if (dt < 100) wait_ms(100 - dt); |
nasiromar | 10:e18685911e84 | 530 | } |
nasiromar | 10:e18685911e84 | 531 | // |
nasiromar | 9:cbb9cfb1f6c5 | 532 | |
nasiromar | 9:cbb9cfb1f6c5 | 533 | |
nasiromar | 10:e18685911e84 | 534 | } |