gcsx_worldplay.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /* GCSx
  2. ** WORLDPLAY.CPP
  3. **
  4. ** World storage
  5. ** Gameplay members
  6. */
  7. /*****************************************************************************
  8. ** Copyright (C) 2003-2006 Janson
  9. **
  10. ** This program is free software; you can redistribute it and/or modify
  11. ** it under the terms of the GNU General Public License as published by
  12. ** the Free Software Foundation; either version 2 of the License, or
  13. ** (at your option) any later version.
  14. **
  15. ** This program is distributed in the hope that it will be useful,
  16. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. ** GNU General Public License for more details.
  19. **
  20. ** You should have received a copy of the GNU General Public License
  21. ** along with this program; if not, write to the Free Software
  22. ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
  23. *****************************************************************************/
  24. #include "all.h"
  25. WorldPlay::WorldPlay(const char* wFilename) throw_File : World(wFilename) { start_func
  26. currentScene = NULL;
  27. librariesLocked = allScriptsLocked = 0;
  28. // @TODO: ensure updated whenever saved game loading is implemented
  29. lastEntityId = 1;
  30. lastSpriteId = 1;
  31. }
  32. WorldPlay::~WorldPlay() { start_func
  33. while (librariesLocked) unlockLibraries();
  34. EntityIndex::iterator end = entitysById.end();
  35. for (EntityIndex::iterator pos = entitysById.begin(); pos != end; ++pos) {
  36. delete (*pos).second;
  37. }
  38. SpriteIndex::iterator end2 = spritesById.end();
  39. for (SpriteIndex::iterator pos2 = spritesById.begin(); pos2 != end2; ++pos2) {
  40. delete (*pos2).second;
  41. }
  42. }
  43. void WorldPlay::gameStart() throw_File { start_func
  44. int errs;
  45. if ( (errs = buildScripts()) ) {
  46. throw FileException("%d error(s) during script compilation- see debug console for details.", errs);
  47. }
  48. lockLibraries();
  49. if (startingScene)
  50. changeScene(findScene(startingScene));
  51. else
  52. changeScene(NULL);
  53. }
  54. void WorldPlay::changeScene(Scene* newScene) { start_func
  55. if (newScene != currentScene) {
  56. if (currentScene) currentScene->markUnlockPlay();
  57. currentScene = newScene;
  58. if (currentScene) {
  59. // @TODO: throw_File
  60. currentScene->markLockPlay();
  61. spawnScene(currentScene);
  62. }
  63. }
  64. }
  65. void WorldPlay::spawnScene(Scene* scene) { start_func
  66. if (scenesSpawned.find(scene->getId()) == scenesSpawned.end()) {
  67. // Spawn scene- first time loading
  68. scene->spawnScene();
  69. scenesSpawned.insert(scene->getId());
  70. }
  71. }
  72. void WorldPlay::draw() { start_func
  73. if (!currentScene) return;
  74. currentScene->draw();
  75. }
  76. void WorldPlay::indexEntity(Entity* addEntity) { start_func
  77. assert(addEntity);
  78. assert(entitysById.find(addEntity->getId()) == entitysById.end());
  79. entitysById.insert(pair<int, Entity*>(addEntity->getId(), addEntity));
  80. }
  81. void WorldPlay::deindexEntity(Entity* remEntity) { start_func
  82. assert(remEntity);
  83. entitysById.erase(remEntity->getId());
  84. }
  85. Entity* WorldPlay::findEntity(int fId) const { start_func
  86. EntityIndex::const_iterator loc = entitysById.find(fId);
  87. if (loc == entitysById.end()) return NULL;
  88. return (*loc).second;
  89. }
  90. int WorldPlay::unusedEntityId() { start_func
  91. while (entitysById.find(++lastEntityId) != entitysById.end()) ;
  92. return lastEntityId;
  93. }
  94. void WorldPlay::indexSprite(Sprite* addSprite) { start_func
  95. assert(addSprite);
  96. assert(spritesById.find(addSprite->getId()) == spritesById.end());
  97. spritesById.insert(pair<int, Sprite*>(addSprite->getId(), addSprite));
  98. }
  99. void WorldPlay::deindexSprite(Sprite* remSprite) { start_func
  100. assert(remSprite);
  101. spritesById.erase(remSprite->getId());
  102. }
  103. Sprite* WorldPlay::findSprite(int fId) const { start_func
  104. SpriteIndex::const_iterator loc = spritesById.find(fId);
  105. if (loc == spritesById.end()) return NULL;
  106. return (*loc).second;
  107. }
  108. int WorldPlay::unusedSpriteId() { start_func
  109. while (spritesById.find(++lastSpriteId) != spritesById.end()) ;
  110. return lastSpriteId;
  111. }
  112. void WorldPlay::cycle() { start_func
  113. if (currentScene) currentScene->cycle();
  114. }
  115. void WorldPlay::lockLibraries() { start_func
  116. if (!librariesLocked) {
  117. allScriptsLocked = config->readNum(LINKED_PREGENERATE);
  118. ScriptIndex::iterator pos;
  119. ScriptIndex::iterator end = scriptsById.end();
  120. for (pos = scriptsById.begin(); pos != end; ++pos) {
  121. assert(!(*pos).second->numErrors());
  122. if (((*pos).second->getType() == Script::SCRIPT_LIBRARY) ||
  123. (allScriptsLocked && ((*pos).second->getType() == Script::SCRIPT_CODE))) {
  124. (*pos).second->markLock();
  125. (*pos).second->link();
  126. }
  127. }
  128. }
  129. ++librariesLocked;
  130. }
  131. void WorldPlay::unlockLibraries() { start_func
  132. assert(librariesLocked);
  133. --librariesLocked;;
  134. if (librariesLocked == 0) {
  135. ScriptIndex::iterator pos;
  136. ScriptIndex::iterator end = scriptsById.end();
  137. for (pos = scriptsById.begin(); pos != end; ++pos) {
  138. if (((*pos).second->getType() == Script::SCRIPT_LIBRARY) ||
  139. (allScriptsLocked && ((*pos).second->getType() == Script::SCRIPT_CODE)))
  140. (*pos).second->markUnlock();
  141. }
  142. }
  143. }