Entity.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. #include "Entity.h"
  2. #include "Dimension.h"
  3. #include "Game.h"
  4. #include "BlockType.h"
  5. Entity::Entity( const EntityType* zType, Framework::Vec3<float> location, int dimensionId, int entityId )
  6. : Inventory( location, true ),
  7. speed( 0, 0, 0 ),
  8. faceDir( 1, 0, 0 ),
  9. zEntityType( zType ),
  10. currentDimensionId( dimensionId ),
  11. removed( 0 ),
  12. gravityMultiplier( 1.f ),
  13. id( entityId )
  14. {}
  15. void Entity::onDeath()
  16. {}
  17. void Entity::tick( const Dimension* zDimension, Game* zGame )
  18. {
  19. Vec3<float> oldPos = location;
  20. // current block cooredinates
  21. int px = (int)floor( location.x );
  22. int py = (int)floor( location.y );
  23. int pz = (int)floor( location.z );
  24. // falling down
  25. speed.z -= (zDimension->getGravity() * gravityMultiplier) / 30.f;
  26. // movement in current tick
  27. Vec3<float> frameSpeed = speed / 30.f;
  28. // loop through all collided blocks
  29. bool needCollisionCheck = 1;
  30. while( needCollisionCheck )
  31. {
  32. needCollisionCheck = 0;
  33. // collision to neighbor of current block current block
  34. if( speed.x > 0 )
  35. {
  36. float xt = ((float)px + 1.f - oldPos.x) / frameSpeed.x;
  37. Vec3<float> tmp = oldPos + frameSpeed * xt;
  38. if( xt <= 1.f && tmp.y >= (float)py && tmp.y < (float)py + 1.f && tmp.z >= (float)pz && tmp.z < (float)pz + 1.f )
  39. {
  40. if( !getDefaultBlock( zGame->zBlockAt( Vec3<int>{ px + 1, py, pz }, zDimension->getDimensionId() ) )->isPassable() )
  41. {
  42. frameSpeed.x = frameSpeed.x * (xt - 0.1f);
  43. speed.x = 0;
  44. }
  45. else
  46. px++;
  47. needCollisionCheck = 1;
  48. continue;
  49. }
  50. }
  51. if( speed.x < 0 )
  52. {
  53. float xt = ((float)px - oldPos.x) / frameSpeed.x;
  54. Vec3<float> tmp = oldPos + frameSpeed * xt;
  55. if( xt <= 1.f && tmp.y >= (float)py && tmp.y < (float)py + 1.f && tmp.z >= (float)pz && tmp.z < (float)pz + 1.f )
  56. {
  57. if( !getDefaultBlock( zGame->zBlockAt( Vec3<int>{ px - 1, py, pz }, zDimension->getDimensionId() ) )->isPassable() )
  58. {
  59. frameSpeed.x = frameSpeed.x * (xt - 0.1f);
  60. speed.x = 0;
  61. }
  62. else
  63. px--;
  64. needCollisionCheck = 1;
  65. continue;
  66. }
  67. }
  68. if( speed.y > 0 )
  69. {
  70. float yt = ((float)py + 1.f - oldPos.y) / frameSpeed.y;
  71. Vec3<float> tmp = oldPos + frameSpeed * yt;
  72. if( yt <= 1.f && tmp.x >= (float)px && tmp.x < (float)px + 1.f && tmp.z >= (float)pz && tmp.z < (float)pz + 1.f )
  73. {
  74. if( !getDefaultBlock( zGame->zBlockAt( Vec3<int>{ px, py + 1, pz }, zDimension->getDimensionId() ) )->isPassable() )
  75. {
  76. frameSpeed.y = frameSpeed.y * (yt - 0.1f);
  77. speed.y = 0;
  78. }
  79. else
  80. py++;
  81. needCollisionCheck = 1;
  82. continue;
  83. }
  84. }
  85. if( speed.y < 0 )
  86. {
  87. float yt = ((float)py - oldPos.y) / frameSpeed.y;
  88. Vec3<float> tmp = oldPos + frameSpeed * yt;
  89. if( yt <= 1.f && tmp.x >= (float)px && tmp.x < (float)px + 1.f && tmp.z >= (float)pz && tmp.z < (float)pz + 1.f )
  90. {
  91. if( !getDefaultBlock( zGame->zBlockAt( Vec3<int>{ px, py - 1, pz }, zDimension->getDimensionId() ) )->isPassable() )
  92. {
  93. frameSpeed.y = frameSpeed.y * (yt - 0.1f);
  94. speed.y = 0;
  95. }
  96. else
  97. py--;
  98. needCollisionCheck = 1;
  99. continue;
  100. }
  101. }
  102. if( speed.z > 0 )
  103. {
  104. float zt = ((float)pz + 1.f - oldPos.z) / frameSpeed.z;
  105. Vec3<float> tmp = oldPos + frameSpeed * zt;
  106. if( zt <= 1.f && tmp.x >= (float)px && tmp.x < (float)px + 1.f && tmp.y >= (float)py && tmp.y < (float)py + 1.f )
  107. {
  108. if( !getDefaultBlock( zGame->zBlockAt( Vec3<int>{ px, py, pz + 1 }, zDimension->getDimensionId() ) )->isPassable() )
  109. {
  110. frameSpeed.z = frameSpeed.z * (zt - 0.1f);
  111. speed.z = 0;
  112. }
  113. else
  114. pz++;
  115. needCollisionCheck = 1;
  116. continue;
  117. }
  118. }
  119. if( speed.z < 0 )
  120. {
  121. float zt = ((float)pz - oldPos.z) / frameSpeed.z;
  122. Vec3<float> tmp = oldPos + frameSpeed * zt;
  123. if( zt <= 1.f && tmp.x >= (float)px && tmp.x < (float)px + 1.f && tmp.y >= (float)py && tmp.y < (float)py + 1 )
  124. {
  125. if( !getDefaultBlock( zGame->zBlockAt( Vec3<int>{ px, py, pz - 1 }, zDimension->getDimensionId() ) )->isPassable() )
  126. {
  127. frameSpeed.z = frameSpeed.z * (zt - 0.1f);
  128. onFall( speed.z );
  129. speed.z = 0;
  130. }
  131. else
  132. pz--;
  133. needCollisionCheck = 1;
  134. continue;
  135. }
  136. }
  137. }
  138. location += frameSpeed;
  139. }
  140. void Entity::api( Framework::StreamReader* zRequest, NetworkResponse* zResponse )
  141. {
  142. // TODO: answer api requests
  143. }
  144. void Entity::onFall( float collisionSpeed )
  145. {
  146. if( collisionSpeed > 5 )
  147. {
  148. // TODO: take damage
  149. }
  150. }
  151. void Entity::setPosition( Framework::Vec3<float> pos )
  152. {
  153. location = pos;
  154. }
  155. float Entity::getMaxHP() const
  156. {
  157. return maxHP;
  158. }
  159. float Entity::getCurrentHP() const
  160. {
  161. return currentHP;
  162. }
  163. float Entity::getStamina() const
  164. {
  165. return stamina;
  166. }
  167. float Entity::getMaxStamina() const
  168. {
  169. return maxStamina;
  170. }
  171. float Entity::getHunger() const
  172. {
  173. return hunger;
  174. }
  175. float Entity::getMaxHunger() const
  176. {
  177. return maxHunger;
  178. }
  179. float Entity::getThirst() const
  180. {
  181. return thirst;
  182. }
  183. float Entity::getMaxThirst() const
  184. {
  185. return maxThirst;
  186. }
  187. Framework::Vec3<float> Entity::getSpeed() const
  188. {
  189. return speed;
  190. }
  191. Framework::Vec3<float> Entity::getFaceDir() const
  192. {
  193. return faceDir;
  194. }
  195. Framework::Vec3<float> Entity::getPosition() const
  196. {
  197. return location;
  198. }
  199. float Entity::getGravityMultiplier() const
  200. {
  201. return gravityMultiplier;
  202. }
  203. int Entity::getCurrentDimensionId() const
  204. {
  205. return currentDimensionId;
  206. }
  207. bool Entity::isRemoved() const
  208. {
  209. return removed;
  210. }
  211. const EntityType* Entity::zType() const
  212. {
  213. return zEntityType;
  214. }
  215. int Entity::getId() const
  216. {
  217. return id;
  218. }