-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.cpp
389 lines (307 loc) · 11.8 KB
/
Game.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
#include "SFML/Graphics.hpp"
#include"SFML/Window.hpp"
#include "SFML/Audio.hpp"
#include<chrono>
#include"World.h"
#include"gameassets.h"
#include"PC.h"
#include"Animation.h"
#include"texturecodex.h"
#include"map.h"
#include"loadingscr.h"
#include<thread>
#include<mutex>
#define collision_down 80
#define collision_up 80
float xmax = 800;
float ymax = 512;
//////////////////////////////////////////////////////////////////////////////////
//// MAIN GAME FUNCTION
//////////////////////////////////////////////////////////////////////////////////
int main(std::string map)
{
///////////////////////////////////////
/// Window Render
///////////////////////////////////////
sf::RenderWindow window(sf::VideoMode(800,512), "Adventures of Joy");
window.setActive(false);
//////////////////////////////////////////////////////////////////////////////
/// Loading screen
//////////////////////////////////////////////////////////////////////////////
loadingscr winload(window);
std::thread loading(&loadingscr::screen, &winload);
loading.detach();
/////////////////////////////////////////////
/// Load map assets, set rain flag
/// Initialize pause flag
/////////////////////////////////////////////
sf::Vector2f slack; sf::FloatRect rekt;
bool pause = false;
mapdomain::map mapdata("game/map/map_02");
bool rainbool=mapdata.rain;
///////////////////////////////////////
/// Load Animation
///////////////////////////////////////
Animation pendulum(0,0,32,96, 0.500f, pendulum_tiles, fsystem::animation_path);
Animation furnace(0,96,32,32, 0.175f, furnace_tiles, fsystem::animation_path);
Animation pot(0,128,32,32, 0.175f, pot_tiles, fsystem::animation_path);
Animation fountain(0,160,64,64, 0.200f, fountain_tiles, fsystem::animation_path);
Animation water(0,224,32,32, 0.150f, water_tiles, fsystem::animation_path);
Animation rain(0,0, 200, 128, 0.175f, rain_tiles, fsystem::rain_path);
Animation waterfall(0,256,96,192, 0.200f, waterfall_tiles, fsystem::animation_path);
Animation waterfall_small(0,448,96,160, 0.200f, waterfall_tiles, fsystem::animation_path);
Animation torch(0, 608,32,64,0.100f, torch_tiles, fsystem::animation_path);
Animation grass(0,672,32,32,1.000f, grass_tiles, fsystem::animation_path);
//////////////////////////////////////
/// Load sounds
//////////////////////////////////////
sf::SoundBuffer buff;
buff.loadFromFile(fsystem::bump);
sf::Sound bump;
bump.setBuffer(buff);
//////////////////////////////////////
/// Textures loading
//////////////////////////////////////
sf::Texture _layer1, _layer2, _layer3;
_layer1.loadFromFile(mapdata.layer[0].filename);
_layer2.loadFromFile(mapdata.layer[1].filename);
_layer3.loadFromFile(mapdata.layer[2].filename);
sf::Sprite layer[3];
layer[0].setTexture(_layer1);layer[1].setTexture(_layer2);layer[2].setTexture(_layer3);
//////////////////////////////////////
///Purging all cell buffer
//////////////////////////////////////
Texturecodex::MurderOrphans();
///////////////////////////////////////////
//// CHARACTER INITIALIZED
///////////////////////////////////////////
PC chara(sf::Vector2f((float)mapdata.entry.x*32,(float)mapdata.entry.y*32),fsystem::heroX);
///////////////////////////////////////
/// LOAD MUSIC
///////////////////////////////////////
sf::Music music;
music.openFromFile(mapdata.music);
music.setLoop(true);
music.setVolume(40);
//////////////////////////////////////////
/// Pause this thread now that all the
///resources are loaded
//////////////////////////////////////////
std::this_thread::sleep_for(std::chrono::milliseconds(4000));
window.setActive(true);
///////////////////////////////////////
/// PLAY MUSIC
///////////////////////////////////////
music.play();
///////////////////////////////////////////
///Time counter initialized
///////////////////////////////////////////
auto tp = std::chrono::steady_clock::now();
//////////////////////////////////////////
/// Game loop starts here
//////////////////////////////////////////
while(window.isOpen())
{
sf::Event e;
//////////////////////////////////
/// EVENT POLLING LOOP
//////////////////////////////////
while(window.pollEvent(e))
{
if(e.type == sf::Event::Closed)
window.close();
}
//////////////////////////////////
/// get dt
//////////////////////////////////
float dt;
const auto new_tp = std::chrono::steady_clock::now();
dt = std::chrono::duration<float>( new_tp - tp ).count();
tp = new_tp;
/////////////////////////////////////////////////////
/// Handle Input
/////////////////////////////////////////////////////
sf::Vector2f dir( 0.0f,0.0f );
if( sf::Keyboard::isKeyPressed( sf::Keyboard::Up ) )
{
sf::Vector2f pos = chara.returnpos();
slack = chara.nextpos(sf::Vector2f(0, -1.0), dt);
if(chara.collision_coordinates.y - dt*(collision_up) <= 0){chara.Update(-dt/2);bump. play();}
// prevent itp from spilling upwards , play a music
else if( ( (int)pos.y/32)*32 >= slack.y && (mapdata.layer[2].tile[(int)pos.x/32][(int)pos.y/32 - 1] > -1))
{chara.Update(-dt/2);bump. play();}
//If nothing happens, let him be free!
else dir.y -= 1.0f;
}
if( sf::Keyboard::isKeyPressed( sf::Keyboard::Down ) )
{
sf::Vector2f pos = chara.returnpos();
sf::Vector2i g = chara.returntile((int)pos.x, (int)pos.y, 0 , 1);
slack = chara.nextpos(sf::Vector2f(0, 1.0), dt);
// stop chara from falling off the screen!
if(chara.collision_coordinates.y + dt*collision_up >= ymax){chara.Update(-dt/2);bump. play();}
// prevent it from spilling downwards
else if( ( (int)pos.y/32)*32 < slack.y
&& (mapdata.layer[2].tile[g.x][g.y] > -1 ))
{chara.Update(-dt/2);bump. play();}
//If nothing happens, let him be free!
else dir.y += 1.0f;
}
if( sf::Keyboard::isKeyPressed( sf::Keyboard::Left ) )
{
sf::Vector2f pos = chara.returnpos();
sf::Vector2i g = chara.returntile((int)pos.x, (int)pos.y, 0 , 0);
slack = chara.nextpos(sf::Vector2f(-1.0, 0), dt);
// stop chara from falling off the screen!
if(chara.collision_coordinates.x - dt*collision_down <= 0){chara.Update(-dt/2);bump. play();}
// prevent it from spilling leftward
else if( ( (int)pos.x/32)*32 < slack.x
&& (mapdata.layer[2].tile[g.x][g.y] > -1 ))
{chara.Update(-dt/2);bump. play();}
//If nothing happens, let him be free!
else dir.x -= 1.0f;
}
if( sf::Keyboard::isKeyPressed( sf::Keyboard::Right ) )
{
sf::Vector2f pos = chara.returnpos();
sf::Vector2i g = chara.returntile((int)pos.x, (int)pos.y, 1 , 0);
slack = chara.nextpos(sf::Vector2f(1.0, 0), dt);
// stop chara from falling off the screen!
if(chara.collision_coordinates.x + dt*collision_down >= xmax){chara.Update(-dt/2);bump. play();}
// prevent it from spilling rightward
else if( ( (int)chara.bottomright.x/32)*32 < slack.x
&& (mapdata.layer[2].tile[g.x][g.y] > -1 ))
{chara.Update(-dt/2);bump. play();}
//If nothing happens, let him be free!
else dir.x += 1.0f;
if( sf::Keyboard::isKeyPressed( sf::Keyboard::Escape ) )
{
//menu shit
//std::cout<<"Esc";
}
}
chara.SetDirection( dir );
// ------- IN CASE PAUSE, STOP UPDATE!!! --------
if(pause == false){
///////////////////////////////
/// Update
///////////////////////////////
chara.Update(dt);
water.update(dt);
fountain.update(dt);
pendulum.update(dt);
furnace.update(dt);
torch.update(dt);
grass.update(dt);
pot.update(dt);
waterfall.update(dt);
waterfall_small.update(dt);
///////////////////////////////
/// Rain - update
///////////////////////////////
if(rainbool == true)
{rain.update(dt);}
}
///////////////////////////////
/// Clear Window
///////////////////////////////
window.clear(sf::Color::Black);
///////////////////////////////
/// Draw Procedure
///////////////////////////////
// -- Starts here --
//////////////////////////////////////////////////////////////////////////////
/// Draw Layers 1 and 3
//////////////////////////////////////////////////////////////////////////////
///------------------------------Layer 1
for(int i=0;i<25;i++)
for(int j=0;j<16;j++)
{
if(mapdata.layer[0].tile[i][j] == -1){ continue;}
if(mapdata.layer[0].tile[i][j] == 0)
grass.draw(window, i*32, j*32);
else{
layer[0].setTexture(_layer1);
layer[0].setTextureRect(sf::IntRect((int)mapdata.layer[0].filecoords[i][j].x * 32, (int)mapdata.layer[0].filecoords[i][j].y * 32, 32, 32));
layer[0].setPosition((float)i*32, (float)j*32);
window.draw(layer[0]);
}
}
///-------------------------------Layer 3
for(int i=0;i<25;i++)
for(int j=0;j<16;j++)
{
if(mapdata.layer[2].tile[i][j] == -1)
continue;
switch(mapdata.layer[2].tile[i][j])
{
case 2:
furnace.draw(window,(int)mapdata.layer[2].filecoords[i][j].x * 32, (int)mapdata.layer[2].filecoords[i][j].y * 32);
break;
case 3:
pot.draw(window,(int)mapdata.layer[2].filecoords[i][j].x * 32, (int)mapdata.layer[2].filecoords[i][j].y * 32);
break;
case 4:
fountain.draw(window,(int)mapdata.layer[2].filecoords[i][j].x * 32, (int)mapdata.layer[2].filecoords[i][j].y * 32);
break;
case 5:
water.draw(window,(int)mapdata.layer[2].filecoords[i][j].x * 32, (int)mapdata.layer[2].filecoords[i][j].y * 32);
break;
case 8:
torch.draw(window,(int)mapdata.layer[2].filecoords[i][j].x * 32, (int)mapdata.layer[2].filecoords[i][j].y * 32);
break;
default:
if(mapdata.layer[2].tile[i][j] == -1) break;
layer[2].setTexture(_layer3);
layer[2].setTextureRect(sf::IntRect((int)mapdata.layer[2].filecoords[i][j].x * 32,
(int)mapdata.layer[2].filecoords[i][j].y * 32, 32, 32));
layer[2].setPosition((float)i*32, (float)j*32);
window.draw(layer[2]);
}
}
//////////////////////////////////////////
/// Layer 1+3 -> chara -> Layer 2 + anims
//////////////////////////////////////////
chara.Draw(window);
////////////////////////////////////////////////////////////////////////////
/// Draw Layer - 2
////////////////////////////////////////////////////////////////////////////
for(int i=0;i<25;i++)
for(int j=0;j<16;j++)
{
if(mapdata.layer[1].tile[i][j] == -1)
continue;
layer[1].setTexture(_layer2);
layer[1].setTextureRect(sf::IntRect((int)mapdata.layer[1].filecoords[i][j].x * 32,
(int)mapdata.layer[1].filecoords[i][j].y * 32, 32, 32));
layer[1].setPosition((float)i*32, (float)j*32);
window.draw(layer[1]);
}
//////////////////////////////////////////////////////////
//// ADDS WATERFALL SMALL AND BIG ANIMATIONS
//// IF ANY FOUND
//////////////////////////////////////////////////////////
for(int i =0;i<25;i++)
for(int j=0;j<16;j++)
{
if(mapdata.layer[2].tile[i][j] == 6)
waterfall.draw(window, (int)mapdata.layer[2].filecoords[i][j].x,(int)mapdata.layer[2].filecoords[i][j].y);
if(mapdata.layer[2].tile[i][j] == 7)
waterfall_small.draw(window, (int)mapdata.layer[2].filecoords[i][j].x,(int)mapdata.layer[2].filecoords[i][j].y);
}
///////////////////////////////
/// RAIN!!!!!!!
///////////////////////////////
if(rainbool == true)
for(int i = 0; i<4; i++)
for(int j = 0; j < 4; j++)
{
rain.draw(window, i*200, j*128);
}
////////////////////////////////////////////
/// Display and purge cell buffers
////////////////////////////////////////////
window.display();
}
return EXIT_SUCCESS;
}