Advertisement
Intreipd

Player

Aug 13th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.86 KB | None | 0 0
  1. //player.h
  2. #pragma once
  3. #include "GameObject.h"
  4.  
  5. #include <map>
  6.  
  7. class Game_AI_AaronApp;
  8.  
  9. class BKeyboardControlled;
  10. class BSeek;
  11. class BFollowPath;
  12. class BWander;
  13. class BCollisionAvoidance;
  14.  
  15. class Path;
  16. class Pathfinder;
  17.  
  18. namespace aie {
  19. class Font;
  20. class Texture;
  21. }
  22.  
  23.  
  24. class Player : public GameObject {
  25. public:
  26. Player(aie::Texture *tex = nullptr, Game_AI_AaronApp *app = nullptr);
  27. virtual ~Player();
  28.  
  29. virtual void update(float deltaTime);
  30. virtual void render(aie::Renderer2D *renderer);
  31.  
  32. void setMousePos(const glm::vec2 &mousePos);
  33.  
  34. protected:
  35. std::shared_ptr<BKeyboardControlled> m_keyboardBehaviour;
  36. std::shared_ptr<BSeek> m_seekBehaviour, m_fleeBehaviour;
  37. std::shared_ptr<BFollowPath> m_followPathBehaviour;
  38. std::shared_ptr<BWander> m_wanderBehavour;
  39. std::shared_ptr<BCollisionAvoidance> m_collisionAvoidance;
  40.  
  41. std::unique_ptr<aie::Font> m_font;
  42.  
  43. glm::vec2 m_mousePos;
  44.  
  45. private:
  46. const char *m_algorithm;
  47.  
  48. };
  49.  
  50.  
  51. //====================================================================================================
  52.  
  53. #include "Entities\Player.h"
  54. #include "Game_AI_AaronApp.h"
  55.  
  56. #include "Behaviours\BehaviourController.h"
  57. #include "Behaviours\BKeyboardControlled.h"
  58. #include "Behaviours\BSeek.h"
  59. #include "Behaviours\BFollowPath.h"
  60. #include "Behaviours\BWander.h"
  61. #include "Behaviours\BCollisionAvoidance.h"
  62.  
  63. #include "Entities\Enemies\Enemy.h"
  64.  
  65. #include "Graph\Path.h"
  66. #include "Pathfinding\Pathfinder.h"
  67.  
  68. #include "GlobalConfig.h"
  69. #include "ResourceManager.h"
  70.  
  71. #include <Font.h>
  72. #include <Input.h>
  73. #include <Renderer2D.h>
  74.  
  75. #include <imgui.h>
  76. #include <jm_utilities.h>
  77.  
  78. #include <iostream>
  79.  
  80. Player::Player(aie::Texture *tex, Game_AI_AaronApp *app) : GameObject(tex, app) {
  81. setFriction(1);
  82.  
  83.  
  84. m_keyboardBehaviour = std::shared_ptr<BKeyboardControlled>(new BKeyboardControlled());
  85. m_keyboardBehaviour->setParent(this);
  86. m_keyboardBehaviour->setStrength(PLAYER_MOVEMENT_SPEED);
  87.  
  88. m_seekBehaviour = std::shared_ptr<BSeek>(new BSeek());
  89. m_seekBehaviour->setParent(this);
  90. m_seekBehaviour->setStrength(PLAYER_MOVEMENT_SPEED);
  91. m_seekBehaviour->setInnerRadius(20);
  92. m_seekBehaviour->setOuterRadius(200);
  93. m_seekBehaviour->onInnerRadiusEnter([this]() {
  94. m_behaviourController->removeBehaviour(m_seekBehaviour);
  95. });
  96.  
  97. m_path = std::unique_ptr<Path>(new Path());
  98. m_followPathBehaviour = std::shared_ptr<BFollowPath>(new BFollowPath());
  99. m_followPathBehaviour->setParent(this);
  100. m_followPathBehaviour->setPath(m_path.get());
  101. m_followPathBehaviour->setStrength(PLAYER_MOVEMENT_SPEED);
  102. m_followPathBehaviour->setNodeRadius(40);
  103. m_followPathBehaviour->onLastNodeReached([this]() {
  104. if (m_followPathBehaviour->isPatrolling())
  105. m_followPathBehaviour->setPatrolDir(m_followPathBehaviour->getPatrolDir() * -1);
  106. else
  107. m_behaviourController->removeBehaviour(m_followPathBehaviour);
  108. });
  109.  
  110. m_wanderBehavour = std::shared_ptr<BWander>(new BWander());
  111. m_wanderBehavour->setParent(this);
  112. m_wanderBehavour->setStrength(1);
  113. m_wanderBehavour->setProjectionDistance(50);
  114. m_wanderBehavour->setRadius(100);
  115. m_wanderBehavour->setPriorityWeight(0.5f);
  116.  
  117. m_fleeBehaviour = std::shared_ptr<BSeek>(new BSeek());
  118. m_fleeBehaviour->setParent(this);
  119. m_fleeBehaviour->setStrength(-PLAYER_MOVEMENT_SPEED);
  120. m_fleeBehaviour->setInnerRadius(150);
  121. m_fleeBehaviour->setOuterRadius(400);
  122. m_fleeBehaviour->onOuterRadiusExit([this]() {
  123. m_behaviourController->removeBehaviour(m_fleeBehaviour);
  124. m_behaviourController->addBehaviour(m_wanderBehaviour);
  125. });
  126.  
  127. m_collisionAvoidance = std::shared_ptr<BCollisionAvoidance>(new BCollisionAvoidance());
  128. m_collisionAvoidance->setParent(this);
  129. m_collisionAvoidance->setVisionDistance(200);
  130. m_collisionAvoidance->setFOV(jm::degToRad(20.f));
  131. m_collisionAvoidance->setStrength(PLAYER_MOVEMENT_SPEED);
  132. m_collisionAvoidance->setCollidableObjects(m_app->getColliders());
  133.  
  134. //m_behaviourController->addBehaviour(m_collisionAvoidance);
  135. }
  136.  
  137. Player::~Player() {
  138. }
  139.  
  140. void Player::update(float deltaTime) {
  141.  
  142. #pragma region ImGui
  143. // Live debugging window
  144. ImGui::Begin("Debugging");
  145.  
  146. if (ImGui::CollapsingHeader("AI")) {
  147. static bool patrolFlag = true;
  148. if (ImGui::Checkbox("Patrolling", &patrolFlag))
  149. m_followPathBehaviour->isPatrolling(patrolFlag);
  150.  
  151. static bool wanderFlag = false;
  152. if (ImGui::Checkbox("Wander", &wanderFlag))
  153. m_behaviourController->addBehaviour(m_wanderBehavour);
  154. if (!wanderFlag)
  155. m_behaviourController->removeBehaviour(m_wanderBehavour);
  156.  
  157.  
  158. // Add a tree
  159. const char* pathfindingAlgorithms[] = { "dijkstra", "astar" };
  160. static int itemIndex = -1;
  161. ImGui::Combo("Combo", &itemIndex, pathfindingAlgorithms, IM_ARRAYSIZE(pathfindingAlgorithms));
  162. m_algorithm = pathfindingAlgorithms[itemIndex];
  163. }
  164.  
  165. ImGui::End();
  166. #pragma endregion
  167.  
  168. GameObject::update(deltaTime);
  169.  
  170. aie::Input *input = aie::Input::getInstance();
  171.  
  172. if (input->isKeyDown(aie::INPUT_KEY_LEFT_CONTROL)) {
  173. if (input->wasMouseButtonPressed(aie::INPUT_MOUSE_BUTTON_LEFT)) {
  174. std::shared_ptr<BSeek> seek = std::shared_ptr<BSeek>(new BSeek());
  175. seek->setParent(this);
  176. seek->setStrength(PLAYER_MOVEMENT_SPEED);
  177. seek->setInnerRadius(20);
  178. seek->setOuterRadius(200);
  179. seek->setPriorityWeight(2.f);
  180. seek->onInnerRadiusEnter([this, seek]() {
  181. m_behaviourController->removeBehaviour(seek);
  182. });
  183. seek->setTarget(m_mousePos);
  184. m_behaviourController->addBehaviour(seek);
  185. } else if (input->wasMouseButtonPressed(aie::INPUT_MOUSE_BUTTON_RIGHT)) {
  186. m_fleeBehaviour->setTarget(m_mousePos);
  187. m_behaviourController->addBehaviour(m_fleeBehaviour);
  188. } else if (input->wasMouseButtonPressed(aie::INPUT_MOUSE_BUTTON_MIDDLE)) {
  189. m_behaviourController->addBehaviour(m_followPathBehaviour);
  190.  
  191. m_path->addPathSegment(m_mousePos);
  192. } else if (input->wasKeyPressed(aie::INPUT_KEY_R))
  193. m_path->clear();
  194. }
  195.  
  196. if (input->isKeyDown(aie::INPUT_KEY_LEFT_SHIFT)) {
  197. if (input->wasMouseButtonPressed(aie::INPUT_MOUSE_BUTTON_LEFT)) {
  198. std::vector<Graph2D::Node*> nearbyNodes;
  199. m_graph->getNearbyNodes(m_mousePos, 15, nearbyNodes);
  200.  
  201. if (!nearbyNodes.empty()) {
  202. m_startNode = nearbyNodes[0];
  203. }
  204. }
  205.  
  206. if (input->wasMouseButtonPressed(aie::INPUT_MOUSE_BUTTON_RIGHT)) {
  207. std::vector<Graph2D::Node*> nearbyNodes;
  208. m_graph->getNearbyNodes(m_mousePos, 15, nearbyNodes);
  209.  
  210. if (!nearbyNodes.empty()) {
  211. m_endNode = nearbyNodes[0];
  212. m_pathfinder = std::move(std::unique_ptr<Pathfinder>(new Pathfinder()));
  213.  
  214. if (m_algorithm == "dijkstra") {
  215. m_pathfinder->findPath(m_startNode, [this](Graph2D::Node *n) {
  216. return n == m_endNode;
  217. });
  218. } else if (m_algorithm == "astar") {
  219. m_pathfinder->findPath(m_startNode, m_endNode, [this]() { return glm::length(m_startNode->data - m_endNode->data); });
  220. }
  221.  
  222. while (!m_pathfinder->pathFound()) {
  223. m_pathfinder->updateSearch();
  224. }
  225.  
  226. m_path->clear();
  227. Path &p = m_pathfinder->getPath();
  228. auto pathPoints = p.getPath();
  229. for (auto iter = pathPoints.rbegin(); iter != pathPoints.rend(); iter++) {
  230. m_path->addPathSegment((*iter));
  231. }
  232.  
  233. m_behaviourController->addBehaviour(m_followPathBehaviour);
  234. }
  235. }
  236.  
  237. if (input->wasMouseButtonPressed(aie::INPUT_MOUSE_BUTTON_MIDDLE)) {
  238. m_startNode = m_endNode = nullptr;
  239. m_pathfinder = std::move(std::unique_ptr<Pathfinder>());
  240. }
  241. }
  242.  
  243. for (auto iter = m_app->getEnemies().begin(); iter != m_app->getEnemies().end(); iter++) {
  244. auto enemy = (*iter);
  245. float dist = glm::length(enemy->getPos() - m_pos);
  246.  
  247. if (dist < m_fleeBehaviour->getInnerRadius()) {
  248. m_fleeBehaviour->setTarget(enemy->getPos());
  249. m_fleeBehaviour->setPriorityWeight((1 / dist - 0.5) * 2);
  250. m_behaviourController->addBehaviour(m_fleeBehaviour);
  251. }
  252. }
  253. }
  254.  
  255. void Player::render(aie::Renderer2D * renderer) {
  256. GameObject::render(renderer);
  257. }
  258.  
  259. void Player::setMousePos(const glm::vec2 &mousePos) {
  260. m_mousePos = mousePos;
  261. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement