Commit 6863cc32f0d07ae434a2982399b359c37b4b3aa9

Authored by Olav Kjartan Larseng
1 parent 62113c7c

changed is locked to spaceship, different weapons and spaceship area restriction

SpaceShooterV4/Enemy.h
... ... @@ -32,8 +32,8 @@ class Enemy : public SceneObject
32 32 void hitLife(float hitPoint);
33 33 float _speed = 2.0f;
34 34  
  35 +
35 36 private:
36   -
37 37 float _armor;
38 38  
39 39 bool _markDelete = false;
... ...
SpaceShooterV4/app/main.cpp
... ... @@ -60,19 +60,21 @@ void display()
60 60 gm->update(counter.fps());
61 61 gm->render();
62 62  
63   - if(keyPressed[KEY_ID_W]==true) gm->getCam()->moveForward();
64   - if(keyPressed[KEY_ID_A]==true) gm->getCam()->moveLeft();
65   - if(keyPressed[KEY_ID_D]==true) gm->getCam()->moveRight();
66   - if(keyPressed[KEY_ID_S]==true) gm->getCam()->moveBackward();
67   - if(keyPressed[KEY_ID_SPACE]==true) gm->getCam()->moveUp();
68   - if(keyPressed[KEY_ID_C]==true) gm->getCam()->moveDown();
  63 + //if(keyPressed[KEY_ID_W]==true) gm->getCam()->moveForward();
  64 + if (keyPressed[KEY_ID_W] == true) gm->getSpaceShip()->moveForward();
  65 + if (keyPressed[KEY_ID_A] == true) gm->getCam()->moveLeft(gm->getSpaceShip()->moveLeft());
  66 + if (keyPressed[KEY_ID_D] == true) gm->getCam()->moveRight(gm->getSpaceShip()->moveRight());
  67 + if (keyPressed[KEY_ID_S] == true) gm->getSpaceShip()->moveBackward();
  68 + //if(keyPressed[KEY_ID_SPACE]==true) gm->getCam()->moveUp();
  69 + if (keyPressed[KEY_ID_C] == true) gm->getSpaceShip()->changeWeapon();
69 70  
70   - if (keyPressed[KEY_ID_X] == true) gm->fireFromSpaceship();
  71 + //if (keyPressed[KEY_ID_X] == true) gm->fireFromSpaceship();
  72 + if (keyPressed[KEY_ID_SPACE] == true) gm->fireFromSpaceship();
71 73  
72   - if (keyPressed[KEY_ID_LEFTARROW] == true) gm->getSpaceShip()->moveLeft();
73   - if (keyPressed[KEY_ID_RIGHTARROW] == true) gm->getSpaceShip()->moveRight();
74   - if (keyPressed[KEY_ID_UPARROW] == true) gm->getSpaceShip()->moveForward();
75   - if (keyPressed[KEY_ID_DOWNARROW] == true) gm->getSpaceShip()->moveBackward();
  74 + //if (keyPressed[KEY_ID_LEFTARROW] == true) gm->getSpaceShip()->moveLeft();
  75 + //if (keyPressed[KEY_ID_RIGHTARROW] == true) gm->getSpaceShip()->moveRight();
  76 + //if (keyPressed[KEY_ID_UPARROW] == true) gm->getSpaceShip()->moveForward();
  77 + //if (keyPressed[KEY_ID_DOWNARROW] == true) gm->getSpaceShip()->moveBackward();
76 78  
77 79 // Lets try out text
78 80  
... ...
SpaceShooterV4/include/Camera.hpp
... ... @@ -13,12 +13,13 @@ class Camera : public SceneObject
13 13 Camera();
14 14 ~Camera();
15 15  
16   - void moveRight();
17   - void moveLeft();
  16 + void moveRight(const float& amount);
  17 + void moveLeft(const float& amount);
18 18 void moveUp();
19 19 void moveDown();
20 20 void moveBackward();
21 21 void moveForward();
  22 + void move(const float & x, const float & y, const float & z);
22 23  
23 24 protected:
24 25 void privateInit();
... ... @@ -26,6 +27,8 @@ class Camera : public SceneObject
26 27 void privateUpdate();
27 28 bool markedForDeletion();
28 29  
  30 +
  31 +
29 32 private:
30 33 float _speed = 400.0f;
31 34 bool _markDelete = false;
... ...
SpaceShooterV4/include/GameManager.hpp
... ... @@ -11,6 +11,8 @@
11 11 #include "Spotlight.h"
12 12 #include "Bullet.h"
13 13  
  14 +#include <random>
  15 +
