| 1 | |
| 2 | /** |
| 3 | * |
| 4 | * @file jj1demolevel.cpp |
| 5 | * |
| 6 | * Part of the OpenJazz project |
| 7 | * |
| 8 | * @par History: |
| 9 | * - 23rd August 2005: Created level.c |
| 10 | * - 22nd July 2008: Created levelload.c from parts of level.c |
| 11 | * - 3rd February 2009: Renamed level.c to level.cpp and levelload.c to |
| 12 | * levelload.cpp |
| 13 | * - 18th July 2009: Created demolevel.cpp from parts of level.cpp and |
| 14 | * levelload.cpp |
| 15 | * - 1st August 2012: Renamed demolevel.cpp to jj1demolevel.cpp |
| 16 | * |
| 17 | * @par Licence: |
| 18 | * Copyright (c) 2005-2017 AJ Thomson |
| 19 | * |
| 20 | * OpenJazz is distributed under the terms of |
| 21 | * the GNU General Public License, version 2.0 |
| 22 | * |
| 23 | * @par Description: |
| 24 | * Deals with the loading and playing of demo levels. |
| 25 | * |
| 26 | */ |
| 27 | |
| 28 | |
| 29 | #include "jj1level.h" |
| 30 | #include "jj1levelplayer.h" |
| 31 | |
| 32 | #include "game/game.h" |
| 33 | #include "game/gamemode.h" |
| 34 | #include "io/controls.h" |
| 35 | #include "io/file.h" |
| 36 | #include "io/gfx/font.h" |
| 37 | #include "io/gfx/video.h" |
| 38 | #include "io/sound.h" |
| 39 | #include "loop.h" |
| 40 | #include "util.h" |
| 41 | |
| 42 | |
| 43 | /** |
| 44 | * Create a JJ1 demo level. |
| 45 | * |
| 46 | * @param owner The current game |
| 47 | * @param fileName Name of the file containing the macro data. |
| 48 | */ |
| 49 | JJ1DemoLevel::JJ1DemoLevel (Game* owner, const char* fileName) : JJ1Level(owner) { |
| 50 | |
| 51 | File* file; |
| 52 | char* levelFile; |
| 53 | int lNum, wNum, ret; |
| 54 | |
| 55 | multiplayer = false; |
| 56 | |
| 57 | try { |
| 58 | |
| 59 | file = new File(fileName, PATH_TYPE_GAME); |
| 60 | |
| 61 | } catch (int e) { |
| 62 | |
| 63 | throw; |
| 64 | |
| 65 | } |
| 66 | |
| 67 | // Check this is a normal level |
| 68 | if (file->loadShort() == 0) { |
| 69 | |
| 70 | delete file; |
| 71 | throw E_DEMOTYPE; |
| 72 | |
| 73 | } |
| 74 | |
| 75 | // Level file to load |
| 76 | lNum = file->loadShort(9); |
| 77 | wNum = file->loadShort(999); |
| 78 | levelFile = createFileName("LEVEL", lNum, wNum); |
| 79 | |
| 80 | // Difficulty |
| 81 | file->loadShort(); |
| 82 | |
| 83 | macro = file->loadBlock(1024); |
| 84 | |
| 85 | delete file; |
| 86 | |
| 87 | // Load level data |
| 88 | |
| 89 | ret = load(levelFile, false); |
| 90 | |
| 91 | delete[] levelFile; |
| 92 | |
| 93 | if (ret < 0) throw ret; |
| 94 | |
| 95 | } |
| 96 | |
| 97 | |
| 98 | /** |
| 99 | * Delete the JJ1 demo level. |
| 100 | */ |
| 101 | JJ1DemoLevel::~JJ1DemoLevel () { |
| 102 | |
| 103 | delete[] macro; |
| 104 | |
| 105 | } |
| 106 | |
| 107 | |
| 108 | /** |
| 109 | * Play the demo. |
| 110 | * |
| 111 | * @return Error code |
| 112 | */ |
| 113 | int JJ1DemoLevel::play () { |
| 114 | |
| 115 | tickOffset = globalTicks; |
| 116 | ticks = 17; |
| 117 | steps = 0; |
| 118 | |
| 119 | video.setPalette(palette); |
| 120 | |
| 121 | playMusic(musicFile); |
| 122 | |
| 123 | while (true) { |
| 124 | |
| 125 | // Do general processing |
| 126 | if (::loop(NORMAL_LOOP, paletteEffects) == E_QUIT) return E_QUIT; |
| 127 | |
| 128 | if (controls.release(C_ESCAPE) || controls.release(C_ENTER)) return E_NONE; |
| 129 | |
| 130 | if (controls.release(C_STATS)) stats ^= S_SCREEN; |
| 131 | |
| 132 | |
| 133 | timeCalcs(); |
| 134 | |
| 135 | |
| 136 | |
| 137 | // Use macro |
| 138 | unsigned char macroPoint = macro[(ticks / 76) & 1023]; |
| 139 | |
| 140 | if (macroPoint & 128) return E_NONE; |
| 141 | |
| 142 | if (macroPoint & 1) { |
| 143 | |
| 144 | localPlayer->setControl(C_LEFT, false); |
| 145 | localPlayer->setControl(C_RIGHT, false); |
| 146 | localPlayer->setControl(C_UP, !(macroPoint & 4)); |
| 147 | |
| 148 | } else { |
| 149 | |
| 150 | localPlayer->setControl(C_LEFT, !(macroPoint & 2)); |
| 151 | localPlayer->setControl(C_RIGHT, macroPoint & 2); |
| 152 | localPlayer->setControl(C_UP, false); |
| 153 | |
| 154 | } |
| 155 | |
| 156 | localPlayer->setControl(C_DOWN, macroPoint & 8); |
| 157 | localPlayer->setControl(C_FIRE, macroPoint & 16); |
| 158 | localPlayer->setControl(C_CHANGE, macroPoint & 32); |
| 159 | localPlayer->setControl(C_JUMP, macroPoint & 64); |
| 160 | localPlayer->setControl(C_SWIM, macroPoint & 64); |
| 161 | |
| 162 | |
| 163 | |
| 164 | // Check if level has been won |
| 165 | if (getStage() == LS_END) return WON; |
| 166 | |
| 167 | |
| 168 | // Process frame-by-frame activity |
| 169 | |
| 170 | // Process step |
| 171 | while (getTimeChange() >= T_STEP) { |
| 172 | |
| 173 | int ret = step(); |
| 174 | steps++; |
| 175 | |
| 176 | if (ret < 0) return ret; |
| 177 | |
| 178 | } |
| 179 | |
| 180 | |
| 181 | // Handle player reactions |
| 182 | if (localPlayer->getJJ1LevelPlayer()->reacted(ticks) == PR_KILLED) return LOST; |
| 183 | |
| 184 | |
| 185 | // Draw the graphics |
| 186 | |
| 187 | draw(); |
| 188 | drawOverlay(LEVEL_BLACK, false, 0, 0, 0, 0); |
| 189 | |
| 190 | |
| 191 | font->showString("demo", canvasW >> 1, 32, alignX::Center); |
| 192 | |
| 193 | |
| 194 | } |
| 195 | |
| 196 | return E_NONE; |
| 197 | |
| 198 | } |
| 199 | |
| 200 |