Soldier.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. //
  2. // Created by jovian on 18/07/17.
  3. //
  4. #include <iostream>
  5. #include "Soldier.h"
  6. Soldier::Soldier(Faction faction, Controller *ctrl, TinyWorld *tinyWorld,
  7. unsigned int imgId, unsigned int camp, unsigned int life)
  8. : Entity(faction, imgId, tinyWorld),
  9. m_ctrl(ctrl), m_camp(camp), m_life(life), m_tinyWorld(tinyWorld), m_homeFixture(nullptr),
  10. m_forceXScale(22.0f), m_forceYScale(5.0f), m_jumpVec(0.0f, -500.0f), m_jumpVelocityLimit(-0.5f),
  11. m_cool(0), m_coolCeil(10) {
  12. // Set the correct image
  13. if (imgId == 0) {
  14. if (m_camp == 0)
  15. m_imgId = 1;
  16. else if (m_camp == 1)
  17. m_imgId = 13;
  18. }
  19. }
  20. void Soldier::shoot(const b2Vec2 &dir) {
  21. m_cool = 0;
  22. m_tinyWorld->addEntity(new Bullet(m_body->GetPosition(), m_tinyWorld, dir, m_camp));
  23. }
  24. void Soldier::update() {
  25. // Life exist
  26. if (m_life == 0)
  27. m_exist = false;
  28. // A soldier fell out the world
  29. if (m_body->GetPosition().y > 100.0f)
  30. m_exist = false;
  31. // Firing
  32. m_cool++;
  33. if (m_ctrl->isFiring() && m_cool > m_coolCeil)
  34. shoot(m_ctrl->getVisor());
  35. // todo Shield activation
  36. // Movement
  37. b2Vec2 move(m_ctrl->getMove());
  38. move.x *= m_forceXScale;
  39. move.y *= m_forceYScale;
  40. m_body->ApplyForceToCenter(move, true);
  41. // Jumping
  42. if (isTouching()
  43. && m_ctrl->isJumping()
  44. && m_body->GetLinearVelocity().y > m_jumpVelocityLimit) {
  45. m_body->ApplyForceToCenter(m_jumpVec, true);
  46. }
  47. // Damage taken by bullet
  48. for (b2ContactEdge *ce = m_body->GetContactList(); ce; ce = ce->next) {
  49. b2Contact *c = ce->contact;
  50. // Get incoming fixture
  51. b2Fixture *incomingFixture(c->GetFixtureA());
  52. if (incomingFixture == m_homeFixture)
  53. incomingFixture = c->GetFixtureB();
  54. // Detect bullet presence
  55. Entity *incomingEntity((Entity *) incomingFixture->GetBody()->GetUserData().pointer);
  56. if (incomingEntity->getFaction() == BULLET) {
  57. // Damage
  58. Bullet *incomingBullet((Bullet*)incomingEntity);
  59. int damage(incomingBullet->getDamage());
  60. if (m_life < damage)
  61. m_life = 0;
  62. else
  63. m_life -= damage;
  64. // Bullet destroyed
  65. incomingEntity->setExistence(false);
  66. }
  67. }
  68. }
  69. void Soldier::createPhysicalShape(b2Vec2 spawn) {
  70. // Already exists ?
  71. if (m_body != nullptr) {
  72. std::cout << "Soldier::createPhysicalShape() > Body non null pointer. Creation aborted." << std::endl;
  73. return;
  74. }
  75. // Creation of physical body
  76. b2BodyDef bodyDef;
  77. bodyDef.type = b2_dynamicBody;
  78. bodyDef.position = spawn;
  79. bodyDef.fixedRotation = true;
  80. m_body = m_tinyWorld->CreateBody(&bodyDef);
  81. // Creation of super shape
  82. b2Vec2 vertices[5];
  83. vertices[0].Set(0.0f, 40.0f / DEFAULT_ZOOM);
  84. vertices[1].Set(20.0f / DEFAULT_ZOOM, -2.0f / DEFAULT_ZOOM);
  85. vertices[2].Set(20.0f / DEFAULT_ZOOM, -38.0f / DEFAULT_ZOOM);
  86. vertices[3].Set(-20.0f / DEFAULT_ZOOM, -38.0f / DEFAULT_ZOOM);
  87. vertices[4].Set(-20.0f / DEFAULT_ZOOM, -2.0f / DEFAULT_ZOOM);
  88. b2PolygonShape allyShape;
  89. allyShape.Set(vertices, 5);
  90. // Definition of fixture
  91. b2FixtureDef fixtureDef;
  92. fixtureDef.shape = &allyShape;
  93. fixtureDef.density = 6.0f;
  94. fixtureDef.friction = 0.3f;
  95. // Fixture filtering
  96. if (m_camp == 0) {
  97. fixtureDef.filter.categoryBits = 0b0001;
  98. fixtureDef.filter.maskBits = 0b1101;
  99. } else if (m_camp == 1) {
  100. fixtureDef.filter.categoryBits = 0b0100;
  101. fixtureDef.filter.maskBits = 0b0111;
  102. }
  103. // Creation of fixture
  104. m_homeFixture = m_body->CreateFixture(&fixtureDef);
  105. // Link this soldier
  106. establishPhysicalLink();
  107. }