-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmissle.h
78 lines (65 loc) · 2.99 KB
/
missle.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
73
74
75
76
77
78
#pragma once
#include <math.h>
#include "entity.h"
#include "animation.h"
#include "bboxed.h"
class Missle : public Entity, public Bboxed {
protected:
Entity *parent;
Entity *target = this;
bool bounded = false;
float time = 0;
float speed = 1000;
public:
Missle(Entity *_parent, float life, float damage, float _speed, float _time, std::vector<Entity*> *renderQueue) : Entity(_parent->getType(), life, damage, _parent->getPosition(), _parent->getRotation() - 90.f, "assets/sprites/missle.png", 0.1, renderQueue), Bboxed(sf::FloatRect(0, 0, 0, 0)) {
parent = _parent;
speed = _speed;
time = _time;
mass = 1;
}
Missle(Entity *_parent, float life, float damage, float _speed, sf::FloatRect bbox, std::vector<Entity*> *renderQueue) : Entity(_parent->getType(), life, damage, _parent->getPosition(), _parent->getRotation() - 90.f, "assets/sprites/missle.png", 0.1, renderQueue), Bboxed(bbox) {
parent = _parent;
bounded = true;
speed = _speed;
mass = 1;
}
Missle(std::string type, float life, float damage, float _speed, sf::Vector2f position, float rotation, float _time, std::vector<Entity*> *renderQueue) : Entity(type, life, damage, position, rotation - 90.f, "assets/sprites/missle.png", 0.1, renderQueue), Bboxed(sf::FloatRect(0, 0, 0, 0)) {
parent = this;
time = _time;
speed = _speed;
mass = 1;
}
Missle(std::string type, float life, float damage, float _speed, sf::Vector2f position, float rotation, sf::FloatRect bbox, std::vector<Entity*> *renderQueue) : Entity(type, life, damage, position, rotation - 90.f, "assets/sprites/missle.png", 0.1, renderQueue), Bboxed(bbox) {
parent = this;
bounded = true;
speed = _speed;
mass = 1;
}
void checkBoundries() {
if(bounded) {
if(!bbox.contains(sprite.getPosition())) destroy();
} else if(getTime() > time) destroy();
}
void targetEntity(Entity *_target) {
target = _target;
}
void collide(Entity *entity) {
if(entity->getType() != getType() && entity->getTime() > 100.f && Missle::getTime() > 100.f && entity->getType() != "animation") {
entity->attack(this);
if(parent != this) parent->addPoint(score);
}
}
void destroy() {
renderQueue->push_back(new Animation(sprite.getPosition(), sprite.getRotation(), 0.5f, "assets/sprites/explosion.png", sf::Vector2i(96, 96), sf::Vector2i(4,4), 50.f, renderQueue));
stop();
}
void animate() {
if(target != this) Entity::lookAt(target->getPosition());
velocity.x = cos(rotation * 3.14159265 / 180) * speed;
velocity.y = sin(rotation * 3.14159265 / 180) * speed;
sprite.setRotation(rotation);
sprite.move(velocity.x * animationTime.asMilliseconds()/1000, velocity.y * animationTime.asMilliseconds()/1000);
checkBoundries();
animationTimer.restart();
}
};