14 16 using namespace std::chrono;
15 17  
16 18 class GameManager : public SceneObject
... ... @@ -24,7 +26,8 @@ class GameManager : public SceneObject
24 26  
25 27 void fireFromSpaceship();
26 28  
27   -
  29 + void fireFromEnemy();
  30 +
28 31 using Rolex = high_resolution_clock;
29 32 using TimePoint = time_point<Rolex>;
30 33  
... ... @@ -45,9 +48,16 @@ class GameManager : public SceneObject
45 48  
46 49 std::shared_ptr<Bullet> _bullet;
47 50  
  51 + std::random_device rd;
  52 +
  53 +
48 54 TimePoint _lastFire;
  55 + TimePoint _lastFireEnemy;
49 56 bool fireFlag = false;
  57 + bool fireFlagEnemy = false;
50 58  
51 59 bool _markDelete = false;
  60 + template<typename T>
  61 + int random(const T & from, const T & to);
52 62 };
53 63  
... ...
SpaceShooterV4/include/SpaceShip.hpp
... ... @@ -32,13 +32,23 @@ class SpaceShip : public SceneObject
32 32 double _secondsToDelay = 0.5;
33 33 clock_t _startTime;
34 34  
  35 + int amountOfWeapons = 2;
  36 + int _weapon = 0;
  37 +
  38 +
35 39 public:
36 40 void fire();
37 41  
38   - void moveBackward();
39   - void moveForward();
40   - void moveRight();
41   - void moveLeft();
  42 + float moveBackward();
  43 + float moveForward();
  44 + float moveRight();
  45 + float moveLeft();
  46 +
  47 + void changeWeapon(const int & w);
  48 +
  49 + void changeWeapon();
  50 +
  51 + int getWeapon();
42 52  
43 53 };
44 54  
... ...
SpaceShooterV4/src/Camera.cpp
... ... @@ -8,7 +8,8 @@
8 8  
9 9 Camera::Camera()
10 10 {
11   - matrix_ = glm::translate(glm::mat4(), glm::vec3(0.0f, -30.0f, -100.0f));
  11 + matrix_ = glm::translate(glm::mat4(), glm::vec3(0.0f, -30.0f, -150.0f));
  12 + matrix_ = glm::rotate(matrix_, 0.2f, glm::vec3(1.0f, 0.0f, 0.0f));
12 13 }
13 14  
14 15 Camera::~Camera()
... ... @@ -36,13 +37,18 @@ bool Camera::markedForDeletion()
36 37 return _markDelete;
37 38 }
38 39  
39   -void Camera::moveRight()
  40 +void Camera::move(const float& x, const float& y, const float& z)
40 41 {
41   - matrix_ = glm::translate(matrix_, glm::vec3(-_speed * _deltaTime, 0.0f, 0.0f));
  42 + matrix_ = glm::translate(matrix_, glm::vec3(x * _deltaTime, y * _deltaTime, z * _deltaTime));
42 43 }
43   -void Camera::moveLeft()
  44 +
  45 +void Camera::moveRight(const float& amount)
  46 +{
  47 + matrix_ = glm::translate(matrix_, glm::vec3(amount, 0.0f, 0.0f));
  48 +}
  49 +void Camera::moveLeft(const float& amount)
44 50 {
45   - matrix_ = glm::translate(matrix_, glm::vec3(_speed * _deltaTime, 0.0f, 0.0f));
  51 + matrix_ = glm::translate(matrix_, glm::vec3(amount, 0.0f, 0.0f));
46 52 }
47 53 void Camera::moveUp()
48 54 {
... ...
SpaceShooterV4/src/GameManager.cpp
1 1 #include "../include/GameManager.hpp"
2 2 #include <chrono>
  3 +#include <random>
  4 +#include "../include/glm/glm.hpp"
  5 +#include "../include/glm/gtc/matrix_transform.hpp"
3 6  
4 7  
5 8 GameManager::GameManager()
... ... @@ -10,6 +13,8 @@ GameManager::~GameManager()
10 13 {
11 14 }
12 15  
  16 +
  17 +
13 18 void GameManager::privateInit()
14 19 {
15 20 // Set default OpenGL states
... ... @@ -18,7 +23,7 @@ void GameManager::privateInit()
18 23 // Adding the camera to the scene
19 24 cam_.reset(new Camera());
20 25 this->addSubObject(cam_);
21   - // matrix_.translate(0.0f, 0.0f, -99.0f);
  26 + //matrix_.translate(0.0f, 0.0f, -99.0f);
22 27  
23 28 bf_.reset(new BattleField());
24 29 this->addSubObject(bf_);
... ... @@ -37,6 +42,7 @@ void GameManager::privateInit()
37 42 this->addSubObject(_enemy);
38 43 }
39 44  
  45 +
40 46 //_spotlight.reset(new Spotlight(glm::vec3{0,0,200}, 60, 80));
41 47 //this->addSubObject(_spotlight);
42 48 }
... ... @@ -51,7 +57,7 @@ void GameManager::privateUpdate()
51 57 // Instead of adding alle objects in the scene as subobject to the camera they are added as subobjects
52 58 // to the game manager. Therefore, we set the game manager matrix equal to the camera matrix.
53 59 this->matrix_ = cam_->getMatrix();
54   -
  60 + this->fireFromEnemy();
