-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShip.cpp
146 lines (119 loc) · 2.42 KB
/
Ship.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include "Color.h"
#include "Config.h"
#include "Ship.h"
#include "SpriteManager.h"
#include <Windows.h>
Ship::Ship(int x, int y) : Entity(x, y)
{
// loads ship sprite
SpriteManager& spriteManager = SpriteManager::getInstance();
this->sprite = spriteManager.load("Assets/Sprites/Player/Ship.txt", WIDTH, HEIGHT);
// loads ship hidden sprite
hiddenSprite = spriteManager.copy(sprite, 176, Color::SILVER, Color::WHITE);
exposedSprite = sprite;
// prepares ship's tractor beam
beam = new TractorBeam(this);
}
Ship::~Ship()
{
delete hiddenSprite;
hiddenSprite = NULL;
}
void Ship::reset()
{
x = PLAYER_STARTING_POS_X;
y = PLAYER_STARTING_POS_Y;
expose();
beam->reset();
}
void Ship::update(long delta)
{
Behaves::update(delta);
// runs animation
animate(delta);
// abducting
if (state == State::Abducting) {
beam->update(delta);
if (beam->hasFinished()) {
state = State::Playing;
}
else {
return;
}
}
// ship actions
if (GetAsyncKeyState(GAME_CONTROL_HIDE)) hide(); // can't move while hidden
else{
if (GetAsyncKeyState(GAME_CONTROL_ABDUCT)) abduct(); // can't move while abducting
else {
if (GetAsyncKeyState(GAME_CONTROL_LEFT)) moveLeft(delta);
if (GetAsyncKeyState(GAME_CONTROL_RIGHT)) moveRight(delta);
}
}
}
void Ship::paint()
{
Entity::paint();
beam->paint();
}
void Ship::moveLeft(long delta)
{
sumLeft += delta;
if (sumLeft >= moveDelay) {
sumLeft -= moveDelay;
if (x > 0) {
expose();
x--;
}
}
}
void Ship::moveRight(long delta)
{
sumRight += delta;
if (sumRight >= moveDelay) {
sumRight -= moveDelay;
if (x < SCREEN_WIDTH - WIDTH) {
expose();
x++;
}
}
}
void Ship::hide()
{
state = State::Hidden;
sprite = hiddenSprite;
}
void Ship::expose()
{
state = State::Playing;
sprite = exposedSprite;
}
void Ship::abduct()
{
if (state == Abducting) return;
expose();
state = State::Abducting;
beam->fire();
}
void Ship::animate(long delta)
{
if (state == State::Hidden) return;
sumAnimation += delta;
if (sumAnimation >= animationDelay) {
sumAnimation -= animationDelay;
// cycles the center pixels
Pixel* temp;
for (int curr = 0; curr < WIDTH; curr++) {
int next = (curr + 1) % WIDTH;
temp = &sprite->map[1][curr];
sprite->map[1][curr] = sprite->map[1][next];
sprite->map[1][next] = *temp;
sprite->map[1][curr].forceRefresh = true;
sprite->map[1][next].forceRefresh = true;
}
}
}
void Ship::behaviorFinished(Behavior* behavior)
{
//
}