Player.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. NetworkMessage msg;
  41. msg.adressGui("gui_item_bar");
  42. char message[9];
  43. message[0] = 1; // set count of items
  44. *(int*)(message + 1) = zSlot->getId();
  45. *(int*)(message + 5) = zSlot->getNumberOfItems();
  46. msg.setMessage(message, 9, 0);
  47. Game::INSTANCE->sendMessage(&msg, this);
  48. return;
  49. }
  50. }
  51. }
  52. void Player::afterPushStack(ItemSlot* zSlot, Direction dir, const Item* zItem, int count)
  53. {
  54. for (auto slot : itemBar)
  55. {
  56. if (slot == zSlot)
  57. {
  58. if (zSlot->getNumberOfItems() > count)
  59. {
  60. NetworkMessage msg;
  61. msg.adressGui("gui_item_bar");
  62. char message[9];
  63. message[0] = 1; // set count of items
  64. *(int*)(message + 1) = zSlot->getId();
  65. *(int*)(message + 5) = zSlot->getNumberOfItems();
  66. msg.setMessage(message, 9, 0);
  67. Game::INSTANCE->sendMessage(&msg, this);
  68. }
  69. else
  70. {
  71. NetworkMessage msg;
  72. msg.adressGui("gui_item_bar");
  73. char message[29];
  74. message[0] = 2; // add new stack
  75. *(int*)(message + 1) = zSlot->getId();
  76. *(int*)(message + 5) = zSlot->getNumberOfItems();
  77. const Item* zItem = zSlot->zStack()->zItem();
  78. *(float*)(message + 9) = zItem->getDamage();
  79. *(float*)(message + 13) = zItem->getMaxDamage();
  80. *(float*)(message + 17) = zItem->getDurability();
  81. *(float*)(message + 21) = zItem->getMaxDurability();
  82. *(int*)(message + 25) = zItem->zItemType()->getId();
  83. msg.setMessage(message, 29, 0);
  84. Game::INSTANCE->sendMessage(&msg, this);
  85. }
  86. return;
  87. }
  88. }
  89. }
  90. void Player::onTargetChange()
  91. {
  92. NetworkMessage msg;
  93. ActionTarget::toMessage(zTarget(), msg);
  94. Game::INSTANCE->sendMessage(&msg, this);
  95. }
  96. Framework::Text Player::getInventoryUIML()
  97. {
  98. Framework::Text result = "<dialog id=\"player_inventory\" title=\"Inventory\" width=\"610\" height=\"450\">";
  99. 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=\"";
  100. result += getId();
  101. result += "\"/>";
  102. 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=\"";
  103. result += getId();
  104. result += "\"/>";
  105. 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=\"";
  106. result += getId();
  107. result += "\"/>";
  108. result += "</dialog>";
  109. return result;
  110. }
  111. Framework::Text Player::getPlayerGUI()
  112. {
  113. Framework::Text result = "<gui id=\"player_gui\">";
  114. result += "<itemBar id=\"gui_item_bar\" margin-bottom=\"9\" align-bottom=\"end\" align-left=\"center\" width=\"592\" height=\"52\" rowSize=\"10\" slotNameFilter=\"ItemBar\" target=\"";
  115. result += getId();
  116. result += "\"/>";
  117. result += "</gui>";
  118. return result;
  119. }
  120. void Player::useItemSlot(ItemSlot* zSlot)
  121. {
  122. if (zSlot->zStack())
  123. {
  124. ItemStack* stack = takeItemsOut(zSlot, 1, NO_DIRECTION);
  125. if (stack)
  126. {
  127. Item* item = stack->extractFromStack();
  128. Entity::useItem(item->zItemType(), item);
  129. if (item->getDurability() > 0 && item->getDamage() < item->getMaxDamage())
  130. { // put used item back
  131. stack->addToStack(item);
  132. // TODO: use inventory wrapper to update the cache of the inventory
  133. if (!zSlot->numberOfAddableItems(stack, NO_DIRECTION))
  134. { // move other items to other space
  135. ItemStack* oldItems = zSlot->takeItemsOut(zSlot->zStack()->getSize(), NO_DIRECTION);
  136. zSlot->addItems(stack, NO_DIRECTION);
  137. addItems(oldItems, NO_DIRECTION);
  138. if (oldItems->getSize() > 0)
  139. {
  140. // TODO: drop remaining items
  141. }
  142. }
  143. else
  144. zSlot->addItems(stack, NO_DIRECTION);
  145. }
  146. else
  147. { // item is broken
  148. // move other items of the same type to the slot
  149. Array< ItemSlot*> fromSlots;
  150. for (ItemSlot* slot : itemBar)
  151. {
  152. if (slot != zSlot)
  153. fromSlots.add(slot);
  154. }
  155. Array<ItemSlot*> targetSlots;
  156. targetSlots.add(zSlot);
  157. TypeItemFilter filter(item->zItemType());
  158. localTransaction(&fromSlots, &targetSlots, &filter, zSlot->getFreeSpace(), NO_DIRECTION, NO_DIRECTION);
  159. // place broken item in inventory
  160. const ItemType* brokenType = item->zItemType()->zBrokenItemType();
  161. if (brokenType)
  162. {
  163. Item* broken = item->zItemType()->breakItem(item);
  164. if (broken)
  165. {
  166. stack->addToStack(broken);
  167. addItems(stack, NO_DIRECTION);
  168. if (stack->getSize() > 0)
  169. {
  170. // TODO: drop remaining items
  171. }
  172. }
  173. }
  174. item->release();
  175. }
  176. stack->release();
  177. }
  178. }
  179. else
  180. Entity::useItem(PlayerHandItemType::INSTANCE, 0); // hand usage
  181. }
  182. void Player::setName(Framework::Text name)
  183. {
  184. this->name = name;
  185. }
  186. const char* Player::getName() const
  187. {
  188. return name;
  189. }
  190. void Player::tick(const Dimension* zDimension)
  191. {
  192. speed = { 0, 0, speed.z };
  193. if ((keyState | Key::MOVE_FRONT) == keyState)
  194. speed += {faceDir.x, faceDir.y, 0};
  195. if ((keyState | Key::MOVE_BACK) == keyState)
  196. speed += {-faceDir.x, -faceDir.y, 0};
  197. if ((keyState | Key::MOVE_RIGHT) == keyState)
  198. {
  199. Vec2<float> norm = { faceDir.x, faceDir.y };
  200. norm.CW90().normalize();
  201. speed += {norm.x, norm.y, 0};
  202. }
  203. if ((keyState | Key::MOVE_LEFT) == keyState)
  204. {
  205. Vec2<float> norm = { faceDir.x, faceDir.y };
  206. norm.CCW90().normalize();
  207. speed += {norm.x, norm.y, 0};
  208. }
  209. Vec2<float> norm = { speed.x, speed.y };
  210. if (norm.getLengthSq() != 0)
  211. {
  212. norm.normalize();
  213. speed.x = norm.x * 4.f; // 4 blocks per second movement speed
  214. speed.y = norm.y * 4.f;
  215. }
  216. if ((keyState | Key::MOVE_DOWN) == keyState && gravityMultiplier == 0.f)
  217. speed.z = -4.f;
  218. if ((keyState | Key::LEFT_HAND_ACTION) == keyState)
  219. useItemSlot(itemBar.get(leftHandPosition));
  220. if ((keyState | Key::RIGHT_HAND_ACTION) == keyState)
  221. useItemSlot(itemBar.get((leftHandPosition + 1) % itemBar.getEintragAnzahl()));
  222. return Entity::tick(zDimension);
  223. }
  224. void Player::playerApi(Framework::StreamReader* zRequest, NetworkMessage* zResponse)
  225. {
  226. char byte;
  227. zRequest->lese(&byte, 1);
  228. switch (byte)
  229. {
  230. case 0:
  231. // stop action
  232. zRequest->lese(&byte, 1);
  233. switch (byte)
  234. {
  235. case 0:
  236. keyState = keyState & ~Key::MOVE_FRONT;
  237. break;
  238. case 1:
  239. keyState = keyState & ~Key::MOVE_LEFT;
  240. break;
  241. case 2:
  242. keyState = keyState & ~Key::MOVE_BACK;
  243. break;
  244. case 3:
  245. keyState = keyState & ~Key::MOVE_RIGHT;
  246. break;
  247. case 4:
  248. if (gravityMultiplier == 0.f)
  249. speed.z = 0;
  250. keyState = keyState & ~Key::MOVE_DOWN;
  251. break;
  252. case 5:
  253. keyState = keyState & ~Key::ROTATE_LEFT;
  254. break;
  255. case 6:
  256. keyState = keyState & ~Key::ROTATE_RIGHT;
  257. break;
  258. case 7:
  259. if (gravityMultiplier == 0.f)
  260. speed.z = 0;
  261. keyState = keyState & ~Key::MOVE_UP;
  262. break;
  263. case 8:
  264. keyState = keyState & ~Key::LEFT_HAND_ACTION;
  265. break;
  266. case 9:
  267. keyState = keyState & ~Key::RIGHT_HAND_ACTION;
  268. break;
  269. }
  270. break;
  271. case 1:
  272. // begin action
  273. zRequest->lese(&byte, 1);
  274. switch (byte)
  275. {
  276. case 0:
  277. keyState = keyState | Key::MOVE_FRONT;
  278. break;
  279. case 1:
  280. keyState = keyState | Key::MOVE_LEFT;
  281. break;
  282. case 2:
  283. keyState = keyState | Key::MOVE_BACK;
  284. break;
  285. case 3:
  286. keyState = keyState | Key::MOVE_RIGHT;
  287. break;
  288. case 4:
  289. keyState = keyState | Key::MOVE_DOWN;
  290. break;
  291. case 5:
  292. keyState = keyState | Key::ROTATE_LEFT;
  293. break;
  294. case 6:
  295. keyState = keyState | Key::ROTATE_RIGHT;
  296. break;
  297. case 7:
  298. if ((keyState | Key::MOVE_UP) != keyState)
  299. {
  300. if (gravityMultiplier > 0)
  301. {
  302. if (jumping)
  303. {
  304. // TODO: check if flight is enabled
  305. gravityMultiplier = 0;
  306. jumping = 0;
  307. speed.z = 1.5f;
  308. }
  309. else
  310. {
  311. jumping = 1;
  312. speed.z = 5.f;
  313. }
  314. }
  315. else
  316. speed.z = 1.5f;
  317. }
  318. keyState = keyState | Key::MOVE_UP;
  319. break;
  320. case 8:
  321. keyState = keyState | Key::LEFT_HAND_ACTION;
  322. break;
  323. case 9:
  324. keyState = keyState | Key::RIGHT_HAND_ACTION;
  325. break;
  326. }
  327. break;
  328. case 2:
  329. // switch target direction
  330. zRequest->lese((char*)&faceDir.x, 4);
  331. zRequest->lese((char*)&faceDir.y, 4);
  332. zRequest->lese((char*)&faceDir.z, 4);
  333. break;
  334. case 3:
  335. { // switch item bar position
  336. zRequest->lese((char*)&leftHandPosition, 4);
  337. leftHandPosition = leftHandPosition % itemBar.getEintragAnzahl();
  338. NetworkMessage msg;
  339. msg.adressGui("gui_item_bar");
  340. char message[5];
  341. message[0] = 3; // set selected slot
  342. *(int*)(message + 1) = leftHandPosition;
  343. msg.setMessage(message, 5, 0);
  344. Game::INSTANCE->sendMessage(&msg, this);
  345. break;
  346. }
  347. case 4:
  348. {
  349. // open inventory
  350. zResponse->openDialog("player_inventory");
  351. Text uiml = getInventoryUIML();
  352. int msgSize = 4 + uiml.getLength();
  353. char* msg = new char[msgSize];
  354. *(int*)msg = uiml.getLength();
  355. memcpy(msg + 4, uiml.getText(), uiml.getLength());
  356. zResponse->setMessage(msg, msgSize, 1);
  357. break;
  358. }
  359. case 5:
  360. {
  361. // request gui
  362. Text uiml = getPlayerGUI();
  363. int msgSize = 6 + uiml.getLength();
  364. char* msg = new char[msgSize];
  365. msg[0] = 2; // gui message
  366. msg[1] = 2; // set gui
  367. *(int*)(msg + 2) = uiml.getLength();
  368. memcpy(msg + 6, uiml.getText(), uiml.getLength());
  369. zResponse->setMessage(msg, msgSize, 1);
  370. break;
  371. }
  372. }
  373. }
  374. void Player::onFall(float collisionSpeed)
  375. {
  376. Entity::onFall(collisionSpeed);
  377. gravityMultiplier = 1.f;
  378. jumping = 0;
  379. }
  380. PlayerEntityType::PlayerEntityType()
  381. : EntityType(ID, ModelInfo("player", "player.ltdb/player.png", 6))
  382. {}
  383. void PlayerEntityType::loadSuperEntity(Entity* zEntity, Framework::StreamReader* zReader) const
  384. {
  385. Player* zPlayer = dynamic_cast<Player*>(zEntity);
  386. if (!zPlayer)
  387. throw "PlayerEntityType::loadSuperEntity was called with an entity witch is not an instance of Player";
  388. zReader->lese((char*)&zPlayer->leftHandPosition, 4);
  389. EntityType::loadSuperEntity(zPlayer, zReader);
  390. }
  391. void PlayerEntityType::saveSuperEntity(Entity* zEntity, Framework::StreamWriter* zWriter) const
  392. {
  393. Player* zPlayer = dynamic_cast<Player*>(zEntity);
  394. if (!zPlayer)
  395. throw "PlayerEntityType::saveSuperEntity was called with an entity witch is not an instance of Player";
  396. zWriter->schreibe((char*)&zPlayer->leftHandPosition, 4);
  397. EntityType::saveSuperEntity(zEntity, zWriter);
  398. }
  399. Entity* PlayerEntityType::createEntity(Framework::Vec3<float> position, int dimensionId, int entityId) const
  400. {
  401. return new Player(position, dimensionId, entityId);
  402. }