55 61 }
56 62  
57 63 bool GameManager::markedForDeletion()
... ... @@ -69,21 +75,89 @@ std::shared_ptr&lt;SpaceShip&gt; GameManager::getSpaceShip()
69 75 return spaceship_;
70 76 }
71 77  
  78 +template <typename T>
  79 +int GameManager::random(const T & from, const T & to)
  80 +{
  81 + std::mt19937 rng(rd());
  82 + std::uniform_int_distribution<T> uni(from, to);
  83 + return uni(rng);
  84 +}
  85 +
72 86 void GameManager::fireFromSpaceship()
73 87 {
  88 + int nextFire = 200;
74 89 if (!fireFlag)
75 90 {
76 91 fireFlag = true;
77 92 _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);
  93 + auto spaceshipmatrix = spaceship_->getMatrix();
  94 + switch (this->getSpaceShip()->getWeapon())
  95 + {
  96 + case 0:
  97 + spaceshipmatrix[3].x -= 10;
  98 + _bullet.reset(new Bullet(spaceshipmatrix, Bullet::Trajectory::FORWARD, 600.0f));
  99 + _bullet->addCollisionWith(this->getAllObjectOfType<Enemy>());
  100 + this->addSubObject(_bullet);
  101 +
  102 + spaceshipmatrix[3].x += 20;
  103 + _bullet.reset(new Bullet(spaceshipmatrix, Bullet::Trajectory::FORWARD, 600.0f));
  104 + _bullet->addCollisionWith(this->getAllObjectOfType<Enemy>());
  105 + this->addSubObject(_bullet);
  106 + break;
  107 +
  108 + case 1:
  109 + spaceshipmatrix[3].x -= 10;
  110 + spaceshipmatrix = glm::rotate(spaceshipmatrix, 0.2f, glm::vec3(0.0f, 1.0f, 0.0f));
  111 + _bullet.reset(new Bullet(spaceshipmatrix, Bullet::Trajectory::FORWARD, 600.0f));
  112 + _bullet->addCollisionWith(this->getAllObjectOfType<Enemy>());
  113 + this->addSubObject(_bullet);
  114 +
  115 + spaceshipmatrix[3].x += 20;
  116 + spaceshipmatrix = glm::rotate(spaceshipmatrix, 0.4f, glm::vec3(0.0f, -1.0f, 0.0f));
  117 + _bullet.reset(new Bullet(spaceshipmatrix, Bullet::Trajectory::FORWARD, 600.0f));
  118 + _bullet->addCollisionWith(this->getAllObjectOfType<Enemy>());
  119 + this->addSubObject(_bullet);
  120 + break;
  121 +
  122 + case 2:
  123 + spaceshipmatrix[3].x -= 10;
  124 + _bullet.reset(new Bullet(spaceshipmatrix, Bullet::Trajectory::FORWARD, 600.0f));
  125 + _bullet->addCollisionWith(this->getAllObjectOfType<Enemy>());
  126 + this->addSubObject(_bullet);
  127 +
  128 + spaceshipmatrix[3].x += 20;
  129 + _bullet.reset(new Bullet(spaceshipmatrix, Bullet::Trajectory::FORWARD, 600.0f));
  130 + _bullet->addCollisionWith(this->getAllObjectOfType<Enemy>());
  131 + this->addSubObject(_bullet);
  132 + break;
  133 +
  134 + default:
  135 + break;
  136 + }
  137 +
81 138 }
82   - else if (duration_cast<milliseconds>(Rolex::now() - _lastFire).count() > 200)
  139 + else if (duration_cast<milliseconds>(Rolex::now() - _lastFire).count() > nextFire)
