ECE 2035 final project
Dependencies: mbed wave_player 4DGL-uLCD-SE MMA8452
main.cpp@2:22d36e7740f1, 2019-04-15 (annotated)
- Committer:
- npatel387
- Date:
- Mon Apr 15 12:25:08 2019 +0000
- Revision:
- 2:22d36e7740f1
- Parent:
- 0:35660d7952f7
final;
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 | |
npatel387 | 2:22d36e7740f1 | 8 | int omnipotent = 0; |
npatel387 | 2:22d36e7740f1 | 9 | int mapToggle = 0; |
npatel387 | 2:22d36e7740f1 | 10 | int finished = 0; |
npatel387 | 2:22d36e7740f1 | 11 | int walk = 0; |
npatel387 | 2:22d36e7740f1 | 12 | |
rconnorlawson | 0:35660d7952f7 | 13 | // Functions in this file |
rconnorlawson | 0:35660d7952f7 | 14 | int get_action (GameInputs inputs); |
rconnorlawson | 0:35660d7952f7 | 15 | int update_game (int action); |
rconnorlawson | 0:35660d7952f7 | 16 | void draw_game (int init); |
rconnorlawson | 0:35660d7952f7 | 17 | void init_main_map (); |
npatel387 | 2:22d36e7740f1 | 18 | void init_second_map(); |
npatel387 | 2:22d36e7740f1 | 19 | int go_up(int x, int y, int i); |
npatel387 | 2:22d36e7740f1 | 20 | int go_down(int x, int y, int i); |
npatel387 | 2:22d36e7740f1 | 21 | int go_right(int x, int y, int i); |
npatel387 | 2:22d36e7740f1 | 22 | int go_left(int x, int y, int i); |
npatel387 | 2:22d36e7740f1 | 23 | int interact(int x, int y); |
npatel387 | 2:22d36e7740f1 | 24 | int NPC_move(int x, int y); |
rconnorlawson | 0:35660d7952f7 | 25 | int main (); |
rconnorlawson | 0:35660d7952f7 | 26 | |
rconnorlawson | 0:35660d7952f7 | 27 | /** |
rconnorlawson | 0:35660d7952f7 | 28 | * The main game state. Must include Player locations and previous locations for |
rconnorlawson | 0:35660d7952f7 | 29 | * drawing to work properly. Other items can be added as needed. |
rconnorlawson | 0:35660d7952f7 | 30 | */ |
rconnorlawson | 0:35660d7952f7 | 31 | struct { |
rconnorlawson | 0:35660d7952f7 | 32 | int x,y; // Current locations |
rconnorlawson | 0:35660d7952f7 | 33 | int px, py; // Previous locations |
rconnorlawson | 0:35660d7952f7 | 34 | int has_key; |
npatel387 | 2:22d36e7740f1 | 35 | int omnipotent; |
npatel387 | 2:22d36e7740f1 | 36 | int has_sword; |
npatel387 | 2:22d36e7740f1 | 37 | int has_shield; |
npatel387 | 2:22d36e7740f1 | 38 | int has_platebody; |
npatel387 | 2:22d36e7740f1 | 39 | int health; |
npatel387 | 2:22d36e7740f1 | 40 | int num_phats; |
rconnorlawson | 0:35660d7952f7 | 41 | } Player; |
rconnorlawson | 0:35660d7952f7 | 42 | |
rconnorlawson | 0:35660d7952f7 | 43 | /** |
rconnorlawson | 0:35660d7952f7 | 44 | * Given the game inputs, determine what kind of update needs to happen. |
rconnorlawson | 0:35660d7952f7 | 45 | * Possbile return values are defined below. |
rconnorlawson | 0:35660d7952f7 | 46 | */ |
rconnorlawson | 0:35660d7952f7 | 47 | #define NO_ACTION 0 |
rconnorlawson | 0:35660d7952f7 | 48 | #define ACTION_BUTTON 1 |
rconnorlawson | 0:35660d7952f7 | 49 | #define MENU_BUTTON 2 |
npatel387 | 2:22d36e7740f1 | 50 | #define OMNIPOTENT 7 |
rconnorlawson | 0:35660d7952f7 | 51 | #define GO_LEFT 3 |
npatel387 | 2:22d36e7740f1 | 52 | #define GO_LEFT2 10 |
rconnorlawson | 0:35660d7952f7 | 53 | #define GO_RIGHT 4 |
npatel387 | 2:22d36e7740f1 | 54 | #define GO_RIGHT2 11 |
rconnorlawson | 0:35660d7952f7 | 55 | #define GO_UP 5 |
npatel387 | 2:22d36e7740f1 | 56 | #define GO_UP2 12 |
rconnorlawson | 0:35660d7952f7 | 57 | #define GO_DOWN 6 |
npatel387 | 2:22d36e7740f1 | 58 | #define GO_DOWN2 13 |
npatel387 | 2:22d36e7740f1 | 59 | #define BAD_END 8 |
npatel387 | 2:22d36e7740f1 | 60 | #define GOOD_END 9 |
npatel387 | 2:22d36e7740f1 | 61 | |
npatel387 | 2:22d36e7740f1 | 62 | char *questStartLine1 = "Wow a knight!what"; |
npatel387 | 2:22d36e7740f1 | 63 | char *questStartLine2 = "perfect timing!"; |
npatel387 | 2:22d36e7740f1 | 64 | char *questStartLine3 = "I beg you,please"; |
npatel387 | 2:22d36e7740f1 | 65 | char *questStartLine4 = "defeat the evil"; |
npatel387 | 2:22d36e7740f1 | 66 | char *questStartLine5 = "dragon Elvarg!"; |
npatel387 | 2:22d36e7740f1 | 67 | char *questStartLine6 = "He resides in a"; |
npatel387 | 2:22d36e7740f1 | 68 | char *questStartLine7 = "cave to the NEast"; |
npatel387 | 2:22d36e7740f1 | 69 | char *questStartLine8 = "You'll need an"; |
npatel387 | 2:22d36e7740f1 | 70 | char *questStartLine9 = "antifire shield"; |
npatel387 | 2:22d36e7740f1 | 71 | char *questStartLine10 = "and Excalibur!"; |
npatel387 | 2:22d36e7740f1 | 72 | char *questStartLine11 = "I hear Excalibur"; |
npatel387 | 2:22d36e7740f1 | 73 | char *questStartLine12 = "can be found in"; |
npatel387 | 2:22d36e7740f1 | 74 | char *questStartLine13 = "the same cave."; |
npatel387 | 2:22d36e7740f1 | 75 | char *questStartLine14 = "It'll be embedded"; |
npatel387 | 2:22d36e7740f1 | 76 | char *questStartLine15 = "in a stone there."; |
npatel387 | 2:22d36e7740f1 | 77 | char *questStartLine16 = "Take my antifire"; |
npatel387 | 2:22d36e7740f1 | 78 | char *questStartLine17 = "shield, man."; |
npatel387 | 2:22d36e7740f1 | 79 | char *questStartLine18 = "If you do defeat"; |
npatel387 | 2:22d36e7740f1 | 80 | char *questStartLine19 = "Elvarg, take"; |
npatel387 | 2:22d36e7740f1 | 81 | char *questStartLine20 = "the key he drops."; |
npatel387 | 2:22d36e7740f1 | 82 | char *questStartLine21 = "It unlocks his"; |
npatel387 | 2:22d36e7740f1 | 83 | char *questStartLine22 = "treasure room"; |
npatel387 | 2:22d36e7740f1 | 84 | char *questStartLine23 = "to the south."; |
npatel387 | 2:22d36e7740f1 | 85 | char *questStartLine24 = "Good luck!"; |
npatel387 | 2:22d36e7740f1 | 86 | char *questStartLine25 = "Come back when"; |
npatel387 | 2:22d36e7740f1 | 87 | char *questStartLine26 = "you're done!"; |
npatel387 | 2:22d36e7740f1 | 88 | |
npatel387 | 2:22d36e7740f1 | 89 | char *questHasShield1 = "Go to the cave,"; |
npatel387 | 2:22d36e7740f1 | 90 | char *questHasShield2 = "find Excalibur,"; |
npatel387 | 2:22d36e7740f1 | 91 | char *questHasShield3 = "and defeat Elvarg"; |
npatel387 | 2:22d36e7740f1 | 92 | |
npatel387 | 2:22d36e7740f1 | 93 | char *questHasSword1 = "Go fight Evlarg!"; |
npatel387 | 2:22d36e7740f1 | 94 | |
npatel387 | 2:22d36e7740f1 | 95 | char *questHasPlatebody1 = "Thank you!!!!"; |
npatel387 | 2:22d36e7740f1 | 96 | char *questHasPlatebody2 = "You have helped"; |
npatel387 | 2:22d36e7740f1 | 97 | char *questHasPlatebody3 = "me out.I am"; |
npatel387 | 2:22d36e7740f1 | 98 | char *questHasPlatebody4 = "very grateful!"; |
npatel387 | 2:22d36e7740f1 | 99 | |
npatel387 | 2:22d36e7740f1 | 100 | char *findSword1 = "You pull and"; |
npatel387 | 2:22d36e7740f1 | 101 | char *findSword2 = "wield Excalibur."; |
npatel387 | 2:22d36e7740f1 | 102 | |
npatel387 | 2:22d36e7740f1 | 103 | char *elvarg1 = "You approach"; |
npatel387 | 2:22d36e7740f1 | 104 | char *elvarg2 = "Elvarg attacks..."; |
npatel387 | 2:22d36e7740f1 | 105 | char *elvarg3a1 = "With no shield,"; |
npatel387 | 2:22d36e7740f1 | 106 | char *elvarg3a2 = "you are burnt to"; |
npatel387 | 2:22d36e7740f1 | 107 | char *elvarg3a3 = "a crisp.(-10 HP)"; |
npatel387 | 2:22d36e7740f1 | 108 | char *elvarg3b1 = "You have no sword"; |
npatel387 | 2:22d36e7740f1 | 109 | char *elvarg3b2 = "Your punches have"; |
npatel387 | 2:22d36e7740f1 | 110 | char *elvarg3b3 = "no effect so you"; |
npatel387 | 2:22d36e7740f1 | 111 | char *elvarg3b4 = "are badly wounded"; |
npatel387 | 2:22d36e7740f1 | 112 | char *elvarg3b5 = "(-9 HP)"; |
npatel387 | 2:22d36e7740f1 | 113 | char *elvarg3c1 = "Using Excalibur &"; |
npatel387 | 2:22d36e7740f1 | 114 | char *elvarg3c2 = "the shield you"; |
npatel387 | 2:22d36e7740f1 | 115 | char *elvarg3c3 = "defeat Elvarg w/"; |
npatel387 | 2:22d36e7740f1 | 116 | char *elvarg3c4 = "only minor wounds"; |
npatel387 | 2:22d36e7740f1 | 117 | char *elvarg3c5 = "(-3 HP).He drops"; |
npatel387 | 2:22d36e7740f1 | 118 | char *elvarg3c6 = "the key to his"; |
npatel387 | 2:22d36e7740f1 | 119 | char *elvarg3c7 = "treasure room!"; |
npatel387 | 2:22d36e7740f1 | 120 | |
npatel387 | 2:22d36e7740f1 | 121 | |
rconnorlawson | 0:35660d7952f7 | 122 | int get_action(GameInputs inputs) |
rconnorlawson | 0:35660d7952f7 | 123 | { |
npatel387 | 2:22d36e7740f1 | 124 | if(Player.health <= 0) |
npatel387 | 2:22d36e7740f1 | 125 | { |
npatel387 | 2:22d36e7740f1 | 126 | return BAD_END; |
npatel387 | 2:22d36e7740f1 | 127 | } |
npatel387 | 2:22d36e7740f1 | 128 | else if(finished) |
npatel387 | 2:22d36e7740f1 | 129 | { |
npatel387 | 2:22d36e7740f1 | 130 | return GOOD_END; |
npatel387 | 2:22d36e7740f1 | 131 | } |
npatel387 | 2:22d36e7740f1 | 132 | else if(!inputs.b1){ |
npatel387 | 2:22d36e7740f1 | 133 | return ACTION_BUTTON; |
npatel387 | 2:22d36e7740f1 | 134 | } |
npatel387 | 2:22d36e7740f1 | 135 | else if(!inputs.b2){ |
npatel387 | 2:22d36e7740f1 | 136 | return OMNIPOTENT; |
npatel387 | 2:22d36e7740f1 | 137 | } |
npatel387 | 2:22d36e7740f1 | 138 | else if(!inputs.b3){ |
npatel387 | 2:22d36e7740f1 | 139 | return MENU_BUTTON; |
npatel387 | 2:22d36e7740f1 | 140 | } |
npatel387 | 2:22d36e7740f1 | 141 | else if(inputs.ax >= .2){ |
npatel387 | 2:22d36e7740f1 | 142 | if(inputs.ax >= .4) |
npatel387 | 2:22d36e7740f1 | 143 | return GO_LEFT2; |
npatel387 | 2:22d36e7740f1 | 144 | else |
npatel387 | 2:22d36e7740f1 | 145 | return GO_LEFT; |
npatel387 | 2:22d36e7740f1 | 146 | } |
npatel387 | 2:22d36e7740f1 | 147 | else if(inputs.ax <= -.3){ |
npatel387 | 2:22d36e7740f1 | 148 | if(inputs.ax <= -.5) |
npatel387 | 2:22d36e7740f1 | 149 | return GO_RIGHT2; |
npatel387 | 2:22d36e7740f1 | 150 | else |
npatel387 | 2:22d36e7740f1 | 151 | return GO_RIGHT; |
npatel387 | 2:22d36e7740f1 | 152 | } |
npatel387 | 2:22d36e7740f1 | 153 | else if(inputs.ay >= .3){ |
npatel387 | 2:22d36e7740f1 | 154 | if(inputs.ay >= .5) |
npatel387 | 2:22d36e7740f1 | 155 | return GO_DOWN2; |
npatel387 | 2:22d36e7740f1 | 156 | else |
npatel387 | 2:22d36e7740f1 | 157 | return GO_DOWN; |
npatel387 | 2:22d36e7740f1 | 158 | } |
npatel387 | 2:22d36e7740f1 | 159 | else if(inputs.ay <= -.2){ |
npatel387 | 2:22d36e7740f1 | 160 | if(inputs.ay <= -.4) |
npatel387 | 2:22d36e7740f1 | 161 | return GO_UP2; |
npatel387 | 2:22d36e7740f1 | 162 | else |
npatel387 | 2:22d36e7740f1 | 163 | return GO_UP; |
npatel387 | 2:22d36e7740f1 | 164 | } |
npatel387 | 2:22d36e7740f1 | 165 | else{ |
npatel387 | 2:22d36e7740f1 | 166 | return NO_ACTION; |
npatel387 | 2:22d36e7740f1 | 167 | } |
rconnorlawson | 0:35660d7952f7 | 168 | } |
rconnorlawson | 0:35660d7952f7 | 169 | |
rconnorlawson | 0:35660d7952f7 | 170 | /** |
rconnorlawson | 0:35660d7952f7 | 171 | * Update the game state based on the user action. For example, if the user |
rconnorlawson | 0:35660d7952f7 | 172 | * requests GO_UP, then this function should determine if that is possible by |
rconnorlawson | 0:35660d7952f7 | 173 | * consulting the map, and update the Player position accordingly. |
rconnorlawson | 0:35660d7952f7 | 174 | * |
rconnorlawson | 0:35660d7952f7 | 175 | * Return values are defined below. FULL_DRAW indicates that for this frame, |
rconnorlawson | 0:35660d7952f7 | 176 | * draw_game should not optimize drawing and should draw every tile, even if |
rconnorlawson | 0:35660d7952f7 | 177 | * the player has not moved. |
rconnorlawson | 0:35660d7952f7 | 178 | */ |
rconnorlawson | 0:35660d7952f7 | 179 | #define NO_RESULT 0 |
rconnorlawson | 0:35660d7952f7 | 180 | #define GAME_OVER 1 |
rconnorlawson | 0:35660d7952f7 | 181 | #define FULL_DRAW 2 |
npatel387 | 2:22d36e7740f1 | 182 | #define RUN 3 |
rconnorlawson | 0:35660d7952f7 | 183 | int update_game(int action) |
rconnorlawson | 0:35660d7952f7 | 184 | { |
rconnorlawson | 0:35660d7952f7 | 185 | // Save player previous location before updating |
rconnorlawson | 0:35660d7952f7 | 186 | Player.px = Player.x; |
rconnorlawson | 0:35660d7952f7 | 187 | Player.py = Player.y; |
rconnorlawson | 0:35660d7952f7 | 188 | |
rconnorlawson | 0:35660d7952f7 | 189 | // Do different things based on the each action. |
rconnorlawson | 0:35660d7952f7 | 190 | // You can define functions like "go_up()" that get called for each case. |
rconnorlawson | 0:35660d7952f7 | 191 | switch(action) |
rconnorlawson | 0:35660d7952f7 | 192 | { |
npatel387 | 2:22d36e7740f1 | 193 | case GO_UP: |
npatel387 | 2:22d36e7740f1 | 194 | if(go_up(Player.x, Player.y, 0)) |
npatel387 | 2:22d36e7740f1 | 195 | return FULL_DRAW; |
npatel387 | 2:22d36e7740f1 | 196 | break; |
npatel387 | 2:22d36e7740f1 | 197 | case GO_UP2: |
npatel387 | 2:22d36e7740f1 | 198 | if(go_up(Player.x, Player.y,1)) |
npatel387 | 2:22d36e7740f1 | 199 | return RUN; |
npatel387 | 2:22d36e7740f1 | 200 | break; |
npatel387 | 2:22d36e7740f1 | 201 | case GO_LEFT: |
npatel387 | 2:22d36e7740f1 | 202 | if(go_left(Player.x, Player.y, 0)) |
npatel387 | 2:22d36e7740f1 | 203 | return FULL_DRAW; |
npatel387 | 2:22d36e7740f1 | 204 | break; |
npatel387 | 2:22d36e7740f1 | 205 | case GO_LEFT2: |
npatel387 | 2:22d36e7740f1 | 206 | if(go_left(Player.x, Player.y, 1)) |
npatel387 | 2:22d36e7740f1 | 207 | return RUN; |
npatel387 | 2:22d36e7740f1 | 208 | break; |
npatel387 | 2:22d36e7740f1 | 209 | case GO_DOWN: |
npatel387 | 2:22d36e7740f1 | 210 | if(go_down(Player.x, Player.y, 0)) |
npatel387 | 2:22d36e7740f1 | 211 | return FULL_DRAW; |
npatel387 | 2:22d36e7740f1 | 212 | break; |
npatel387 | 2:22d36e7740f1 | 213 | case GO_DOWN2: |
npatel387 | 2:22d36e7740f1 | 214 | if(go_down(Player.x, Player.y, 1)) |
npatel387 | 2:22d36e7740f1 | 215 | return RUN; |
npatel387 | 2:22d36e7740f1 | 216 | break; |
npatel387 | 2:22d36e7740f1 | 217 | case GO_RIGHT: |
npatel387 | 2:22d36e7740f1 | 218 | if(go_right(Player.x, Player.y, 0)) |
npatel387 | 2:22d36e7740f1 | 219 | return FULL_DRAW; |
npatel387 | 2:22d36e7740f1 | 220 | break; |
npatel387 | 2:22d36e7740f1 | 221 | case GO_RIGHT2: |
npatel387 | 2:22d36e7740f1 | 222 | if(go_right(Player.x, Player.y, 1)) |
npatel387 | 2:22d36e7740f1 | 223 | return RUN; |
npatel387 | 2:22d36e7740f1 | 224 | break; |
npatel387 | 2:22d36e7740f1 | 225 | case ACTION_BUTTON: |
npatel387 | 2:22d36e7740f1 | 226 | if(interact(Player.x, Player.y)) |
npatel387 | 2:22d36e7740f1 | 227 | return FULL_DRAW; |
npatel387 | 2:22d36e7740f1 | 228 | break; |
npatel387 | 2:22d36e7740f1 | 229 | case OMNIPOTENT: //toggle omnipotent mode |
npatel387 | 2:22d36e7740f1 | 230 | Player.omnipotent = !omnipotent; |
npatel387 | 2:22d36e7740f1 | 231 | omnipotent = !omnipotent; |
npatel387 | 2:22d36e7740f1 | 232 | break; |
npatel387 | 2:22d36e7740f1 | 233 | case MENU_BUTTON: //open menu |
npatel387 | 2:22d36e7740f1 | 234 | draw_menu(Player.omnipotent, Player.has_sword, Player.has_shield, Player.has_platebody, Player.has_key); |
npatel387 | 2:22d36e7740f1 | 235 | return FULL_DRAW; |
npatel387 | 2:22d36e7740f1 | 236 | case BAD_END: |
npatel387 | 2:22d36e7740f1 | 237 | return BAD_END; |
npatel387 | 2:22d36e7740f1 | 238 | case GOOD_END: |
npatel387 | 2:22d36e7740f1 | 239 | return GOOD_END; |
npatel387 | 2:22d36e7740f1 | 240 | default: |
npatel387 | 2:22d36e7740f1 | 241 | return NPC_move(Player.x, Player.y); |
rconnorlawson | 0:35660d7952f7 | 242 | } |
rconnorlawson | 0:35660d7952f7 | 243 | return NO_RESULT; |
rconnorlawson | 0:35660d7952f7 | 244 | } |
rconnorlawson | 0:35660d7952f7 | 245 | |
npatel387 | 2:22d36e7740f1 | 246 | //Functions to move player |
npatel387 | 2:22d36e7740f1 | 247 | int go_up(int x, int y, int i) |
npatel387 | 2:22d36e7740f1 | 248 | { |
npatel387 | 2:22d36e7740f1 | 249 | MapItem *item = get_north(x, y); |
npatel387 | 2:22d36e7740f1 | 250 | MapItem *item2 = get_north(x,y-1); |
npatel387 | 2:22d36e7740f1 | 251 | if((item->walkable && item->type != PLANT2) || Player.omnipotent) |
npatel387 | 2:22d36e7740f1 | 252 | { |
npatel387 | 2:22d36e7740f1 | 253 | if((i && item2->walkable && item2->type != PLANT2) || Player.omnipotent) |
npatel387 | 2:22d36e7740f1 | 254 | { |
npatel387 | 2:22d36e7740f1 | 255 | Player.y = Player.y - 2; |
npatel387 | 2:22d36e7740f1 | 256 | return RUN; |
npatel387 | 2:22d36e7740f1 | 257 | } |
npatel387 | 2:22d36e7740f1 | 258 | Player.y--; |
npatel387 | 2:22d36e7740f1 | 259 | return 1; |
npatel387 | 2:22d36e7740f1 | 260 | } |
npatel387 | 2:22d36e7740f1 | 261 | else if(item->type == PLANT2) |
npatel387 | 2:22d36e7740f1 | 262 | { |
npatel387 | 2:22d36e7740f1 | 263 | Player.health--; |
npatel387 | 2:22d36e7740f1 | 264 | speaker.period(1.0/150.0); // 500hz period |
npatel387 | 2:22d36e7740f1 | 265 | speaker =0.25; //25% duty cycle - mid range volume |
npatel387 | 2:22d36e7740f1 | 266 | wait(.02); |
npatel387 | 2:22d36e7740f1 | 267 | speaker=0.0; // turn off audio |
npatel387 | 2:22d36e7740f1 | 268 | wait(0.5); |
npatel387 | 2:22d36e7740f1 | 269 | return 1; |
npatel387 | 2:22d36e7740f1 | 270 | } |
npatel387 | 2:22d36e7740f1 | 271 | else if(item->type == ROLLING && item2 == NULL) |
npatel387 | 2:22d36e7740f1 | 272 | { |
npatel387 | 2:22d36e7740f1 | 273 | map_erase(x,y-1); |
npatel387 | 2:22d36e7740f1 | 274 | add_rolling(x,y-2); |
npatel387 | 2:22d36e7740f1 | 275 | Player.y--; |
npatel387 | 2:22d36e7740f1 | 276 | return 1; |
npatel387 | 2:22d36e7740f1 | 277 | } |
npatel387 | 2:22d36e7740f1 | 278 | return 0; |
npatel387 | 2:22d36e7740f1 | 279 | } |
npatel387 | 2:22d36e7740f1 | 280 | |
npatel387 | 2:22d36e7740f1 | 281 | int go_down(int x, int y, int i) |
npatel387 | 2:22d36e7740f1 | 282 | { |
npatel387 | 2:22d36e7740f1 | 283 | MapItem *item = get_south(x, y); |
npatel387 | 2:22d36e7740f1 | 284 | MapItem *item2 = get_south(x, y+1); |
npatel387 | 2:22d36e7740f1 | 285 | if((item->walkable && item->type != PLANT2) || Player.omnipotent) |
npatel387 | 2:22d36e7740f1 | 286 | { |
npatel387 | 2:22d36e7740f1 | 287 | if((i && item2->walkable && item2->type != PLANT2) || Player.omnipotent) |
npatel387 | 2:22d36e7740f1 | 288 | { |
npatel387 | 2:22d36e7740f1 | 289 | Player.y = Player.y + 2; |
npatel387 | 2:22d36e7740f1 | 290 | return RUN; |
npatel387 | 2:22d36e7740f1 | 291 | } |
npatel387 | 2:22d36e7740f1 | 292 | Player.y++; |
npatel387 | 2:22d36e7740f1 | 293 | return 1; |
npatel387 | 2:22d36e7740f1 | 294 | } |
npatel387 | 2:22d36e7740f1 | 295 | else if(item->type == PLANT2) |
npatel387 | 2:22d36e7740f1 | 296 | { |
npatel387 | 2:22d36e7740f1 | 297 | Player.health--; |
npatel387 | 2:22d36e7740f1 | 298 | speaker.period(1.0/150.0); // 500hz period |
npatel387 | 2:22d36e7740f1 | 299 | speaker =0.25; //25% duty cycle - mid range volume |
npatel387 | 2:22d36e7740f1 | 300 | wait(.02); |
npatel387 | 2:22d36e7740f1 | 301 | speaker=0.0; // turn off audio |
npatel387 | 2:22d36e7740f1 | 302 | wait(0.5); |
npatel387 | 2:22d36e7740f1 | 303 | return 1; |
npatel387 | 2:22d36e7740f1 | 304 | } |
npatel387 | 2:22d36e7740f1 | 305 | else if(item->type == ROLLING && item2 == NULL) |
npatel387 | 2:22d36e7740f1 | 306 | { |
npatel387 | 2:22d36e7740f1 | 307 | map_erase(x,y+1); |
npatel387 | 2:22d36e7740f1 | 308 | add_rolling(x,y+2); |
npatel387 | 2:22d36e7740f1 | 309 | Player.y++; |
npatel387 | 2:22d36e7740f1 | 310 | return 1; |
npatel387 | 2:22d36e7740f1 | 311 | } |
npatel387 | 2:22d36e7740f1 | 312 | return 0; |
npatel387 | 2:22d36e7740f1 | 313 | } |
npatel387 | 2:22d36e7740f1 | 314 | |
npatel387 | 2:22d36e7740f1 | 315 | int go_left(int x, int y, int i) |
npatel387 | 2:22d36e7740f1 | 316 | { |
npatel387 | 2:22d36e7740f1 | 317 | MapItem *item = get_west(x, y); |
npatel387 | 2:22d36e7740f1 | 318 | MapItem *item2 = get_west(x-1,y); |
npatel387 | 2:22d36e7740f1 | 319 | if((item->walkable && item->type != PLANT2) || Player.omnipotent) |
npatel387 | 2:22d36e7740f1 | 320 | { |
npatel387 | 2:22d36e7740f1 | 321 | if((i && item2->walkable && item2->type != PLANT2) || Player.omnipotent) |
npatel387 | 2:22d36e7740f1 | 322 | { |
npatel387 | 2:22d36e7740f1 | 323 | Player.x = Player.x - 2; |
npatel387 | 2:22d36e7740f1 | 324 | return RUN; |
npatel387 | 2:22d36e7740f1 | 325 | } |
npatel387 | 2:22d36e7740f1 | 326 | Player.x--; |
npatel387 | 2:22d36e7740f1 | 327 | return 1; |
npatel387 | 2:22d36e7740f1 | 328 | } |
npatel387 | 2:22d36e7740f1 | 329 | else if(item->type == PLANT2) |
npatel387 | 2:22d36e7740f1 | 330 | { |
npatel387 | 2:22d36e7740f1 | 331 | Player.health--; |
npatel387 | 2:22d36e7740f1 | 332 | speaker.period(1.0/150.0); // 500hz period |
npatel387 | 2:22d36e7740f1 | 333 | speaker =0.25; //25% duty cycle - mid range volume |
npatel387 | 2:22d36e7740f1 | 334 | wait(.02); |
npatel387 | 2:22d36e7740f1 | 335 | speaker=0.0; // turn off audio |
npatel387 | 2:22d36e7740f1 | 336 | wait(0.5); |
npatel387 | 2:22d36e7740f1 | 337 | return 1; |
npatel387 | 2:22d36e7740f1 | 338 | } |
npatel387 | 2:22d36e7740f1 | 339 | else if(item->type == ROLLING && item2 == NULL) |
npatel387 | 2:22d36e7740f1 | 340 | { |
npatel387 | 2:22d36e7740f1 | 341 | map_erase(x-1,y); |
npatel387 | 2:22d36e7740f1 | 342 | add_rolling(x-2,y); |
npatel387 | 2:22d36e7740f1 | 343 | Player.x--; |
npatel387 | 2:22d36e7740f1 | 344 | return 1; |
npatel387 | 2:22d36e7740f1 | 345 | } |
npatel387 | 2:22d36e7740f1 | 346 | return 0; |
npatel387 | 2:22d36e7740f1 | 347 | } |
npatel387 | 2:22d36e7740f1 | 348 | |
npatel387 | 2:22d36e7740f1 | 349 | int go_right(int x, int y, int i) |
npatel387 | 2:22d36e7740f1 | 350 | { |
npatel387 | 2:22d36e7740f1 | 351 | MapItem *item = get_east(x, y); |
npatel387 | 2:22d36e7740f1 | 352 | MapItem *item2 = get_east(x+1, y); |
npatel387 | 2:22d36e7740f1 | 353 | if((item->walkable && item->type != PLANT2) || Player.omnipotent) |
npatel387 | 2:22d36e7740f1 | 354 | { |
npatel387 | 2:22d36e7740f1 | 355 | if((i && item2->walkable && item2->type != PLANT2) || Player.omnipotent) |
npatel387 | 2:22d36e7740f1 | 356 | { |
npatel387 | 2:22d36e7740f1 | 357 | Player.x = Player.x + 2; |
npatel387 | 2:22d36e7740f1 | 358 | return RUN; |
npatel387 | 2:22d36e7740f1 | 359 | } |
npatel387 | 2:22d36e7740f1 | 360 | Player.x++; |
npatel387 | 2:22d36e7740f1 | 361 | return 1; |
npatel387 | 2:22d36e7740f1 | 362 | } |
npatel387 | 2:22d36e7740f1 | 363 | else if(item->type == PLANT2) |
npatel387 | 2:22d36e7740f1 | 364 | { |
npatel387 | 2:22d36e7740f1 | 365 | Player.health--; |
npatel387 | 2:22d36e7740f1 | 366 | speaker.period(1.0/150.0); // 500hz period |
npatel387 | 2:22d36e7740f1 | 367 | speaker =0.25; //25% duty cycle - mid range volume |
npatel387 | 2:22d36e7740f1 | 368 | wait(.02); |
npatel387 | 2:22d36e7740f1 | 369 | speaker=0.0; // turn off audio |
npatel387 | 2:22d36e7740f1 | 370 | wait(0.5); |
npatel387 | 2:22d36e7740f1 | 371 | return 1; |
npatel387 | 2:22d36e7740f1 | 372 | } |
npatel387 | 2:22d36e7740f1 | 373 | else if(item->type == ROLLING && item2 == NULL) |
npatel387 | 2:22d36e7740f1 | 374 | { |
npatel387 | 2:22d36e7740f1 | 375 | map_erase(x+1,y); |
npatel387 | 2:22d36e7740f1 | 376 | add_rolling(x+2,y); |
npatel387 | 2:22d36e7740f1 | 377 | Player.x++; |
npatel387 | 2:22d36e7740f1 | 378 | return 1; |
npatel387 | 2:22d36e7740f1 | 379 | } |
npatel387 | 2:22d36e7740f1 | 380 | return 0; |
npatel387 | 2:22d36e7740f1 | 381 | } |
npatel387 | 2:22d36e7740f1 | 382 | |
npatel387 | 2:22d36e7740f1 | 383 | int interact(int x, int y) |
npatel387 | 2:22d36e7740f1 | 384 | { |
npatel387 | 2:22d36e7740f1 | 385 | MapItem *north = get_north(x,y); |
npatel387 | 2:22d36e7740f1 | 386 | MapItem *south = get_south(x,y); |
npatel387 | 2:22d36e7740f1 | 387 | MapItem *east = get_east(x,y); |
npatel387 | 2:22d36e7740f1 | 388 | MapItem *west = get_west(x,y); |
npatel387 | 2:22d36e7740f1 | 389 | if(north->type == STARTNPC || south->type == STARTNPC || east->type == STARTNPC || west->type == STARTNPC) |
npatel387 | 2:22d36e7740f1 | 390 | { |
npatel387 | 2:22d36e7740f1 | 391 | if(Player.has_platebody) |
npatel387 | 2:22d36e7740f1 | 392 | { |
npatel387 | 2:22d36e7740f1 | 393 | speech(questHasPlatebody1, questHasPlatebody2, questHasPlatebody3, questHasPlatebody4); |
npatel387 | 2:22d36e7740f1 | 394 | finished = 1; |
npatel387 | 2:22d36e7740f1 | 395 | return 1; |
npatel387 | 2:22d36e7740f1 | 396 | } |
npatel387 | 2:22d36e7740f1 | 397 | else if(!Player.has_platebody && Player.has_key) |
npatel387 | 2:22d36e7740f1 | 398 | { |
npatel387 | 2:22d36e7740f1 | 399 | speech("Search the", "treasure room", "first! Then", "come talk"); |
npatel387 | 2:22d36e7740f1 | 400 | return 1; |
npatel387 | 2:22d36e7740f1 | 401 | } |
npatel387 | 2:22d36e7740f1 | 402 | else if(Player.has_sword) |
npatel387 | 2:22d36e7740f1 | 403 | { |
npatel387 | 2:22d36e7740f1 | 404 | speech(questHasSword1, "", "", ""); |
npatel387 | 2:22d36e7740f1 | 405 | return 1; |
npatel387 | 2:22d36e7740f1 | 406 | } |
npatel387 | 2:22d36e7740f1 | 407 | else if(Player.has_shield ) |
npatel387 | 2:22d36e7740f1 | 408 | { |
npatel387 | 2:22d36e7740f1 | 409 | speech(questHasShield1, questHasShield2, questHasShield3, ""); |
npatel387 | 2:22d36e7740f1 | 410 | return 1; |
npatel387 | 2:22d36e7740f1 | 411 | } |
npatel387 | 2:22d36e7740f1 | 412 | else |
npatel387 | 2:22d36e7740f1 | 413 | { |
npatel387 | 2:22d36e7740f1 | 414 | speech(questStartLine1, questStartLine2, questStartLine3, questStartLine4); |
npatel387 | 2:22d36e7740f1 | 415 | speech(questStartLine5, questStartLine6, questStartLine7, questStartLine8); |
npatel387 | 2:22d36e7740f1 | 416 | speech(questStartLine9, questStartLine10, questStartLine11, questStartLine12); |
npatel387 | 2:22d36e7740f1 | 417 | speech(questStartLine13, questStartLine14, questStartLine15, questStartLine16); |
npatel387 | 2:22d36e7740f1 | 418 | speech(questStartLine17, questStartLine18, questStartLine19, questStartLine20); |
npatel387 | 2:22d36e7740f1 | 419 | speech(questStartLine21, questStartLine22, questStartLine23, questStartLine24); |
npatel387 | 2:22d36e7740f1 | 420 | speech(questStartLine25, questStartLine26,"",""); |
npatel387 | 2:22d36e7740f1 | 421 | Player.has_shield = 1; |
npatel387 | 2:22d36e7740f1 | 422 | return 1; |
npatel387 | 2:22d36e7740f1 | 423 | } |
npatel387 | 2:22d36e7740f1 | 424 | } |
npatel387 | 2:22d36e7740f1 | 425 | else if(north->type == CAVE || south->type == CAVE || east->type == CAVE || west->type == CAVE) |
npatel387 | 2:22d36e7740f1 | 426 | { |
npatel387 | 2:22d36e7740f1 | 427 | if(mapToggle == 0) |
npatel387 | 2:22d36e7740f1 | 428 | { |
npatel387 | 2:22d36e7740f1 | 429 | mapToggle = !mapToggle; |
npatel387 | 2:22d36e7740f1 | 430 | set_active_map(1); |
npatel387 | 2:22d36e7740f1 | 431 | Player.x = 5; |
npatel387 | 2:22d36e7740f1 | 432 | Player.y = 5; |
npatel387 | 2:22d36e7740f1 | 433 | } |
npatel387 | 2:22d36e7740f1 | 434 | else |
npatel387 | 2:22d36e7740f1 | 435 | { |
npatel387 | 2:22d36e7740f1 | 436 | mapToggle = !mapToggle; |
npatel387 | 2:22d36e7740f1 | 437 | set_active_map(0); |
npatel387 | 2:22d36e7740f1 | 438 | Player.x = 39; |
npatel387 | 2:22d36e7740f1 | 439 | Player.y = 4; |
npatel387 | 2:22d36e7740f1 | 440 | } |
npatel387 | 2:22d36e7740f1 | 441 | return 1; |
npatel387 | 2:22d36e7740f1 | 442 | } |
npatel387 | 2:22d36e7740f1 | 443 | else if(north->type == SWORDINSTONE || south->type == SWORDINSTONE || east->type == SWORDINSTONE || west->type == SWORDINSTONE) |
npatel387 | 2:22d36e7740f1 | 444 | { |
npatel387 | 2:22d36e7740f1 | 445 | if(Player.has_shield) |
npatel387 | 2:22d36e7740f1 | 446 | { |
npatel387 | 2:22d36e7740f1 | 447 | Player.has_sword = 1; |
npatel387 | 2:22d36e7740f1 | 448 | speech(findSword1, findSword2, "", ""); |
npatel387 | 2:22d36e7740f1 | 449 | map_erase(15,15); |
npatel387 | 2:22d36e7740f1 | 450 | draw_plant(15,15); |
npatel387 | 2:22d36e7740f1 | 451 | } |
npatel387 | 2:22d36e7740f1 | 452 | else |
npatel387 | 2:22d36e7740f1 | 453 | { |
npatel387 | 2:22d36e7740f1 | 454 | speech("You should", "talk to the man", "where you started", "first"); |
npatel387 | 2:22d36e7740f1 | 455 | } |
npatel387 | 2:22d36e7740f1 | 456 | return 1; |
npatel387 | 2:22d36e7740f1 | 457 | } |
npatel387 | 2:22d36e7740f1 | 458 | else if(north->type == ELVARG || south->type == ELVARG || east->type == ELVARG || west->type == ELVARG) |
npatel387 | 2:22d36e7740f1 | 459 | { |
npatel387 | 2:22d36e7740f1 | 460 | speech(elvarg1, elvarg2, "", ""); |
npatel387 | 2:22d36e7740f1 | 461 | if(!Player.has_shield) |
npatel387 | 2:22d36e7740f1 | 462 | { |
npatel387 | 2:22d36e7740f1 | 463 | speech(elvarg3a1, elvarg3a2, elvarg3a3, ""); |
npatel387 | 2:22d36e7740f1 | 464 | Player.health = Player.health - 10; |
npatel387 | 2:22d36e7740f1 | 465 | return 1; |
npatel387 | 2:22d36e7740f1 | 466 | } |
npatel387 | 2:22d36e7740f1 | 467 | else if(!Player.has_sword) |
npatel387 | 2:22d36e7740f1 | 468 | { |
npatel387 | 2:22d36e7740f1 | 469 | speech(elvarg3b1, elvarg3b2, elvarg3b3, elvarg3b4); |
npatel387 | 2:22d36e7740f1 | 470 | speech(elvarg3b5, "", "", ""); |
npatel387 | 2:22d36e7740f1 | 471 | Player.health = Player.health - 9; |
npatel387 | 2:22d36e7740f1 | 472 | return 1; |
npatel387 | 2:22d36e7740f1 | 473 | } |
npatel387 | 2:22d36e7740f1 | 474 | else |
npatel387 | 2:22d36e7740f1 | 475 | { |
npatel387 | 2:22d36e7740f1 | 476 | Player.has_key = 1; |
npatel387 | 2:22d36e7740f1 | 477 | speech(elvarg3c1, elvarg3c2, elvarg3c3, elvarg3c4); |
npatel387 | 2:22d36e7740f1 | 478 | speech(elvarg3c5, elvarg3c6, elvarg3c7, ""); |
npatel387 | 2:22d36e7740f1 | 479 | map_erase(10,10); |
npatel387 | 2:22d36e7740f1 | 480 | Player.health = Player.health - 3; |
npatel387 | 2:22d36e7740f1 | 481 | return 1; |
npatel387 | 2:22d36e7740f1 | 482 | } |
npatel387 | 2:22d36e7740f1 | 483 | } |
npatel387 | 2:22d36e7740f1 | 484 | else if(north->type == GATE || south->type == GATE || east->type == GATE || west->type == GATE) |
npatel387 | 2:22d36e7740f1 | 485 | { |
npatel387 | 2:22d36e7740f1 | 486 | if(Player.has_key) |
npatel387 | 2:22d36e7740f1 | 487 | { |
npatel387 | 2:22d36e7740f1 | 488 | speech("You unlock the", "gate with the key", "", ""); |
npatel387 | 2:22d36e7740f1 | 489 | map_erase(10,45); |
npatel387 | 2:22d36e7740f1 | 490 | return 1; |
npatel387 | 2:22d36e7740f1 | 491 | } |
npatel387 | 2:22d36e7740f1 | 492 | else |
npatel387 | 2:22d36e7740f1 | 493 | { |
npatel387 | 2:22d36e7740f1 | 494 | speech("Requires Elvarg's", "key to open", "", ""); |
npatel387 | 2:22d36e7740f1 | 495 | return 1; |
npatel387 | 2:22d36e7740f1 | 496 | } |
npatel387 | 2:22d36e7740f1 | 497 | } |
npatel387 | 2:22d36e7740f1 | 498 | else if(north->type == TREASURE || south->type == TREASURE || east->type == TREASURE || west->type == TREASURE) |
npatel387 | 2:22d36e7740f1 | 499 | { |
npatel387 | 2:22d36e7740f1 | 500 | Player.has_platebody = 1; |
npatel387 | 2:22d36e7740f1 | 501 | speech("You are amazed", "to find a", "legendary rune", "platebody."); |
npatel387 | 2:22d36e7740f1 | 502 | speech("you equip the", "legendary armor.", "Time to return", "to that weird"); |
npatel387 | 2:22d36e7740f1 | 503 | speech("guy. His request", "has been", "fulfilled.", ""); |
npatel387 | 2:22d36e7740f1 | 504 | return 1; |
npatel387 | 2:22d36e7740f1 | 505 | } |
npatel387 | 2:22d36e7740f1 | 506 | else if(north->type == PHAT || south->type == PHAT || east->type == PHAT || west->type == PHAT) |
npatel387 | 2:22d36e7740f1 | 507 | { |
npatel387 | 2:22d36e7740f1 | 508 | if(Player.num_phats == 0) |
npatel387 | 2:22d36e7740f1 | 509 | speech("find all 5", "hidden party", "hats!", ""); |
npatel387 | 2:22d36e7740f1 | 510 | Player.num_phats++; |
npatel387 | 2:22d36e7740f1 | 511 | if(Player.num_phats == 5) |
npatel387 | 2:22d36e7740f1 | 512 | { |
npatel387 | 2:22d36e7740f1 | 513 | speech("you found all", "5 party hats!", "You are very", "special!"); |
npatel387 | 2:22d36e7740f1 | 514 | } |
npatel387 | 2:22d36e7740f1 | 515 | if(north->type == PHAT) |
npatel387 | 2:22d36e7740f1 | 516 | map_erase(x, y-1); |
npatel387 | 2:22d36e7740f1 | 517 | if(south->type == PHAT) |
npatel387 | 2:22d36e7740f1 | 518 | map_erase(x, y+1); |
npatel387 | 2:22d36e7740f1 | 519 | if(east->type == PHAT) |
npatel387 | 2:22d36e7740f1 | 520 | map_erase(x+1, y); |
npatel387 | 2:22d36e7740f1 | 521 | if(west->type == PHAT) |
npatel387 | 2:22d36e7740f1 | 522 | map_erase(x-1, y); |
npatel387 | 2:22d36e7740f1 | 523 | return 1; |
npatel387 | 2:22d36e7740f1 | 524 | } |
npatel387 | 2:22d36e7740f1 | 525 | |
npatel387 | 2:22d36e7740f1 | 526 | return 0; |
npatel387 | 2:22d36e7740f1 | 527 | } |
npatel387 | 2:22d36e7740f1 | 528 | |
npatel387 | 2:22d36e7740f1 | 529 | int NPC_move(int x, int y) |
npatel387 | 2:22d36e7740f1 | 530 | { |
npatel387 | 2:22d36e7740f1 | 531 | if(!((Player.x == (7+!walk)) && (Player.y == 7)) && (map_width() == 50)) |
npatel387 | 2:22d36e7740f1 | 532 | { |
npatel387 | 2:22d36e7740f1 | 533 | wait_ms(200); |
npatel387 | 2:22d36e7740f1 | 534 | map_erase(7+walk, 7); |
npatel387 | 2:22d36e7740f1 | 535 | walk = !walk; |
npatel387 | 2:22d36e7740f1 | 536 | add_startNPC(7+walk,7); |
npatel387 | 2:22d36e7740f1 | 537 | wait_ms(200); |
npatel387 | 2:22d36e7740f1 | 538 | return FULL_DRAW; |
npatel387 | 2:22d36e7740f1 | 539 | } |
npatel387 | 2:22d36e7740f1 | 540 | return 0; |
npatel387 | 2:22d36e7740f1 | 541 | } |
npatel387 | 2:22d36e7740f1 | 542 | |
rconnorlawson | 0:35660d7952f7 | 543 | /** |
rconnorlawson | 0:35660d7952f7 | 544 | * Entry point for frame drawing. This should be called once per iteration of |
rconnorlawson | 0:35660d7952f7 | 545 | * the game loop. This draws all tiles on the screen, followed by the status |
rconnorlawson | 0:35660d7952f7 | 546 | * bars. Unless init is nonzero, this function will optimize drawing by only |
rconnorlawson | 0:35660d7952f7 | 547 | * drawing tiles that have changed from the previous frame. |
rconnorlawson | 0:35660d7952f7 | 548 | */ |
rconnorlawson | 0:35660d7952f7 | 549 | void draw_game(int init) |
npatel387 | 2:22d36e7740f1 | 550 | { |
npatel387 | 2:22d36e7740f1 | 551 | if(init == BAD_END) |
npatel387 | 2:22d36e7740f1 | 552 | { |
npatel387 | 2:22d36e7740f1 | 553 | speaker.period(1.0/150.0); // 500hz period |
npatel387 | 2:22d36e7740f1 | 554 | speaker =0.25; //25% duty cycle - mid range volume |
npatel387 | 2:22d36e7740f1 | 555 | wait(.5); |
npatel387 | 2:22d36e7740f1 | 556 | speaker=0.0; // turn off audio |
npatel387 | 2:22d36e7740f1 | 557 | wait(0.5); |
npatel387 | 2:22d36e7740f1 | 558 | draw_bad(); |
npatel387 | 2:22d36e7740f1 | 559 | wait(10000); |
npatel387 | 2:22d36e7740f1 | 560 | } |
npatel387 | 2:22d36e7740f1 | 561 | if(init == GOOD_END) |
npatel387 | 2:22d36e7740f1 | 562 | { |
npatel387 | 2:22d36e7740f1 | 563 | draw_good(); |
npatel387 | 2:22d36e7740f1 | 564 | wait(10000); |
npatel387 | 2:22d36e7740f1 | 565 | } |
npatel387 | 2:22d36e7740f1 | 566 | |
npatel387 | 2:22d36e7740f1 | 567 | |
rconnorlawson | 0:35660d7952f7 | 568 | // Draw game border first |
rconnorlawson | 0:35660d7952f7 | 569 | if(init) draw_border(); |
rconnorlawson | 0:35660d7952f7 | 570 | |
rconnorlawson | 0:35660d7952f7 | 571 | // Iterate over all visible map tiles |
rconnorlawson | 0:35660d7952f7 | 572 | for (int i = -5; i <= 5; i++) // Iterate over columns of tiles |
rconnorlawson | 0:35660d7952f7 | 573 | { |
rconnorlawson | 0:35660d7952f7 | 574 | for (int j = -4; j <= 4; j++) // Iterate over one column of tiles |
rconnorlawson | 0:35660d7952f7 | 575 | { |
rconnorlawson | 0:35660d7952f7 | 576 | // Here, we have a given (i,j) |
rconnorlawson | 0:35660d7952f7 | 577 | |
rconnorlawson | 0:35660d7952f7 | 578 | // Compute the current map (x,y) of this tile |
rconnorlawson | 0:35660d7952f7 | 579 | int x = i + Player.x; |
rconnorlawson | 0:35660d7952f7 | 580 | int y = j + Player.y; |
rconnorlawson | 0:35660d7952f7 | 581 | |
rconnorlawson | 0:35660d7952f7 | 582 | // Compute the previous map (px, py) of this tile |
rconnorlawson | 0:35660d7952f7 | 583 | int px = i + Player.px; |
rconnorlawson | 0:35660d7952f7 | 584 | int py = j + Player.py; |
rconnorlawson | 0:35660d7952f7 | 585 | |
rconnorlawson | 0:35660d7952f7 | 586 | // Compute u,v coordinates for drawing |
rconnorlawson | 0:35660d7952f7 | 587 | int u = (i+5)*11 + 3; |
rconnorlawson | 0:35660d7952f7 | 588 | int v = (j+4)*11 + 15; |
rconnorlawson | 0:35660d7952f7 | 589 | |
rconnorlawson | 0:35660d7952f7 | 590 | // Figure out what to draw |
rconnorlawson | 0:35660d7952f7 | 591 | DrawFunc draw = NULL; |
npatel387 | 2:22d36e7740f1 | 592 | if (init && i == 0 && j == 0 && get_here(Player.x, Player.y)->walkable != 2) // Only draw the player on init |
rconnorlawson | 0:35660d7952f7 | 593 | { |
npatel387 | 2:22d36e7740f1 | 594 | if(init == RUN) |
npatel387 | 2:22d36e7740f1 | 595 | { |
npatel387 | 2:22d36e7740f1 | 596 | draw_player(u,v,4); |
npatel387 | 2:22d36e7740f1 | 597 | wait_ms(100); |
npatel387 | 2:22d36e7740f1 | 598 | } |
npatel387 | 2:22d36e7740f1 | 599 | if(Player.has_platebody) |
npatel387 | 2:22d36e7740f1 | 600 | draw_player(u, v, 3); |
npatel387 | 2:22d36e7740f1 | 601 | else if(Player.has_sword) |
npatel387 | 2:22d36e7740f1 | 602 | draw_player(u, v, 2); |
npatel387 | 2:22d36e7740f1 | 603 | else if(Player.has_shield) |
npatel387 | 2:22d36e7740f1 | 604 | draw_player(u ,v, 1); |
npatel387 | 2:22d36e7740f1 | 605 | else |
npatel387 | 2:22d36e7740f1 | 606 | draw_player(u ,v, 0); |
rconnorlawson | 0:35660d7952f7 | 607 | continue; |
rconnorlawson | 0:35660d7952f7 | 608 | } |
rconnorlawson | 0:35660d7952f7 | 609 | else if (x >= 0 && y >= 0 && x < map_width() && y < map_height()) // Current (i,j) in the map |
rconnorlawson | 0:35660d7952f7 | 610 | { |
rconnorlawson | 0:35660d7952f7 | 611 | MapItem* curr_item = get_here(x, y); |
rconnorlawson | 0:35660d7952f7 | 612 | MapItem* prev_item = get_here(px, py); |
rconnorlawson | 0:35660d7952f7 | 613 | if (init || curr_item != prev_item) // Only draw if they're different |
rconnorlawson | 0:35660d7952f7 | 614 | { |
rconnorlawson | 0:35660d7952f7 | 615 | if (curr_item) // There's something here! Draw it |
rconnorlawson | 0:35660d7952f7 | 616 | { |
rconnorlawson | 0:35660d7952f7 | 617 | draw = curr_item->draw; |
rconnorlawson | 0:35660d7952f7 | 618 | } |
rconnorlawson | 0:35660d7952f7 | 619 | else // There used to be something, but now there isn't |
rconnorlawson | 0:35660d7952f7 | 620 | { |
rconnorlawson | 0:35660d7952f7 | 621 | draw = draw_nothing; |
rconnorlawson | 0:35660d7952f7 | 622 | } |
rconnorlawson | 0:35660d7952f7 | 623 | } |
rconnorlawson | 0:35660d7952f7 | 624 | } |
rconnorlawson | 0:35660d7952f7 | 625 | else if (init) // If doing a full draw, but we're out of bounds, draw the walls. |
rconnorlawson | 0:35660d7952f7 | 626 | { |
rconnorlawson | 0:35660d7952f7 | 627 | draw = draw_wall; |
rconnorlawson | 0:35660d7952f7 | 628 | } |
rconnorlawson | 0:35660d7952f7 | 629 | |
rconnorlawson | 0:35660d7952f7 | 630 | // Actually draw the tile |
rconnorlawson | 0:35660d7952f7 | 631 | if (draw) draw(u, v); |
rconnorlawson | 0:35660d7952f7 | 632 | } |
rconnorlawson | 0:35660d7952f7 | 633 | } |
rconnorlawson | 0:35660d7952f7 | 634 | |
rconnorlawson | 0:35660d7952f7 | 635 | // Draw status bars |
npatel387 | 2:22d36e7740f1 | 636 | draw_upper_status(Player.x,Player.y); |
npatel387 | 2:22d36e7740f1 | 637 | draw_lower_status(Player.health, Player.num_phats); |
rconnorlawson | 0:35660d7952f7 | 638 | } |
rconnorlawson | 0:35660d7952f7 | 639 | |
rconnorlawson | 0:35660d7952f7 | 640 | |
rconnorlawson | 0:35660d7952f7 | 641 | /** |
rconnorlawson | 0:35660d7952f7 | 642 | * Initialize the main world map. Add walls around the edges, interior chambers, |
rconnorlawson | 0:35660d7952f7 | 643 | * and plants in the background so you can see motion. |
rconnorlawson | 0:35660d7952f7 | 644 | */ |
rconnorlawson | 0:35660d7952f7 | 645 | void init_main_map() |
rconnorlawson | 0:35660d7952f7 | 646 | { |
rconnorlawson | 0:35660d7952f7 | 647 | // "Random" plants |
rconnorlawson | 0:35660d7952f7 | 648 | Map* map = set_active_map(0); |
rconnorlawson | 0:35660d7952f7 | 649 | for(int i = map_width() + 3; i < map_area(); i += 39) |
rconnorlawson | 0:35660d7952f7 | 650 | { |
rconnorlawson | 0:35660d7952f7 | 651 | add_plant(i % map_width(), i / map_width()); |
rconnorlawson | 0:35660d7952f7 | 652 | } |
rconnorlawson | 0:35660d7952f7 | 653 | pc.printf("plants\r\n"); |
rconnorlawson | 0:35660d7952f7 | 654 | |
rconnorlawson | 0:35660d7952f7 | 655 | pc.printf("Adding walls!\r\n"); |
rconnorlawson | 0:35660d7952f7 | 656 | add_wall(0, 0, HORIZONTAL, map_width()); |
rconnorlawson | 0:35660d7952f7 | 657 | add_wall(0, map_height()-1, HORIZONTAL, map_width()); |
rconnorlawson | 0:35660d7952f7 | 658 | add_wall(0, 0, VERTICAL, map_height()); |
rconnorlawson | 0:35660d7952f7 | 659 | add_wall(map_width()-1, 0, VERTICAL, map_height()); |
rconnorlawson | 0:35660d7952f7 | 660 | pc.printf("Walls done!\r\n"); |
rconnorlawson | 0:35660d7952f7 | 661 | |
npatel387 | 2:22d36e7740f1 | 662 | //draw in treasure room |
npatel387 | 2:22d36e7740f1 | 663 | add_wall(6, 45, VERTICAL, 4); |
npatel387 | 2:22d36e7740f1 | 664 | add_wall(7, 45, HORIZONTAL, 3); |
npatel387 | 2:22d36e7740f1 | 665 | add_gate(10,45); |
npatel387 | 2:22d36e7740f1 | 666 | add_wall(11, 45, HORIZONTAL, 3); |
npatel387 | 2:22d36e7740f1 | 667 | add_wall(14, 45, VERTICAL, 4); |
npatel387 | 2:22d36e7740f1 | 668 | |
npatel387 | 2:22d36e7740f1 | 669 | |
npatel387 | 2:22d36e7740f1 | 670 | add_startNPC(7,7); |
npatel387 | 2:22d36e7740f1 | 671 | pc.printf("NPCs!\r\n"); |
npatel387 | 2:22d36e7740f1 | 672 | |
npatel387 | 2:22d36e7740f1 | 673 | add_boulder(3,3); |
npatel387 | 2:22d36e7740f1 | 674 | |
npatel387 | 2:22d36e7740f1 | 675 | add_cave(40,4); |
npatel387 | 2:22d36e7740f1 | 676 | pc.printf("Cave!\r\n"); |
npatel387 | 2:22d36e7740f1 | 677 | add_rolling(39,4); |
npatel387 | 2:22d36e7740f1 | 678 | add_rolling(41,4); |
npatel387 | 2:22d36e7740f1 | 679 | add_rolling(40,5); |
npatel387 | 2:22d36e7740f1 | 680 | add_rolling(40,3); |
npatel387 | 2:22d36e7740f1 | 681 | |
npatel387 | 2:22d36e7740f1 | 682 | add_treasure(10,47); |
npatel387 | 2:22d36e7740f1 | 683 | pc.printf("Treasure!\r\n"); |
npatel387 | 2:22d36e7740f1 | 684 | |
npatel387 | 2:22d36e7740f1 | 685 | add_phat(45,45); |
npatel387 | 2:22d36e7740f1 | 686 | add_phat(25,25); |
npatel387 | 2:22d36e7740f1 | 687 | add_phat(10,48); |
npatel387 | 2:22d36e7740f1 | 688 | |
npatel387 | 2:22d36e7740f1 | 689 | print_map(); |
npatel387 | 2:22d36e7740f1 | 690 | } |
npatel387 | 2:22d36e7740f1 | 691 | |
npatel387 | 2:22d36e7740f1 | 692 | void init_second_map() |
npatel387 | 2:22d36e7740f1 | 693 | { |
npatel387 | 2:22d36e7740f1 | 694 | // "Random" plants |
npatel387 | 2:22d36e7740f1 | 695 | Map* map = set_active_map(1); |
npatel387 | 2:22d36e7740f1 | 696 | for(int i = map_width() + 3; i < map_area(); i += 39) |
npatel387 | 2:22d36e7740f1 | 697 | { |
npatel387 | 2:22d36e7740f1 | 698 | add_plant2(i % map_width(), i / map_width()); |
npatel387 | 2:22d36e7740f1 | 699 | } |
npatel387 | 2:22d36e7740f1 | 700 | pc.printf("plants\r\n"); |
npatel387 | 2:22d36e7740f1 | 701 | |
npatel387 | 2:22d36e7740f1 | 702 | pc.printf("Adding walls!\r\n"); |
npatel387 | 2:22d36e7740f1 | 703 | add_wall(0, 0, HORIZONTAL, map_width()); |
npatel387 | 2:22d36e7740f1 | 704 | add_wall(0, map_height()-1, HORIZONTAL, map_width()); |
npatel387 | 2:22d36e7740f1 | 705 | add_wall(0, 0, VERTICAL, map_height()); |
npatel387 | 2:22d36e7740f1 | 706 | add_wall(map_width()-1, 0, VERTICAL, map_height()); |
npatel387 | 2:22d36e7740f1 | 707 | pc.printf("Walls done!\r\n"); |
npatel387 | 2:22d36e7740f1 | 708 | |
npatel387 | 2:22d36e7740f1 | 709 | //add sprites |
npatel387 | 2:22d36e7740f1 | 710 | add_cave(4,5); |
npatel387 | 2:22d36e7740f1 | 711 | add_elvarg(10,10); |
npatel387 | 2:22d36e7740f1 | 712 | add_swordInStone(15,15); |
npatel387 | 2:22d36e7740f1 | 713 | add_phat(3,10); |
npatel387 | 2:22d36e7740f1 | 714 | add_phat(10,17); |
rconnorlawson | 0:35660d7952f7 | 715 | print_map(); |
rconnorlawson | 0:35660d7952f7 | 716 | } |
rconnorlawson | 0:35660d7952f7 | 717 | |
rconnorlawson | 0:35660d7952f7 | 718 | /** |
rconnorlawson | 0:35660d7952f7 | 719 | * Program entry point! This is where it all begins. |
rconnorlawson | 0:35660d7952f7 | 720 | * This function orchestrates all the parts of the game. Most of your |
rconnorlawson | 0:35660d7952f7 | 721 | * implementation should be elsewhere - this holds the game loop, and should |
rconnorlawson | 0:35660d7952f7 | 722 | * read like a road map for the rest of the code. |
rconnorlawson | 0:35660d7952f7 | 723 | */ |
rconnorlawson | 0:35660d7952f7 | 724 | int main() |
rconnorlawson | 0:35660d7952f7 | 725 | { |
rconnorlawson | 0:35660d7952f7 | 726 | // First things first: initialize hardware |
rconnorlawson | 0:35660d7952f7 | 727 | ASSERT_P(hardware_init() == ERROR_NONE, "Hardware init failed!"); |
rconnorlawson | 0:35660d7952f7 | 728 | |
npatel387 | 2:22d36e7740f1 | 729 | draw_start(); |
npatel387 | 2:22d36e7740f1 | 730 | |
rconnorlawson | 0:35660d7952f7 | 731 | // Initialize the maps |
rconnorlawson | 0:35660d7952f7 | 732 | maps_init(); |
npatel387 | 2:22d36e7740f1 | 733 | init_second_map(); |
rconnorlawson | 0:35660d7952f7 | 734 | init_main_map(); |
rconnorlawson | 0:35660d7952f7 | 735 | |
rconnorlawson | 0:35660d7952f7 | 736 | // Initialize game state |
rconnorlawson | 0:35660d7952f7 | 737 | set_active_map(0); |
rconnorlawson | 0:35660d7952f7 | 738 | Player.x = Player.y = 5; |
npatel387 | 2:22d36e7740f1 | 739 | Player.has_key = 0; |
npatel387 | 2:22d36e7740f1 | 740 | Player.has_sword = 0; |
npatel387 | 2:22d36e7740f1 | 741 | Player.has_shield = 0; |
npatel387 | 2:22d36e7740f1 | 742 | Player.has_platebody = 0; |
npatel387 | 2:22d36e7740f1 | 743 | Player.health = 10; |
rconnorlawson | 0:35660d7952f7 | 744 | |
rconnorlawson | 0:35660d7952f7 | 745 | // Initial drawing |
npatel387 | 2:22d36e7740f1 | 746 | uLCD.filled_rectangle(0, 0, 127, 8, BLACK); |
npatel387 | 2:22d36e7740f1 | 747 | uLCD.filled_rectangle(0, 119, 127, 127, BLACK); |
rconnorlawson | 0:35660d7952f7 | 748 | draw_game(true); |
npatel387 | 2:22d36e7740f1 | 749 | |
npatel387 | 2:22d36e7740f1 | 750 | // Variables |
npatel387 | 2:22d36e7740f1 | 751 | GameInputs in; |
npatel387 | 2:22d36e7740f1 | 752 | int action; |
npatel387 | 2:22d36e7740f1 | 753 | int draw; |
rconnorlawson | 0:35660d7952f7 | 754 | // Main game loop |
rconnorlawson | 0:35660d7952f7 | 755 | while(1) |
rconnorlawson | 0:35660d7952f7 | 756 | { |
rconnorlawson | 0:35660d7952f7 | 757 | // Timer to measure game update speed |
rconnorlawson | 0:35660d7952f7 | 758 | Timer t; t.start(); |
rconnorlawson | 0:35660d7952f7 | 759 | |
rconnorlawson | 0:35660d7952f7 | 760 | // Actuall do the game update: |
npatel387 | 2:22d36e7740f1 | 761 | // 1. Read inputs |
npatel387 | 2:22d36e7740f1 | 762 | in = read_inputs(); |
npatel387 | 2:22d36e7740f1 | 763 | // 2. Determine action (get_action) |
npatel387 | 2:22d36e7740f1 | 764 | action = get_action(in); |
rconnorlawson | 0:35660d7952f7 | 765 | // 3. Update game (update_game) |
npatel387 | 2:22d36e7740f1 | 766 | draw = update_game(action); |
rconnorlawson | 0:35660d7952f7 | 767 | // 3b. Check for game over |
rconnorlawson | 0:35660d7952f7 | 768 | // 4. Draw frame (draw_game) |
npatel387 | 2:22d36e7740f1 | 769 | draw_game(draw); |
rconnorlawson | 0:35660d7952f7 | 770 | |
rconnorlawson | 0:35660d7952f7 | 771 | // 5. Frame delay |
rconnorlawson | 0:35660d7952f7 | 772 | t.stop(); |
rconnorlawson | 0:35660d7952f7 | 773 | int dt = t.read_ms(); |
rconnorlawson | 0:35660d7952f7 | 774 | if (dt < 100) wait_ms(100 - dt); |
rconnorlawson | 0:35660d7952f7 | 775 | } |
rconnorlawson | 0:35660d7952f7 | 776 | } |