Randall Kliman / Mbed 2 deprecated NEOPUNK

Dependencies:   mbed wave_player 4DGL-uLCD-SE MMA8452

Committer:
Sterofin
Date:
Fri Nov 30 02:53:05 2018 +0000
Revision:
2:06c63d567719
Parent:
0:35660d7952f7
Child:
3:664c79e2ceb5
doot

Who changed what in which revision?

UserRevisionLine numberNew 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"
Sterofin 2:06c63d567719 7 #include "hash_table.h"
Sterofin 2:06c63d567719 8
Sterofin 2:06c63d567719 9 GameInputs in;
Sterofin 2:06c63d567719 10 int actions;
Sterofin 2:06c63d567719 11 int update;
Sterofin 2:06c63d567719 12 bool god;
Sterofin 2:06c63d567719 13 bool puzzle = false;
Sterofin 2:06c63d567719 14 bool mapact = 0;
Sterofin 2:06c63d567719 15 int story = 0;
Sterofin 2:06c63d567719 16 unsigned int m_z=12434,m_w=33254;
Sterofin 2:06c63d567719 17 unsigned int rnd() {
Sterofin 2:06c63d567719 18 m_z = 36969 * (m_z & 65535) + (m_z >>16);
Sterofin 2:06c63d567719 19 m_w = 18000 * (m_w & 65535) + (m_w >>16);
Sterofin 2:06c63d567719 20 return ((m_z <<16) + m_w);
Sterofin 2:06c63d567719 21 }
Sterofin 2:06c63d567719 22
Sterofin 2:06c63d567719 23 unsigned int n[4];
Sterofin 2:06c63d567719 24
rconnorlawson 0:35660d7952f7 25
rconnorlawson 0:35660d7952f7 26 // Functions in this file
rconnorlawson 0:35660d7952f7 27 int get_action (GameInputs inputs);
rconnorlawson 0:35660d7952f7 28 int update_game (int action);
rconnorlawson 0:35660d7952f7 29 void draw_game (int init);
rconnorlawson 0:35660d7952f7 30 void init_main_map ();
rconnorlawson 0:35660d7952f7 31 int main ();
rconnorlawson 0:35660d7952f7 32
rconnorlawson 0:35660d7952f7 33 /**
rconnorlawson 0:35660d7952f7 34 * The main game state. Must include Player locations and previous locations for
rconnorlawson 0:35660d7952f7 35 * drawing to work properly. Other items can be added as needed.
rconnorlawson 0:35660d7952f7 36 */
rconnorlawson 0:35660d7952f7 37 struct {
rconnorlawson 0:35660d7952f7 38 int x,y; // Current locations
rconnorlawson 0:35660d7952f7 39 int px, py; // Previous locations
rconnorlawson 0:35660d7952f7 40 int has_key;
Sterofin 2:06c63d567719 41 int speed;
Sterofin 2:06c63d567719 42 bool can_teleport;
rconnorlawson 0:35660d7952f7 43 } Player;
rconnorlawson 0:35660d7952f7 44
Sterofin 2:06c63d567719 45 struct {
Sterofin 2:06c63d567719 46 bool has_boots;
Sterofin 2:06c63d567719 47 bool has_teleport;
Sterofin 2:06c63d567719 48 bool has_gun;
Sterofin 2:06c63d567719 49 bool has_printer;
Sterofin 2:06c63d567719 50 } Inventory;
Sterofin 2:06c63d567719 51
rconnorlawson 0:35660d7952f7 52 /**
rconnorlawson 0:35660d7952f7 53 * Given the game inputs, determine what kind of update needs to happen.
rconnorlawson 0:35660d7952f7 54 * Possbile return values are defined below.
rconnorlawson 0:35660d7952f7 55 */
rconnorlawson 0:35660d7952f7 56 #define NO_ACTION 0
rconnorlawson 0:35660d7952f7 57 #define ACTION_BUTTON 1
rconnorlawson 0:35660d7952f7 58 #define MENU_BUTTON 2
rconnorlawson 0:35660d7952f7 59 #define GO_LEFT 3
rconnorlawson 0:35660d7952f7 60 #define GO_RIGHT 4
rconnorlawson 0:35660d7952f7 61 #define GO_UP 5
rconnorlawson 0:35660d7952f7 62 #define GO_DOWN 6
Sterofin 2:06c63d567719 63 #define THRESH 0.3
Sterofin 2:06c63d567719 64 #define GOD_MODE 7
Sterofin 2:06c63d567719 65 #define BUTTON_3 8
Sterofin 2:06c63d567719 66 #define BUTTON_4 9
Sterofin 2:06c63d567719 67 int walkk;
rconnorlawson 0:35660d7952f7 68 int get_action(GameInputs inputs)
rconnorlawson 0:35660d7952f7 69 {
Sterofin 2:06c63d567719 70 //pc.printf("%d,%d\r\n",inputs.ax,inputs.ay);
Sterofin 2:06c63d567719 71 if(inputs.ax < -THRESH) return GO_UP;
Sterofin 2:06c63d567719 72 if(inputs.ax >= THRESH) return GO_DOWN;
Sterofin 2:06c63d567719 73 if(inputs.ay >= THRESH) return GO_RIGHT;
Sterofin 2:06c63d567719 74 if(inputs.ay < -THRESH) return GO_LEFT;
Sterofin 2:06c63d567719 75 if(!inputs.b1) return ACTION_BUTTON;
Sterofin 2:06c63d567719 76 if(!inputs.b2) return MENU_BUTTON;
Sterofin 2:06c63d567719 77 if(!inputs.b3 && !puzzle){
Sterofin 2:06c63d567719 78 pc.printf("Button 3 pressed");
Sterofin 2:06c63d567719 79 god = !god;
Sterofin 2:06c63d567719 80 if(god) uLCD.filled_rectangle(128,0,119,7,WHITE);
Sterofin 2:06c63d567719 81 else if(!god) uLCD.filled_rectangle(128,0,119,7,BLACK);
Sterofin 2:06c63d567719 82 }
Sterofin 2:06c63d567719 83 else if(!inputs.b3) return BUTTON_3;
Sterofin 2:06c63d567719 84 if(!inputs.b4) return BUTTON_4;
rconnorlawson 0:35660d7952f7 85 return NO_ACTION;
rconnorlawson 0:35660d7952f7 86 }
rconnorlawson 0:35660d7952f7 87
Sterofin 2:06c63d567719 88 int go_up()
Sterofin 2:06c63d567719 89 {
Sterofin 2:06c63d567719 90 if(get_north(Player.x,Player.y)->walkable || god)
Sterofin 2:06c63d567719 91 {
Sterofin 2:06c63d567719 92 for(walkk = 1;walkk < Player.speed;walkk++){
Sterofin 2:06c63d567719 93 if(!get_north(Player.x,Player.y-walkk)->walkable) break;
Sterofin 2:06c63d567719 94 }
Sterofin 2:06c63d567719 95 Player.y-=walkk;
Sterofin 2:06c63d567719 96 return 1;
Sterofin 2:06c63d567719 97 }else return 0;
Sterofin 2:06c63d567719 98 }
Sterofin 2:06c63d567719 99
Sterofin 2:06c63d567719 100 int go_down()
Sterofin 2:06c63d567719 101 {
Sterofin 2:06c63d567719 102 if(get_south(Player.x,Player.y)->walkable || god)
Sterofin 2:06c63d567719 103 {
Sterofin 2:06c63d567719 104 for(walkk = 1;walkk < Player.speed;walkk++){
Sterofin 2:06c63d567719 105 if(!get_south(Player.x,Player.y+walkk)->walkable) break;
Sterofin 2:06c63d567719 106 }
Sterofin 2:06c63d567719 107 Player.y+=walkk;
Sterofin 2:06c63d567719 108 return 1;
Sterofin 2:06c63d567719 109 }else return 0;
Sterofin 2:06c63d567719 110 }
Sterofin 2:06c63d567719 111
Sterofin 2:06c63d567719 112 int go_left()
Sterofin 2:06c63d567719 113 {
Sterofin 2:06c63d567719 114 if(get_west(Player.x,Player.y)->walkable || god)
Sterofin 2:06c63d567719 115 {
Sterofin 2:06c63d567719 116 for(walkk = 1;walkk < Player.speed;walkk++){
Sterofin 2:06c63d567719 117 if(!get_west(Player.x-walkk,Player.y)->walkable) break;
Sterofin 2:06c63d567719 118 }
Sterofin 2:06c63d567719 119 Player.x-=walkk;
Sterofin 2:06c63d567719 120 return 1;
Sterofin 2:06c63d567719 121 }else return 0;
Sterofin 2:06c63d567719 122 }
Sterofin 2:06c63d567719 123
Sterofin 2:06c63d567719 124 int go_right()
Sterofin 2:06c63d567719 125 {
Sterofin 2:06c63d567719 126 if(get_east(Player.x,Player.y)->walkable || god)
Sterofin 2:06c63d567719 127 {
Sterofin 2:06c63d567719 128 for(walkk = 1;walkk < Player.speed;walkk++){
Sterofin 2:06c63d567719 129 if(!get_east(Player.x+walkk,Player.y)->walkable) break;
Sterofin 2:06c63d567719 130 }
Sterofin 2:06c63d567719 131 Player.x+=walkk;
Sterofin 2:06c63d567719 132 return 1;
Sterofin 2:06c63d567719 133 }else return 0;
Sterofin 2:06c63d567719 134 }
Sterofin 2:06c63d567719 135
Sterofin 2:06c63d567719 136 //Looks at if there's a block of the desired type next to the player
Sterofin 2:06c63d567719 137 int checkType(int t)
Sterofin 2:06c63d567719 138 {
Sterofin 2:06c63d567719 139 MapItem* n = get_north(Player.x,Player.y);
Sterofin 2:06c63d567719 140 MapItem* s = get_south(Player.x,Player.y);
Sterofin 2:06c63d567719 141 MapItem* e = get_east(Player.x,Player.y);
Sterofin 2:06c63d567719 142 MapItem* w = get_west(Player.x,Player.y);
Sterofin 2:06c63d567719 143 //pc.printf("%d\r\n",n->type);
Sterofin 2:06c63d567719 144 // pc.printf("%d\r\n",s->type);
Sterofin 2:06c63d567719 145 // pc.printf("%d\r\n",e->type);
Sterofin 2:06c63d567719 146 // pc.printf("%d\r\n",w->type);
Sterofin 2:06c63d567719 147 if(n->type == t || s->type == t || e->type == t || w->type == t)
Sterofin 2:06c63d567719 148 {
Sterofin 2:06c63d567719 149 return 1;
Sterofin 2:06c63d567719 150 }else return 0;
Sterofin 2:06c63d567719 151 }
Sterofin 2:06c63d567719 152
Sterofin 2:06c63d567719 153 MapItem* find_type(int t)
Sterofin 2:06c63d567719 154 {
Sterofin 2:06c63d567719 155 MapItem* n = get_north(Player.x,Player.y);
Sterofin 2:06c63d567719 156 MapItem* s = get_south(Player.x,Player.y);
Sterofin 2:06c63d567719 157 MapItem* e = get_east(Player.x,Player.y);
Sterofin 2:06c63d567719 158 MapItem* w = get_west(Player.x,Player.y);
Sterofin 2:06c63d567719 159 //pc.printf("%d\r\n",n->type);
Sterofin 2:06c63d567719 160 // pc.printf("%d\r\n",s->type);
Sterofin 2:06c63d567719 161 // pc.printf("%d\r\n",e->type);
Sterofin 2:06c63d567719 162 // pc.printf("%d\r\n",w->type);
Sterofin 2:06c63d567719 163 if(n->type == t) return n;
Sterofin 2:06c63d567719 164 else if(s->type == t) return s;
Sterofin 2:06c63d567719 165 else if(e->type == t) return e;
Sterofin 2:06c63d567719 166 else if(w->type == t) return w;
Sterofin 2:06c63d567719 167 else return NULL;
Sterofin 2:06c63d567719 168 }
Sterofin 2:06c63d567719 169
Sterofin 2:06c63d567719 170 int convert(int num)
Sterofin 2:06c63d567719 171 {
Sterofin 2:06c63d567719 172 if(num == 1) return ACTION_BUTTON;
Sterofin 2:06c63d567719 173 else if(num == 2) return MENU_BUTTON;
Sterofin 2:06c63d567719 174 else if(num == 3) return BUTTON_3;
Sterofin 2:06c63d567719 175 else if(num == 4) return BUTTON_4;
Sterofin 2:06c63d567719 176 return 5;
Sterofin 2:06c63d567719 177 }
Sterofin 2:06c63d567719 178
Sterofin 2:06c63d567719 179 #define mx 4
Sterofin 2:06c63d567719 180 #define my 3
Sterofin 2:06c63d567719 181 void run_minigame(){
Sterofin 2:06c63d567719 182 char buffer[100];
Sterofin 2:06c63d567719 183 n[0] = rnd()%4+1;
Sterofin 2:06c63d567719 184 n[1] = rnd()%4+1;
Sterofin 2:06c63d567719 185 n[2] = rnd()%4+1;
Sterofin 2:06c63d567719 186 n[3] = rnd()%4+1;
Sterofin 2:06c63d567719 187 sprintf(buffer,"of numbers: %d%d%d%d",n[0],n[1],n[2],n[3]);
Sterofin 2:06c63d567719 188 speech(buffer,"");
Sterofin 2:06c63d567719 189 int solved = 0;
Sterofin 2:06c63d567719 190 while(solved < 4)
Sterofin 2:06c63d567719 191 {
Sterofin 2:06c63d567719 192 in = read_inputs();
Sterofin 2:06c63d567719 193 actions = get_action(in);
Sterofin 2:06c63d567719 194 if(solved == 0){
Sterofin 2:06c63d567719 195 if(actions == convert(n[0])){
Sterofin 2:06c63d567719 196 uLCD.locate(mx,my);
Sterofin 2:06c63d567719 197 sprintf(buffer," %d - - - ",n[0]);
Sterofin 2:06c63d567719 198 uLCD.printf(buffer);
Sterofin 2:06c63d567719 199 solved++;
Sterofin 2:06c63d567719 200 wait(0.5);
Sterofin 2:06c63d567719 201 }
Sterofin 2:06c63d567719 202 }
Sterofin 2:06c63d567719 203 else if(solved == 1){
Sterofin 2:06c63d567719 204 if(actions == convert(n[1])){
Sterofin 2:06c63d567719 205 uLCD.locate(mx,my);
Sterofin 2:06c63d567719 206 sprintf(buffer," %d %d - - ",n[0],n[1]);
Sterofin 2:06c63d567719 207 uLCD.printf(buffer);
Sterofin 2:06c63d567719 208 solved++;
Sterofin 2:06c63d567719 209 wait(0.5);
Sterofin 2:06c63d567719 210 }
Sterofin 2:06c63d567719 211 }
Sterofin 2:06c63d567719 212 else if(solved == 2){
Sterofin 2:06c63d567719 213 if(actions == convert(n[2])){
Sterofin 2:06c63d567719 214 uLCD.locate(mx,my);
Sterofin 2:06c63d567719 215 sprintf(buffer," %d %d %d - ",n[0],n[1],n[2]);
Sterofin 2:06c63d567719 216 uLCD.printf(buffer);
Sterofin 2:06c63d567719 217 solved++;
Sterofin 2:06c63d567719 218 wait(0.5);
Sterofin 2:06c63d567719 219 }
Sterofin 2:06c63d567719 220 }
Sterofin 2:06c63d567719 221 else if(solved == 3){
Sterofin 2:06c63d567719 222 if(actions == convert(n[3])){
Sterofin 2:06c63d567719 223 uLCD.locate(mx,my);
Sterofin 2:06c63d567719 224 sprintf(buffer," %d %d %d %d ",n[0],n[1],n[2],n[3]);
Sterofin 2:06c63d567719 225 uLCD.printf(buffer);
Sterofin 2:06c63d567719 226 solved++;
Sterofin 2:06c63d567719 227 wait(0.5);
Sterofin 2:06c63d567719 228 }
Sterofin 2:06c63d567719 229 }
Sterofin 2:06c63d567719 230 }
Sterofin 2:06c63d567719 231 }
Sterofin 2:06c63d567719 232
rconnorlawson 0:35660d7952f7 233 /**
rconnorlawson 0:35660d7952f7 234 * Update the game state based on the user action. For example, if the user
rconnorlawson 0:35660d7952f7 235 * requests GO_UP, then this function should determine if that is possible by
rconnorlawson 0:35660d7952f7 236 * consulting the map, and update the Player position accordingly.
rconnorlawson 0:35660d7952f7 237 *
rconnorlawson 0:35660d7952f7 238 * Return values are defined below. FULL_DRAW indicates that for this frame,
rconnorlawson 0:35660d7952f7 239 * draw_game should not optimize drawing and should draw every tile, even if
rconnorlawson 0:35660d7952f7 240 * the player has not moved.
rconnorlawson 0:35660d7952f7 241 */
rconnorlawson 0:35660d7952f7 242 #define NO_RESULT 0
rconnorlawson 0:35660d7952f7 243 #define GAME_OVER 1
rconnorlawson 0:35660d7952f7 244 #define FULL_DRAW 2
Sterofin 2:06c63d567719 245 MapItem* boi;
rconnorlawson 0:35660d7952f7 246 int update_game(int action)
rconnorlawson 0:35660d7952f7 247 {
rconnorlawson 0:35660d7952f7 248 // Save player previous location before updating
rconnorlawson 0:35660d7952f7 249 Player.px = Player.x;
rconnorlawson 0:35660d7952f7 250 Player.py = Player.y;
rconnorlawson 0:35660d7952f7 251
rconnorlawson 0:35660d7952f7 252 // Do different things based on the each action.
rconnorlawson 0:35660d7952f7 253 // You can define functions like "go_up()" that get called for each case.
rconnorlawson 0:35660d7952f7 254 switch(action)
rconnorlawson 0:35660d7952f7 255 {
Sterofin 2:06c63d567719 256 case GO_UP:
Sterofin 2:06c63d567719 257 go_up();
Sterofin 2:06c63d567719 258 break;
Sterofin 2:06c63d567719 259 case GO_LEFT:
Sterofin 2:06c63d567719 260 go_left();
Sterofin 2:06c63d567719 261 break;
Sterofin 2:06c63d567719 262 case GO_DOWN:
Sterofin 2:06c63d567719 263 go_down();
Sterofin 2:06c63d567719 264 break;
Sterofin 2:06c63d567719 265 case GO_RIGHT:
Sterofin 2:06c63d567719 266 go_right();
Sterofin 2:06c63d567719 267 break;
Sterofin 2:06c63d567719 268 case ACTION_BUTTON:
Sterofin 2:06c63d567719 269 pc.printf("Action Button Pressed\r\n");
Sterofin 2:06c63d567719 270 if(checkType(NPORTAL))
Sterofin 2:06c63d567719 271 {
Sterofin 2:06c63d567719 272 if(story == 2 || story == 3 || story == 4){
Sterofin 2:06c63d567719 273 speech("Don't leave! The","hack is almost");
Sterofin 2:06c63d567719 274 speech("finished!","");
Sterofin 2:06c63d567719 275 return FULL_DRAW;
Sterofin 2:06c63d567719 276 }
Sterofin 2:06c63d567719 277 pc.printf("Entering Portal");
Sterofin 2:06c63d567719 278 if(!mapact){
Sterofin 2:06c63d567719 279 set_active_map(1);
Sterofin 2:06c63d567719 280 mapact = 1;
Sterofin 2:06c63d567719 281 Player.x = 2;
Sterofin 2:06c63d567719 282 Player.y = 5;
Sterofin 2:06c63d567719 283 }
Sterofin 2:06c63d567719 284 else if(mapact){
Sterofin 2:06c63d567719 285 set_active_map(0);
Sterofin 2:06c63d567719 286 mapact = 0;
Sterofin 2:06c63d567719 287 Player.x = 37;
Sterofin 2:06c63d567719 288 Player.y = 44;
Sterofin 2:06c63d567719 289 }
Sterofin 2:06c63d567719 290 uLCD.filled_rectangle(0,0,127,9, 0x0);
Sterofin 2:06c63d567719 291 return FULL_DRAW;
Sterofin 2:06c63d567719 292 }
Sterofin 2:06c63d567719 293 else if(checkType(NPC))
Sterofin 2:06c63d567719 294 {
Sterofin 2:06c63d567719 295 if(story == 0){
Sterofin 2:06c63d567719 296 speech("Hey, want to make","some quick cash?");
Sterofin 2:06c63d567719 297 speech("Head over to the","netPortal next");
Sterofin 2:06c63d567719 298 speech("door. Hack","BULLETCORPs server");
Sterofin 2:06c63d567719 299 speech("so we can steal","some of their");
Sterofin 2:06c63d567719 300 speech("advanced tech.","That is, if");
Sterofin 2:06c63d567719 301 speech("you're up for","the job");
Sterofin 2:06c63d567719 302 story++;
Sterofin 2:06c63d567719 303 }
Sterofin 2:06c63d567719 304 else if(story == 1)
Sterofin 2:06c63d567719 305 {
Sterofin 2:06c63d567719 306 speech("C'mon! What are","you waiting for?");
Sterofin 2:06c63d567719 307 }
Sterofin 2:06c63d567719 308 else if(story == 6)
Sterofin 2:06c63d567719 309 {
Sterofin 2:06c63d567719 310 speech("Thanks for the","codes. here's a ");
Sterofin 2:06c63d567719 311 speech("chip that will","let you into");
Sterofin 2:06c63d567719 312 speech("BULLETCORP's","secret tech room");
Sterofin 2:06c63d567719 313 Player.has_key = true;
Sterofin 2:06c63d567719 314 }
Sterofin 2:06c63d567719 315 return FULL_DRAW;
Sterofin 2:06c63d567719 316 }
Sterofin 2:06c63d567719 317 else if(checkType(TERMINAL))
Sterofin 2:06c63d567719 318 {
Sterofin 2:06c63d567719 319 if(story == 1){
Sterofin 2:06c63d567719 320 speech("Pondsmith:","~ksshhk~");
Sterofin 2:06c63d567719 321 speech("Nice, you're in.","Use the terminals");
Sterofin 2:06c63d567719 322 speech("to decypher the","BULLETCORP access");
Sterofin 2:06c63d567719 323 speech("codes. There are","4 codes. Each");
Sterofin 2:06c63d567719 324 speech("terminal will","give one of the");
Sterofin 2:06c63d567719 325 speech("access codes.","");
Sterofin 2:06c63d567719 326 uLCD.cls();
Sterofin 2:06c63d567719 327 puzzle = true;
Sterofin 2:06c63d567719 328 uLCD.locate(mx,my);
Sterofin 2:06c63d567719 329 uLCD.printf(" - - - - ");
Sterofin 2:06c63d567719 330 speech("Ok, here is","the first string");
Sterofin 2:06c63d567719 331 run_minigame();
Sterofin 2:06c63d567719 332 story++;
Sterofin 2:06c63d567719 333 if((boi = find_type(TERMINAL))){
Sterofin 2:06c63d567719 334 boi->type = HACKED_TERMINAL;
Sterofin 2:06c63d567719 335 boi->draw = draw_hacked_terminal;
Sterofin 2:06c63d567719 336 }
Sterofin 2:06c63d567719 337 puzzle = false;
Sterofin 2:06c63d567719 338 return FULL_DRAW;
Sterofin 2:06c63d567719 339 }
Sterofin 2:06c63d567719 340 else if(story == 2){
Sterofin 2:06c63d567719 341 uLCD.cls();
Sterofin 2:06c63d567719 342 puzzle = true;
Sterofin 2:06c63d567719 343 uLCD.locate(mx,my);
Sterofin 2:06c63d567719 344 uLCD.printf(" - - - - ");
Sterofin 2:06c63d567719 345 speech("Ok, here is","the second string");
Sterofin 2:06c63d567719 346 run_minigame();
Sterofin 2:06c63d567719 347 story++;
Sterofin 2:06c63d567719 348 if((boi = find_type(TERMINAL))){
Sterofin 2:06c63d567719 349 boi->type = HACKED_TERMINAL;
Sterofin 2:06c63d567719 350 boi->draw = draw_hacked_terminal;
Sterofin 2:06c63d567719 351 }
Sterofin 2:06c63d567719 352 puzzle = false;
Sterofin 2:06c63d567719 353 return FULL_DRAW;
Sterofin 2:06c63d567719 354 }
Sterofin 2:06c63d567719 355 else if(story == 3){
Sterofin 2:06c63d567719 356 uLCD.cls();
Sterofin 2:06c63d567719 357 puzzle = true;
Sterofin 2:06c63d567719 358 uLCD.locate(mx,my);
Sterofin 2:06c63d567719 359 uLCD.printf(" - - - - ");
Sterofin 2:06c63d567719 360 speech("Ok, here is","the third string");
Sterofin 2:06c63d567719 361 run_minigame();
Sterofin 2:06c63d567719 362 story++;
Sterofin 2:06c63d567719 363 if((boi = find_type(TERMINAL))){
Sterofin 2:06c63d567719 364 boi->type = HACKED_TERMINAL;
Sterofin 2:06c63d567719 365 boi->draw = draw_hacked_terminal;
Sterofin 2:06c63d567719 366 }
Sterofin 2:06c63d567719 367 puzzle = false;
Sterofin 2:06c63d567719 368 return FULL_DRAW;
Sterofin 2:06c63d567719 369 }
Sterofin 2:06c63d567719 370 else if(story == 4){
Sterofin 2:06c63d567719 371 uLCD.cls();
Sterofin 2:06c63d567719 372 puzzle = true;
Sterofin 2:06c63d567719 373 uLCD.locate(mx,my);
Sterofin 2:06c63d567719 374 uLCD.printf(" - - - - ");
Sterofin 2:06c63d567719 375 speech("Ok, here is","the fourth string");
Sterofin 2:06c63d567719 376 run_minigame();
Sterofin 2:06c63d567719 377 story++;
Sterofin 2:06c63d567719 378 if((boi = find_type(TERMINAL))){
Sterofin 2:06c63d567719 379 boi->type = HACKED_TERMINAL;
Sterofin 2:06c63d567719 380 boi->draw = draw_hacked_terminal;
Sterofin 2:06c63d567719 381 }
Sterofin 2:06c63d567719 382 puzzle = false;
Sterofin 2:06c63d567719 383 return FULL_DRAW;
Sterofin 2:06c63d567719 384 }
Sterofin 2:06c63d567719 385 }
Sterofin 2:06c63d567719 386 else if(checkType(DOOR) && Player.has_key == true){
Sterofin 2:06c63d567719 387 if(boi = find_type(DOOR)) boi->walkable = true;
Sterofin 2:06c63d567719 388 }
Sterofin 2:06c63d567719 389 else if(checkType(TELEPORT)){
Sterofin 2:06c63d567719 390 Inventory.has_teleport = true;
Sterofin 2:06c63d567719 391 if(boi = find_type(TELEPORT)) deleteItem(get_active_map()->items, XY_KEY(boi->x, boi->y));
Sterofin 2:06c63d567719 392 }
Sterofin 2:06c63d567719 393 else if(checkType(KILL)){
Sterofin 2:06c63d567719 394 Inventory.has_gun = true;
Sterofin 2:06c63d567719 395 if(boi = find_type(KILL)) deleteItem(get_active_map()->items, XY_KEY(boi->x, boi->y));
Sterofin 2:06c63d567719 396 }
Sterofin 2:06c63d567719 397 else if(checkType(BOOTS)){
Sterofin 2:06c63d567719 398 Inventory.has_boots = true;
Sterofin 2:06c63d567719 399 Player.speed = 2;
Sterofin 2:06c63d567719 400 if(boi = find_type(BOOTS)) deleteItem(get_active_map()->items, XY_KEY(boi->x, boi->y));
Sterofin 2:06c63d567719 401 }
Sterofin 2:06c63d567719 402 else if(checkType(PRINTER)){
Sterofin 2:06c63d567719 403 Inventory.has_printer = true;
Sterofin 2:06c63d567719 404 if(boi = find_type(PRINTER)) deleteItem(get_active_map()->items, XY_KEY(boi->x, boi->y));
Sterofin 2:06c63d567719 405 }
Sterofin 2:06c63d567719 406 break;
Sterofin 2:06c63d567719 407 case MENU_BUTTON:
Sterofin 2:06c63d567719 408 pc.printf("Menu Button Pressed\r\n");
Sterofin 2:06c63d567719 409 break;
Sterofin 2:06c63d567719 410 case BUTTON_4:
Sterofin 2:06c63d567719 411 pc.printf("Button 4 Pressed\r\n");
Sterofin 2:06c63d567719 412 //NVIC_SystemReset();
Sterofin 2:06c63d567719 413 break;
rconnorlawson 0:35660d7952f7 414 default: break;
rconnorlawson 0:35660d7952f7 415 }
rconnorlawson 0:35660d7952f7 416 return NO_RESULT;
rconnorlawson 0:35660d7952f7 417 }
rconnorlawson 0:35660d7952f7 418
rconnorlawson 0:35660d7952f7 419 /**
rconnorlawson 0:35660d7952f7 420 * Entry point for frame drawing. This should be called once per iteration of
rconnorlawson 0:35660d7952f7 421 * the game loop. This draws all tiles on the screen, followed by the status
rconnorlawson 0:35660d7952f7 422 * bars. Unless init is nonzero, this function will optimize drawing by only
rconnorlawson 0:35660d7952f7 423 * drawing tiles that have changed from the previous frame.
rconnorlawson 0:35660d7952f7 424 */
rconnorlawson 0:35660d7952f7 425 void draw_game(int init)
rconnorlawson 0:35660d7952f7 426 {
rconnorlawson 0:35660d7952f7 427 // Draw game border first
rconnorlawson 0:35660d7952f7 428 if(init) draw_border();
rconnorlawson 0:35660d7952f7 429
rconnorlawson 0:35660d7952f7 430 // Iterate over all visible map tiles
rconnorlawson 0:35660d7952f7 431 for (int i = -5; i <= 5; i++) // Iterate over columns of tiles
rconnorlawson 0:35660d7952f7 432 {
rconnorlawson 0:35660d7952f7 433 for (int j = -4; j <= 4; j++) // Iterate over one column of tiles
rconnorlawson 0:35660d7952f7 434 {
rconnorlawson 0:35660d7952f7 435 // Here, we have a given (i,j)
rconnorlawson 0:35660d7952f7 436
rconnorlawson 0:35660d7952f7 437 // Compute the current map (x,y) of this tile
rconnorlawson 0:35660d7952f7 438 int x = i + Player.x;
rconnorlawson 0:35660d7952f7 439 int y = j + Player.y;
rconnorlawson 0:35660d7952f7 440
rconnorlawson 0:35660d7952f7 441 // Compute the previous map (px, py) of this tile
rconnorlawson 0:35660d7952f7 442 int px = i + Player.px;
rconnorlawson 0:35660d7952f7 443 int py = j + Player.py;
rconnorlawson 0:35660d7952f7 444
rconnorlawson 0:35660d7952f7 445 // Compute u,v coordinates for drawing
rconnorlawson 0:35660d7952f7 446 int u = (i+5)*11 + 3;
rconnorlawson 0:35660d7952f7 447 int v = (j+4)*11 + 15;
rconnorlawson 0:35660d7952f7 448
rconnorlawson 0:35660d7952f7 449 // Figure out what to draw
rconnorlawson 0:35660d7952f7 450 DrawFunc draw = NULL;
Sterofin 2:06c63d567719 451 if (i == 0 && j == 0) // Only draw the player on init
rconnorlawson 0:35660d7952f7 452 {
rconnorlawson 0:35660d7952f7 453 draw_player(u, v, Player.has_key);
rconnorlawson 0:35660d7952f7 454 continue;
rconnorlawson 0:35660d7952f7 455 }
rconnorlawson 0:35660d7952f7 456 else if (x >= 0 && y >= 0 && x < map_width() && y < map_height()) // Current (i,j) in the map
rconnorlawson 0:35660d7952f7 457 {
rconnorlawson 0:35660d7952f7 458 MapItem* curr_item = get_here(x, y);
rconnorlawson 0:35660d7952f7 459 MapItem* prev_item = get_here(px, py);
rconnorlawson 0:35660d7952f7 460 if (init || curr_item != prev_item) // Only draw if they're different
rconnorlawson 0:35660d7952f7 461 {
rconnorlawson 0:35660d7952f7 462 if (curr_item) // There's something here! Draw it
rconnorlawson 0:35660d7952f7 463 {
rconnorlawson 0:35660d7952f7 464 draw = curr_item->draw;
rconnorlawson 0:35660d7952f7 465 }
rconnorlawson 0:35660d7952f7 466 else // There used to be something, but now there isn't
rconnorlawson 0:35660d7952f7 467 {
rconnorlawson 0:35660d7952f7 468 draw = draw_nothing;
rconnorlawson 0:35660d7952f7 469 }
rconnorlawson 0:35660d7952f7 470 }
rconnorlawson 0:35660d7952f7 471 }
rconnorlawson 0:35660d7952f7 472 else if (init) // If doing a full draw, but we're out of bounds, draw the walls.
rconnorlawson 0:35660d7952f7 473 {
rconnorlawson 0:35660d7952f7 474 draw = draw_wall;
rconnorlawson 0:35660d7952f7 475 }
rconnorlawson 0:35660d7952f7 476
rconnorlawson 0:35660d7952f7 477 // Actually draw the tile
rconnorlawson 0:35660d7952f7 478 if (draw) draw(u, v);
rconnorlawson 0:35660d7952f7 479 }
rconnorlawson 0:35660d7952f7 480 }
rconnorlawson 0:35660d7952f7 481
Sterofin 2:06c63d567719 482 // Draw status bars
Sterofin 2:06c63d567719 483 draw_upper_status(Player.x,Player.y);
rconnorlawson 0:35660d7952f7 484 draw_lower_status();
rconnorlawson 0:35660d7952f7 485 }
rconnorlawson 0:35660d7952f7 486
rconnorlawson 0:35660d7952f7 487
rconnorlawson 0:35660d7952f7 488 /**
rconnorlawson 0:35660d7952f7 489 * Initialize the main world map. Add walls around the edges, interior chambers,
rconnorlawson 0:35660d7952f7 490 * and plants in the background so you can see motion.
rconnorlawson 0:35660d7952f7 491 */
rconnorlawson 0:35660d7952f7 492 void init_main_map()
rconnorlawson 0:35660d7952f7 493 {
rconnorlawson 0:35660d7952f7 494 Map* map = set_active_map(0);
rconnorlawson 0:35660d7952f7 495
rconnorlawson 0:35660d7952f7 496 pc.printf("Adding walls!\r\n");
rconnorlawson 0:35660d7952f7 497 add_wall(0, 0, HORIZONTAL, map_width());
rconnorlawson 0:35660d7952f7 498 add_wall(0, map_height()-1, HORIZONTAL, map_width());
rconnorlawson 0:35660d7952f7 499 add_wall(0, 0, VERTICAL, map_height());
rconnorlawson 0:35660d7952f7 500 add_wall(map_width()-1, 0, VERTICAL, map_height());
Sterofin 2:06c63d567719 501
Sterofin 2:06c63d567719 502
Sterofin 2:06c63d567719 503 add_wall(0,38,HORIZONTAL,4);
Sterofin 2:06c63d567719 504 add_wall(6,38,HORIZONTAL,15);
Sterofin 2:06c63d567719 505 add_wall(0,29,HORIZONTAL,2);
Sterofin 2:06c63d567719 506 add_wall(4,29,HORIZONTAL,26);
Sterofin 2:06c63d567719 507 add_door(2,29,false);
Sterofin 2:06c63d567719 508 add_door(3,29,false);
Sterofin 2:06c63d567719 509 add_boots(3,22);
Sterofin 2:06c63d567719 510 add_teleport(5,24);
Sterofin 2:06c63d567719 511 add_orgprint(8,21);
Sterofin 2:06c63d567719 512 add_kill(15,19);
Sterofin 2:06c63d567719 513 add_wall(0,17,HORIZONTAL,40);
Sterofin 2:06c63d567719 514 add_wall(0,13,HORIZONTAL,2);
Sterofin 2:06c63d567719 515 add_door(2,13,false);
Sterofin 2:06c63d567719 516 add_door(3,13,false);
Sterofin 2:06c63d567719 517 add_wall(4,13,HORIZONTAL,36);
Sterofin 2:06c63d567719 518 add_wall(39,0,VERTICAL,13);
Sterofin 2:06c63d567719 519 add_wall(11,0,VERTICAL,13);
Sterofin 2:06c63d567719 520 add_wall(39,17,VERTICAL,13);
Sterofin 2:06c63d567719 521 add_wall(39,38,VERTICAL,12);
Sterofin 2:06c63d567719 522 add_wall(26,38,VERTICAL,10);
Sterofin 2:06c63d567719 523 add_wall(21,38,VERTICAL,4);
Sterofin 2:06c63d567719 524 add_wall(21,44,VERTICAL,5);
Sterofin 2:06c63d567719 525 add_wall(31,38,HORIZONTAL,9);
Sterofin 2:06c63d567719 526 add_wall(27,38,HORIZONTAL,2);
Sterofin 2:06c63d567719 527 add_netPortal(38,44);
Sterofin 2:06c63d567719 528 add_NPC(5,42);
rconnorlawson 0:35660d7952f7 529 pc.printf("Walls done!\r\n");
rconnorlawson 0:35660d7952f7 530
Sterofin 2:06c63d567719 531 //print_map();
Sterofin 2:06c63d567719 532 }
Sterofin 2:06c63d567719 533
Sterofin 2:06c63d567719 534 void init_sub_map()
Sterofin 2:06c63d567719 535 {
Sterofin 2:06c63d567719 536 set_active_map(1);
Sterofin 2:06c63d567719 537 pc.printf("Adding walls!\r\n");
Sterofin 2:06c63d567719 538 add_wall(0, 0, HORIZONTAL, map_width());
Sterofin 2:06c63d567719 539 add_wall(0, map_height()-1, HORIZONTAL, map_width());
Sterofin 2:06c63d567719 540 add_wall(0, 0, VERTICAL, map_height());
Sterofin 2:06c63d567719 541 add_wall(map_width()-1, 0, VERTICAL, map_height());
Sterofin 2:06c63d567719 542 add_terminal(3,3);
Sterofin 2:06c63d567719 543 add_terminal(7,3);
Sterofin 2:06c63d567719 544 add_terminal(3,7);
Sterofin 2:06c63d567719 545 add_terminal(7,7);
Sterofin 2:06c63d567719 546 add_netPortal(1,5);
rconnorlawson 0:35660d7952f7 547 }
rconnorlawson 0:35660d7952f7 548
rconnorlawson 0:35660d7952f7 549 /**
rconnorlawson 0:35660d7952f7 550 * Program entry point! This is where it all begins.
rconnorlawson 0:35660d7952f7 551 * This function orchestrates all the parts of the game. Most of your
rconnorlawson 0:35660d7952f7 552 * implementation should be elsewhere - this holds the game loop, and should
rconnorlawson 0:35660d7952f7 553 * read like a road map for the rest of the code.
rconnorlawson 0:35660d7952f7 554 */
Sterofin 2:06c63d567719 555
Sterofin 2:06c63d567719 556
Sterofin 2:06c63d567719 557
rconnorlawson 0:35660d7952f7 558 int main()
rconnorlawson 0:35660d7952f7 559 {
rconnorlawson 0:35660d7952f7 560 // First things first: initialize hardware
rconnorlawson 0:35660d7952f7 561 ASSERT_P(hardware_init() == ERROR_NONE, "Hardware init failed!");
Sterofin 2:06c63d567719 562 //hardware_init();
Sterofin 2:06c63d567719 563 pc.printf("Initializing Game\r\n");
Sterofin 2:06c63d567719 564 in = read_inputs();
Sterofin 2:06c63d567719 565 actions = get_action(in);
Sterofin 2:06c63d567719 566 uLCD.text_width(2); //4X size text
Sterofin 2:06c63d567719 567 uLCD.text_height(2);
Sterofin 2:06c63d567719 568 uLCD.color(0x00ffd8);
Sterofin 2:06c63d567719 569 uLCD.set_font(FONT_5X7);
Sterofin 2:06c63d567719 570 uLCD.text_bold(true);
Sterofin 2:06c63d567719 571 uLCD.printf("\n\n N E O\n");
Sterofin 2:06c63d567719 572 uLCD.color(0xff00e4);
Sterofin 2:06c63d567719 573 uLCD.printf("\n P U N K\n");
Sterofin 2:06c63d567719 574 uLCD.text_width(1); //4X size text
Sterofin 2:06c63d567719 575 uLCD.text_height(1);
Sterofin 2:06c63d567719 576 uLCD.color(RED);
Sterofin 2:06c63d567719 577 bool swap = 0;
Sterofin 2:06c63d567719 578 int flash = 0;
Sterofin 2:06c63d567719 579 while(actions != ACTION_BUTTON)
Sterofin 2:06c63d567719 580 {
Sterofin 2:06c63d567719 581 if(flash >= 600){
Sterofin 2:06c63d567719 582 swap = !swap;
Sterofin 2:06c63d567719 583 flash = 0;
Sterofin 2:06c63d567719 584 if(swap) draw_menu_2();
Sterofin 2:06c63d567719 585 else if(!swap) draw_menu_1();
Sterofin 2:06c63d567719 586 }
Sterofin 2:06c63d567719 587 if(swap) flash+=3;
Sterofin 2:06c63d567719 588 else if(!swap) flash++;
Sterofin 2:06c63d567719 589
Sterofin 2:06c63d567719 590
Sterofin 2:06c63d567719 591 in = read_inputs();
Sterofin 2:06c63d567719 592 actions = get_action(in);
Sterofin 2:06c63d567719 593 }
Sterofin 2:06c63d567719 594 uLCD.cls();
Sterofin 2:06c63d567719 595 uLCD.text_width(1); //4X size text
Sterofin 2:06c63d567719 596 uLCD.text_height(1);
Sterofin 2:06c63d567719 597 uLCD.color(GREEN);
Sterofin 2:06c63d567719 598 uLCD.set_font(FONT_7X8);
rconnorlawson 0:35660d7952f7 599 // Initialize the maps
rconnorlawson 0:35660d7952f7 600 maps_init();
rconnorlawson 0:35660d7952f7 601 init_main_map();
Sterofin 2:06c63d567719 602 init_sub_map();
Sterofin 2:06c63d567719 603 set_active_map(0);
Sterofin 2:06c63d567719 604 print_map();
Sterofin 2:06c63d567719 605 pc.printf("Map initialized\r\n");
rconnorlawson 0:35660d7952f7 606
rconnorlawson 0:35660d7952f7 607 // Initialize game state
Sterofin 2:06c63d567719 608 Player.x = 3;
Sterofin 2:06c63d567719 609 Player.y = 33;
Sterofin 2:06c63d567719 610 Player.speed = 1;
rconnorlawson 0:35660d7952f7 611 // Initial drawing
rconnorlawson 0:35660d7952f7 612 draw_game(true);
rconnorlawson 0:35660d7952f7 613
rconnorlawson 0:35660d7952f7 614 // Main game loop
rconnorlawson 0:35660d7952f7 615 while(1)
rconnorlawson 0:35660d7952f7 616 {
Sterofin 2:06c63d567719 617 //pc.printf("Game Running\r\n");
rconnorlawson 0:35660d7952f7 618 // Timer to measure game update speed
rconnorlawson 0:35660d7952f7 619 Timer t; t.start();
rconnorlawson 0:35660d7952f7 620
Sterofin 2:06c63d567719 621 // Actually do the game update:
Sterofin 2:06c63d567719 622 // 1. Read inputs
Sterofin 2:06c63d567719 623 in = read_inputs();
Sterofin 2:06c63d567719 624 //pc.printf("Gathering Sensor Data\r\n");
Sterofin 2:06c63d567719 625 // 2. Determine action (get_action)
Sterofin 2:06c63d567719 626 actions = get_action(in);
Sterofin 2:06c63d567719 627 //pc.printf("Determining Action\r\n");
rconnorlawson 0:35660d7952f7 628 // 3. Update game (update_game)
Sterofin 2:06c63d567719 629 update = update_game(actions);
Sterofin 2:06c63d567719 630 //pc.printf("Updating Game State\r\n");
Sterofin 2:06c63d567719 631 //pc.printf("Game State: %d\r\n", gameState);
rconnorlawson 0:35660d7952f7 632 // 3b. Check for game over
Sterofin 2:06c63d567719 633 //if(update == 0) break;
rconnorlawson 0:35660d7952f7 634 // 4. Draw frame (draw_game)
Sterofin 2:06c63d567719 635 //pc.printf("Drawing Game\r\n");
Sterofin 2:06c63d567719 636 draw_game(update);
rconnorlawson 0:35660d7952f7 637
rconnorlawson 0:35660d7952f7 638 // 5. Frame delay
rconnorlawson 0:35660d7952f7 639 t.stop();
rconnorlawson 0:35660d7952f7 640 int dt = t.read_ms();
rconnorlawson 0:35660d7952f7 641 if (dt < 100) wait_ms(100 - dt);
Sterofin 2:06c63d567719 642
Sterofin 2:06c63d567719 643 if(story == 5){
Sterofin 2:06c63d567719 644 speech("Nice! You got","the codes.");
Sterofin 2:06c63d567719 645 speech("Come back to me","and i'll");
Sterofin 2:06c63d567719 646 speech("compile them into","a security chip");
Sterofin 2:06c63d567719 647 speech("you can use to","break into");
Sterofin 2:06c63d567719 648 speech("BULLETCORP","");
Sterofin 2:06c63d567719 649 story++;
Sterofin 2:06c63d567719 650 draw_game(FULL_DRAW);
Sterofin 2:06c63d567719 651 }
rconnorlawson 0:35660d7952f7 652 }
rconnorlawson 0:35660d7952f7 653 }