-
Notifications
You must be signed in to change notification settings - Fork 48
/
Bird.cpp
184 lines (147 loc) · 3.92 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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
//
// Bird.cpp
// FlappingBird
//
// Created by C0deH4cker on 1/30/14.
// Copyright (c) 2014 C0deH4cker. All rights reserved.
//
#include "Bird.h"
#include <sge.h>
#include <helpers.h>
#include "FlappingBird.h"
using namespace sge;
const float Bird::flapSpeed = -515.0f;
const float Bird::maxSpeed = 680.0f;
const float Bird::gravity = 2000.0f;
const double Bird::defaultFps = 2.0;
const float w = 17.0f, h = 12.0f;
const int POSCOUNT = 10;
Bird::Bird(const Texture2D* sprites, float groundHeight)
: speed(0.0f), ground(groundHeight), dead(false), started(false), paused(false),
curFrame(0), fps(POSCOUNT * defaultFps), extra(0.0), onGround(false),
frames{
{sprites, {223.0f, 124.0f, w, h}}, // wing down
{sprites, {264.0f, 90.0f, w, h}}, // wing center
{sprites, {264.0f, 64.0f, w, h}} // wing top
} {
Window* window = Game::instance()->window;
float x = window->getWidth() / 5.0f;
float y = window->getHeight() / 2.0f;
bounds.width = 2.5f * w;
bounds.height = 2.5f * h;
bounds.x = x - bounds.width / 2.0f;
bounds.y = y - bounds.height / 2.0f;
startingPos = bounds.topLeft();
}
void Bird::update(double deltaTime) {
if(onGround) return;
speed += gravity * deltaTime / 2.0f;
if(speed > maxSpeed)
speed = maxSpeed;
bounds.y += speed * deltaTime;
speed += gravity * deltaTime / 2.0f;
if(speed > maxSpeed)
speed = maxSpeed;
if(bounds.midY() + bounds.width / 2.0f >= ground) {
die();
onGround = true;
bounds.y = ground + 2.5f - (bounds.width + bounds.height) / 2.0f;
}
}
void Bird::draw(double deltaTime) {
const double maxFps = 3.5;
const double endtime = 1.2;
const double coef = maxFps / (endtime*endtime);
const double offset = 0.0 * endtime;
int i;
double elapsed = timer.elapsed();
if(started) {
double val = elapsed - offset;
double freq = CLAMP(maxFps - coef * val*val, 0.0f, maxFps);
setFlapsPerSec(freq);
i = getFrame();
frames[i].draw(bounds, getRotation());
}
else {
Rectangle frame(bounds);
frame.y += 6.0f * sinf(8.0f * elapsed);
// Dead bird always drawn using middle frame
i = dead ? 1 : getFrame();
frames[i].draw(frame);
}
}
void Bird::flap() {
if(dead) return;
started = true;
timer.reset();
wingTimer.reset();
if(bounds.top() < 0.0f) return;
speed = flapSpeed;
}
void Bird::die() {
dead = true;
}
void Bird::reset() {
bounds.x = startingPos.x;
bounds.y = startingPos.y;
speed = 0.0f;
timer.reset();
wingTimer.reset();
setFlapsPerSec(defaultFps);
dead = false;
started = false;
paused = false;
onGround = false;
}
void Bird::pause() {
timer.pause();
wingTimer.pause();
paused = true;
}
void Bird::resume() {
timer.resume();
wingTimer.resume();
paused = false;
}
bool Bird::isDead() const {
return dead;
}
const Rectangle& Bird::getBounds() const {
return bounds;
}
Rectangle Bird::getBBox() const {
// Bounding box is a square of dimensions width X width.
// Makes it easier to work with in terms of rotation, and
// still quite accurate.
float diff = bounds.width - bounds.height;
return Rectangle(bounds.x + diff / 2, bounds.y,
bounds.height, bounds.height);
}
float Bird::getRotation() const {
const float maxrot = M_PI / 10.0f;
const float minrot = -M_PI_2;
const float endtime = 0.4f;
const float offset = 0.5f * endtime;
const float coef = (maxrot - minrot) / (endtime*endtime);
if(onGround)
return minrot;
float val = MAX(timer.elapsed() - offset, 0.0f);
return CLAMP(maxrot - coef * val*val, minrot, maxrot);
}
void Bird::setFlapsPerSec(double flaps) {
fps = POSCOUNT * flaps;
}
const int positions[POSCOUNT] = {1,0,0,0,1,1,2,2,2,1};
int Bird::getFrame() {
if(paused) return positions[curFrame % POSCOUNT];
if(fps <= 0.0 || onGround)
return positions[0];
double elapsed = wingTimer.elapsed() + extra;
double freq = 1.0 / fps;
if(elapsed > freq) {
curFrame = (int)(curFrame + elapsed / freq) % POSCOUNT;
extra = fmod(elapsed, freq);
wingTimer.reset();
}
return positions[curFrame % POSCOUNT];
}