-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlevi.cpp
151 lines (120 loc) · 3.08 KB
/
levi.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
147
#include "levi.h"
#include "frame.h"
#include "GameWindow.h"
#include <cmath>
Levi::Levi(): stop(0), jump(true), gameEnd(false) {
}
void Levi::Init() {
x = -800;
y = 300;
framecount = 0;
width = 150;
height = 125;
levi_timer = al_create_timer(1.0 / 60.0);
al_start_timer(levi_timer);
char buffer[15];
for (int i = 1; i <= 2; i++) {
sprintf(buffer, "./images/run_%d.png", i);
ALLEGRO_BITMAP* img = al_load_bitmap(buffer);
imgData[LeviState::RUNNING].push_back(img);
}
die = al_load_bitmap("./images/dead.png");
isDying = false;
dyingDuration = 2.5;
dyingTimeElapsed = 0.0;
shimmeringDuration = 1.5;
shimmeringTimeElapsed = 0.0;
}
float Levi::getX() const { return x; }
float Levi::getY() const { return y; }
int Levi::getWidth() const { return width; }
int Levi::getHeight() const { return height; }
void Levi::Die() {
isDying = true;
dyingTimeElapsed = 0.0;
shimmeringTimeElapsed = 0.0;
}
int Levi::Stop() const {
return stop;
}
bool Levi::Shimmering() const {
return isShimmering;
}
bool Levi::Jump() const{
return jump;
}
void Levi::Continue() {
x += 0.5;
}
bool Levi::End() const{
return gameEnd;
}
void Levi::resetEnd(){
gameEnd = false;
}
void Levi::Update(double deltaTime) {
if(x <= 200) x += 0.5;
else if(x > 1800) gameEnd = true;
else{
jump = false;
if (isDying) {
stop = 1;
dyingTimeElapsed += deltaTime * 1000;
//printf("dying: %f\n", dyingTimeElapsed);
if (dyingTimeElapsed >= dyingDuration) {
isDying = false;
isShimmering = true;
dyingTimeElapsed = 0.0;
}
}
if (isShimmering) {
stop = 2;
shimmeringTimeElapsed += deltaTime;
//printf("shimmer: %f \n", shimmeringTimeElapsed);
if (shimmeringTimeElapsed >= shimmeringDuration) {
isShimmering = false;
shimmeringTimeElapsed = 0.0;
stop = 0;
}
}
}
if (!stop) {
switchRate = 200;
framecount++;
if (framecount >= 2 * switchRate) {
framecount = 0;
}
}
}
void Levi::moveUp() {
if(y > 170 && x > 200) y -= 135;
}
void Levi::moveDown() {
if(y < 830 && x > 200) y += 135;
}
void Levi::Draw() {
int frameIndex = (framecount < switchRate) ? 0 : 1;
ALLEGRO_BITMAP* current_frame = imgData[LeviState::RUNNING][frameIndex];
if (isDying) {
al_draw_bitmap(die, x, y, 0);
} else if (isShimmering) {
if (static_cast<int>(shimmeringTimeElapsed * 10) % 2 == 0) {
al_draw_bitmap(current_frame, x, y, 0);
}
} else {
al_draw_bitmap(current_frame, x, y, 0);
}
}
Levi::~Levi() {
al_destroy_timer(levi_timer);
al_destroy_bitmap(die);
for (auto& state : imgData) {
for (auto& img : state.second) {
if (img) {
al_destroy_bitmap(img);
img = nullptr;
}
}
}
imgData.clear();
}