-
Notifications
You must be signed in to change notification settings - Fork 0
/
rocket.h
72 lines (59 loc) · 2.63 KB
/
rocket.h
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
#pragma once
#include "missle.h"
class Rocket: public Entity {
protected:
bool engineOn = false;
float springiness = 0.5;
float throttle = 0.3;
float engineISP = 10;
sf::Texture texture1;
public:
Rocket(std::string type, float life, float damage, sf::Vector2f position, std::vector<Entity*> *renderQueue) : Entity(type, life, damage, position, 0, "assets/sprites/rocket-off.png", 0.1, renderQueue) {
texture1.loadFromFile("assets/sprites/rocket-on.png");
sprite.setOrigin(texture.getSize().x /2, texture.getSize().y /1.5);
mass = 500;
};
void throttleToggle() {
if(engineOn == true) {
engineOn = false;
} else {
engineOn = true;
}
}
void setThrottle(float _throttle) {
if(_throttle > 0.3 && _throttle <= 1) throttle = _throttle;
}
void spawnMissle() {
cooldownTime = cooldownTimer.getElapsedTime();
if(cooldownTime.asMilliseconds() > 100.0f) {
renderQueue->push_back(new Missle(this, 0, 30, 1000, 5000, renderQueue));
cooldownTimer.restart();
}
}
void animate() {
sprite.setRotation(rotation + 90.f);
if(engineOn) {
sprite.setTexture(texture1);
velocity.x += cos((sprite.getRotation() - 90.f) * 3.14159265 / 180) * engineISP * throttle * animationTime.asMilliseconds()/1000;
velocity.y += sin((sprite.getRotation() - 90.f) * 3.14159265 / 180) * engineISP * throttle * animationTime.asMilliseconds()/1000;
} else sprite.setTexture(texture);
sprite.move(velocity.x, velocity.y);
animationTimer.restart();
};
void collide(Entity *entity) {
if(entity->getType() != Rocket::getType() && entity->getType() != "animation") {
entity->attack(this);
float colliderMass = entity->getMass();
sf::Vector2f colliderPosition = entity->getPosition();
sf::Vector2f colliderVelocity = entity->getVelocity();
sf::Vector2f DeltaPosition = colliderPosition - position;
sf::Vector2f DeltaVelocity = colliderVelocity - velocity;
//calculation of new velocities:
velocity += (colliderMass / (mass + colliderMass)) * (float)(((DeltaVelocity.x * DeltaPosition.x) + (DeltaVelocity.y * DeltaPosition.y)) / (pow((DeltaPosition.x), 2) + pow((DeltaPosition.y), 2))) * DeltaPosition;
}
}
void destroy() {
renderQueue->push_back(new Animation(sprite.getPosition(), sprite.getRotation(), 2, "assets/sprites/explosion.png", sf::Vector2i(96, 96), sf::Vector2i(4,4), 50.f, renderQueue));
stop();
}
};