Player.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. #include "Player.h"
  2. #include "Game.h"
  3. #include "PlayerHand.h"
  4. #include "ItemFilter.h"
  5. Player::Player(Framework::Vec3<float> location, int dimensionId, int entityId)
  6. : Entity(PlayerEntityType::INSTANCE, location, dimensionId, entityId),
  7. BasicShapedCrafter(3, 3, this)
  8. {
  9. for (int i = 0; i < 10; i++)
  10. {
  11. ItemSlot* slot = new ItemSlot("ItemBar", 50, 0, i, 0, ANY_DIRECTION, 0);
  12. itemBar.add(slot);
  13. addSlot(slot);
  14. }
  15. for (int i = 0; i < 30; i++)
  16. {
  17. ItemSlot* slot = new ItemSlot("Inventory", 50, 0, i + 10, 0, ANY_DIRECTION, 0);
  18. addSlot(slot);
  19. }
  20. leftHandPosition = 0;
  21. maxHP = 10;
  22. currentHP = 10;
  23. stamina = 10;
  24. maxStamina = 10;
  25. hunger = 10;
  26. maxHunger = 10;
  27. thirst = 10;
  28. maxThirst = 10;
  29. keyState = 0;
  30. jumping = 0;
  31. faceOffset = { 0.f, 0.f, 1.5f };
  32. targetDistanceLimit = 4;
  33. }
  34. void Player::afterPullStack(ItemSlot* zSlot, Direction dir, const Item* zItem, int count)
  35. {
  36. for (auto slot : itemBar)
  37. {
  38. if (slot == zSlot)
  39. {
  40. needUpdate = 1;
  41. return;
  42. }
  43. }
  44. }
  45. void Player::afterPushStack(ItemSlot* zSlot, Direction dir, const Item* zItem, int count)
  46. {
  47. for (auto slot : itemBar)
  48. {
  49. if (slot == zSlot)
  50. {
  51. needUpdate = 1;
  52. return;
  53. }
  54. }
  55. }
  56. Framework::Text Player::getInventoryUIML()
  57. {
  58. Framework::Text result = "<dialog id=\"player_inventory\" title=\"Inventory\" width=\"610\" height=\"450\">";
  59. result += "<craftingGrid id=\"crafting\" margin-top=\"9\" align-top=\"start\" align-left=\"start\" margin-left=\"9\" width=\"272\" height=\"172\" rowSize=\"3\" colSize=\"3\" numOutputSlots=\"1\" target=\"";
  60. result += getId();
  61. result += "\"/>";
  62. result += "<inventory id=\"inventory\" margin-bottom=\"18\" align-bottom=\"item_bar\" align-left=\"start\" margin-left=\"9\" width=\"592\" height=\"172\" rowSize=\"10\" numSlots=\"30\" slotNameFilter=\"Inventory\" target=\"";
  63. result += getId();
  64. result += "\"/>";
  65. result += "<inventory id=\"item_bar\" margin-bottom=\"9\" align-bottom=\"end\" align-left=\"start\" margin-left=\"9\" width=\"592\" height=\"52\" rowSize=\"10\" numSlots=\"10\" slotNameFilter=\"ItemBar\" target=\"";
  66. result += getId();
  67. result += "\"/>";
  68. result += "</dialog>";
  69. return result;
  70. }
  71. void Player::useItemSlot(ItemSlot* zSlot)
  72. {
  73. if (zSlot->zStack())
  74. {
  75. ItemStack* stack = takeItemsOut(zSlot, 1, NO_DIRECTION);
  76. if (stack)
  77. {
  78. Item* item = stack->extractFromStack();
  79. Entity::useItem(item->zItemType(), item);
  80. if (item->getDurability() > 0 && item->getDamage() < item->getMaxDamage())
  81. { // put used item back
  82. stack->addToStack(item);
  83. // TODO: use inventory wrapper to update the cache of the inventory
  84. if (!zSlot->numberOfAddableItems(stack, NO_DIRECTION))
  85. { // move other items to other space
  86. ItemStack* oldItems = zSlot->takeItemsOut(zSlot->zStack()->getSize(), NO_DIRECTION);
  87. zSlot->addItems(stack, NO_DIRECTION);
  88. addItems(oldItems, NO_DIRECTION);
  89. if (oldItems->getSize() > 0)
  90. {
  91. // TODO: drop remaining items
  92. }
  93. }
  94. else
  95. zSlot->addItems(stack, NO_DIRECTION);
  96. }
  97. else
  98. { // item is broken
  99. // move other items of the same type to the slot
  100. Array< ItemSlot*> fromSlots;
  101. for (ItemSlot* slot : itemBar)
  102. {
  103. if (slot != zSlot)
  104. fromSlots.add(slot);
  105. }
  106. Array<ItemSlot*> targetSlots;
  107. targetSlots.add(zSlot);
  108. TypeItemFilter filter(item->zItemType());
  109. localTransaction(&fromSlots, &targetSlots, &filter, zSlot->getFreeSpace(), NO_DIRECTION, NO_DIRECTION);
  110. // place broken item in inventory
  111. const ItemType* brokenType = item->zItemType()->zBrokenItemType();
  112. if (brokenType)
  113. {
  114. Item* broken = item->zItemType()->breakItem(item);
  115. if (broken)
  116. {
  117. stack->addToStack(broken);
  118. addItems(stack, NO_DIRECTION);
  119. if (stack->getSize() > 0)
  120. {
  121. // TODO: drop remaining items
  122. }
  123. }
  124. }
  125. item->release();
  126. }
  127. stack->release();
  128. }
  129. }
  130. else
  131. Entity::useItem(PlayerHandItemType::INSTANCE, 0); // hand usage
  132. }
  133. void Player::setName(Framework::Text name)
  134. {
  135. this->name = name;
  136. }
  137. const char* Player::getName() const
  138. {
  139. return name;
  140. }
  141. void Player::tick(const Dimension* zDimension)
  142. {
  143. speed = { 0, 0, speed.z };
  144. if ((keyState | Key::MOVE_FRONT) == keyState)
  145. speed += {faceDir.x, faceDir.y, 0};
  146. if ((keyState | Key::MOVE_BACK) == keyState)
  147. speed += {-faceDir.x, -faceDir.y, 0};
  148. if ((keyState | Key::MOVE_RIGHT) == keyState)
  149. {
  150. Vec2<float> norm = { faceDir.x, faceDir.y };
  151. norm.CW90().normalize();
  152. speed += {norm.x, norm.y, 0};
  153. }
  154. if ((keyState | Key::MOVE_LEFT) == keyState)
  155. {
  156. Vec2<float> norm = { faceDir.x, faceDir.y };
  157. norm.CCW90().normalize();
  158. speed += {norm.x, norm.y, 0};
  159. }
  160. Vec2<float> norm = { speed.x, speed.y };
  161. if (norm.getLengthSq() != 0)
  162. {
  163. norm.normalize();
  164. speed.x = norm.x * 4.f; // 4 blocks per second movement speed
  165. speed.y = norm.y * 4.f;
  166. }
  167. if ((keyState | Key::MOVE_DOWN) == keyState && gravityMultiplier == 0.f)
  168. speed.z = -4.f;
  169. if ((keyState | Key::LEFT_HAND_ACTION) == keyState)
  170. useItemSlot(itemBar.get(leftHandPosition));
  171. if ((keyState | Key::RIGHT_HAND_ACTION) == keyState)
  172. useItemSlot(itemBar.get((leftHandPosition + 1) % itemBar.getEintragAnzahl()));
  173. return Entity::tick(zDimension);
  174. }
  175. void Player::playerApi(Framework::StreamReader* zRequest, NetworkResponse* zResponse)
  176. {
  177. char byte;
  178. zRequest->lese(&byte, 1);
  179. switch (byte)
  180. {
  181. case 0:
  182. // stop action
  183. zRequest->lese(&byte, 1);
  184. switch (byte)
  185. {
  186. case 0:
  187. keyState = keyState & ~Key::MOVE_FRONT;
  188. break;
  189. case 1:
  190. keyState = keyState & ~Key::MOVE_LEFT;
  191. break;
  192. case 2:
  193. keyState = keyState & ~Key::MOVE_BACK;
  194. break;
  195. case 3:
  196. keyState = keyState & ~Key::MOVE_RIGHT;
  197. break;
  198. case 4:
  199. if (gravityMultiplier == 0.f)
  200. speed.z = 0;
  201. keyState = keyState & ~Key::MOVE_DOWN;
  202. break;
  203. case 5:
  204. keyState = keyState & ~Key::ROTATE_LEFT;
  205. break;
  206. case 6:
  207. keyState = keyState & ~Key::ROTATE_RIGHT;
  208. break;
  209. case 7:
  210. if (gravityMultiplier == 0.f)
  211. speed.z = 0;
  212. keyState = keyState & ~Key::MOVE_UP;
  213. break;
  214. case 8:
  215. keyState = keyState & ~Key::LEFT_HAND_ACTION;
  216. break;
  217. case 9:
  218. keyState = keyState & ~Key::RIGHT_HAND_ACTION;
  219. break;
  220. }
  221. break;
  222. case 1:
  223. // begin action
  224. zRequest->lese(&byte, 1);
  225. switch (byte)
  226. {
  227. case 0:
  228. keyState = keyState | Key::MOVE_FRONT;
  229. break;
  230. case 1:
  231. keyState = keyState | Key::MOVE_LEFT;
  232. break;
  233. case 2:
  234. keyState = keyState | Key::MOVE_BACK;
  235. break;
  236. case 3:
  237. keyState = keyState | Key::MOVE_RIGHT;
  238. break;
  239. case 4:
  240. keyState = keyState | Key::MOVE_DOWN;
  241. break;
  242. case 5:
  243. keyState = keyState | Key::ROTATE_LEFT;
  244. break;
  245. case 6:
  246. keyState = keyState | Key::ROTATE_RIGHT;
  247. break;
  248. case 7:
  249. if ((keyState | Key::MOVE_UP) != keyState)
  250. {
  251. if (gravityMultiplier > 0)
  252. {
  253. if (jumping)
  254. {
  255. // TODO: check if flight is enabled
  256. gravityMultiplier = 0;
  257. jumping = 0;
  258. speed.z = 1.5f;
  259. }
  260. else
  261. {
  262. jumping = 1;
  263. speed.z = 5.f;
  264. }
  265. }
  266. else
  267. speed.z = 1.5f;
  268. }
  269. keyState = keyState | Key::MOVE_UP;
  270. break;
  271. case 8:
  272. keyState = keyState | Key::LEFT_HAND_ACTION;
  273. break;
  274. case 9:
  275. keyState = keyState | Key::RIGHT_HAND_ACTION;
  276. break;
  277. }
  278. break;
  279. case 2:
  280. // switch target direction
  281. zRequest->lese((char*)&faceDir.x, 4);
  282. zRequest->lese((char*)&faceDir.y, 4);
  283. zRequest->lese((char*)&faceDir.z, 4);
  284. break;
  285. case 3:
  286. // switch item bar position
  287. zRequest->lese((char*)&leftHandPosition, 4);
  288. leftHandPosition = leftHandPosition % itemBar.getEintragAnzahl();
  289. needUpdate = 1;
  290. break;
  291. case 4:
  292. {
  293. // open inventory
  294. zResponse->openDialog("player_inventory");
  295. Text uiml = getInventoryUIML();
  296. int msgSize = 5 + uiml.getLength();
  297. char* msg = new char[msgSize];
  298. msg[0] = 0; // open dialog
  299. *(int*)(msg + 1) = uiml.getLength();
  300. memcpy(msg + 5, uiml.getText(), uiml.getLength());
  301. zResponse->setMessage(msg, msgSize, 1);
  302. break;
  303. }
  304. }
  305. }
  306. void Player::onFall(float collisionSpeed)
  307. {
  308. Entity::onFall(collisionSpeed);
  309. gravityMultiplier = 1.f;
  310. jumping = 0;
  311. }
  312. PlayerEntityType::PlayerEntityType()
  313. : EntityType(ID, ModelInfo("player", "player.ltdb/player.png", 6))
  314. {}
  315. void PlayerEntityType::loadSuperEntity(Entity* zEntity, Framework::StreamReader* zReader) const
  316. {
  317. Player* zPlayer = dynamic_cast<Player*>(zEntity);
  318. if (!zPlayer)
  319. throw "PlayerEntityType::loadSuperEntity was called with an entity witch is not an instance of Player";
  320. zReader->lese((char*)&zPlayer->leftHandPosition, 4);
  321. EntityType::loadSuperEntity(zPlayer, zReader);
  322. }
  323. void PlayerEntityType::saveSuperEntity(Entity* zEntity, Framework::StreamWriter* zWriter) const
  324. {
  325. Player* zPlayer = dynamic_cast<Player*>(zEntity);
  326. if (!zPlayer)
  327. throw "PlayerEntityType::saveSuperEntity was called with an entity witch is not an instance of Player";
  328. zWriter->schreibe((char*)&zPlayer->leftHandPosition, 4);
  329. EntityType::saveSuperEntity(zEntity, zWriter);
  330. }
  331. Entity* PlayerEntityType::createEntity(Framework::Vec3<float> position, int dimensionId, int entityId) const
  332. {
  333. return new Player(position, dimensionId, entityId);
  334. }