Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed FATFileSystem
Game_Manager/Game_Manager.cpp@26:716bcd47f3ca, 2019-05-10 (annotated)
- Committer:
- rottenegg
- Date:
- Fri May 10 21:25:27 2019 +0000
- Revision:
- 26:716bcd47f3ca
- Parent:
- 21:f3b0ce18b44f
FINAL_SUBMISSION; ; Changes:; WDplayer: Major Memory Leek fixed related to fclose(); Game_Manager: Tuned Scene Order and Improved Random number generator.; SceneFuctions: Added a Randomly changing Object to Scene 4.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rottenegg | 14:165fff6d1854 | 1 | #include "Game_Manager.h" |
rottenegg | 14:165fff6d1854 | 2 | #include "AllPaths.h" |
rottenegg | 14:165fff6d1854 | 3 | |
rottenegg | 14:165fff6d1854 | 4 | Timer Time; |
rottenegg | 21:f3b0ce18b44f | 5 | Serial PC(USBTX,USBRX); |
rottenegg | 14:165fff6d1854 | 6 | |
rottenegg | 21:f3b0ce18b44f | 7 | Game_Manager::Game_Manager(PinName mosi, |
rottenegg | 21:f3b0ce18b44f | 8 | PinName miso, |
rottenegg | 21:f3b0ce18b44f | 9 | PinName sclk, |
rottenegg | 21:f3b0ce18b44f | 10 | PinName cs, |
rottenegg | 21:f3b0ce18b44f | 11 | PinName sce, |
rottenegg | 21:f3b0ce18b44f | 12 | PinName rst, |
rottenegg | 21:f3b0ce18b44f | 13 | PinName dc, |
rottenegg | 21:f3b0ce18b44f | 14 | PinName mosilcd, |
rottenegg | 21:f3b0ce18b44f | 15 | PinName sclklcd, |
rottenegg | 21:f3b0ce18b44f | 16 | PinName led, |
rottenegg | 21:f3b0ce18b44f | 17 | PinName b1, |
rottenegg | 21:f3b0ce18b44f | 18 | PinName jh, |
rottenegg | 21:f3b0ce18b44f | 19 | PinName jv, |
rottenegg | 14:165fff6d1854 | 20 | PinName wd, |
rottenegg | 21:f3b0ce18b44f | 21 | PinName sc, |
rottenegg | 21:f3b0ce18b44f | 22 | PinName start ) |
rottenegg | 14:165fff6d1854 | 23 | : |
rottenegg | 21:f3b0ce18b44f | 24 | sd(new SDFileSystem(mosi, miso, sclk, cs, "sd")), |
rottenegg | 21:f3b0ce18b44f | 25 | lcd(new Bitmap(sce,rst,dc,mosilcd,sclklcd,led)), |
rottenegg | 21:f3b0ce18b44f | 26 | CM(new CaMove(b1,jh,jv)), |
rottenegg | 14:165fff6d1854 | 27 | wav(new WDplayer(wd)), |
rottenegg | 21:f3b0ce18b44f | 28 | SNC(new SceneCreator(sc)), |
rottenegg | 21:f3b0ce18b44f | 29 | str(new DigitalIn(start)) |
rottenegg | 21:f3b0ce18b44f | 30 | { |
rottenegg | 21:f3b0ce18b44f | 31 | str->mode(PullDown); |
rottenegg | 21:f3b0ce18b44f | 32 | }; |
rottenegg | 14:165fff6d1854 | 33 | |
rottenegg | 21:f3b0ce18b44f | 34 | Game_Manager::~Game_Manager() { |
rottenegg | 21:f3b0ce18b44f | 35 | delete (sd); |
rottenegg | 21:f3b0ce18b44f | 36 | delete (lcd); |
rottenegg | 21:f3b0ce18b44f | 37 | delete (CM); |
rottenegg | 21:f3b0ce18b44f | 38 | delete (wav); |
rottenegg | 21:f3b0ce18b44f | 39 | delete (SNC); |
rottenegg | 21:f3b0ce18b44f | 40 | delete (str); |
rottenegg | 21:f3b0ce18b44f | 41 | } |
rottenegg | 14:165fff6d1854 | 42 | |
rottenegg | 14:165fff6d1854 | 43 | bool Game_Manager::boot_error() { |
rottenegg | 14:165fff6d1854 | 44 | //Check SD card Status |
rottenegg | 14:165fff6d1854 | 45 | lcd->init(); |
rottenegg | 21:f3b0ce18b44f | 46 | this->boot_msg(0); |
rottenegg | 14:165fff6d1854 | 47 | sd->disk_initialize(); |
rottenegg | 14:165fff6d1854 | 48 | int check = sd->disk_status(); |
rottenegg | 14:165fff6d1854 | 49 | if (check == 1) { |
rottenegg | 21:f3b0ce18b44f | 50 | this->boot_msg(1); |
rottenegg | 14:165fff6d1854 | 51 | return true; |
rottenegg | 14:165fff6d1854 | 52 | } else { |
rottenegg | 14:165fff6d1854 | 53 | std::cerr << "Error Report" << std::endl; |
rottenegg | 14:165fff6d1854 | 54 | if (this->check()) { |
rottenegg | 21:f3b0ce18b44f | 55 | this->boot_msg(2); |
rottenegg | 14:165fff6d1854 | 56 | return true; |
rottenegg | 14:165fff6d1854 | 57 | } else { |
rottenegg | 14:165fff6d1854 | 58 | std::cerr << "Boot Successful No Errors" << std::endl; |
rottenegg | 14:165fff6d1854 | 59 | } |
rottenegg | 14:165fff6d1854 | 60 | } |
rottenegg | 14:165fff6d1854 | 61 | return false; |
rottenegg | 14:165fff6d1854 | 62 | } |
rottenegg | 14:165fff6d1854 | 63 | |
rottenegg | 21:f3b0ce18b44f | 64 | void Game_Manager::boot_msg(int opt) { |
rottenegg | 21:f3b0ce18b44f | 65 | lcd->clear(); |
rottenegg | 21:f3b0ce18b44f | 66 | switch(opt) { |
rottenegg | 21:f3b0ce18b44f | 67 | case 0: |
rottenegg | 21:f3b0ce18b44f | 68 | lcd->printString("BOOTING...",0,0); |
rottenegg | 21:f3b0ce18b44f | 69 | break; |
rottenegg | 21:f3b0ce18b44f | 70 | case 1: |
rottenegg | 21:f3b0ce18b44f | 71 | lcd->printString("SD Card Error",0,0); |
rottenegg | 21:f3b0ce18b44f | 72 | lcd->printString("Details in ",0,1); |
rottenegg | 21:f3b0ce18b44f | 73 | lcd->printString("Terminal try",0,2); |
rottenegg | 21:f3b0ce18b44f | 74 | lcd->printString("Re-inserting",0,3); |
rottenegg | 21:f3b0ce18b44f | 75 | lcd->printString("SD Card",0,4); |
rottenegg | 21:f3b0ce18b44f | 76 | break; |
rottenegg | 21:f3b0ce18b44f | 77 | case 2: |
rottenegg | 21:f3b0ce18b44f | 78 | lcd->printString("Game Files",0,0); |
rottenegg | 21:f3b0ce18b44f | 79 | lcd->printString("Missing",0,1); |
rottenegg | 21:f3b0ce18b44f | 80 | lcd->printString("Details in",0,2); |
rottenegg | 21:f3b0ce18b44f | 81 | lcd->printString("Terminal",0,3); |
rottenegg | 21:f3b0ce18b44f | 82 | break; |
rottenegg | 21:f3b0ce18b44f | 83 | } |
rottenegg | 21:f3b0ce18b44f | 84 | lcd->refresh(); |
rottenegg | 21:f3b0ce18b44f | 85 | } |
rottenegg | 21:f3b0ce18b44f | 86 | |
rottenegg | 21:f3b0ce18b44f | 87 | void Game_Manager::first_scene(int &Random){ |
rottenegg | 21:f3b0ce18b44f | 88 | //Initializing |
rottenegg | 21:f3b0ce18b44f | 89 | SNC->set_dead(false); |
rottenegg | 14:165fff6d1854 | 90 | Time.start(); |
rottenegg | 21:f3b0ce18b44f | 91 | wav->intWD(All[55], true); |
rottenegg | 14:165fff6d1854 | 92 | SNC->Scenesp(*lcd,*CM,0,40,20,Fd,0,*wav); |
rottenegg | 14:165fff6d1854 | 93 | Time.stop(); |
rottenegg | 26:716bcd47f3ca | 94 | Random = ((int)(Time.read()*10)) % 3; |
rottenegg | 21:f3b0ce18b44f | 95 | Time.reset(); |
rottenegg | 21:f3b0ce18b44f | 96 | } |
rottenegg | 21:f3b0ce18b44f | 97 | |
rottenegg | 21:f3b0ce18b44f | 98 | void Game_Manager::main() { |
rottenegg | 21:f3b0ce18b44f | 99 | int Random; |
rottenegg | 21:f3b0ce18b44f | 100 | this->first_scene(Random); |
rottenegg | 26:716bcd47f3ca | 101 | PC.printf("%d\n",Random); |
rottenegg | 26:716bcd47f3ca | 102 | if (Random == 1) { |
rottenegg | 21:f3b0ce18b44f | 103 | wav->intWD(All[55], true); |
rottenegg | 21:f3b0ce18b44f | 104 | SNC->Scenesp(*lcd,*CM,3,58,26,Bd,Random,*wav); //mirror |
rottenegg | 21:f3b0ce18b44f | 105 | } else { |
rottenegg | 21:f3b0ce18b44f | 106 | wav->intWD(All[57], true); |
rottenegg | 21:f3b0ce18b44f | 107 | SNC->Scenesp(*lcd,*CM,1,35,29,Fd,Random,*wav); //fork |
rottenegg | 21:f3b0ce18b44f | 108 | } |
rottenegg | 21:f3b0ce18b44f | 109 | wav->intWD(All[56], true); |
rottenegg | 21:f3b0ce18b44f | 110 | SNC->Scenesp(*lcd,*CM,4,69,19,Rt,Random,*wav); //corridor |
rottenegg | 26:716bcd47f3ca | 111 | if (Random != 1) { |
rottenegg | 21:f3b0ce18b44f | 112 | SNC->set_dead(false); |
rottenegg | 21:f3b0ce18b44f | 113 | wav->intWD(All[56], true); |
rottenegg | 21:f3b0ce18b44f | 114 | SNC->Scenesp(*lcd,*CM,2,61,17,Lt,Random,*wav); //chase |
rottenegg | 21:f3b0ce18b44f | 115 | } |
rottenegg | 21:f3b0ce18b44f | 116 | this->end_game(Random); |
rottenegg | 21:f3b0ce18b44f | 117 | } |
rottenegg | 21:f3b0ce18b44f | 118 | |
rottenegg | 21:f3b0ce18b44f | 119 | void Game_Manager::end_game(int Random) { |
rottenegg | 21:f3b0ce18b44f | 120 | lcd->normalMode(); |
rottenegg | 21:f3b0ce18b44f | 121 | if (SNC->get_dead()) { |
rottenegg | 21:f3b0ce18b44f | 122 | this->endingD(); |
rottenegg | 26:716bcd47f3ca | 123 | } else if (Random == 1) { |
rottenegg | 21:f3b0ce18b44f | 124 | this->endingG(); |
rottenegg | 14:165fff6d1854 | 125 | } else { |
rottenegg | 21:f3b0ce18b44f | 126 | this->endingB(); |
rottenegg | 21:f3b0ce18b44f | 127 | } |
rottenegg | 21:f3b0ce18b44f | 128 | } |
rottenegg | 21:f3b0ce18b44f | 129 | void Game_Manager::menu() { |
rottenegg | 21:f3b0ce18b44f | 130 | lcd->clear(); |
rottenegg | 21:f3b0ce18b44f | 131 | lcd->renderBMP(All[32],0,7); |
rottenegg | 21:f3b0ce18b44f | 132 | lcd->printString(".SOUL.SEEKER..",0,0); |
rottenegg | 21:f3b0ce18b44f | 133 | while (str->read() == 0) { |
rottenegg | 21:f3b0ce18b44f | 134 | lcd->printString("Press Start",8,5); |
rottenegg | 21:f3b0ce18b44f | 135 | wait(0.15); |
rottenegg | 21:f3b0ce18b44f | 136 | lcd->refresh(); |
rottenegg | 21:f3b0ce18b44f | 137 | lcd->printString(" ",0,5); |
rottenegg | 21:f3b0ce18b44f | 138 | wait(0.15); |
rottenegg | 21:f3b0ce18b44f | 139 | lcd->refresh(); |
rottenegg | 21:f3b0ce18b44f | 140 | } |
rottenegg | 21:f3b0ce18b44f | 141 | } |
rottenegg | 21:f3b0ce18b44f | 142 | |
rottenegg | 21:f3b0ce18b44f | 143 | void Game_Manager::endingG() { |
rottenegg | 21:f3b0ce18b44f | 144 | for (int i = 42;i <= 50;i++) { |
rottenegg | 21:f3b0ce18b44f | 145 | lcd->clear(); |
rottenegg | 21:f3b0ce18b44f | 146 | lcd->renderBMP(All[i],0,0); |
rottenegg | 21:f3b0ce18b44f | 147 | lcd->refresh(); |
rottenegg | 21:f3b0ce18b44f | 148 | wait(0.5); |
rottenegg | 21:f3b0ce18b44f | 149 | } |
rottenegg | 21:f3b0ce18b44f | 150 | } |
rottenegg | 21:f3b0ce18b44f | 151 | |
rottenegg | 21:f3b0ce18b44f | 152 | void Game_Manager::endingB() { |
rottenegg | 21:f3b0ce18b44f | 153 | for (int i = 42;i <= 49;i++) { |
rottenegg | 21:f3b0ce18b44f | 154 | lcd->clear(); |
rottenegg | 21:f3b0ce18b44f | 155 | lcd->renderBMP(All[i],0,0); |
rottenegg | 21:f3b0ce18b44f | 156 | lcd->refresh(); |
rottenegg | 21:f3b0ce18b44f | 157 | wait(0.5); |
rottenegg | 21:f3b0ce18b44f | 158 | } |
rottenegg | 21:f3b0ce18b44f | 159 | for (int i = 51;i <= 54;i++) { |
rottenegg | 21:f3b0ce18b44f | 160 | lcd->clear(); |
rottenegg | 21:f3b0ce18b44f | 161 | lcd->renderBMP(All[i],0,0); |
rottenegg | 21:f3b0ce18b44f | 162 | lcd->refresh(); |
rottenegg | 21:f3b0ce18b44f | 163 | wait(0.5); |
rottenegg | 14:165fff6d1854 | 164 | } |
rottenegg | 21:f3b0ce18b44f | 165 | } |
rottenegg | 21:f3b0ce18b44f | 166 | |
rottenegg | 21:f3b0ce18b44f | 167 | void Game_Manager::endingD() { |
rottenegg | 21:f3b0ce18b44f | 168 | for (int i = 33;i <= 41;i++) { |
rottenegg | 21:f3b0ce18b44f | 169 | lcd->clear(); |
rottenegg | 21:f3b0ce18b44f | 170 | lcd->renderBMP(All[i],0,0); |
rottenegg | 21:f3b0ce18b44f | 171 | lcd->refresh(); |
rottenegg | 21:f3b0ce18b44f | 172 | wait(0.5); |
rottenegg | 21:f3b0ce18b44f | 173 | } |
rottenegg | 21:f3b0ce18b44f | 174 | } |
rottenegg | 21:f3b0ce18b44f | 175 | |
rottenegg | 21:f3b0ce18b44f | 176 | void Game_Manager::tutorial() { |
rottenegg | 21:f3b0ce18b44f | 177 | this->tut_msg5(); |
rottenegg | 21:f3b0ce18b44f | 178 | if (str->read() == 0) { |
rottenegg | 21:f3b0ce18b44f | 179 | this->tut_msg0(); |
rottenegg | 21:f3b0ce18b44f | 180 | this->tut_msg1(); |
rottenegg | 21:f3b0ce18b44f | 181 | this->tut_msg2(); |
rottenegg | 21:f3b0ce18b44f | 182 | this->tut_msg3(); |
rottenegg | 15:3d29fb195958 | 183 | } |
rottenegg | 21:f3b0ce18b44f | 184 | this->tut_msg4(); |
rottenegg | 21:f3b0ce18b44f | 185 | } |
rottenegg | 21:f3b0ce18b44f | 186 | |
rottenegg | 21:f3b0ce18b44f | 187 | void Game_Manager::tut_msg0() { |
rottenegg | 21:f3b0ce18b44f | 188 | lcd->clear(); |
rottenegg | 21:f3b0ce18b44f | 189 | lcd->printString("How.to.Play.?..",0,0); |
rottenegg | 21:f3b0ce18b44f | 190 | lcd->printString("A - Button is",0,1); |
rottenegg | 21:f3b0ce18b44f | 191 | lcd->printString("used to check",0,2); |
rottenegg | 21:f3b0ce18b44f | 192 | lcd->printString("objects. make",0,3); |
rottenegg | 21:f3b0ce18b44f | 193 | lcd->printString("sure you face",0,4); |
rottenegg | 21:f3b0ce18b44f | 194 | lcd->printString("the object.",0,5); |
rottenegg | 21:f3b0ce18b44f | 195 | lcd->refresh(); |
rottenegg | 21:f3b0ce18b44f | 196 | wait(3); |
rottenegg | 21:f3b0ce18b44f | 197 | } |
rottenegg | 21:f3b0ce18b44f | 198 | |
rottenegg | 21:f3b0ce18b44f | 199 | void Game_Manager::tut_msg1() { |
rottenegg | 21:f3b0ce18b44f | 200 | lcd->clear(); |
rottenegg | 21:f3b0ce18b44f | 201 | lcd->printString("How.to.Play.?..",0,0); |
rottenegg | 21:f3b0ce18b44f | 202 | lcd->printString("Use the ",0,2); |
rottenegg | 21:f3b0ce18b44f | 203 | lcd->printString("Joystick to ",0,3); |
rottenegg | 21:f3b0ce18b44f | 204 | lcd->printString("Move.",0,4); |
rottenegg | 21:f3b0ce18b44f | 205 | lcd->refresh(); |
rottenegg | 21:f3b0ce18b44f | 206 | wait(3); |
rottenegg | 21:f3b0ce18b44f | 207 | } |
rottenegg | 21:f3b0ce18b44f | 208 | |
rottenegg | 21:f3b0ce18b44f | 209 | void Game_Manager::tut_msg2() { |
rottenegg | 21:f3b0ce18b44f | 210 | lcd->clear(); |
rottenegg | 21:f3b0ce18b44f | 211 | lcd->printString("In.Game.Help....",0,0); |
rottenegg | 21:f3b0ce18b44f | 212 | lcd->printString("Whenever you",0,2); |
rottenegg | 21:f3b0ce18b44f | 213 | lcd->printString("progress the",0,3); |
rottenegg | 21:f3b0ce18b44f | 214 | lcd->printString("the screen ",0,4); |
rottenegg | 21:f3b0ce18b44f | 215 | lcd->printString("flash like...",0,05); |
rottenegg | 21:f3b0ce18b44f | 216 | lcd->refresh(); |
rottenegg | 21:f3b0ce18b44f | 217 | wait(3); |
rottenegg | 21:f3b0ce18b44f | 218 | SNC->flashy(*lcd); |
rottenegg | 21:f3b0ce18b44f | 219 | } |
rottenegg | 21:f3b0ce18b44f | 220 | |
rottenegg | 21:f3b0ce18b44f | 221 | void Game_Manager::tut_msg3() { |
rottenegg | 21:f3b0ce18b44f | 222 | lcd->clear(); |
rottenegg | 21:f3b0ce18b44f | 223 | lcd->printString("In.Game.Help....",0,0); |
rottenegg | 21:f3b0ce18b44f | 224 | lcd->printString("Objective is",0,2); |
rottenegg | 21:f3b0ce18b44f | 225 | lcd->printString("to progress",0,3); |
rottenegg | 21:f3b0ce18b44f | 226 | lcd->printString("each scene",0,4); |
rottenegg | 21:f3b0ce18b44f | 227 | lcd->refresh(); |
rottenegg | 21:f3b0ce18b44f | 228 | wait(4); |
rottenegg | 21:f3b0ce18b44f | 229 | } |
rottenegg | 21:f3b0ce18b44f | 230 | |
rottenegg | 21:f3b0ce18b44f | 231 | void Game_Manager::tut_msg4() { |
rottenegg | 21:f3b0ce18b44f | 232 | lcd->clear(); |
rottenegg | 21:f3b0ce18b44f | 233 | lcd->printString(".The.Story...",0,0); |
rottenegg | 21:f3b0ce18b44f | 234 | lcd->printString("Something has",0,2); |
rottenegg | 21:f3b0ce18b44f | 235 | lcd->printString("been stolen",0,3); |
rottenegg | 21:f3b0ce18b44f | 236 | lcd->printString("from you find",0,4); |
rottenegg | 21:f3b0ce18b44f | 237 | lcd->printString("that which gone",0,5); |
rottenegg | 21:f3b0ce18b44f | 238 | lcd->refresh(); |
rottenegg | 21:f3b0ce18b44f | 239 | wait(4); |
rottenegg | 21:f3b0ce18b44f | 240 | SNC->flashy(*lcd); |
rottenegg | 21:f3b0ce18b44f | 241 | } |
rottenegg | 21:f3b0ce18b44f | 242 | |
rottenegg | 21:f3b0ce18b44f | 243 | void Game_Manager::tut_msg5() { |
rottenegg | 21:f3b0ce18b44f | 244 | lcd->clear(); |
rottenegg | 21:f3b0ce18b44f | 245 | lcd->printString(".Tutorial....",0,0); |
rottenegg | 21:f3b0ce18b44f | 246 | lcd->printString("Hold start to",0,2); |
rottenegg | 21:f3b0ce18b44f | 247 | lcd->printString("skip now else",0,3); |
rottenegg | 21:f3b0ce18b44f | 248 | lcd->printString(" wait ",0,4); |
rottenegg | 21:f3b0ce18b44f | 249 | lcd->refresh(); |
rottenegg | 21:f3b0ce18b44f | 250 | wait(3); |
rottenegg | 14:165fff6d1854 | 251 | } |
rottenegg | 14:165fff6d1854 | 252 | |
rottenegg | 14:165fff6d1854 | 253 | bool Game_Manager::check() { |
rottenegg | 14:165fff6d1854 | 254 | FILE *bmp; |
rottenegg | 14:165fff6d1854 | 255 | bool error = false; |
rottenegg | 21:f3b0ce18b44f | 256 | for (int i = 0; i <= 57; i++) { |
rottenegg | 14:165fff6d1854 | 257 | bmp = fopen(All[i],"r"); |
rottenegg | 14:165fff6d1854 | 258 | if (bmp == NULL) { |
rottenegg | 14:165fff6d1854 | 259 | std::cerr << All[i] << " is not present" << std::endl; |
rottenegg | 14:165fff6d1854 | 260 | error = true; |
rottenegg | 14:165fff6d1854 | 261 | } else { |
rottenegg | 14:165fff6d1854 | 262 | fclose(bmp); |
rottenegg | 14:165fff6d1854 | 263 | } |
rottenegg | 14:165fff6d1854 | 264 | } |
rottenegg | 14:165fff6d1854 | 265 | return error; |
rottenegg | 14:165fff6d1854 | 266 | } |