Commit 62113c7c9ae4e6913fe37b036c6ed9924130e4e9

Authored by Olav Kjartan Larseng
1 parent 627e8549

implemented a get object of type method and a timer for spaceship fire. Added a …

…method which can take a collection of collision object
SpaceShooterV4/Bullet.cpp
... ... @@ -73,7 +73,11 @@ void Bullet::collide()
73 73 it != _collisionObjects.end();)
74 74 {
75 75 const auto collObj = (*it);
76   - if (Collision::detectCollisionSphereVsSphere(glm::vec3(getMatrix()[3]), glm::vec3(collObj->getMatrix()[3])))
  76 + if (collObj->markedForDeletion())
  77 + {
  78 + it = _collisionObjects.erase(it);
  79 + }
  80 + else if (Collision::detectCollisionSphereVsSphere(glm::vec3(getMatrix()[3]), glm::vec3(collObj->getMatrix()[3])))
77 81 {
78 82 std::shared_ptr<Enemy> enemy = std::dynamic_pointer_cast<Enemy>(collObj);
79 83 if (enemy)
... ... @@ -84,9 +88,11 @@ void Bullet::collide()
84 88 _markDelete = true;
85 89 break;
86 90 }
87   - else {
  91 + else
  92 + {
88 93 it++;
89 94 }
  95 +
90 96 }
91 97 }
92 98 }
... ... @@ -96,12 +102,12 @@ bool Bullet::markedForDeletion()
96 102 return _markDelete;
97 103 }
98 104  
99   -void Bullet::addCollisionWith(std::shared_ptr<SceneObject> obj)
  105 +void Bullet::addCollisionWith(const std::shared_ptr<SceneObject>& obj)
100 106 {
101 107 _collisionObjects.push_back(obj);
102 108 }
103 109  
104   -void Bullet::addCollisionWith(std::vector<std::shared_ptr<SceneObject>> cObj)
  110 +void Bullet::addCollisionWith(const std::vector<std::shared_ptr<SceneObject>>& cObj)
105 111 {
106 112 for (auto i : cObj)
107 113 {
... ...
SpaceShooterV4/Bullet.h
... ... @@ -30,8 +30,8 @@ class Bullet : public SceneObject
30 30 bool markedForDeletion();
31 31  
32 32 public:
33   - void addCollisionWith(std::shared_ptr<SceneObject> obj);
34   - void addCollisionWith(std::vector<std::shared_ptr<SceneObject>> cObj);
  33 + void addCollisionWith(const std::shared_ptr<SceneObject>& obj);
  34 + void addCollisionWith(const std::vector<std::shared_ptr<SceneObject>>& cObj);
35 35 float _bulletPower = 1.0f;
36 36  
37 37 };
... ...
SpaceShooterV4/Enemy.cpp
... ... @@ -16,14 +16,24 @@ Enemy::Enemy(const Trajectory&amp; t)
16 16 _trajectory = t;
17 17 }
18 18  
  19 +Enemy::Enemy(const glm::mat4 & startMatrix, const Trajectory & t)
  20 +{
  21 + setMatrix(startMatrix);
  22 + _trajectory = t;
  23 +}
  24 +
  25 +Enemy::Enemy(const glm::vec3 & translate, const Trajectory & t)
  26 +{
  27 + matrix_ = glm::translate(glm::mat4(), translate);
  28 + _trajectory = t;
  29 +}
  30 +
19 31 Enemy::~Enemy()
20 32 {
21 33 }
22 34  
23 35 void Enemy::privateInit()
24 36 {
25   - matrix_ = glm::translate(glm::mat4(), glm::vec3(0.0f, 0.0f, -400.0f));
26   -
27 37 _texture = SOIL_load_OGL_texture
28 38 (
29 39 "assets/textures/tex_brick.jpg",
... ... @@ -84,7 +94,7 @@ void Enemy::privateUpdate()
84 94 matrix_ = glm::translate(matrix_, glm::vec3(
85 95 0.125 * glm::sin(2 * M_PI * _period * _deltaTime + _phase),
86 96 0.0f,
87   - 0.006125f));
  97 + _speed * _deltaTime));