83 140 {
84 141 fireFlag = false;
85 142 }
86   -
87 143 }
88 144  
  145 +void GameManager::fireFromEnemy()
  146 +{
  147 + if (!fireFlagEnemy)
  148 + {
  149 + fireFlagEnemy = true;
  150 + _lastFireEnemy = Rolex::now();
  151 + auto enemies = this->getAllObjectOfType<Enemy>();
  152 + int ran = this->random(int(0), int(enemies.size()) - int(1));
  153 + auto enemyPick = enemies.at(ran);
  154 + _bullet.reset(new Bullet(enemyPick->getMatrix(), Bullet::Trajectory::BACKWARD, 350.00f));
  155 + _bullet->addCollisionWith(this->getSpaceShip());
  156 + this->addSubObject(_bullet);
  157 + }
  158 + else if (duration_cast<milliseconds>(Rolex::now() - _lastFireEnemy).count() > 200)
  159 + {
  160 + fireFlagEnemy = false;
  161 + }
  162 +}
89 163  
... ...
SpaceShooterV4/src/SpaceShip.cpp
... ... @@ -3,6 +3,7 @@
3 3 #include "include/glm/glm.hpp"
4 4 #include "include/glm/gtc/matrix_transform.hpp"
5 5 #include "Bullet.h"
  6 +#include <iostream>
6 7  
7 8  
8 9 SpaceShip::SpaceShip()
... ... @@ -74,24 +75,66 @@ bool SpaceShip::markedForDeletion()
74 75 return _markDelete;
75 76 }
76 77  
77   -void SpaceShip::moveBackward()
  78 +float SpaceShip::moveBackward()
78 79 {
79   - matrix_ = glm::translate(matrix_, glm::vec3(0.0f, 0.0f, _speed * _deltaTime));
  80 + if (matrix_[3][2] < 0)
  81 + {
  82 + matrix_ = glm::translate(matrix_, glm::vec3(0.0f, 0.0f, _speed * _deltaTime));
  83 + return _speed * _deltaTime;
  84 + }
  85 + return 0.0f;
80 86 }
81 87  
82   -void SpaceShip::moveForward()
  88 +float SpaceShip::moveForward()
83 89 {
84   - matrix_ = glm::translate(matrix_, glm::vec3(0.0f, 0.0f, -_speed * _deltaTime));
  90 + if (matrix_[3][2] > -320)
  91 + {
  92 + matrix_ = glm::translate(matrix_, glm::vec3(0.0f, 0.0f, -_speed * _deltaTime));
  93 + return -_speed * _deltaTime;
  94 + }
  95 + return 0.0f;
85 96 }
86 97  
87   -void SpaceShip::moveRight()
  98 +float SpaceShip::moveRight()
88 99 {
89   - matrix_ = glm::translate(matrix_, glm::vec3(_speed * _deltaTime, 0.0f, 0.0f));
  100 + if (matrix_[3][0] < 160)
  101 + {
  102 + matrix_ = glm::translate(matrix_, glm::vec3(_speed * _deltaTime, 0.0f, 0.0f));
  103 + return -(_speed * _deltaTime);
  104 + }
  105 + return 0.0f;
90 106 }
91 107  
92   -void SpaceShip::moveLeft()
  108 +float SpaceShip::moveLeft()
93 109 {
94   - matrix_ = glm::translate(matrix_, glm::vec3(-_speed * _deltaTime, 0.0f, 0.0f));
  110 + if (matrix_[3][0] > -150)
  111 + {
  112 + matrix_ = glm::translate(matrix_, glm::vec3(-_speed * _deltaTime, 0.0f, 0.0f));
  113 + return (_speed * _deltaTime);
  114 + }
  115 + return 0.0f;
  116 +}
  117 +
  118 +void SpaceShip::changeWeapon(const int& w)
  119 +{
  120 + _weapon = w;
  121 +}
  122 +
  123 +void SpaceShip::changeWeapon()
  124 +{
  125 + if (_weapon >= amountOfWeapons)
  126 + {
  127 + _weapon = 0;
  128 + }
  129 + else {
  130 + _weapon++;
  131 + }
  132 + std::cout << _weapon << std::endl;
  133 +}
  134 +
  135 +int SpaceShip::getWeapon()
  136 +{
  137 + return _weapon;
95 138 }
96 139  
97 140 void SpaceShip::fire()
... ...
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/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   - Enemy.cpp
  1 + GameManager.cpp
2 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...
13 3 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
14 4 LINK : warning LNK4075: ignoring '/INCREMENTAL' due to '/LTCG' specification
15 5 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/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