Finished V1
Dependencies: mbed wave_player 4DGL-uLCD-SE MMA8452
main.cpp@4:2297a714936f, 2019-11-19 (annotated)
- Committer:
- trich9
- Date:
- Tue Nov 19 16:53:47 2019 +0000
- Revision:
- 4:2297a714936f
- Parent:
- 3:e4690ad5a4d2
- Child:
- 5:2fb023cdc666
11/19/2019;
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" |
rconnorlawson | 0:35660d7952f7 | 7 | |
rconnorlawson | 0:35660d7952f7 | 8 | // Functions in this file |
rconnorlawson | 0:35660d7952f7 | 9 | int get_action (GameInputs inputs); |
rconnorlawson | 0:35660d7952f7 | 10 | int update_game (int action); |
rconnorlawson | 0:35660d7952f7 | 11 | void draw_game (int init); |
rconnorlawson | 0:35660d7952f7 | 12 | void init_main_map (); |
rconnorlawson | 0:35660d7952f7 | 13 | int main (); |
rconnorlawson | 0:35660d7952f7 | 14 | |
trich9 | 4:2297a714936f | 15 | |
trich9 | 4:2297a714936f | 16 | //MYCODE |
trich9 | 4:2297a714936f | 17 | GameInputs gInputs; |
trich9 | 4:2297a714936f | 18 | int action; |
trich9 | 4:2297a714936f | 19 | int gUpdate; |
trich9 | 4:2297a714936f | 20 | int omnipotent; |
trich9 | 4:2297a714936f | 21 | |
trich9 | 4:2297a714936f | 22 | int dialogueNumNPC = 0; |
trich9 | 4:2297a714936f | 23 | //animationstuff |
trich9 | 4:2297a714936f | 24 | int updateCount = 0; |
trich9 | 4:2297a714936f | 25 | |
trich9 | 4:2297a714936f | 26 | //NPC |
trich9 | 4:2297a714936f | 27 | int NPCx = 6; |
trich9 | 4:2297a714936f | 28 | int NPCy = 5; |
trich9 | 4:2297a714936f | 29 | int walkCycleNPC = 0; |
trich9 | 4:2297a714936f | 30 | |
trich9 | 4:2297a714936f | 31 | int go_up(int x, int y); |
trich9 | 4:2297a714936f | 32 | int go_down(int x, int y); |
trich9 | 4:2297a714936f | 33 | int go_right(int x, int y); |
trich9 | 4:2297a714936f | 34 | int go_left(int x, int y); |
trich9 | 4:2297a714936f | 35 | int action_button(int x, int y); |
trich9 | 4:2297a714936f | 36 | void init_map2(); |
trich9 | 4:2297a714936f | 37 | void animation_update(); |
trich9 | 4:2297a714936f | 38 | void moveNPC(); |
trich9 | 4:2297a714936f | 39 | |
trich9 | 4:2297a714936f | 40 | |
trich9 | 4:2297a714936f | 41 | |
trich9 | 4:2297a714936f | 42 | |
rconnorlawson | 0:35660d7952f7 | 43 | /** |
rconnorlawson | 0:35660d7952f7 | 44 | * The main game state. Must include Player locations and previous locations for |
rconnorlawson | 0:35660d7952f7 | 45 | * drawing to work properly. Other items can be added as needed. |
rconnorlawson | 0:35660d7952f7 | 46 | */ |
rconnorlawson | 0:35660d7952f7 | 47 | struct { |
rconnorlawson | 0:35660d7952f7 | 48 | int x,y; // Current locations |
rconnorlawson | 0:35660d7952f7 | 49 | int px, py; // Previous locations |
rconnorlawson | 0:35660d7952f7 | 50 | int has_key; |
trich9 | 4:2297a714936f | 51 | |
trich9 | 4:2297a714936f | 52 | //MYCODE |
trich9 | 4:2297a714936f | 53 | //int omnipotent; |
rashidi1saeed | 2:1ca97843a334 | 54 | // You can add other properties for the player here |
rconnorlawson | 0:35660d7952f7 | 55 | } Player; |
rconnorlawson | 0:35660d7952f7 | 56 | |
trich9 | 4:2297a714936f | 57 | |
rconnorlawson | 0:35660d7952f7 | 58 | /** |
rconnorlawson | 0:35660d7952f7 | 59 | * Given the game inputs, determine what kind of update needs to happen. |
rconnorlawson | 0:35660d7952f7 | 60 | * Possbile return values are defined below. |
rconnorlawson | 0:35660d7952f7 | 61 | */ |
rconnorlawson | 0:35660d7952f7 | 62 | #define NO_ACTION 0 |
rconnorlawson | 0:35660d7952f7 | 63 | #define ACTION_BUTTON 1 |
rconnorlawson | 0:35660d7952f7 | 64 | #define MENU_BUTTON 2 |
rconnorlawson | 0:35660d7952f7 | 65 | #define GO_LEFT 3 |
rconnorlawson | 0:35660d7952f7 | 66 | #define GO_RIGHT 4 |
rconnorlawson | 0:35660d7952f7 | 67 | #define GO_UP 5 |
rconnorlawson | 0:35660d7952f7 | 68 | #define GO_DOWN 6 |
trich9 | 4:2297a714936f | 69 | #define ACTION_BUTTON 7 |
rconnorlawson | 0:35660d7952f7 | 70 | int get_action(GameInputs inputs) |
rconnorlawson | 0:35660d7952f7 | 71 | { |
trich9 | 4:2297a714936f | 72 | //MYCODE |
trich9 | 4:2297a714936f | 73 | //accelerometer cases |
trich9 | 4:2297a714936f | 74 | if(inputs.ax >= 0.4){ |
trich9 | 4:2297a714936f | 75 | return GO_RIGHT; |
trich9 | 4:2297a714936f | 76 | } |
trich9 | 4:2297a714936f | 77 | if(inputs.ax < -0.4){ |
trich9 | 4:2297a714936f | 78 | return GO_LEFT; |
trich9 | 4:2297a714936f | 79 | } |
trich9 | 4:2297a714936f | 80 | if(inputs.ay >= 0.4){ |
trich9 | 4:2297a714936f | 81 | return GO_UP; |
trich9 | 4:2297a714936f | 82 | } |
trich9 | 4:2297a714936f | 83 | if(inputs.ay < -0.15){ |
trich9 | 4:2297a714936f | 84 | return GO_DOWN; |
trich9 | 4:2297a714936f | 85 | } |
trich9 | 4:2297a714936f | 86 | |
trich9 | 4:2297a714936f | 87 | if(!inputs.b1){ |
trich9 | 4:2297a714936f | 88 | if(omnipotent != 1){ |
trich9 | 4:2297a714936f | 89 | omnipotent = 1; |
trich9 | 4:2297a714936f | 90 | } |
trich9 | 4:2297a714936f | 91 | else{ |
trich9 | 4:2297a714936f | 92 | omnipotent = 0; |
trich9 | 4:2297a714936f | 93 | } |
trich9 | 4:2297a714936f | 94 | wait(1); |
trich9 | 4:2297a714936f | 95 | } |
trich9 | 4:2297a714936f | 96 | |
trich9 | 4:2297a714936f | 97 | if(!inputs.b3){ |
trich9 | 4:2297a714936f | 98 | return ACTION_BUTTON; |
trich9 | 4:2297a714936f | 99 | } |
trich9 | 4:2297a714936f | 100 | |
trich9 | 4:2297a714936f | 101 | //ENDMYCODE |
rconnorlawson | 0:35660d7952f7 | 102 | return NO_ACTION; |
rconnorlawson | 0:35660d7952f7 | 103 | } |
rconnorlawson | 0:35660d7952f7 | 104 | |
rconnorlawson | 0:35660d7952f7 | 105 | /** |
rconnorlawson | 0:35660d7952f7 | 106 | * Update the game state based on the user action. For example, if the user |
rconnorlawson | 0:35660d7952f7 | 107 | * requests GO_UP, then this function should determine if that is possible by |
rconnorlawson | 0:35660d7952f7 | 108 | * consulting the map, and update the Player position accordingly. |
rconnorlawson | 0:35660d7952f7 | 109 | * |
rconnorlawson | 0:35660d7952f7 | 110 | * Return values are defined below. FULL_DRAW indicates that for this frame, |
rconnorlawson | 0:35660d7952f7 | 111 | * draw_game should not optimize drawing and should draw every tile, even if |
rconnorlawson | 0:35660d7952f7 | 112 | * the player has not moved. |
rconnorlawson | 0:35660d7952f7 | 113 | */ |
rconnorlawson | 0:35660d7952f7 | 114 | #define NO_RESULT 0 |
rconnorlawson | 0:35660d7952f7 | 115 | #define GAME_OVER 1 |
rconnorlawson | 0:35660d7952f7 | 116 | #define FULL_DRAW 2 |
rconnorlawson | 0:35660d7952f7 | 117 | int update_game(int action) |
rconnorlawson | 0:35660d7952f7 | 118 | { |
rconnorlawson | 0:35660d7952f7 | 119 | // Save player previous location before updating |
rconnorlawson | 0:35660d7952f7 | 120 | Player.px = Player.x; |
rconnorlawson | 0:35660d7952f7 | 121 | Player.py = Player.y; |
rconnorlawson | 0:35660d7952f7 | 122 | |
trich9 | 4:2297a714936f | 123 | //MYCODE |
trich9 | 4:2297a714936f | 124 | animation_update(); |
trich9 | 4:2297a714936f | 125 | //ENDMYCODE |
trich9 | 4:2297a714936f | 126 | |
rconnorlawson | 0:35660d7952f7 | 127 | // Do different things based on the each action. |
rconnorlawson | 0:35660d7952f7 | 128 | // You can define functions like "go_up()" that get called for each case. |
rconnorlawson | 0:35660d7952f7 | 129 | switch(action) |
rconnorlawson | 0:35660d7952f7 | 130 | { |
trich9 | 4:2297a714936f | 131 | case GO_UP: |
trich9 | 4:2297a714936f | 132 | //MYCODE |
trich9 | 4:2297a714936f | 133 | if(go_up(Player.px, Player.py)){ |
trich9 | 4:2297a714936f | 134 | return FULL_DRAW; |
trich9 | 4:2297a714936f | 135 | } |
trich9 | 4:2297a714936f | 136 | else{ |
trich9 | 4:2297a714936f | 137 | break; |
trich9 | 4:2297a714936f | 138 | } |
trich9 | 4:2297a714936f | 139 | case GO_LEFT: |
trich9 | 4:2297a714936f | 140 | if(go_left(Player.px, Player.py)){ |
trich9 | 4:2297a714936f | 141 | return FULL_DRAW; |
trich9 | 4:2297a714936f | 142 | } |
trich9 | 4:2297a714936f | 143 | else{ |
trich9 | 4:2297a714936f | 144 | break; |
trich9 | 4:2297a714936f | 145 | } |
trich9 | 4:2297a714936f | 146 | case GO_DOWN: |
trich9 | 4:2297a714936f | 147 | if(go_down(Player.px, Player.py)){ |
trich9 | 4:2297a714936f | 148 | return FULL_DRAW; |
trich9 | 4:2297a714936f | 149 | } |
trich9 | 4:2297a714936f | 150 | else{ |
trich9 | 4:2297a714936f | 151 | break; |
trich9 | 4:2297a714936f | 152 | } |
trich9 | 4:2297a714936f | 153 | case GO_RIGHT: |
trich9 | 4:2297a714936f | 154 | if(go_right(Player.px, Player.py)){ |
trich9 | 4:2297a714936f | 155 | return FULL_DRAW; |
trich9 | 4:2297a714936f | 156 | } |
trich9 | 4:2297a714936f | 157 | else{ |
trich9 | 4:2297a714936f | 158 | break; |
trich9 | 4:2297a714936f | 159 | } |
trich9 | 4:2297a714936f | 160 | case ACTION_BUTTON: |
trich9 | 4:2297a714936f | 161 | if(action_button(Player.px, Player.py)){ |
trich9 | 4:2297a714936f | 162 | return FULL_DRAW; |
trich9 | 4:2297a714936f | 163 | } |
trich9 | 4:2297a714936f | 164 | else{ |
trich9 | 4:2297a714936f | 165 | break; |
trich9 | 4:2297a714936f | 166 | } |
rconnorlawson | 0:35660d7952f7 | 167 | case MENU_BUTTON: break; |
rconnorlawson | 0:35660d7952f7 | 168 | default: break; |
rconnorlawson | 0:35660d7952f7 | 169 | } |
trich9 | 4:2297a714936f | 170 | return FULL_DRAW; |
trich9 | 4:2297a714936f | 171 | } |
trich9 | 4:2297a714936f | 172 | |
trich9 | 4:2297a714936f | 173 | //MYCODE |
trich9 | 4:2297a714936f | 174 | int go_up(int x, int y){ |
trich9 | 4:2297a714936f | 175 | MapItem *northItem = get_north(x, y); |
trich9 | 4:2297a714936f | 176 | if(northItem->walkable || omnipotent){ |
trich9 | 4:2297a714936f | 177 | Player.y--; |
trich9 | 4:2297a714936f | 178 | return 1; |
trich9 | 4:2297a714936f | 179 | } |
trich9 | 4:2297a714936f | 180 | return 0; |
trich9 | 4:2297a714936f | 181 | } |
trich9 | 4:2297a714936f | 182 | int go_down(int x, int y){ |
trich9 | 4:2297a714936f | 183 | MapItem *southItem = get_south(x, y); |
trich9 | 4:2297a714936f | 184 | if(southItem->walkable || omnipotent){ |
trich9 | 4:2297a714936f | 185 | Player.y++; |
trich9 | 4:2297a714936f | 186 | return 1; |
trich9 | 4:2297a714936f | 187 | } |
trich9 | 4:2297a714936f | 188 | return 0; |
trich9 | 4:2297a714936f | 189 | } |
trich9 | 4:2297a714936f | 190 | int go_left(int x, int y){ |
trich9 | 4:2297a714936f | 191 | MapItem *westItem = get_west(x, y); |
trich9 | 4:2297a714936f | 192 | if(westItem->walkable || omnipotent){ |
trich9 | 4:2297a714936f | 193 | Player.x--; |
trich9 | 4:2297a714936f | 194 | return 1; |
trich9 | 4:2297a714936f | 195 | } |
trich9 | 4:2297a714936f | 196 | return 0; |
trich9 | 4:2297a714936f | 197 | } |
trich9 | 4:2297a714936f | 198 | int go_right(int x, int y){ |
trich9 | 4:2297a714936f | 199 | MapItem *eastItem = get_east(x, y); |
trich9 | 4:2297a714936f | 200 | if(eastItem->walkable || omnipotent){ |
trich9 | 4:2297a714936f | 201 | Player.x++; |
trich9 | 4:2297a714936f | 202 | return 1; |
trich9 | 4:2297a714936f | 203 | } |
trich9 | 4:2297a714936f | 204 | return 0; |
trich9 | 4:2297a714936f | 205 | } |
trich9 | 4:2297a714936f | 206 | |
trich9 | 4:2297a714936f | 207 | int action_button(int x, int y){ |
trich9 | 4:2297a714936f | 208 | MapItem *eastItem = get_east(x, y); |
trich9 | 4:2297a714936f | 209 | MapItem *westItem = get_west(x, y); |
trich9 | 4:2297a714936f | 210 | MapItem *southItem = get_south(x, y); |
trich9 | 4:2297a714936f | 211 | MapItem *northItem = get_north(x, y); |
trich9 | 4:2297a714936f | 212 | MapItem *hereItem = get_here(x, y); |
trich9 | 4:2297a714936f | 213 | |
trich9 | 4:2297a714936f | 214 | |
trich9 | 4:2297a714936f | 215 | |
trich9 | 4:2297a714936f | 216 | //eastItem || westItem || northItem || southItem || hereItem |
trich9 | 4:2297a714936f | 217 | if(eastItem -> type == NPC || westItem -> type == NPC || northItem -> type == NPC || southItem -> type == NPC || hereItem -> type == NPC){ |
trich9 | 4:2297a714936f | 218 | |
trich9 | 4:2297a714936f | 219 | if(dialogueNumNPC == 0){ |
trich9 | 4:2297a714936f | 220 | char *talk1 = ""; |
trich9 | 4:2297a714936f | 221 | //1234567890123456789012345678901234567901234567890 |
trich9 | 4:2297a714936f | 222 | char *talk2 = "Go down that ladder above me to get a sonar"; |
trich9 | 4:2297a714936f | 223 | speech(talk1, talk2); |
trich9 | 4:2297a714936f | 224 | add_ladder(6, 3); |
trich9 | 4:2297a714936f | 225 | dialogueNumNPC = 1; |
trich9 | 4:2297a714936f | 226 | |
trich9 | 4:2297a714936f | 227 | } |
trich9 | 4:2297a714936f | 228 | else if(dialogueNumNPC == 1){ |
trich9 | 4:2297a714936f | 229 | char *talk1 = ""; |
trich9 | 4:2297a714936f | 230 | //1234567890123456789012345678901234567901234567890 |
trich9 | 4:2297a714936f | 231 | char *talk2 = "*ahem* I said go down that ladder right above me."; |
trich9 | 4:2297a714936f | 232 | speech(talk1, talk2); |
trich9 | 4:2297a714936f | 233 | } |
trich9 | 4:2297a714936f | 234 | |
trich9 | 4:2297a714936f | 235 | |
trich9 | 4:2297a714936f | 236 | } |
trich9 | 4:2297a714936f | 237 | //ladder interaction |
trich9 | 4:2297a714936f | 238 | if(eastItem -> type == ladder || westItem -> type == ladder || northItem -> type == ladder || southItem -> type == ladder || hereItem -> type == ladder){ |
trich9 | 4:2297a714936f | 239 | //add_ladder(8, 8); |
trich9 | 4:2297a714936f | 240 | init_map2(); |
trich9 | 4:2297a714936f | 241 | } |
trich9 | 4:2297a714936f | 242 | return 1; |
rconnorlawson | 0:35660d7952f7 | 243 | } |
rconnorlawson | 0:35660d7952f7 | 244 | |
rconnorlawson | 0:35660d7952f7 | 245 | /** |
rconnorlawson | 0:35660d7952f7 | 246 | * Entry point for frame drawing. This should be called once per iteration of |
rconnorlawson | 0:35660d7952f7 | 247 | * the game loop. This draws all tiles on the screen, followed by the status |
rconnorlawson | 0:35660d7952f7 | 248 | * bars. Unless init is nonzero, this function will optimize drawing by only |
rconnorlawson | 0:35660d7952f7 | 249 | * drawing tiles that have changed from the previous frame. |
rconnorlawson | 0:35660d7952f7 | 250 | */ |
rconnorlawson | 0:35660d7952f7 | 251 | void draw_game(int init) |
rconnorlawson | 0:35660d7952f7 | 252 | { |
rconnorlawson | 0:35660d7952f7 | 253 | // Draw game border first |
rconnorlawson | 0:35660d7952f7 | 254 | if(init) draw_border(); |
rconnorlawson | 0:35660d7952f7 | 255 | |
rconnorlawson | 0:35660d7952f7 | 256 | // Iterate over all visible map tiles |
rconnorlawson | 0:35660d7952f7 | 257 | for (int i = -5; i <= 5; i++) // Iterate over columns of tiles |
rconnorlawson | 0:35660d7952f7 | 258 | { |
rconnorlawson | 0:35660d7952f7 | 259 | for (int j = -4; j <= 4; j++) // Iterate over one column of tiles |
rconnorlawson | 0:35660d7952f7 | 260 | { |
rconnorlawson | 0:35660d7952f7 | 261 | // Here, we have a given (i,j) |
rconnorlawson | 0:35660d7952f7 | 262 | |
rconnorlawson | 0:35660d7952f7 | 263 | // Compute the current map (x,y) of this tile |
rconnorlawson | 0:35660d7952f7 | 264 | int x = i + Player.x; |
rconnorlawson | 0:35660d7952f7 | 265 | int y = j + Player.y; |
rconnorlawson | 0:35660d7952f7 | 266 | |
rconnorlawson | 0:35660d7952f7 | 267 | // Compute the previous map (px, py) of this tile |
rconnorlawson | 0:35660d7952f7 | 268 | int px = i + Player.px; |
rconnorlawson | 0:35660d7952f7 | 269 | int py = j + Player.py; |
rconnorlawson | 0:35660d7952f7 | 270 | |
rconnorlawson | 0:35660d7952f7 | 271 | // Compute u,v coordinates for drawing |
rconnorlawson | 0:35660d7952f7 | 272 | int u = (i+5)*11 + 3; |
rconnorlawson | 0:35660d7952f7 | 273 | int v = (j+4)*11 + 15; |
rconnorlawson | 0:35660d7952f7 | 274 | |
rconnorlawson | 0:35660d7952f7 | 275 | // Figure out what to draw |
rconnorlawson | 0:35660d7952f7 | 276 | DrawFunc draw = NULL; |
rconnorlawson | 0:35660d7952f7 | 277 | if (init && i == 0 && j == 0) // Only draw the player on init |
rconnorlawson | 0:35660d7952f7 | 278 | { |
rconnorlawson | 0:35660d7952f7 | 279 | draw_player(u, v, Player.has_key); |
rconnorlawson | 0:35660d7952f7 | 280 | continue; |
rconnorlawson | 0:35660d7952f7 | 281 | } |
rconnorlawson | 0:35660d7952f7 | 282 | else if (x >= 0 && y >= 0 && x < map_width() && y < map_height()) // Current (i,j) in the map |
rconnorlawson | 0:35660d7952f7 | 283 | { |
rconnorlawson | 0:35660d7952f7 | 284 | MapItem* curr_item = get_here(x, y); |
rconnorlawson | 0:35660d7952f7 | 285 | MapItem* prev_item = get_here(px, py); |
rconnorlawson | 0:35660d7952f7 | 286 | if (init || curr_item != prev_item) // Only draw if they're different |
rconnorlawson | 0:35660d7952f7 | 287 | { |
rconnorlawson | 0:35660d7952f7 | 288 | if (curr_item) // There's something here! Draw it |
rconnorlawson | 0:35660d7952f7 | 289 | { |
rconnorlawson | 0:35660d7952f7 | 290 | draw = curr_item->draw; |
rconnorlawson | 0:35660d7952f7 | 291 | } |
rconnorlawson | 0:35660d7952f7 | 292 | else // There used to be something, but now there isn't |
rconnorlawson | 0:35660d7952f7 | 293 | { |
rconnorlawson | 0:35660d7952f7 | 294 | draw = draw_nothing; |
rconnorlawson | 0:35660d7952f7 | 295 | } |
rconnorlawson | 0:35660d7952f7 | 296 | } |
rconnorlawson | 0:35660d7952f7 | 297 | } |
rconnorlawson | 0:35660d7952f7 | 298 | else if (init) // If doing a full draw, but we're out of bounds, draw the walls. |
rconnorlawson | 0:35660d7952f7 | 299 | { |
rconnorlawson | 0:35660d7952f7 | 300 | draw = draw_wall; |
rconnorlawson | 0:35660d7952f7 | 301 | } |
rconnorlawson | 0:35660d7952f7 | 302 | |
rconnorlawson | 0:35660d7952f7 | 303 | // Actually draw the tile |
rconnorlawson | 0:35660d7952f7 | 304 | if (draw) draw(u, v); |
rconnorlawson | 0:35660d7952f7 | 305 | } |
rconnorlawson | 0:35660d7952f7 | 306 | } |
rconnorlawson | 0:35660d7952f7 | 307 | |
trich9 | 4:2297a714936f | 308 | // Draw status bars |
trich9 | 4:2297a714936f | 309 | if(Player.x != Player.px || Player.y != Player.py){ |
trich9 | 4:2297a714936f | 310 | draw_upper_status(Player.x, Player.y); |
trich9 | 4:2297a714936f | 311 | } |
trich9 | 4:2297a714936f | 312 | |
rconnorlawson | 0:35660d7952f7 | 313 | draw_lower_status(); |
rconnorlawson | 0:35660d7952f7 | 314 | } |
rconnorlawson | 0:35660d7952f7 | 315 | |
rconnorlawson | 0:35660d7952f7 | 316 | |
rconnorlawson | 0:35660d7952f7 | 317 | /** |
rconnorlawson | 0:35660d7952f7 | 318 | * Initialize the main world map. Add walls around the edges, interior chambers, |
rashidi1saeed | 2:1ca97843a334 | 319 | * and plants in the background so you can see motion. Note: using the similar |
rashidi1saeed | 3:e4690ad5a4d2 | 320 | * procedure you can init the secondary map(s). |
rconnorlawson | 0:35660d7952f7 | 321 | */ |
rconnorlawson | 0:35660d7952f7 | 322 | void init_main_map() |
rconnorlawson | 0:35660d7952f7 | 323 | { |
rconnorlawson | 0:35660d7952f7 | 324 | // "Random" plants |
rconnorlawson | 0:35660d7952f7 | 325 | Map* map = set_active_map(0); |
rconnorlawson | 0:35660d7952f7 | 326 | for(int i = map_width() + 3; i < map_area(); i += 39) |
rconnorlawson | 0:35660d7952f7 | 327 | { |
rconnorlawson | 0:35660d7952f7 | 328 | add_plant(i % map_width(), i / map_width()); |
rconnorlawson | 0:35660d7952f7 | 329 | } |
rconnorlawson | 0:35660d7952f7 | 330 | pc.printf("plants\r\n"); |
rconnorlawson | 0:35660d7952f7 | 331 | |
rconnorlawson | 0:35660d7952f7 | 332 | pc.printf("Adding walls!\r\n"); |
rconnorlawson | 0:35660d7952f7 | 333 | add_wall(0, 0, HORIZONTAL, map_width()); |
rconnorlawson | 0:35660d7952f7 | 334 | add_wall(0, map_height()-1, HORIZONTAL, map_width()); |
rconnorlawson | 0:35660d7952f7 | 335 | add_wall(0, 0, VERTICAL, map_height()); |
rconnorlawson | 0:35660d7952f7 | 336 | add_wall(map_width()-1, 0, VERTICAL, map_height()); |
rconnorlawson | 0:35660d7952f7 | 337 | pc.printf("Walls done!\r\n"); |
trich9 | 4:2297a714936f | 338 | |
trich9 | 4:2297a714936f | 339 | |
trich9 | 4:2297a714936f | 340 | //MYCODE |
trich9 | 4:2297a714936f | 341 | add_NPC(6, 5); |
rconnorlawson | 0:35660d7952f7 | 342 | print_map(); |
rconnorlawson | 0:35660d7952f7 | 343 | } |
rconnorlawson | 0:35660d7952f7 | 344 | |
rconnorlawson | 0:35660d7952f7 | 345 | /** |
rconnorlawson | 0:35660d7952f7 | 346 | * Program entry point! This is where it all begins. |
rconnorlawson | 0:35660d7952f7 | 347 | * This function orchestrates all the parts of the game. Most of your |
rconnorlawson | 0:35660d7952f7 | 348 | * implementation should be elsewhere - this holds the game loop, and should |
rconnorlawson | 0:35660d7952f7 | 349 | * read like a road map for the rest of the code. |
rconnorlawson | 0:35660d7952f7 | 350 | */ |
rconnorlawson | 0:35660d7952f7 | 351 | int main() |
rconnorlawson | 0:35660d7952f7 | 352 | { |
rconnorlawson | 0:35660d7952f7 | 353 | // First things first: initialize hardware |
rconnorlawson | 0:35660d7952f7 | 354 | ASSERT_P(hardware_init() == ERROR_NONE, "Hardware init failed!"); |
rconnorlawson | 0:35660d7952f7 | 355 | |
rconnorlawson | 0:35660d7952f7 | 356 | // Initialize the maps |
rconnorlawson | 0:35660d7952f7 | 357 | maps_init(); |
rconnorlawson | 0:35660d7952f7 | 358 | init_main_map(); |
rconnorlawson | 0:35660d7952f7 | 359 | |
rconnorlawson | 0:35660d7952f7 | 360 | // Initialize game state |
rconnorlawson | 0:35660d7952f7 | 361 | set_active_map(0); |
rconnorlawson | 0:35660d7952f7 | 362 | Player.x = Player.y = 5; |
trich9 | 4:2297a714936f | 363 | |
trich9 | 4:2297a714936f | 364 | //MYCODE |
trich9 | 4:2297a714936f | 365 | omnipotent = 0; |
trich9 | 4:2297a714936f | 366 | |
rconnorlawson | 0:35660d7952f7 | 367 | // Initial drawing |
rconnorlawson | 0:35660d7952f7 | 368 | draw_game(true); |
rconnorlawson | 0:35660d7952f7 | 369 | |
rconnorlawson | 0:35660d7952f7 | 370 | // Main game loop |
rconnorlawson | 0:35660d7952f7 | 371 | while(1) |
rconnorlawson | 0:35660d7952f7 | 372 | { |
rconnorlawson | 0:35660d7952f7 | 373 | // Timer to measure game update speed |
rconnorlawson | 0:35660d7952f7 | 374 | Timer t; t.start(); |
rconnorlawson | 0:35660d7952f7 | 375 | |
rconnorlawson | 0:35660d7952f7 | 376 | // Actuall do the game update: |
trich9 | 4:2297a714936f | 377 | // 1. Read inputs |
trich9 | 4:2297a714936f | 378 | //MYCODE |
trich9 | 4:2297a714936f | 379 | gInputs = read_inputs(); |
trich9 | 4:2297a714936f | 380 | |
trich9 | 4:2297a714936f | 381 | // 2. Determine action (get_action) |
trich9 | 4:2297a714936f | 382 | //MYCODE |
trich9 | 4:2297a714936f | 383 | action = get_action(gInputs); |
trich9 | 4:2297a714936f | 384 | |
rconnorlawson | 0:35660d7952f7 | 385 | // 3. Update game (update_game) |
trich9 | 4:2297a714936f | 386 | //MYCODE |
trich9 | 4:2297a714936f | 387 | gUpdate = update_game(action); |
rconnorlawson | 0:35660d7952f7 | 388 | // 3b. Check for game over |
trich9 | 4:2297a714936f | 389 | |
trich9 | 4:2297a714936f | 390 | |
rconnorlawson | 0:35660d7952f7 | 391 | // 4. Draw frame (draw_game) |
trich9 | 4:2297a714936f | 392 | draw_game(gUpdate); |
rconnorlawson | 0:35660d7952f7 | 393 | |
rconnorlawson | 0:35660d7952f7 | 394 | // 5. Frame delay |
rconnorlawson | 0:35660d7952f7 | 395 | t.stop(); |
rconnorlawson | 0:35660d7952f7 | 396 | int dt = t.read_ms(); |
rconnorlawson | 0:35660d7952f7 | 397 | if (dt < 100) wait_ms(100 - dt); |
rconnorlawson | 0:35660d7952f7 | 398 | } |
rconnorlawson | 0:35660d7952f7 | 399 | } |
trich9 | 4:2297a714936f | 400 | |
trich9 | 4:2297a714936f | 401 | //MYCODE |
trich9 | 4:2297a714936f | 402 | void init_map2() |
trich9 | 4:2297a714936f | 403 | { |
trich9 | 4:2297a714936f | 404 | Map* map = set_active_map(1); |
trich9 | 4:2297a714936f | 405 | |
trich9 | 4:2297a714936f | 406 | Player.x = 2; |
trich9 | 4:2297a714936f | 407 | Player.y = 2; |
trich9 | 4:2297a714936f | 408 | |
trich9 | 4:2297a714936f | 409 | pc.printf("Adding walls!\r\n"); |
trich9 | 4:2297a714936f | 410 | add_wall(0, 0, HORIZONTAL, map_width()); |
trich9 | 4:2297a714936f | 411 | add_wall(0, map_height()-1, HORIZONTAL, map_width()); |
trich9 | 4:2297a714936f | 412 | add_wall(0, 0, VERTICAL, map_height()); |
trich9 | 4:2297a714936f | 413 | add_wall(map_width()-1, 0, VERTICAL, map_height()); |
trich9 | 4:2297a714936f | 414 | pc.printf("Walls done!\r\n"); |
trich9 | 4:2297a714936f | 415 | |
trich9 | 4:2297a714936f | 416 | |
trich9 | 4:2297a714936f | 417 | print_map(); |
trich9 | 4:2297a714936f | 418 | } |
trich9 | 4:2297a714936f | 419 | |
trich9 | 4:2297a714936f | 420 | //once every 10 in game cycles the aniamtions and npc movements will happen |
trich9 | 4:2297a714936f | 421 | void animation_update(){ |
trich9 | 4:2297a714936f | 422 | updateCount++; |
trich9 | 4:2297a714936f | 423 | if(updateCount > 9){ |
trich9 | 4:2297a714936f | 424 | moveNPC(); |
trich9 | 4:2297a714936f | 425 | updateCount = 0; |
trich9 | 4:2297a714936f | 426 | } |
trich9 | 4:2297a714936f | 427 | } |
trich9 | 4:2297a714936f | 428 | |
trich9 | 4:2297a714936f | 429 | void moveNPC(){ |
trich9 | 4:2297a714936f | 430 | if(map_width() == 75){ |
trich9 | 4:2297a714936f | 431 | map_erase(NPCx, NPCy); |
trich9 | 4:2297a714936f | 432 | //walkCycleNPC = 0 => go right |
trich9 | 4:2297a714936f | 433 | //wCNPC = 1 => go left |
trich9 | 4:2297a714936f | 434 | if(NPCx < 10 && !walkCycleNPC) NPCx++; |
trich9 | 4:2297a714936f | 435 | else if(NPCx > 2) NPCx--; |
trich9 | 4:2297a714936f | 436 | |
trich9 | 4:2297a714936f | 437 | if(NPCx == 10) walkCycleNPC = 1; |
trich9 | 4:2297a714936f | 438 | else if(NPCx == 2) walkCycleNPC = 0; |
trich9 | 4:2297a714936f | 439 | |
trich9 | 4:2297a714936f | 440 | add_NPC(NPCx, NPCy); |
trich9 | 4:2297a714936f | 441 | } |
trich9 | 4:2297a714936f | 442 | |
trich9 | 4:2297a714936f | 443 | } |