Entity.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. //
  2. // Created by jovian on 18/07/17.
  3. //
  4. #include <iostream>
  5. #include "Entity.h"
  6. Entity::Entity(Faction faction, unsigned int imgId, b2World *physics)
  7. : m_faction(faction), m_exist(true), m_physics(physics), m_body(nullptr), m_imgId(imgId) {
  8. }
  9. Entity::~Entity() {
  10. if (m_body != nullptr)
  11. m_physics->DestroyBody(m_body);
  12. }
  13. Visual *Entity::makeVisual() {
  14. return new Visual(m_imgId, m_body->GetPosition(), m_body->GetAngle());
  15. }
  16. bool Entity::isExist() const {
  17. return m_exist;
  18. }
  19. bool Entity::isTouching() const {
  20. bool touch(false);
  21. for (b2ContactEdge *ce(m_body->GetContactList()); ce && !touch; ce = ce->next)
  22. touch = ce->contact->IsTouching();
  23. return touch;
  24. }
  25. b2Vec2 Entity::getPos() const {
  26. if (m_body == nullptr)
  27. return b2Vec2(0.0f, 0.0f);
  28. else
  29. return m_body->GetPosition();
  30. }
  31. void Entity::establishPhysicalLink() {
  32. if (m_body == nullptr) {
  33. std::cout << "Entity::establishPhysicalLink() > Body is invalid for a " << m_faction << " object." << std::endl;
  34. return;
  35. } else {
  36. m_body->GetUserData().pointer = (uintptr_t) this;
  37. }
  38. }
  39. Faction Entity::getFaction() const {
  40. return m_faction;
  41. }
  42. void Entity::setExistence(bool exist) {
  43. Entity::m_exist = exist;
  44. }