88 98 }
89 99  
90 100 }
... ...
SpaceShooterV4/Enemy.h
... ... @@ -20,6 +20,8 @@ class Enemy : public SceneObject
20 20 public:
21 21 Enemy();
22 22 explicit Enemy(const Trajectory& t);
  23 + explicit Enemy(const glm::mat4& startMatrix, const Trajectory& t);
  24 + explicit Enemy(const glm::vec3& translate, const Trajectory& t);
23 25 ~Enemy();
24 26  
25 27 void privateInit();
... ... @@ -28,9 +30,10 @@ class Enemy : public SceneObject
28 30 bool markedForDeletion();
29 31  
30 32 void hitLife(float hitPoint);
  33 + float _speed = 2.0f;
31 34  
32 35 private:
33   - float _speed = 2.0f;
  36 +
34 37 float _armor;
35 38  
36 39 bool _markDelete = false;
... ...
SpaceShooterV4/include/FpsCounter.hpp
... ... @@ -18,8 +18,8 @@
18 18 * AND THEE SHALT NOT CALLETH IT ANYWH'RE ELSE!!!
19 19 *
20 20 * @author <Daniel Aaron Salwerowicz>
21   - * @co-author <Olav Kjartan Larseng> - added delta time
22   - * Copyright Kobol Dev 2018 & Larseng IT
  21 + *
  22 + * Copyright Kobol Dev 2018
23 23 *
24 24 */
25 25 namespace siut {
... ...
SpaceShooterV4/include/GameManager.hpp
... ... @@ -11,6 +11,8 @@
11 11 #include "Spotlight.h"
12 12 #include "Bullet.h"
13 13  
  14 +using namespace std::chrono;
  15 +
14 16 class GameManager : public SceneObject
15 17 {
16 18 public:
... ... @@ -21,6 +23,10 @@ class GameManager : public SceneObject
21 23 std::shared_ptr<SpaceShip> getSpaceShip();
22 24  
23 25 void fireFromSpaceship();
  26 +
  27 +
  28 + using Rolex = high_resolution_clock;
  29 + using TimePoint = time_point<Rolex>;
24 30  
25 31  
26 32 protected:
... ... @@ -39,8 +45,9 @@ class GameManager : public SceneObject
39 45  
40 46 std::shared_ptr<Bullet> _bullet;
41 47  
  48 + TimePoint _lastFire;
  49 + bool fireFlag = false;
  50 +
42 51 bool _markDelete = false;
43   -
44   -
45 52 };
46 53  
... ...
SpaceShooterV4/include/SceneObject.hpp
... ... @@ -54,6 +54,13 @@ class SceneObject
54 54 void setMatrix(const glm::mat4& m) { matrix_ = m; }
55 55 glm::mat4& getMatrix() { return matrix_; }
56 56  
  57 + // Get all objects of a certain type
  58 + template <typename T>
  59 + std::vector<std::shared_ptr<SceneObject>> getAllObjectOfType();
  60 +
  61 + // Deletion queue for SceneObjects
  62 + virtual bool markedForDeletion() = 0;
  63 +
57 64 protected:
58 65 // Override this method with your own render-implementation.
59 66 virtual void privateRender() {}
... ... @@ -61,8 +68,8 @@ class SceneObject
61 68 virtual void privateUpdate() {}
62 69 // Override this method with your own init-implementation.
63 70 virtual void privateInit() {}
64   - // Deletion queue for SceneObjects
65   - virtual bool markedForDeletion() = 0;
  71 +
  72 +
66 73  
67 74 // This member contains the time since last frame. It is set
68 75 // before privateUpdate is called.
... ... @@ -78,3 +85,17 @@ class SceneObject
78 85 std::vector<std::shared_ptr<SceneObject> > children_;
79 86  
80 87 };
  88 +
  89 +template<typename T>
  90 +inline std::vector<std::shared_ptr<SceneObject>> SceneObject::getAllObjectOfType()
  91 +{
  92 + std::vector<std::shared_ptr<SceneObject>> objects;
  93 + for (auto i : children_)
  94 + {
  95 + if (std::dynamic_pointer_cast<T>(i))
  96 + {
  97 + objects.push_back(i);
  98 + }
  99 + }
  100 + return objects;
  101 +}
