Repository navigation
Expand file tree
/
Copy pathDirector.cpp
More file actions
117 lines (106 loc) · 3.21 KB
/
Copy pathDirector.cpp
File metadata and controls
117 lines (106 loc) · 3.21 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
#include "Director.h"
#include "Textor.h"
#include "Game.h"
#include "Player.h"
#include "Controller.h"
#include "StartMenu.h"
#include "Lobby.h"
#include "Server.h"
Director::Director(sf::RenderWindow* window_, OSHandler* osHandler_) {
window = window_;
osHandler = osHandler_;
textor = new Textor(osHandler);
window->setKeyRepeatEnabled(false);
inGame = false;
inLobby = false;
ipAddress = "";
portAddress = "";
game = new Game(window_, osHandler_);
game->setDirector(this);
lobby = new Lobby(game, this);
state = START;
joinState = IP;
a = nullptr;
p = 0;
startMenu = new StartMenu(game);
controller = new Controller(game, this);
}
void Director::run() {
update();
draw();
}
void Director::draw() {
switch(state) {
case START:
break;
case LOBBY:
lobby->draw();
break;
case JOIN:
if(joinState == IP) {
window->draw(textor->write("ENTER HOST'S IP ADDRESS",
sf::Vector2f(window->getSize().x/2, 250)));
window->draw(textor->write(ipAddress,
sf::Vector2f(window->getSize().x/2, 350)));
}
else if(joinState == PORT) {
window->draw(textor->write("ENTER HOST'S PORT",
sf::Vector2f(window->getSize().x/2, 250)));
window->draw(textor->write(portAddress,
sf::Vector2f(window->getSize().x/2, 350)));
}
break;
default: ;
}
}
void Director::update() {
switch(state) {
case START:
startMenu->run();
controller->checkInput();
if(game->getContactListener()->getHostGame()) {
cout << "HOOKED HOST GAME" << endl;
delete startMenu;
joinAs = SERVER;
state = LOBBY;
}
else if(game->getContactListener()->getJoinGame()) {
cout << "HOOKED JOIN GAME" << endl;
delete startMenu;
joinAs = CLIENT;
state = JOIN;
}
break;
case LOBBY:
if(!inLobby) {
if(joinAs == SERVER) {
lobby->init(SERVER);
}
else if(joinAs == CLIENT) {
lobby->init(CLIENT, new sf::IpAddress(ipAddress), atoi(portAddress.c_str()));
}
inLobby = true;
}
controller->checkInput();
lobby->update();
break;
case JOIN:
controller->checkInput();
break;
case GAME:
{
if(joinAs == SERVER) {
sf::Packet packet = game->getPacketParser()->pack(UDPNetwork::GAME_STARTED);
dynamic_cast<Server*>(lobby->getLocalHost())->broadCast(packet);
}
if(!inGame) {
game->init();
inGame = true;
}
game->run();
controller->checkInput();
}
break;
default: ;
}
}