This repository was archived by the owner on Nov 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlocal.cpp
More file actions
148 lines (121 loc) · 3.75 KB
/
local.cpp
File metadata and controls
148 lines (121 loc) · 3.75 KB
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
#include <Game.hpp>
namespace Game {
GameProcess* GameProcess::gp = nullptr;
unsigned int GameProcess::money = 30;
Sound GameProcess::open_sound = Sound("open.ogg");
Sound GameProcess::click_sound = Sound("click.ogg");
Music GameProcess::theme_music = Music("default.ogg");
void GameProcess::execute() {
this->restart();
while(this->isRunning()) {
Time elapsed = this->clock.getElapsedTime();
Event event;
while(window.pollEvent(event)) {
switch(event.type) {
case Event::Closed:
this->window.close();
break;
case Event::Resized:
break;
case Event::KeyPressed:
Input::press(event.key.code);
break;
case Event::KeyReleased:
Input::release(event.key.code);
break;
case Event::MouseButtonPressed:
Mouse::press(event.mouseButton.button);
break;
case Event::MouseButtonReleased:
Mouse::release(event.mouseButton.button);
break;
default:
break;
};
};
if((this->frame)/60 <= elapsed.asSeconds()) this->nextFrame();
bool pause_requested = Input::isPressed(Keyboard::Escape);
if(
pause_requested && this->paused &&
(this->in("PauseMenu") || this->in("ResolutionMenu"))
) this->resume();
else if(pause_requested && this->menu == nullptr) this->pause(false);
if(this->paused && GameProcess::theme_music.getStatus() != Music::Paused)
GameProcess::theme_music.pause();
else if(!this->paused && GameProcess::theme_music.getStatus() == Music::Paused)
GameProcess::theme_music.play();
Input::update();
Mouse::update();
};
this->clear();
};
void GameProcess::clear() {
this->restarted = true;
this->menu = nullptr;
for(unsigned int i = 0; i < this->objects.length(); i++) {
this->objects.get(i)->free();
};
this->objects.clear();
this->queue_free.clear();
};
bool GameProcess::isRunning() {
return this->window.isOpen();
};
void GameProcess::nextFrame() {
if(this->restarted && this->objects.length() == 0) {
Background::create();
Spawn::create();
Base::create();
Player::create();
Interface::create();
this->restarted = false;
};
if(this->frame < 60) this->frame++;
else {
this->frame = 0;
this->clock.restart();
};
this->window.clear();
if(this->frame_instances_amount != this->objects.length() || !this->objects.isSorted()) {
GameProcess::sort();
this->frame_instances_amount = this->objects.length();
};
for(unsigned int i = 0; i < this->frame_instances_amount; i++) {
Object* object = this->objects.get(i);
if(!object->isPausable()) object->step();
else if(!this->paused) {
object->collision();
object->step();
};
if(this->restarted) return;
if(object->visible) object->draw();
};
this->window.display();
for(unsigned int i = 0; i < this->queue_free.length(); i++) {
Object* object = this->queue_free.get(i);
string type = object->type();
this->objects.remove(object);
if(
(type == "Player" || type == "Base") &&
!GameProcess::in("DefeatMenu")
) GameProcess::defeat();
object->free();
};
this->queue_free.clear();
};
GameProcess::~GameProcess() {
Sprites::clear();
this->clear();
this->gp = nullptr;
};
GameProcess::GameProcess() {
if(this->gp != nullptr) {
delete this->gp;
};
srand(time(NULL));
this->gp = this;
this->window.setFramerateLimit(60);
GameProcess::theme_music.setVolume(30);
GameProcess::theme_music.play();
};
};