EntityType.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #include "EntityType.h"
  2. #include "Entity.h"
  3. #include "Game.h"
  4. #include "ItemType.h"
  5. EntityType::EntityType(Framework::Text name, ModelInfo* model)
  6. : ReferenceCounter(),
  7. name(name),
  8. model(model),
  9. id(-1)
  10. {}
  11. EntityType::~EntityType()
  12. {
  13. if (model) model->release();
  14. }
  15. void EntityType::loadSuperEntity(
  16. Entity* zEntity, Framework::StreamReader* zReader) const
  17. {
  18. zEntity->loadInventory(zReader);
  19. zReader->lese((char*)&zEntity->id, 4);
  20. zReader->lese((char*)&zEntity->maxHP, 4);
  21. zReader->lese((char*)&zEntity->currentHP, 4);
  22. zReader->lese((char*)&zEntity->stamina, 4);
  23. zReader->lese((char*)&zEntity->maxStamina, 4);
  24. zReader->lese((char*)&zEntity->hunger, 4);
  25. zReader->lese((char*)&zEntity->maxHunger, 4);
  26. zReader->lese((char*)&zEntity->thirst, 4);
  27. zReader->lese((char*)&zEntity->chatSecurityLevel, 4);
  28. zReader->lese((char*)&zEntity->maxThirst, 4);
  29. zReader->lese((char*)&zEntity->speed.x, 4);
  30. zReader->lese((char*)&zEntity->speed.y, 4);
  31. zReader->lese((char*)&zEntity->speed.z, 4);
  32. zReader->lese((char*)&zEntity->faceDir.x, 4);
  33. zReader->lese((char*)&zEntity->faceDir.y, 4);
  34. zReader->lese((char*)&zEntity->dimensionId, 4);
  35. zReader->lese((char*)&zEntity->location.x, 4);
  36. zReader->lese((char*)&zEntity->location.y, 4);
  37. zReader->lese((char*)&zEntity->location.z, 4);
  38. zReader->lese((char*)&zEntity->gravityMultiplier, 4);
  39. zReader->lese((char*)&zEntity->jumpSpeed, 4);
  40. if (zEntity->target) delete zEntity->target;
  41. zEntity->target = ActionTarget::load(zReader);
  42. // load item skills
  43. zEntity->skills.leeren();
  44. int skillCount;
  45. zReader->lese((char*)&skillCount, 4);
  46. for (int i = 0; i < skillCount; i++)
  47. {
  48. int type;
  49. zReader->lese((char*)&type, 4);
  50. ItemSkill* skill
  51. = Game::INSTANCE->zItemType(type)->createDefaultItemSkill();
  52. skill->load(zReader);
  53. zEntity->skills.add(skill);
  54. }
  55. }
  56. void EntityType::saveSuperEntity(
  57. Entity* zEntity, Framework::StreamWriter* zWriter) const
  58. {
  59. zEntity->saveInventory(zWriter);
  60. zWriter->schreibe((char*)&zEntity->id, 4);
  61. zWriter->schreibe((char*)&zEntity->maxHP, 4);
  62. zWriter->schreibe((char*)&zEntity->currentHP, 4);
  63. zWriter->schreibe((char*)&zEntity->stamina, 4);
  64. zWriter->schreibe((char*)&zEntity->maxStamina, 4);
  65. zWriter->schreibe((char*)&zEntity->hunger, 4);
  66. zWriter->schreibe((char*)&zEntity->maxHunger, 4);
  67. zWriter->schreibe((char*)&zEntity->thirst, 4);
  68. zWriter->schreibe((char*)&zEntity->chatSecurityLevel, 4);
  69. zWriter->schreibe((char*)&zEntity->maxThirst, 4);
  70. zWriter->schreibe((char*)&zEntity->speed.x, 4);
  71. zWriter->schreibe((char*)&zEntity->speed.y, 4);
  72. zWriter->schreibe((char*)&zEntity->speed.z, 4);
  73. zWriter->schreibe((char*)&zEntity->faceDir.x, 4);
  74. zWriter->schreibe((char*)&zEntity->faceDir.y, 4);
  75. zWriter->schreibe((char*)&zEntity->dimensionId, 4);
  76. zWriter->schreibe((char*)&zEntity->location.x, 4);
  77. zWriter->schreibe((char*)&zEntity->location.y, 4);
  78. zWriter->schreibe((char*)&zEntity->location.z, 4);
  79. zWriter->schreibe((char*)&zEntity->gravityMultiplier, 4);
  80. zWriter->schreibe((char*)&zEntity->jumpSpeed, 4);
  81. ActionTarget::save(zEntity->target, zWriter);
  82. // save item skills
  83. int skillCount = zEntity->skills.getEintragAnzahl();
  84. zWriter->schreibe((char*)&skillCount, 4);
  85. for (ItemSkill* skill : zEntity->skills)
  86. {
  87. int type = skill->getItemTypeId();
  88. zWriter->schreibe((char*)&type, 4);
  89. skill->save(zWriter);
  90. }
  91. }
  92. void EntityType::createSuperEntity(Entity* zEntity) const {}
  93. bool EntityType::initialize(Game* zGame)
  94. {
  95. return true;
  96. }
  97. Entity* EntityType::loadEntity(Framework::StreamReader* zReader) const
  98. {
  99. Entity* entity = createEntity(Framework::Vec3<float>(0, 0, 0), 0, 0);
  100. loadSuperEntity(entity, zReader);
  101. return entity;
  102. }
  103. void EntityType::saveEntity(
  104. Entity* zEntity, Framework::StreamWriter* zWriter) const
  105. {
  106. saveSuperEntity(zEntity, zWriter);
  107. }
  108. Entity* EntityType::createEntityAt(
  109. Framework::Vec3<float> position, int dimensionId) const
  110. {
  111. Entity* entity = createEntity(
  112. position, dimensionId, Game::INSTANCE->getNextEntityId());
  113. createSuperEntity(entity);
  114. return entity;
  115. }
  116. int EntityType::getId() const
  117. {
  118. return id;
  119. }
  120. ModelInfo* EntityType::zModel() const
  121. {
  122. return model;
  123. }
  124. void EntityType::setTypeId(int id)
  125. {
  126. this->id = id;
  127. }
  128. Framework::Text EntityType::getName() const
  129. {
  130. return name;
  131. }