-
Notifications
You must be signed in to change notification settings - Fork 1
/
enemy.cpp
111 lines (85 loc) · 2.27 KB
/
enemy.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
* enemy.cpp
*
* Created on: 2013-07-28
* Author: Liam
*/
#include "enemy.h"
Enemy::Enemy():m_patroldist(), m_home() {
m_properties = ENEMY;
}
Enemy::Enemy(vec2d pos, int width, int height, int patroldist):m_home(pos) {
m_pos = pos;
m_patroldist = patroldist;
m_properties = ENEMY;
CHAR chars[(int)(width * height)];
COL colours[(int)(width * height)];
for (int i = 0; i < (int)(width * height); ++i) {
chars[i] = 233;
colours[i] = 12;
}
Image image(width, height, chars, colours);
m_image = image;
m_inputvel.setX(ENEMY_PATROL_SPEED);
}
void Enemy::update(float dt) {
if (m_pos.getX() - m_home.getX() > m_patroldist)
m_inputvel.setX(-ENEMY_PATROL_SPEED);
else if (m_home.getX() - m_pos.getX() > m_patroldist)
m_inputvel.setX(ENEMY_PATROL_SPEED);
vec2d accel(0, GRAVITY);
m_vel += accel * dt;
if (m_pos.getX() < m_image.getWidth()) m_pos.setX(m_image.getWidth());
if (m_pos.getY() < m_image.getHeight()) m_pos.setY(m_image.getHeight());
if (m_pos.getX() > WIDTH - m_image.getWidth()) m_pos.setX(WIDTH - m_image.getWidth());
if (m_pos.getY() > HEIGHT - m_image.getHeight()) {
m_pos.setY(HEIGHT - m_image.getHeight());
m_vel.setY(0);
}
}
void Enemy::resize(const int &w, const int &h) {
CHAR chars[w * h];
COL colours[w * h];
for (int i = 0; i < w * h; ++i) {
chars[i] = 233;
colours[i] = 12;
}
Image image(w, h, chars, colours);
m_image = image;
}
bool Enemy::write(std::ofstream &file) {
if (file.is_open()) {
int id = ENEMYCREATOR;
file << std::dec << id << ' ';
Entity::write(file);
file << std::dec << m_patroldist << ' ';
file << m_home.getX() << ' ';
file << m_home.getY() << ' ';
file << m_image.getWidth() << ' ';
file << m_image.getHeight() << ' ';
file << std::endl;
return true;
} else return false;
}
bool Enemy::read(std::ifstream &file) {
if (file.is_open()) {
float f;
int w, h;
Entity::read(file);
file >> m_patroldist;
file >> f;
m_home.setX(f);
file >> f;
m_home.setY(f);
file >> w;
file >> h;
resize(w, h);
file.get();
return true;
} else return false;
}
Entity *_EnemyCreator::create(const vec2d &pos, const vec2d &size, const int &properties) const {
Entity *entity = new Enemy(pos, size.getX(), size.getY(), properties);
return entity;
}
Enemy_Creator EnemyCreator;