EntityType.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include "EntityType.h"
  2. #include "Entity.h"
  3. #include "Game.h"
  4. EntityType::EntityType(int id, ModelInfo model)
  5. : ReferenceCounter(),
  6. id(id),
  7. model(model)
  8. {
  9. StaticRegistry<EntityType>::INSTANCE.registerT(this, id);
  10. }
  11. void EntityType::loadSuperEntity(
  12. Entity* zEntity, Framework::StreamReader* zReader) const
  13. {
  14. zEntity->loadInventory(zReader);
  15. zReader->lese((char*)&zEntity->id, 4);
  16. zReader->lese((char*)&zEntity->maxHP, 4);
  17. zReader->lese((char*)&zEntity->currentHP, 4);
  18. zReader->lese((char*)&zEntity->stamina, 4);
  19. zReader->lese((char*)&zEntity->maxStamina, 4);
  20. zReader->lese((char*)&zEntity->hunger, 4);
  21. zReader->lese((char*)&zEntity->maxHunger, 4);
  22. zReader->lese((char*)&zEntity->thirst, 4);
  23. zReader->lese((char*)&zEntity->maxThirst, 4);
  24. zReader->lese((char*)&zEntity->speed.x, 4);
  25. zReader->lese((char*)&zEntity->speed.y, 4);
  26. zReader->lese((char*)&zEntity->speed.z, 4);
  27. zReader->lese((char*)&zEntity->faceDir.x, 4);
  28. zReader->lese((char*)&zEntity->faceDir.y, 4);
  29. zReader->lese((char*)&zEntity->currentDimensionId, 4);
  30. zReader->lese((char*)&zEntity->location.x, 4);
  31. zReader->lese((char*)&zEntity->location.y, 4);
  32. zReader->lese((char*)&zEntity->location.z, 4);
  33. if (zEntity->target) delete zEntity->target;
  34. zEntity->target = ActionTarget::load(zReader);
  35. // load item skills
  36. zEntity->skills.leeren();
  37. int skillCount;
  38. zReader->lese((char*)&skillCount, 4);
  39. for (int i = 0; i < skillCount; i++)
  40. {
  41. int type;
  42. zReader->lese((char*)&type, 4);
  43. zEntity->skills.add(
  44. StaticRegistry<ItemType>::INSTANCE.zElement(type)->loadItemSkill(
  45. zReader));
  46. }
  47. }
  48. void EntityType::saveSuperEntity(
  49. Entity* zEntity, Framework::StreamWriter* zWriter) const
  50. {
  51. zEntity->saveInventory(zWriter);
  52. zWriter->schreibe((char*)&zEntity->id, 4);
  53. zWriter->schreibe((char*)&zEntity->maxHP, 4);
  54. zWriter->schreibe((char*)&zEntity->currentHP, 4);
  55. zWriter->schreibe((char*)&zEntity->stamina, 4);
  56. zWriter->schreibe((char*)&zEntity->maxStamina, 4);
  57. zWriter->schreibe((char*)&zEntity->hunger, 4);
  58. zWriter->schreibe((char*)&zEntity->maxHunger, 4);
  59. zWriter->schreibe((char*)&zEntity->thirst, 4);
  60. zWriter->schreibe((char*)&zEntity->maxThirst, 4);
  61. zWriter->schreibe((char*)&zEntity->speed.x, 4);
  62. zWriter->schreibe((char*)&zEntity->speed.y, 4);
  63. zWriter->schreibe((char*)&zEntity->speed.z, 4);
  64. zWriter->schreibe((char*)&zEntity->faceDir.x, 4);
  65. zWriter->schreibe((char*)&zEntity->faceDir.y, 4);
  66. zWriter->schreibe((char*)&zEntity->currentDimensionId, 4);
  67. zWriter->schreibe((char*)&zEntity->location.x, 4);
  68. zWriter->schreibe((char*)&zEntity->location.y, 4);
  69. zWriter->schreibe((char*)&zEntity->location.z, 4);
  70. ActionTarget::save(zEntity->target, zWriter);
  71. // save item skills
  72. int skillCount = zEntity->skills.getEintragAnzahl();
  73. zWriter->schreibe((char*)&skillCount, 4);
  74. for (ItemSkill* skill : zEntity->skills)
  75. {
  76. int type = skill->zSkillType()->getId();
  77. zWriter->schreibe((char*)&type, 4);
  78. skill->zSkillType()->saveItemSkill(skill, zWriter);
  79. }
  80. }
  81. void EntityType::createSuperEntity(Entity* zEntity) const {}
  82. Entity* EntityType::loadEntity(Framework::StreamReader* zReader) const
  83. {
  84. Entity* entity = createEntity(Framework::Vec3<float>(0, 0, 0), 0, 0);
  85. loadSuperEntity(entity, zReader);
  86. return entity;
  87. }
  88. void EntityType::saveEntity(
  89. Entity* zEntity, Framework::StreamWriter* zWriter) const
  90. {
  91. saveSuperEntity(zEntity, zWriter);
  92. }
  93. Entity* EntityType::createEntityAt(
  94. Framework::Vec3<float> position, int dimensionId) const
  95. {
  96. Entity* entity = createEntity(
  97. position, dimensionId, Game::INSTANCE->getNextEntityId());
  98. createSuperEntity(entity);
  99. return entity;
  100. }
  101. int EntityType::getId() const
  102. {
  103. return id;
  104. }
  105. const ModelInfo& EntityType::getModel() const
  106. {
  107. return model;
  108. }