-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbird.cpp
75 lines (64 loc) · 1.77 KB
/
bird.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
#include <iostream>
#include "bird.hpp"
Bird::Bird(int x, int y, int floor_height, Texture* texture, std::vector<Rect> frames) {
this->x = x;
this->y = y;
this->floor_height = floor_height;
animation = new Animation(texture, std::move(frames), FRAMES_PER_ANIMATION_FRAME);
state = Gliding;
}
void Bird::Flap() {
if (state == Gliding)
state = Alive;
if (state == Alive)
speed_y = FLAP_SPEED;
}
void Bird::Kill(double distance_travelled) {
speed_y = 0;
state = Dead;
x_died = x;
distance_died = distance_travelled;
animation->Pause();
animation->frame_index = 0;
}
void Bird::Update(double delta_time, double distance_travelled) {
animation->Update();
if (state != Gliding) {
speed_y += GRAVITY;// * delta_time;
if (speed_y > TERMINAL_VELOCITY)
speed_y = TERMINAL_VELOCITY;
y += (int) speed_y;
}
switch (state) {
case Alive:
{
// If bird is above screen
auto f = animation->frames[0];
if (y - f.h / 2 < 0) {
y = f.h / 2;
speed_y = 0;
}
break;
}
case Dead:
x = x_died - (int)(distance_travelled - distance_died);
if (y > floor_height)
y = floor_height;
break;
}
}
void Bird::Render(Renderer *renderer) {
Rect dest_rect = GetRect();
double angle = (state == Dead)? 90 : speed_y * 10.0;
renderer->Render(animation->texture, animation->CurrentFrame(), &dest_rect, angle);
}
Rect Bird::GetRect() {
auto f = animation->frames[0];
return {x - f.w / 2, y - f.h / 2, f.w, f.h};
}
bool Bird::IsAlive() {
return state == Alive || state == Gliding;
}
Bird::~Bird() {
delete animation;
}