-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpaceShip.cpp
More file actions
85 lines (74 loc) · 2.28 KB
/
Copy pathSpaceShip.cpp
File metadata and controls
85 lines (74 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/gtx/rotate_vector.hpp>
#include "SpaceShip.hpp"
#include "sre/Renderer.hpp"
#include "sre/SpriteAtlas.hpp"
SpaceShip::SpaceShip(const sre::Sprite &sprite, bool dead, glm::vec2 lastPosition) : GameObject(sprite) {
this->dead = dead;
scale = glm::vec2(0.5f,0.5f);
winSize = sre::Renderer::instance->getDrawableSize();
radius = 23;
radiuss = 23;
if (dead) {
position = lastPosition;
}
else {
position = winSize * 0.5f;
}
velocity = glm::vec2(0.0f,0.0f);
}
void SpaceShip::update(float deltaTime) {
if (!dead){
if (thrust){
float acceleration = deltaTime*thrustPower;
glm::vec2 direction = glm::rotateZ(glm::vec3(0,acceleration,0), glm::radians(rotation));
velocity += direction;
float speed = glm::length(velocity);
if (speed > maxSpeed){
velocity = velocity * (maxSpeed / speed);
}
} else {
velocity = velocity * (1.0f - drag*deltaTime);
}
position += velocity * deltaTime;
if (rotateCCW){
rotation += deltaTime * rotationSpeed;
}
if (rotateCW){
rotation -= deltaTime * rotationSpeed;
}
// wrap around
if (this->position.x < 0){
position.x += winSize.x;
} else if (this->position.x > winSize.x){
position.x -= winSize.x;
}
if (this->position.y < 0){
position.y += winSize.y;
} else if (this->position.y > winSize.y){
position.y -= winSize.y;
}
}
}
glm::vec2 SpaceShip::getPosition() {
return glm::vec2(position.x, position.y);
}
//void SpaceShip::onCollision(std::shared_ptr<GameObject> other) {
// isForDeletion = true;
//}
void SpaceShip::onKey(SDL_Event &keyEvent) {
if (keyEvent.key.keysym.sym == SDLK_UP){
thrust = keyEvent.type == SDL_KEYDOWN;
}
if (keyEvent.key.keysym.sym == SDLK_LEFT){
rotateCCW = keyEvent.type == SDL_KEYDOWN;
}
if (keyEvent.key.keysym.sym == SDLK_RIGHT){
rotateCW = keyEvent.type == SDL_KEYDOWN;
}
if (keyEvent.key.keysym.sym == SDLK_SPACE) {
fire = keyEvent.type == SDL_KEYDOWN;
}
}
void SpaceShip::spawnLaser() {
}