-
Notifications
You must be signed in to change notification settings - Fork 1
/
Game.cc
181 lines (140 loc) · 5.22 KB
/
Game.cc
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
#include <iostream>
#include <random>
#include <gf/Event.h>
#include <gf/Font.h>
#include <gf/Window.h>
#include <gf/RenderWindow.h>
#include <gf/Sprite.h>
#include <gf/Text.h>
#include <gf/TextureAtlas.h>
#include <gf/ResourceManager.h>
#include <gf/ViewContainer.h>
#include <gf/Views.h>
#include <gf/Time.h>
#include "bits/Terrain.h"
#include "bits/Ballon.h"
#include "bits/Equipe.h"
#include "bits/Physics.h"
int main(int argc, char* argv[]) {
bool debug = false;
if(argc == 2 && (std::string(argv[1]) == "--debug" || std::string(argv[1]) == "-d")){
printf("%s", argv[1]);
debug = true;
}
// Create the main window and the renderer
gf::Window window("Futsall", { 1024, 768 });
window.setVerticalSyncEnabled(true);
window.setFramerateLimit(60);
gf::RenderWindow renderer(window);
window.setFullscreen();
gf::ViewContainer views;
gf::ExtendView view(gf::vec(0, 0), gf::vec(GROUND_LENGTH, GROUND_HEIGH) * 70.0f);
views.addView(view);
view.setInitialFramebufferSize({ 1024, 768 });
gf::Text score;
int scoreLeft = 0;
int scoreRight = 0;
/*(ref 2. update)*/
gf::Clock clock;
gf::Clock chronometre;
renderer.clear(gf::Color::Black); //nettoyer l'écran avant de commencer
gf::ResourceManager resources;
resources.addSearchDir("data");
gf::TextureAtlas atlas("futsal_spritesheet.xml", resources);
Terrain terrain(atlas, resources, debug);
terrain.setData();
Ballon ballon(resources, debug);
Equipe equipeLeft = Equipe(true, debug);
equipeLeft.addJoueur(0, 1, resources);
equipeLeft.addJoueur(1, 2, resources);
equipeLeft.addJoueur(1, 1, resources);
equipeLeft.addJoueur(1, 2, resources);
equipeLeft.addJoueur(2, 2, resources);
equipeLeft.switchCurrentToClosest(ballon);
Equipe equipeRight = Equipe(false, debug);
equipeRight.addJoueur(0, 0, resources);
equipeRight.addJoueur(1, 1, resources);
equipeRight.addJoueur(1, 2, resources);
equipeRight.addJoueur(1, 1, resources);
equipeRight.addJoueur(2, 2, resources);
equipeRight.switchCurrentToClosest(ballon);
Physics physic(ballon, equipeLeft, equipeRight, terrain);
gf::Text chronoText;
gf::Text scoreLeftText, scoreRightText;
// Start the game loop
while (window.isOpen()){
if(chronometre.getElapsedTime().asSeconds() > 5*60){
window.close();
}
// Process events
gf::Event event;
// 1. recevoir les actions du joueur
while (window.pollEvent(event)){ //reception action sur clavier
equipeLeft.deplacement(event);
equipeRight.deplacement(event);
switch (event.type){
case gf::EventType::Closed: //fermeture de la fenetre
window.close();
break;
case gf::EventType::KeyPressed: //touche appuyé
switch (event.key.keycode){
case gf::Keycode::Escape:
window.close();
break;
case gf::Keycode::Tab:
equipeLeft.switchCurrentToClosest(ballon);
break;
case gf::Keycode::Space:
equipeRight.switchCurrentToClosest(ballon);
break;
default:
break;
}
break;
default:
break;
}
//equipeRight.isDefending(ballon, terrain);
views.processEvent(event);
}
// 2. update 60 fois MINIMUM
gf::Time ips = clock.restart();
equipeLeft.update(ips);
equipeRight.update(ips);
physic.collisionEquipeEquipe();
physic.collisionsEquipeTerrain();
physic.collisionBallonEquipe();
physic.collisionsBallonTerrain();
if(physic.ballInLeftGoal()){
scoreRight++;
equipeLeft.engagement(true);
equipeRight.engagement(false);
ballon.engagement();
equipeLeft.switchCurrentToClosest(ballon);
equipeRight.switchCurrentToClosest(ballon);
std::cout << "Équipe Gauche: " << scoreLeft << "\tÉquipe droite: " << scoreRight << std::endl;
}
if(physic.ballInRightGoal()){
scoreLeft++;
equipeLeft.engagement(false);
equipeRight.engagement(true);
ballon.engagement();
equipeLeft.switchCurrentToClosest(ballon);
equipeRight.switchCurrentToClosest(ballon);
std::cout << "Équipe Gauche: " << scoreLeft << "\tÉquipe droite: " << scoreRight << std::endl;
}
chronoText.setString(std::to_string(chronometre.getElapsedTime().asSeconds()));
scoreLeftText.setString(std::to_string(scoreLeft));
scoreRightText.setString(std::to_string(scoreRight));
ballon.update(ips);
// 3. Draw the entities
renderer.setView(view);
renderer.clear();
terrain.render(renderer);
ballon.render(renderer);
equipeLeft.render(renderer);
equipeRight.render(renderer);
renderer.display();
}
return 0;
}