gcsx_spawn.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* GCSx
  2. ** SPAWN.CPP
  3. **
  4. ** Spawn-point support (starting point for sprite/script)
  5. ** Doesn't include any editor-only functionality
  6. ** Usually simply referred to as sprites/objects/scripts in the editor
  7. */
  8. /*****************************************************************************
  9. ** Copyright (C) 2003-2006 Janson
  10. **
  11. ** This program is free software; you can redistribute it and/or modify
  12. ** it under the terms of the GNU General Public License as published by
  13. ** the Free Software Foundation; either version 2 of the License, or
  14. ** (at your option) any later version.
  15. **
  16. ** This program is distributed in the hope that it will be useful,
  17. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ** GNU General Public License for more details.
  20. **
  21. ** You should have received a copy of the GNU General Public License
  22. ** along with this program; if not, write to the Free Software
  23. ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
  24. *****************************************************************************/
  25. #include "all.h"
  26. Spawn::Spawn(int newId) : name(blankString) { start_func
  27. script = NULL;
  28. animgroup = NULL;
  29. tileset = NULL;
  30. subid = 0;
  31. x = y = 0;
  32. id = newId;
  33. }
  34. Spawn::~Spawn() { start_func
  35. }
  36. void Spawn::load(class FileRead* file, const World* world) throw_File { start_func
  37. id = file->readInt();
  38. file->readStr(name);
  39. int code = file->readInt();
  40. if (code) {
  41. script = world->findScript(code);
  42. if (!script) throw FileException("Corrupted spawn-point content");
  43. }
  44. else script = NULL;
  45. code = file->readInt();
  46. if (code) {
  47. animgroup = world->findAnimGroup(code);
  48. if (!animgroup) throw FileException("Corrupted spawn-point content");
  49. }
  50. else animgroup = NULL;
  51. code = file->readInt();
  52. if (code) {
  53. tileset = world->findTileSet(code);
  54. if (!tileset) throw FileException("Corrupted spawn-point content");
  55. }
  56. else tileset = NULL;
  57. subid = file->readInt();
  58. x = file->readInt();
  59. y = file->readInt();
  60. if (((animgroup) && (tileset)) ||
  61. ((subid) && (!animgroup) && (!tileset)) ||
  62. ((subid <= 0) && ((animgroup) || (tileset))) ||
  63. (!id))
  64. throw FileException("Corrupted spawn-point content");
  65. }
  66. void Spawn::generate(Layer* toLayer, WorldPlay* toWorld) { start_func
  67. assert(toWorld);
  68. Sprite* spr = NULL;
  69. if (toLayer) {
  70. spr = new Sprite(toWorld->unusedSpriteId());
  71. if (tileset)
  72. spr->setImage(tileset, subid);
  73. else if (animgroup)
  74. spr->setImage(animgroup, subid, 1);
  75. else
  76. spr->setImage();
  77. spr->moveTo(x, y);
  78. toLayer->indexSprite(spr);
  79. }
  80. if (script) {
  81. Entity* obj = new Entity(toWorld->unusedEntityId());
  82. obj->setName(name);
  83. obj->setSprite(spr);
  84. obj->setScript(script);
  85. spr->setEntity(obj);
  86. if (toLayer)
  87. toLayer->getScene()->indexEntity(obj);
  88. else
  89. toWorld->indexEntity(obj);
  90. }
  91. }