... ...
SpaceShooterV4/src/GameManager.cpp
1 1 #include "../include/GameManager.hpp"
  2 +#include <chrono>
  3 +
2 4  
3 5 GameManager::GameManager()
4 6 {
... ... @@ -28,8 +30,12 @@ void GameManager::privateInit()
28 30 spaceship_.reset(new SpaceShip());
29 31 this->addSubObject(spaceship_);
30 32  
31   - _enemy.reset(new Enemy(Enemy::Trajectory::SINUS));
32   - this->addSubObject(_enemy);
  33 + for (int i = 0; i < 5; i++)
  34 + {
  35 + _enemy.reset(new Enemy(glm::vec3{- 100 + (50 * (i + 1)), 0, -1500}, Enemy::Trajectory::SINUS));
  36 + _enemy->_speed = 25.0f;
  37 + this->addSubObject(_enemy);
  38 + }
33 39  
34 40 //_spotlight.reset(new Spotlight(glm::vec3{0,0,200}, 60, 80));
35 41 //this->addSubObject(_spotlight);
... ... @@ -45,6 +51,7 @@ void GameManager::privateUpdate()
45 51 // Instead of adding alle objects in the scene as subobject to the camera they are added as subobjects
46 52 // to the game manager. Therefore, we set the game manager matrix equal to the camera matrix.
47 53 this->matrix_ = cam_->getMatrix();
  54 +
48 55 }
49 56  
50 57 bool GameManager::markedForDeletion()
... ... @@ -52,7 +59,6 @@ bool GameManager::markedForDeletion()
52 59 return _markDelete;
53 60 }
54 61  
55   -
56 62 std::shared_ptr<Camera> GameManager::getCam()
57 63 {
58 64 return cam_;
... ... @@ -65,9 +71,19 @@ std::shared_ptr&lt;SpaceShip&gt; GameManager::getSpaceShip()
65 71  
66 72 void GameManager::fireFromSpaceship()
67 73 {
68   - _bullet.reset(new Bullet(spaceship_->getMatrix(), Bullet::Trajectory::FORWARD, 350.00f));
69   - _bullet->addCollisionWith(_enemy);
70   - this->addSubObject(_bullet);
  74 + if (!fireFlag)
  75 + {
  76 + fireFlag = true;
  77 + _lastFire = Rolex::now();
  78 + _bullet.reset(new Bullet(spaceship_->getMatrix(), Bullet::Trajectory::FORWARD, 350.00f));
  79 + _bullet->addCollisionWith(this->getAllObjectOfType<Enemy>());
  80 + this->addSubObject(_bullet);
  81 + }
  82 + else if (duration_cast<milliseconds>(Rolex::now() - _lastFire).count() > 200)
  83 + {
  84 + fireFlag = false;
  85 + }
  86 +
71 87 }
72 88  
73 89  
... ...
SpaceShooterV4/src/SceneObject.cpp
... ... @@ -69,4 +69,6 @@ void SceneObject::removeSubObject(const std::shared_ptr&lt;SceneObject&gt; child)
69 69 children_.erase(it);
70 70 break;
71 71 }
72   -}
73 72 \ No newline at end of file
  73 +}
  74 +
  75 +
