/
main.cpp
225 lines (183 loc) · 4.13 KB
/
main.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#include <stdio.h>
#include <iostream>
#include <memory>
#include <stdlib.h>
#include "XGame.h"
#include <X11/keysym.h>
#include <list>
#include <vector>
#include <algorithm>
#include <functional>
std::vector<std::string> alienColors = {"Yellow", "Blue", "Beige", "Green", "Pink"};
class Block;
std::list<Block *> blocks;
int cameraYTarget, maxPoint = 0xFFFFFF, maxZ=0;
class Block : public Sprite {
public:
int ty;
int tx;
Block( XGame *g ) : Sprite(g){
blocks.push_back(this);
z = maxZ--;
}
virtual void update(){
if( ty - cameraYTarget > 600 ){
ty -= 600;
tx = rand()%10*70;
z = maxZ--;
}
y -= (y - ty) * 0.3;
x -= (x - tx) * 0.3;
Sprite::update();
}
virtual ~Block(){
blocks.remove(this);
}
};
class Player : public Sprite {
public:
int walkSpeed;
bool jumping, alive;
int jumpStartSpeed;
bool keys[0xFFFF];
std::string color;
int h, w;
Player( XGame *g ) : Sprite(g){
frameTime = 3;
color = alienColors[rand()%alienColors.size()];
walkSpeed = 18;
jumpStartSpeed = 60;
alive = true;
stretch = false;
jumping = false;
y = 400;
x = 70*6;
for( int i=0; i<0xFFFF; ++i ) keys[i]=false;
texture = game->loadSpritesheet("aliens");
stateIdle();
XGameTexture *t = game->getTexture(texture);
applyAnimation();
h = t->cropHeight;
w = t->cropWidth;
g->onKeyDown = std::bind(&Player::onKeyDown, this, std::placeholders::_1);
g->onKeyUp = std::bind(&Player::onKeyUp, this, std::placeholders::_1);
}
void stateIdle(){
animation = color + "_idle";
}
void stateRun(bool mirror){
frame = loopStart = 1;
loopEnd = 3;
this->mirror = mirror;
animation = color + "_walk";
}
void stateJump(){
frame = loopStart = 0;
loopEnd = 1;
velY = -jumpStartSpeed;
animation = color + "_jump";
jumping = true;
}
virtual void update(){
if( velX < 0 && x < -w ) x = 800;
if( velX > 0 && x > 800 ) x = -w;
velY += 10;
if( alive ){
if( velY > 0 ){
jumping = true;
for( auto it=blocks.begin(); it != blocks.end(); ++it ){
auto block=*it;
if(
block->y >= y+h-4 && block->y < y+velY+h
&& block->x+w*0.7f >= x && block->x <= x + w*0.7f
){
velY = 0;
jumping = false;
y = block->y - h;
// std::cout << "Points: " << maxPoint << " / " << y << std::endl;
if( maxPoint > y-398 ) maxPoint = y-398;
applyInput();
break;
}
}
if( jumping && y - cameraYTarget > 700 )
alive = false;
if( !jumping && y - cameraYTarget < 100 )
cameraYTarget -= 400;
}
} else {
cameraYTarget = y;
if( game->cameraY > 800 ) game->gameOver = true;
}
game->cameraY -= (game->cameraY - cameraYTarget) * 0.3;
z = maxZ-1;
Sprite::update();
}
void applyInput(){
if( keys[XK_Left] ){
velX = -walkSpeed;
mirror = true;
if( !jumping ) stateRun(true);
} else if( keys[XK_Right] ){
velX = walkSpeed;
mirror = false;
if( !jumping ) stateRun(false);
} else {
velX *= 0.5;
}
if( !jumping ){
if(velX==0 && velY==0){
stateIdle();
}
if( keys[XK_space] ){
stateJump();
}
}
}
void onKeyDown( int key ){
if( key == XK_Escape ) game->running = false;
if( !alive ) return;
keys[key] = true;
applyInput();
}
void onKeyUp( int key ){
if( !alive ) return;
keys[key] = false;
if( !jumping ) applyInput();
}
};
void create(XGame *game){
Sprite *bg = new Sprite(game);
bg->texture = game->loadTexture("bg.png");
bg->z = 0x7FFFFFFF;
bg->fixed = true;
cameraYTarget = 0;
game->cameraY = game->cameraX = 0;
Block *b = new Block(game);
int blockTex = b->texture = game->loadTexture("castle.png");
b->tx = b->x = 70*6;
b->ty = b->y = 70*7;
for( int i=0; i<15; ++i ){
b = new Block(game);
b->texture = blockTex;
b->tx = b->x = rand()%10*70;
b->ty = rand()%8*70;
b->y = - 70 - rand()%10*70;
}
new Player(game);
}
int main(){
srand(time(NULL));
try{
auto game = std::unique_ptr<XGame>( XGame::create() );
game->init();
game->fps = 24;
game->initScene = create;
game->run();
}catch( XGameError xge ){
std::cout << "ERROR: " << xge.message << std::endl;
return -1;
}
std::cout << "Pontuacao maxima: " << -maxPoint << std::endl;
return 0;
}