| 1 |
/* |
| 2 |
* Copyright (C) 2004 Ivo Danihelka ([email protected]) |
| 3 |
* |
| 4 |
* This program is free software; you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU General Public License as published by |
| 6 |
* the Free Software Foundation; either version 2 of the License, or |
| 7 |
* (at your option) any later version. |
| 8 |
*/ |
| 9 |
#include "WorldMap.h" |
| 10 |
|
| 11 |
#include "LevelNode.h" |
| 12 |
#include "NodeDrawer.h" |
| 13 |
#include "LevelStatus.h" |
| 14 |
#include "ResImagePack.h" |
| 15 |
#include "StateManager.h" |
| 16 |
#include "WorldInput.h" |
| 17 |
|
| 18 |
#include "Log.h" |
| 19 |
#include "Path.h" |
| 20 |
#include "WorldBranch.h" |
| 21 |
#include "OptionAgent.h" |
| 22 |
#include "VideoAgent.h" |
| 23 |
#include "SoundAgent.h" |
| 24 |
#include "LogicException.h" |
| 25 |
#include "ResDialogPack.h" |
| 26 |
#include "LevelDesc.h" |
| 27 |
#include "Level.h" |
| 28 |
#include "Pedometer.h" |
| 29 |
#include "LayeredPicture.h" |
| 30 |
#include "MenuOptions.h" |
| 31 |
#include "DemoMode.h" |
| 32 |
#include "PosterScroller.h" |
| 33 |
#include "MovieState.h" |
| 34 |
#include "Log.h" |
| 35 |
|
| 36 |
//----------------------------------------------------------------- |
| 37 |
WorldMap::WorldMap() |
| 38 |
: m_lastMouseLoc(-1, -1) |
| 39 |
{ |
| 40 |
m_selected = NULL; |
| 41 |
m_startNode = NULL; |
| 42 |
m_ending = NULL; |
| 43 |
prepareBg(); |
| 44 |
|
| 45 |
m_drawer = new NodeDrawer(); |
| 46 |
m_descPack = new ResDialogPack(); |
| 47 |
m_levelStatus = new LevelStatus(); |
| 48 |
takeHandler(new WorldInput(this)); |
| 49 |
registerDrawable(m_bg); |
| 50 |
registerDrawable(this); |
| 51 |
} |
| 52 |
//----------------------------------------------------------------- |
| 53 |
WorldMap::~WorldMap() |
| 54 |
{ |
| 55 |
if (m_startNode) { |
| 56 |
delete m_startNode; |
| 57 |
} |
| 58 |
if (m_ending) { |
| 59 |
delete m_ending; |
| 60 |
} |
| 61 |
delete m_bg; |
| 62 |
m_descPack->removeAll(); |
| 63 |
delete m_descPack; |
| 64 |
delete m_drawer; |
| 65 |
delete m_levelStatus; |
| 66 |
} |
| 67 |
//----------------------------------------------------------------- |
| 68 |
/** |
| 69 |
* Prepare background with buttons. |
| 70 |
*/ |
| 71 |
void |
| 72 |
WorldMap::prepareBg() |
| 73 |
{ |
| 74 |
m_bg = new LayeredPicture( |
| 75 |
Path::dataReadPath("images/menu/map.png"), |
| 76 |
V2(0, 0), |
| 77 |
Path::dataReadPath("images/menu/map_lower.png"), |
| 78 |
Path::dataReadPath("images/menu/map_mask.png")); |
| 79 |
|
| 80 |
m_maskIntro = m_bg->getMaskAt(V2(0, 0)); |
| 81 |
m_maskExit = m_bg->getMaskAt(V2(m_bg->getW() - 1, 0)); |
| 82 |
m_maskCredits = m_bg->getMaskAt(V2(0, m_bg->getH() - 1)); |
| 83 |
m_maskOptions = m_bg->getMaskAt(V2(m_bg->getW() - 1, m_bg->getH() - 1)); |
| 84 |
m_activeMask = m_bg->getNoMask(); |
| 85 |
} |
| 86 |
//----------------------------------------------------------------- |
| 87 |
/** |
| 88 |
* Read dots postions and level descriptions. |
| 89 |
* @throws LogicException when cannot parse data file |
| 90 |
*/ |
| 91 |
void |
| 92 |
WorldMap::initMap(const Path &mapfile) |
| 93 |
{ |
| 94 |
WorldBranch parser(NULL); |
| 95 |
m_startNode = parser.parseMap(mapfile, &m_ending, m_descPack); |
| 96 |
if (NULL == m_startNode) { |
| 97 |
throw LogicException(ExInfo("cannot create world map") |
| 98 |
.addInfo("file", mapfile.getNative())); |
| 99 |
} |
| 100 |
} |
| 101 |
//----------------------------------------------------------------- |
| 102 |
/** |
| 103 |
* Display menu and play menu music. |
| 104 |
*/ |
| 105 |
void |
| 106 |
WorldMap::own_initState() |
| 107 |
{ |
| 108 |
m_levelStatus->setRunning(true); |
| 109 |
own_resumeState(); |
| 110 |
} |
| 111 |
//----------------------------------------------------------------- |
| 112 |
void |
| 113 |
WorldMap::own_updateState() |
| 114 |
{ |
| 115 |
if (m_ending && m_selected == m_ending) { |
| 116 |
runSelected(); |
| 117 |
} |
| 118 |
else { |
| 119 |
watchCursor(); |
| 120 |
} |
| 121 |
} |
| 122 |
//----------------------------------------------------------------- |
| 123 |
/** |
| 124 |
* Display menu and play menu music. |
| 125 |
*/ |
| 126 |
void |
| 127 |
WorldMap::own_resumeState() |
| 128 |
{ |
| 129 |
LevelNode *nextLevel = NULL; |
| 130 |
if (m_levelStatus->wasRunning()) { |
| 131 |
if (m_levelStatus->isComplete()) { |
| 132 |
markSolved(); |
| 133 |
if (checkEnding()) { |
| 134 |
nextLevel = m_ending; |
| 135 |
} |
| 136 |
} |
| 137 |
m_levelStatus->setRunning(false); |
| 138 |
|
| 139 |
OptionAgent *options = OptionAgent::agent(); |
| 140 |
options->setParam("caption", findDesc("menu")); |
| 141 |
options->setParam("screen_width", m_bg->getW()); |
| 142 |
options->setParam("screen_height", m_bg->getH()); |
| 143 |
VideoAgent::agent()->initVideoMode(); |
| 144 |
} |
| 145 |
m_selected = nextLevel; |
| 146 |
|
| 147 |
SoundAgent::agent()->playMusic( |
| 148 |
Path::dataReadPath("music/menu.ogg"), NULL); |
| 149 |
} |
| 150 |
//----------------------------------------------------------------- |
| 151 |
/** |
| 152 |
* Stop music. |
| 153 |
*/ |
| 154 |
void |
| 155 |
WorldMap::own_cleanState() |
| 156 |
{ |
| 157 |
SoundAgent::agent()->stopMusic(); |
| 158 |
} |
| 159 |
//----------------------------------------------------------------- |
| 160 |
/** |
| 161 |
* Mark node or mask under cursor as selected. |
| 162 |
*/ |
| 163 |
void |
| 164 |
WorldMap::watchCursor() |
| 165 |
{ |
| 166 |
V2 mouseLoc = getInput()->getMouseLoc(); |
| 167 |
if (!m_lastMouseLoc.equals(mouseLoc)) { |
| 168 |
m_lastMouseLoc = mouseLoc; |
| 169 |
m_selected = m_startNode->findSelected(mouseLoc); |
| 170 |
} |
| 171 |
|
| 172 |
m_activeMask = m_bg->getMaskAtWorld(mouseLoc); |
| 173 |
if (m_activeMask == m_maskIntro |
| 174 |
|| m_activeMask == m_maskExit |
| 175 |
|| m_activeMask == m_maskCredits |
| 176 |
|| m_activeMask == m_maskOptions) |
| 177 |
{ |
| 178 |
m_bg->setActiveMask(m_activeMask); |
| 179 |
} |
| 180 |
else { |
| 181 |
m_bg->setNoActive(); |
| 182 |
} |
| 183 |
} |
| 184 |
//----------------------------------------------------------------- |
| 185 |
/** |
| 186 |
* Select next unsolved level. |
| 187 |
*/ |
| 188 |
void |
| 189 |
WorldMap::selectNextLevel() |
| 190 |
{ |
| 191 |
m_selected = m_startNode->findNextOpen(m_selected); |
| 192 |
} |
| 193 |
//----------------------------------------------------------------- |
| 194 |
/** |
| 195 |
* Start level under pressed button. |
| 196 |
* Start pedometer when level is solved already. |
| 197 |
*/ |
| 198 |
void |
| 199 |
WorldMap::runSelected() |
| 200 |
{ |
| 201 |
Level *level = createSelected(); |
| 202 |
if (level) { |
| 203 |
m_levelStatus->prepareRun(m_selected->getCodename(), |
| 204 |
m_selected->getPoster(), |
| 205 |
m_selected->getBestMoves(), |
| 206 |
m_selected->getBestAuthor()); |
| 207 |
level->fillStatus(m_levelStatus); |
| 208 |
|
| 209 |
if (m_selected->getState() == LevelNode::STATE_SOLVED) { |
| 210 |
Pedometer *pedometer = new Pedometer(m_levelStatus, level); |
| 211 |
pushState(pedometer); |
| 212 |
} |
| 213 |
else { |
| 214 |
pushState(level); |
| 215 |
} |
| 216 |
} |
| 217 |
else { |
| 218 |
if (m_activeMask == m_maskIntro) { |
| 219 |
runIntro(); |
| 220 |
} |
| 221 |
else if (m_activeMask == m_maskExit) { |
| 222 |
quitState(); |
| 223 |
} |
| 224 |
else if (m_activeMask == m_maskCredits) { |
| 225 |
runCredits(); |
| 226 |
} |
| 227 |
else if (m_activeMask == m_maskOptions) { |
| 228 |
runOptions(); |
| 229 |
} |
| 230 |
} |
| 231 |
} |
| 232 |
//----------------------------------------------------------------- |
| 233 |
/** |
| 234 |
* Return selected level or NULL. |
| 235 |
*/ |
| 236 |
Level * |
| 237 |
WorldMap::createSelected() const |
| 238 |
{ |
| 239 |
Level *result = NULL; |
| 240 |
if (m_selected) { |
| 241 |
result = m_selected->createLevel(); |
| 242 |
result->fillDesc(this); |
| 243 |
} |
| 244 |
return result; |
| 245 |
} |
| 246 |
//----------------------------------------------------------------- |
| 247 |
void |
| 248 |
WorldMap::markSolved() |
| 249 |
{ |
| 250 |
if (m_selected) { |
| 251 |
m_selected->setState(LevelNode::STATE_SOLVED); |
| 252 |
} |
| 253 |
} |
| 254 |
//----------------------------------------------------------------- |
| 255 |
/** |
| 256 |
* Try run ending node. |
| 257 |
* Ending node is started when all levels were solved. |
| 258 |
* @return true when ending is started |
| 259 |
*/ |
| 260 |
bool |
| 261 |
WorldMap::checkEnding() const |
| 262 |
{ |
| 263 |
bool result = false; |
| 264 |
if (m_ending && m_selected != m_ending) { |
| 265 |
if (m_selected->isLeaf()) { |
| 266 |
if (m_startNode->areAllSolved()) { |
| 267 |
result = true; |
| 268 |
} |
| 269 |
} |
| 270 |
} |
| 271 |
return result; |
| 272 |
} |
| 273 |
//----------------------------------------------------------------- |
| 274 |
void |
| 275 |
WorldMap::drawOn(SDL_Surface *screen) |
| 276 |
{ |
| 277 |
m_drawer->setScreen(screen); |
| 278 |
m_startNode->drawPath(m_drawer); |
| 279 |
if (m_selected) { |
| 280 |
m_drawer->drawSelect(m_selected->getLoc()); |
| 281 |
m_drawer->drawSelected(findLevelName(m_selected->getCodename())); |
| 282 |
} |
| 283 |
} |
| 284 |
//----------------------------------------------------------------- |
| 285 |
std::string |
| 286 |
WorldMap::findLevelName(const std::string &codename) const |
| 287 |
{ |
| 288 |
std::string result; |
| 289 |
const LevelDesc *desc = |
| 290 |
dynamic_cast<const LevelDesc*>(m_descPack->findDialogHard(codename)); |
| 291 |
if (desc) { |
| 292 |
result = desc->getLevelName(); |
| 293 |
} |
| 294 |
else { |
| 295 |
result = codename; |
| 296 |
} |
| 297 |
return result; |
| 298 |
} |
| 299 |
//----------------------------------------------------------------- |
| 300 |
std::string |
| 301 |
WorldMap::findDesc(const std::string &codename) const |
| 302 |
{ |
| 303 |
std::string result; |
| 304 |
const LevelDesc *desc = |
| 305 |
dynamic_cast<const LevelDesc*>(m_descPack->findDialogHard(codename)); |
| 306 |
if (desc) { |
| 307 |
result = desc->getDesc(); |
| 308 |
} |
| 309 |
else { |
| 310 |
result = "???"; |
| 311 |
} |
| 312 |
return result; |
| 313 |
} |
| 314 |
//----------------------------------------------------------------- |
| 315 |
void |
| 316 |
WorldMap::runIntro() |
| 317 |
{ |
| 318 |
#ifdef HAVE_SMPEG |
| 319 |
Path movieFile = Path::dataReadPath("images/menu/intro.mpg"); |
| 320 |
if (movieFile.exists()) { |
| 321 |
pushState(new MovieState(movieFile)); |
| 322 |
return; |
| 323 |
} |
| 324 |
|
| 325 |
LOG_WARNING(ExInfo("cannot find intro") |
| 326 |
.addInfo("file", movieFile.getNative())); |
| 327 |
#endif |
| 328 |
|
| 329 |
pushState(new DemoMode(Path::dataReadPath("script/share/demo_intro.lua"))); |
| 330 |
} |
| 331 |
//----------------------------------------------------------------- |
| 332 |
void |
| 333 |
WorldMap::runCredits() |
| 334 |
{ |
| 335 |
pushState(new PosterScroller( |
| 336 |
Path::dataReadPath("images/menu/credits.png"))); |
| 337 |
} |
| 338 |
//----------------------------------------------------------------- |
| 339 |
void |
| 340 |
WorldMap::runOptions() |
| 341 |
{ |
| 342 |
MenuOptions *options = new MenuOptions(); |
| 343 |
pushState(options); |
| 344 |
} |
| 345 |
|