-
Notifications
You must be signed in to change notification settings - Fork 1
/
rocket.cpp
81 lines (63 loc) · 1.52 KB
/
rocket.cpp
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
/*
* rocket.cpp
*
* Created on: 2013-09-02
* Author: Liam
*/
#include "rocket.h"
#include "player.h"
Rocket::Rocket() {
m_properties = PROJECTILE;
fillImage();
}
Rocket::Rocket(const vec2d &pos, const vec2d &direction):m_direction(direction.normalize()) {
m_properties = PROJECTILE;
m_pos = pos;
m_inputvel = m_direction * 20;
fillImage();
}
void Rocket::update(float dt) {
if (m_pSubject) {
vec2d dir = static_cast<Player *>(m_pSubject)->getPos() - m_pos;
m_direction = dir.normalize();
}
m_inputvel = m_direction * 20;
}
bool Rocket::write(std::ofstream &file) {
if (file.is_open()) {
int id = ROCKETCREATOR;
file << std::dec << id << ' ';
Entity::write(file);
file << std::hex << static_cast<Player *>(m_pSubject) << std::endl;
return true;
} else return false;
}
bool Rocket::read(std::ifstream &file) {
if (file.is_open()) {
Entity::read(file);
unsigned int i;
char c = ' ';
while (c != 'x' && c != '\n')
c = file.get();
if (c != '\n') {
file >> std::hex >> i;
m_pSubject = (Subject *)i;
}
return true;
} else return false;
}
void Rocket::fixup() {
Subject *pSubject = static_cast<Player *>(AddressTranslator::FindAddress((unsigned int)m_pSubject));
setSubject(pSubject);
}
void Rocket::fillImage() {
CHAR chars[1];
COL colours[1];
chars[0] = 232;
colours[0] = 12 | BACKGROUND_BLUE;
Image image(1, 1, chars, colours);
setImage(image);
}
Entity *_RocketCreator::create(const vec2d &pos, const vec2d &dir, const int &dummy) const {
return new Rocket(pos, dir);
}