Entity.cpp 14 KB

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