forked from C0deH4cker/FlappingBird
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Level.cpp
170 lines (131 loc) · 3.08 KB
/
Level.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
//
// Level.cpp
// FlappingBird
//
// Created by C0deH4cker on 1/30/14.
// Copyright (c) 2014 C0deH4cker. All rights reserved.
//
#include "Level.h"
#include <random>
#include <iostream>
#include <math.h>
#include <time.h>
#include <sge.h>
#include "Pipe.h"
#include "FlappingBird.h"
using namespace sge;
const int Level::scrollSpeed = 230;
const float Level::pipeDistance = 240.0f;
const float Level::pipeSpread = 65.0f;
Level::Level(const Content& content, Rectangle bounds)
: distance(0.0f), rng((unsigned)time(NULL)), paused(false), started(false),
distribution(0.1f, 0.7f), viewport(bounds),
sprites(content.load<Texture2D>("spritesheet.png", GL_NEAREST)),
score(sprites),
background(sprites, {0.0f, 0.0f, 144.0f, 256.0f}),
pipeTop(sprites, {302.0f, 0.0f, 26.0f, 135.0f}),
pipeBottom(sprites, {330.0f, 0.0f, 26.0f, 121.0f}),
ground(sprites, {146.0f, 0.0f, 154.0f, 54.0f}),
groundRect(0.0f, viewport.height - 2.0f * ground.getHeight(),
4.0f * ground.getWidth(), 3.0f * ground.getHeight()) {
bird = new Bird(sprites, viewport.height - 2.0f * ground.getHeight());
}
Level::~Level() {
delete sprites;
}
void Level::update(double deltaTime) {
if(paused) return;
float dx = scrollSpeed * deltaTime;
if(!bird->isDead()) {
groundRect.x -= dx;
float overlap = viewport.width - groundRect.right();
if(overlap > 0.0f)
groundRect.x = fmodf(groundRect.x, 28.0f);
}
if(!started) return;
bird->update(deltaTime);
if(bird->isDead()) return;
auto it = pipes.begin();
while(it != pipes.end()) {
(*it)->scroll(dx);
if((*it)->didScore(bird))
++score;
if((*it)->collides(bird)) {
bird->die();
showScore();
}
if((*it)->offScreen()) {
delete *it;
it = pipes.erase(it);
}
else ++it;
}
float fromEdge = fmodf(distance, pipeDistance) + dx - pipeDistance;
// Should only execute once at max unless FPS < 1
while(fromEdge > 0.0f) {
addPipe(fromEdge);
fromEdge -= pipeDistance;
}
distance += dx;
}
void Level::draw(double deltaTime) {
background.draw(viewport);
for(auto it = pipes.begin(); it != pipes.end(); ++it) {
(*it)->draw();
}
bird->draw(paused ? 0.0 : deltaTime);
ground.draw(groundRect);
score.draw();
}
void Level::charTyped(unsigned char uc) {
switch(uc) {
case ' ':
if(paused)
resume();
if(!started)
started = true;
bird->flap();
break;
case 'p':
if(paused)
resume();
else
pause();
break;
case 'r':
restart();
break;
default:
break;
}
}
void Level::pause() {
paused = true;
bird->pause();
}
void Level::resume() {
paused = false;
bird->resume();
}
bool Level::isPaused() const {
return paused;
}
void Level::restart() {
bird->reset();
distance = 0.0f;
pipes.clear();
score = 0;
paused = false;
started = false;
}
void Level::addPipe(float scrolled) {
float x, y;
x = viewport.width - scrolled;
/* arcsin(2x-1)/pi+0.5 */
y = viewport.height * (asinf(2.0f * distribution(rng) - 1.0f) / M_PI + 0.5f);
Pipe* pipe = new Pipe(Vector2(x, y), &pipeTop, &pipeBottom);
pipes.push_back(pipe);
}
void Level::showScore() {
// TODO: implement final score display
}