Advertisement
Guest User

Untitled

a guest
Jul 21st, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.20 KB | None | 0 0
  1. #ifndef SAMPLE_SCENE_1
  2. #define SAMPLE_SCENE_1
  3.  
  4. #include "Scene.h"
  5. #include "GameObject.h"
  6. int bossHP= 5;
  7. bool playerHit = false;
  8. class SampleScene1 : public Scene
  9. {
  10. public:
  11. void onPollEvent(const Event& event)
  12. {
  13. if (event.type == sf::Event::KeyReleased)
  14. if (event.key.code == Keyboard::B)
  15. manager->changeScene("HelloWorld");
  16. }
  17. void start()
  18. {
  19.  
  20. //Red circle
  21. float scalar = 100.f;
  22. Vector2f rightVector(1, 0);
  23.  
  24. currentIndex = 1;
  25.  
  26. //Control circle
  27. Vector2f midPosition(100, 300);
  28. Vector2f bossmidPosition(410, 50);
  29.  
  30. boss = addSquare(Color::Green, 75.f);
  31. walls.push_back(boss);
  32. boss->bossSetPos(bossmidPosition - (rightVector) * 50.f);
  33.  
  34. player = addSquare(Color::White, 25.f);
  35. walls.push_back(boss);
  36. player->setPosition(midPosition - (rightVector) * 50.f);
  37. }
  38.  
  39. void update(float deltaTime)
  40. {
  41. const int INPUT_SIZE = 4;
  42. //Playerdirection
  43. Keyboard::Key directionKeys[INPUT_SIZE]{
  44. Keyboard::A, Keyboard::D,
  45. Keyboard::W, Keyboard::S
  46. };
  47.  
  48. Vector2f direction[INPUT_SIZE]{
  49. Vector2f(-3,0), Vector2f(3,0),
  50. Vector2f(0,-3), Vector2f(0,3)
  51. };
  52. //Bossdirection
  53. Keyboard::Key bossdirectionKeys[INPUT_SIZE]{
  54. Keyboard::Left, Keyboard::Right
  55. //Keyboard::Up, Keyboard::Down
  56. };
  57.  
  58. Vector2f bossDirection[INPUT_SIZE]{
  59. Vector2f(-3,0), Vector2f(3,0),
  60. Vector2f(0,-3), Vector2f(0,3)
  61. };
  62. Vector2f fishDir[INPUT_SIZE]{
  63. Vector2f(-1,0), Vector2f(1,0),
  64. Vector2f(0,-1), Vector2f(0,1)
  65. };
  66.  
  67. const Vector2f& currentPosition = player->getPosition();
  68. const Vector2f& bosscurrentPosition = boss->getPosition();
  69. Vector2f offset;
  70. //Player Movement
  71. for (int i = 0; i < INPUT_SIZE; i++)
  72. {
  73. if (Keyboard::isKeyPressed(directionKeys[i]))
  74. {
  75. currentIndex = i;
  76. offset = direction[currentIndex];
  77. //How can we speed up the movement?
  78. //Change the vector 2f things on the direction for speed, the higher the better
  79. player->setPosition(currentPosition + offset);
  80. }
  81. }
  82. //Boss movement
  83. for (int i = 0; i < INPUT_SIZE; i++)
  84. {
  85. if (Keyboard::isKeyPressed(bossdirectionKeys[i]))
  86. {
  87. currentIndex = i;
  88. offset = bossDirection[currentIndex];
  89. //How can we speed up the movement?
  90. //Change the vector 2f things on the direction for speed, the higher the better
  91. boss->setPosition(bosscurrentPosition + offset);
  92. }
  93. }
  94. //Spawn Fish
  95. //if (fishSpawnTime.getElapsedTime().asSeconds() > 3)
  96. //{
  97. // int random = rand() % 2;
  98. // Vector2f fishDirection = fishDir[random];
  99. // GameObject* fish = addSquare(Color::Blue, 40.f);
  100.  
  101. // float y = 200 + (10.f * (rand() % 30));
  102. // float x = 0;
  103. // if (fishDirection.x < 0)
  104. // x = window->getSize().x;
  105.  
  106. // Projectile* d = new Projectile(10, fish);
  107. // fish->getBody()->setVelocity(fishDirection);
  108. // fish->setPosition(Vector2f(x, y));
  109.  
  110. // projectiles.push_back(d);
  111. // fishSpawnTime.restart();
  112. //}
  113. //bullet making
  114. float time = shootTime.getElapsedTime().asSeconds();
  115. if (time > 1 && Keyboard::isKeyPressed(Keyboard::Space))
  116. {
  117. GameObject* bullet = addSquare(Color::White, 10);
  118. Projectile* d = new Projectile(5, bullet);
  119. bullet->getBody()->setVelocity(Vector2f(0, -5));
  120. bullet->setPosition(player->getPosition());
  121. projectiles.push_back(d);
  122. shootTime.restart();
  123. }
  124. //float time = shootTime.getElapsedTime().asSeconds();
  125. if (time > 1 && Keyboard::isKeyPressed(Keyboard::BackSpace))
  126. {
  127. GameObject* bossBullet = addSquare(Color::Green, 20);
  128. bossProjectile* b = new bossProjectile(5, bossBullet);
  129. bossBullet->getBody()->setVelocity(Vector2f(0, 5));
  130. bossBullet->bossSetPos(boss->getPosition());
  131. // + Vector2f(-30, 20))
  132. bossProj.push_back(b);
  133. shootTime.restart();
  134. }
  135. //Check for any collision with the walls
  136. BoxCollider* playerCollider = player->getCollider();
  137. BoxCollider* bossCollider = boss->getCollider();
  138. bool hasCollided = isCollidingWithWall(playerCollider);
  139.  
  140. //Return the player to it's previous position before the offset took place
  141. if (hasCollided)
  142. player->setPosition(currentPosition - offset);
  143.  
  144.  
  145. for (int i = 0; i < gameobjects.size(); i++)
  146. {
  147. GameObject* go = gameobjects[i];
  148. if (!go->getIsActive())
  149. continue;
  150. go->update();
  151. }
  152.  
  153. for (int i = projectiles.size() - 1; i >= 0; i--)
  154. {
  155. Projectile* bullet = projectiles[i];
  156. bullet->countdown -= deltaTime;
  157. BoxCollider* bulletCollider = bullet->gameObject->getCollider();
  158. if (bullet->countdown <= 0 || isCollidingWithWall(bulletCollider))
  159. {
  160.  
  161. bossHP--;
  162. cout << bossHP << endl;
  163.  
  164. removeGameobject(bullet->gameObject);
  165. projectiles.erase(projectiles.begin() + i);
  166.  
  167. }
  168. }
  169.  
  170. for (int i = bossProj.size() - 1; i >= 0; i--)
  171. {
  172. bossProjectile* bossBullet = bossProj[i];
  173. bossBullet->countdown -= deltaTime;
  174. BoxCollider* bossBulletCollider = bossBullet->gameObject->getCollider();
  175. if (bossBullet->countdown <= 0 || isCollidingWithWallStuff(bossBulletCollider))
  176. {
  177. playerHit = true;
  178.  
  179. removeGameobject(bossBullet->gameObject);
  180. bossProj.erase(bossProj.begin() + i);
  181. }
  182. }
  183. if (playerHit)
  184. {
  185. Vector2f youregone(-500, 0);
  186. player->setPosition(currentPosition + youregone);
  187. cout << "OVER" << endl;
  188. //removeGameobject(player);
  189. }
  190. else if (bossHP <= 0)
  191. {
  192. cout << "Boss dead" << endl;
  193. //removeGameobject(boss);
  194. }
  195. }
  196.  
  197. void end()
  198. {
  199. deleteAllShapes();
  200. projectiles.clear();
  201. walls.clear();
  202. }
  203.  
  204. void render()
  205. {
  206. for (int i = 0; i < gameobjects.size(); i++)
  207. {
  208. GameObject* go = gameobjects[i];
  209. if (!go->getIsActive())
  210. continue;
  211. go->render(*window);
  212. }
  213. }
  214.  
  215. std::string getName()
  216. {
  217. return "SampleScene";
  218. }
  219.  
  220. private:
  221. Clock shootTime;
  222. Clock fishSpawnTime;
  223.  
  224. int currentIndex = 1;
  225. GameObject* player;
  226. GameObject* boss;
  227.  
  228. vector<GameObject*> walls;
  229. vector<GameObject*> gameobjects;
  230. vector<Projectile*> projectiles;
  231. vector<bossProjectile*>bossProj;
  232.  
  233. GameObject* addSquare(const Color& color, float size = 100)
  234. {
  235. GameObject* go = new GameObject();
  236. go->initAsSquare(color, size);
  237. gameobjects.push_back(go);
  238. return go;
  239. }
  240.  
  241. void removeGameobject(GameObject* go)
  242. {
  243. for (int i = gameobjects.size() - 1; i >= 0; i--)
  244. if (go == gameobjects[i])
  245. gameobjects.erase(gameobjects.begin() + i);
  246. delete go;
  247. }
  248.  
  249. void deleteAllShapes()
  250. {
  251. for (int i = 0; i < gameobjects.size(); i++)
  252. delete gameobjects[i];
  253. gameobjects.clear();
  254. }
  255.  
  256. bool isCollidingWithGroup(BoxCollider* collider, const vector<GameObject*>& objs)
  257. {
  258. for (int i = 0; i < objs.size(); i++)
  259. {
  260. GameObject* target = objs[i];
  261. BoxCollider* other = target->getCollider();
  262. if (collider->isColliding(other))
  263. return true;
  264. }
  265. return false;
  266. }
  267.  
  268. bool isCollidingStuff(BoxCollider* collider, GameObject*& objs)
  269. {
  270. GameObject* target = objs;
  271. BoxCollider* other = target->getCollider();
  272. if (collider->isColliding(other))
  273. return true;
  274. return false;
  275. }
  276.  
  277. bool isCollidingWithWall(BoxCollider* collider)
  278. {
  279.  
  280. return isCollidingWithGroup(collider, walls);
  281. }
  282.  
  283. bool isCollidingWithWallStuff(BoxCollider* collider)
  284. {
  285.  
  286. return isCollidingStuff(collider, player);
  287. }
  288. };
  289. #endif // !SAMPLE_SCENE_1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement