Entity.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. #include "Entity.h"
  2. #include "Dimension.h"
  3. #include "Game.h"
  4. #include "BlockType.h"
  5. #include "ItemSkill.h"
  6. #include "PlaceBlockUpdate.h"
  7. #include "EntityRemovedUpdate.h"
  8. #include "EntityChangedUpdate.h"
  9. ActionTarget::ActionTarget( Vec3<int> blockPos, Direction blockSide )
  10. : blockPos( blockPos ),
  11. targetBlockSide( blockSide ),
  12. entityId( -1 )
  13. {}
  14. ActionTarget::ActionTarget( int entityId )
  15. : entityId( entityId )
  16. {}
  17. bool ActionTarget::isBlock( Framework::Vec3<int> blockPos, Direction blockSide ) const
  18. {
  19. return this->entityId == -1 && this->blockPos == blockPos && this->targetBlockSide == targetBlockSide;
  20. }
  21. bool ActionTarget::isEntity( int entityId ) const
  22. {
  23. return this->entityId == entityId;
  24. }
  25. void ActionTarget::applyItemSkillOnTarget( Entity* zActor, ItemSkill* zItemSkill, Item* zUsedItem )
  26. {
  27. if( entityId >= 0 )
  28. {
  29. // TODO: get entity from game and apply skill
  30. }
  31. else
  32. {
  33. Block* block = Game::INSTANCE->zRealBlockInstance( blockPos, zActor->getCurrentDimensionId() );
  34. if( block )
  35. zItemSkill->use( zActor, zUsedItem, block );
  36. }
  37. }
  38. void ActionTarget::placeBlock( Entity* zActor, Item* zItem )
  39. {
  40. // TODO: check stamina of actor
  41. Block* block = zItem->zPlacedBlockType()->createBlockAt( blockPos + getDirection( targetBlockSide ), zItem );
  42. if( block )
  43. {
  44. if( Game::INSTANCE->requestWorldUpdate( new PlaceBlockUpdate( block, block->getPos(), zActor->getCurrentDimensionId() ) ) )
  45. {
  46. zItem->onPlaced();
  47. // TODO: decrese stamina of actor
  48. }
  49. }
  50. }
  51. void ActionTarget::save( Framework::StreamWriter* zWriter ) const
  52. {
  53. if( entityId >= 0 )
  54. {
  55. char b = 1;
  56. zWriter->schreibe( &b, 1 );
  57. zWriter->schreibe( (char*)&entityId, 4 );
  58. }
  59. else
  60. {
  61. char b = 2;
  62. zWriter->schreibe( &b, 1 );
  63. zWriter->schreibe( (char*)&blockPos.x, 4 );
  64. zWriter->schreibe( (char*)&blockPos.y, 4 );
  65. zWriter->schreibe( (char*)&blockPos.z, 4 );
  66. zWriter->schreibe( (char*)&targetBlockSide, 4 );
  67. }
  68. }
  69. Entity::Entity( const EntityType* zType, Framework::Vec3<float> location, int dimensionId, int entityId )
  70. : Inventory( location, true ),
  71. speed( 0, 0, 0 ),
  72. faceDir( 1, 0, 0 ),
  73. target( 0 ),
  74. zEntityType( zType ),
  75. currentDimensionId( dimensionId ),
  76. removed( 0 ),
  77. gravityMultiplier( 1.f ),
  78. needUpdate( 0 ),
  79. id( entityId )
  80. {}
  81. void Entity::onDeath()
  82. {
  83. removed = 1;
  84. Game::INSTANCE->requestWorldUpdate( new EntityRemovedUpdate( id, currentDimensionId, location ) );
  85. }
  86. void Entity::useItem( const ItemType* zType, Item* zItem )
  87. {
  88. if( zItem && zItem->isEatable() )
  89. { // TODO: eat item
  90. zItem->applyFoodEffects( this );
  91. }
  92. else if( zItem && zItem->isPlaceable() )
  93. { // TODO: place item
  94. if( target )
  95. target->placeBlock( this, zItem );
  96. }
  97. else if( !zItem || zItem->isUsable() )
  98. { // use item skill
  99. if( target )
  100. {
  101. ItemSkill* selected = 0;
  102. for( ItemSkill* skill : skills )
  103. {
  104. if( skill->zSkillType() == zType )
  105. {
  106. selected = skill;
  107. break;
  108. }
  109. }
  110. if( !selected )
  111. {
  112. selected = zType->createDefaultItemSkill();
  113. skills.add( selected );
  114. }
  115. target->applyItemSkillOnTarget( this, selected, zItem );
  116. }
  117. }
  118. }
  119. void Entity::prepareTick( const Dimension* zDimension )
  120. {
  121. Vec3<float> headPosition = location + faceOffset;
  122. int px = (int)floor( headPosition.x );
  123. int py = (int)floor( headPosition.y );
  124. int pz = (int)floor( headPosition.z );
  125. faceDir.normalize();
  126. Direction dir = BOTTOM;
  127. while( true )
  128. {
  129. if( getDefaultBlock( Game::INSTANCE->zBlockAt( Vec3<int>{ px, py, pz }, zDimension->getDimensionId() ) )->isInteractable() )
  130. {
  131. if( !target || !target->isBlock( { px, py, pz }, dir ) )
  132. {
  133. delete target;
  134. target = new ActionTarget( { px, py, pz }, dir );
  135. needUpdate = 1;
  136. }
  137. break;
  138. }
  139. // collision to neighbor of current block
  140. if( faceDir.x > 0 )
  141. {
  142. float xt = ((float)px + 1.f - headPosition.x) / faceDir.x;
  143. Vec3<float> tmp = headPosition + faceDir * xt;
  144. if( xt <= targetDistanceLimit && tmp.y >= (float)py && tmp.y < (float)py + 1.f && tmp.z >= (float)pz && tmp.z < (float)pz + 1.f )
  145. {
  146. dir = WEST;
  147. px++;
  148. continue;
  149. }
  150. }
  151. if( faceDir.x < 0 )
  152. {
  153. float xt = ((float)px - headPosition.x) / faceDir.x;
  154. Vec3<float> tmp = headPosition + faceDir * xt;
  155. if( xt <= targetDistanceLimit && tmp.y >= (float)py && tmp.y < (float)py + 1.f && tmp.z >= (float)pz && tmp.z < (float)pz + 1.f )
  156. {
  157. dir = EAST;
  158. px--;
  159. continue;
  160. }
  161. }
  162. if( faceDir.y > 0 )
  163. {
  164. float yt = ((float)py + 1.f - headPosition.y) / faceDir.y;
  165. Vec3<float> tmp = headPosition + faceDir * yt;
  166. if( yt <= targetDistanceLimit && tmp.x >= (float)px && tmp.x < (float)px + 1.f && tmp.z >= (float)pz && tmp.z < (float)pz + 1.f )
  167. {
  168. dir = NORTH;
  169. py++;
  170. continue;
  171. }
  172. }
  173. if( faceDir.y < 0 )
  174. {
  175. float yt = ((float)py - headPosition.y) / faceDir.y;
  176. Vec3<float> tmp = headPosition + faceDir * yt;
  177. if( yt <= targetDistanceLimit && tmp.x >= (float)px && tmp.x < (float)px + 1.f && tmp.z >= (float)pz && tmp.z < (float)pz + 1.f )
  178. {
  179. dir = SOUTH;
  180. py--;
  181. continue;
  182. }
  183. }
  184. if( faceDir.z > 0 )
  185. {
  186. float zt = ((float)pz + 1.f - headPosition.z) / faceDir.z;
  187. Vec3<float> tmp = headPosition + faceDir * zt;
  188. if( zt <= targetDistanceLimit && tmp.x >= (float)px && tmp.x < (float)px + 1.f && tmp.y >= (float)py && tmp.y < (float)py + 1.f )
  189. {
  190. dir = BOTTOM;
  191. pz++;
  192. continue;
  193. }
  194. }
  195. if( faceDir.z < 0 )
  196. {
  197. float zt = ((float)pz - headPosition.z) / faceDir.z;
  198. Vec3<float> tmp = headPosition + faceDir * zt;
  199. if( zt <= targetDistanceLimit && tmp.x >= (float)px && tmp.x < (float)px + 1.f && tmp.y >= (float)py && tmp.y < (float)py + 1 )
  200. {
  201. dir = TOP;
  202. pz--;
  203. continue;
  204. }
  205. }
  206. if( target )
  207. {
  208. delete target;
  209. target = 0;
  210. needUpdate = 1;
  211. }
  212. break;
  213. }
  214. }
  215. void Entity::tick( const Dimension* zDimension )
  216. {
  217. Vec3<float> oldPos = location;
  218. // current block cooredinates
  219. int px = (int)floor( location.x );
  220. int py = (int)floor( location.y );
  221. int pz = (int)floor( location.z );
  222. // falling down
  223. speed.z -= (zDimension->getGravity() * gravityMultiplier) / 30.f;
  224. // movement in current tick
  225. Vec3<float> frameSpeed = speed / 30.f;
  226. // loop through all collided blocks
  227. bool needCollisionCheck = 1;
  228. while( needCollisionCheck )
  229. {
  230. needCollisionCheck = 0;
  231. // collision to neighbor of current block
  232. if( speed.x > 0 )
  233. {
  234. float xt = ((float)px + 1.f - oldPos.x) / frameSpeed.x;
  235. Vec3<float> tmp = oldPos + frameSpeed * xt;
  236. if( xt <= 1.f && tmp.y >= (float)py && tmp.y < (float)py + 1.f && tmp.z >= (float)pz && tmp.z < (float)pz + 1.f )
  237. {
  238. if( !getDefaultBlock( Game::INSTANCE->zBlockAt( Vec3<int>{ px + 1, py, pz }, zDimension->getDimensionId() ) )->isPassable() )
  239. {
  240. frameSpeed.x = frameSpeed.x * (xt - 0.1f);
  241. speed.x = 0;
  242. }
  243. else
  244. px++;
  245. needCollisionCheck = 1;
  246. continue;
  247. }
  248. }
  249. if( speed.x < 0 )
  250. {
  251. float xt = ((float)px - oldPos.x) / frameSpeed.x;
  252. Vec3<float> tmp = oldPos + frameSpeed * xt;
  253. if( xt <= 1.f && tmp.y >= (float)py && tmp.y < (float)py + 1.f && tmp.z >= (float)pz && tmp.z < (float)pz + 1.f )
  254. {
  255. if( !getDefaultBlock( Game::INSTANCE->zBlockAt( Vec3<int>{ px - 1, py, pz }, zDimension->getDimensionId() ) )->isPassable() )
  256. {
  257. frameSpeed.x = frameSpeed.x * (xt - 0.1f);
  258. speed.x = 0;
  259. }
  260. else
  261. px--;
  262. needCollisionCheck = 1;
  263. continue;
  264. }
  265. }
  266. if( speed.y > 0 )
  267. {
  268. float yt = ((float)py + 1.f - oldPos.y) / frameSpeed.y;
  269. Vec3<float> tmp = oldPos + frameSpeed * yt;
  270. if( yt <= 1.f && tmp.x >= (float)px && tmp.x < (float)px + 1.f && tmp.z >= (float)pz && tmp.z < (float)pz + 1.f )
  271. {
  272. if( !getDefaultBlock( Game::INSTANCE->zBlockAt( Vec3<int>{ px, py + 1, pz }, zDimension->getDimensionId() ) )->isPassable() )
  273. {
  274. frameSpeed.y = frameSpeed.y * (yt - 0.1f);
  275. speed.y = 0;
  276. }
  277. else
  278. py++;
  279. needCollisionCheck = 1;
  280. continue;
  281. }
  282. }
  283. if( speed.y < 0 )
  284. {
  285. float yt = ((float)py - oldPos.y) / frameSpeed.y;
  286. Vec3<float> tmp = oldPos + frameSpeed * yt;
  287. if( yt <= 1.f && tmp.x >= (float)px && tmp.x < (float)px + 1.f && tmp.z >= (float)pz && tmp.z < (float)pz + 1.f )
  288. {
  289. if( !getDefaultBlock( Game::INSTANCE->zBlockAt( Vec3<int>{ px, py - 1, pz }, zDimension->getDimensionId() ) )->isPassable() )
  290. {
  291. frameSpeed.y = frameSpeed.y * (yt - 0.1f);
  292. speed.y = 0;
  293. }
  294. else
  295. py--;
  296. needCollisionCheck = 1;
  297. continue;
  298. }
  299. }
  300. if( speed.z > 0 )
  301. {
  302. float zt = ((float)pz + 1.f - oldPos.z) / frameSpeed.z;
  303. Vec3<float> tmp = oldPos + frameSpeed * zt;
  304. if( zt <= 1.f && tmp.x >= (float)px && tmp.x < (float)px + 1.f && tmp.y >= (float)py && tmp.y < (float)py + 1.f )
  305. {
  306. if( !getDefaultBlock( Game::INSTANCE->zBlockAt( Vec3<int>{ px, py, pz + 1 }, zDimension->getDimensionId() ) )->isPassable() )
  307. {
  308. frameSpeed.z = frameSpeed.z * (zt - 0.1f);
  309. speed.z = 0;
  310. }
  311. else
  312. pz++;
  313. needCollisionCheck = 1;
  314. continue;
  315. }
  316. }
  317. if( speed.z < 0 )
  318. {
  319. float zt = ((float)pz - oldPos.z) / frameSpeed.z;
  320. Vec3<float> tmp = oldPos + frameSpeed * zt;
  321. if( zt <= 1.f && tmp.x >= (float)px && tmp.x < (float)px + 1.f && tmp.y >= (float)py && tmp.y < (float)py + 1 )
  322. {
  323. if( !getDefaultBlock( Game::INSTANCE->zBlockAt( Vec3<int>{ px, py, pz - 1 }, zDimension->getDimensionId() ) )->isPassable() )
  324. {
  325. frameSpeed.z = frameSpeed.z * (zt - 0.1f);
  326. onFall( speed.z );
  327. speed.z = 0;
  328. }
  329. else
  330. pz--;
  331. needCollisionCheck = 1;
  332. continue;
  333. }
  334. }
  335. }
  336. location += frameSpeed;
  337. if( oldPos != location || needUpdate )
  338. {
  339. needUpdate = 0;
  340. Game::INSTANCE->requestWorldUpdate( new EntityChangedUpdate( id, location, currentDimensionId ) );
  341. }
  342. }
  343. void Entity::api( Framework::StreamReader* zRequest, NetworkResponse* zResponse )
  344. {
  345. // TODO: answer api requests
  346. }
  347. void Entity::onFall( float collisionSpeed )
  348. {
  349. if( collisionSpeed > 5 )
  350. {
  351. // TODO: take damage
  352. }
  353. }
  354. void Entity::setPosition( Framework::Vec3<float> pos )
  355. {
  356. location = pos;
  357. }
  358. float Entity::getMaxHP() const
  359. {
  360. return maxHP;
  361. }
  362. float Entity::getCurrentHP() const
  363. {
  364. return currentHP;
  365. }
  366. float Entity::getStamina() const
  367. {
  368. return stamina;
  369. }
  370. float Entity::getMaxStamina() const
  371. {
  372. return maxStamina;
  373. }
  374. float Entity::getHunger() const
  375. {
  376. return hunger;
  377. }
  378. float Entity::getMaxHunger() const
  379. {
  380. return maxHunger;
  381. }
  382. float Entity::getThirst() const
  383. {
  384. return thirst;
  385. }
  386. float Entity::getMaxThirst() const
  387. {
  388. return maxThirst;
  389. }
  390. Framework::Vec3<float> Entity::getSpeed() const
  391. {
  392. return speed;
  393. }
  394. Framework::Vec3<float> Entity::getFaceDir() const
  395. {
  396. return faceDir;
  397. }
  398. Framework::Vec3<float> Entity::getPosition() const
  399. {
  400. return location;
  401. }
  402. float Entity::getGravityMultiplier() const
  403. {
  404. return gravityMultiplier;
  405. }
  406. int Entity::getCurrentDimensionId() const
  407. {
  408. return currentDimensionId;
  409. }
  410. bool Entity::isRemoved() const
  411. {
  412. return removed;
  413. }
  414. const EntityType* Entity::zType() const
  415. {
  416. return zEntityType;
  417. }
  418. const ActionTarget* Entity::zTarget() const
  419. {
  420. return target;
  421. }
  422. int Entity::getId() const
  423. {
  424. return id;
  425. }