... ...
SpaceShooterV4/src/SpaceShip.cpp
... ... @@ -96,18 +96,6 @@ void SpaceShip::moveLeft()
96 96  
97 97 void SpaceShip::fire()
98 98 {
99   - if (!_fireFlag) {
100   - _fireFlag = true;
101   - _startTime = clock();
102   -
103   - }
104   - else {
105   - auto secondsPassed = (clock() - _startTime) / CLOCKS_PER_SEC;
106   - if (secondsPassed >= 0.35)
107   - {
108   - _fireFlag = false;
109   - }
110   - }
111 99  
112 100 }
113 101  
... ...
SpaceShooterV4/x64/Debug/BattleField.obj
No preview for this file type
SpaceShooterV4/x64/Debug/Bullet.obj
No preview for this file type
SpaceShooterV4/x64/Debug/Camera.obj
No preview for this file type
SpaceShooterV4/x64/Debug/Collision.obj
No preview for this file type
SpaceShooterV4/x64/Debug/Enemy.obj
No preview for this file type
SpaceShooterV4/x64/Debug/GameManager.obj
No preview for this file type
SpaceShooterV4/x64/Debug/SceneObject.obj
No preview for this file type
SpaceShooterV4/x64/Debug/SpaceShip.obj
No preview for this file type
SpaceShooterV4/x64/Debug/SpaceShooterV4.log
1 1  Enemy.cpp
2   -d:\csmaster\opengl projects\projects\spaceshooterv4\spaceshooterv4\spaceshooterv4\enemy.h(38): warning C4305: 'initializing': truncation from 'double' to 'float'
  2 +d:\csmaster\opengl projects\projects\spaceshooterv4\spaceshooterv4\spaceshooterv4\enemy.h(41): warning C4305: 'initializing': truncation from 'double' to 'float'
  3 + Generating Code...
  4 + Compiling...
  5 + main.cpp
  6 +d:\csmaster\opengl projects\projects\spaceshooterv4\spaceshooterv4\spaceshooterv4\include\fpscounter.hpp(60): warning C4244: 'return': conversion from '__int64' to 'double', possible loss of data
  7 +d:\csmaster\opengl projects\projects\spaceshooterv4\spaceshooterv4\spaceshooterv4\enemy.h(41): warning C4305: 'initializing': truncation from 'double' to 'float'
  8 + Generating Code...
  9 + Compiling...
  10 + Camera.cpp
  11 +d:\csmaster\opengl projects\projects\spaceshooterv4\spaceshooterv4\spaceshooterv4\include\fpscounter.hpp(60): warning C4244: 'return': conversion from '__int64' to 'double', possible loss of data
  12 + Generating Code...
3 13 SOIL.lib(SOIL.obj) : MSIL .netmodule or module compiled with /GL found; restarting link with /LTCG; add /LTCG to the link command line to improve linker performance
4 14 LINK : warning LNK4075: ignoring '/INCREMENTAL' due to '/LTCG' specification
5 15 LINK : warning LNK4098: defaultlib 'MSVCRT' conflicts with use of other libs; use /NODEFAULTLIB:library
... ...
SpaceShooterV4/x64/Debug/SpaceShooterV4.tlog/CL.read.1.tlog
1 1 Binary files a/SpaceShooterV4/x64/Debug/SpaceShooterV4.tlog/CL.read.1.tlog and b/SpaceShooterV4/x64/Debug/SpaceShooterV4.tlog/CL.read.1.tlog differ
... ...
SpaceShooterV4/x64/Debug/SpaceShooterV4.tlog/CL.write.1.tlog
1 1 Binary files a/SpaceShooterV4/x64/Debug/SpaceShooterV4.tlog/CL.write.1.tlog and b/SpaceShooterV4/x64/Debug/SpaceShooterV4.tlog/CL.write.1.tlog differ
... ...
SpaceShooterV4/x64/Debug/SpaceShooterV4.tlog/SpaceShooterV4.write.1u.tlog
1 1 Binary files a/SpaceShooterV4/x64/Debug/SpaceShooterV4.tlog/SpaceShooterV4.write.1u.tlog and b/SpaceShooterV4/x64/Debug/SpaceShooterV4.tlog/SpaceShooterV4.write.1u.tlog differ
... ...
SpaceShooterV4/x64/Debug/Spotlight.obj
No preview for this file type
SpaceShooterV4/x64/Debug/main.obj
No preview for this file type
SpaceShooterV4/x64/Debug/vc141.idb
No preview for this file type
SpaceShooterV4/x64/Debug/vc141.pdb
No preview for this file type
x64/Debug/SpaceShooterV4.exe
No preview for this file type
x64/Debug/SpaceShooterV4.pdb
No preview for this file type