-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.cpp
146 lines (127 loc) · 5.29 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
#include <iostream>
#include <SFML/Graphics.hpp>
#include <types.hpp>
#include <networking/network_client.hpp>
void gameMovePad(networking::network_client &client, const bool &wPressed, const bool &sPressed) {
double movePad = -wPressed + sPressed;
client.write_message(networking::message(MovePad, networking::make_packet(movePad)));
}
void start(networking::network_client &client, const std::string &address) {
std::cout << "Connecting to " << address << " :3\n";
client.start(address, 25565);
if (!client.running()) {
std::cout << "Failed to connect to " << address << " v.v\n";
return;
}
std::cout << "Connected to " << address << " :3\n";
}
enum InputType {
Mouse, Keyboard, InputCount
};
int main(int argc, char * const*argv) {
Game game;
sf::Font openSans;
if (!openSans.loadFromFile("./assets/OpenSans.ttf")) {
std::cerr << "Failed to load font\n";
}
sf::Text player1ScoreText{openSans, "0", 30}, player2ScoreText{openSans, "0", 30};
player1ScoreText.setPosition({800 / 5. - 10, 35});
player2ScoreText.setPosition({800 / 5. * 4 - 10, 35});
int currentPlayer = 0;
bool gameStarted = false;
float mousePos = 0;
InputType input = Mouse;
networking::network_client client(netsock::tcp_client::create());
client.on_message(PlayerAssignment, [¤tPlayer](networking::message message) {
message.content() >> currentPlayer;
});
client.on_message(GameStart, [&gameStarted](const networking::message &message){
gameStarted = true;
});
client.on_message(GameEnd, [&gameStarted](const networking::message &message){
gameStarted = false;
});
client.on_message(GameUpdate, [&game, &mousePos, &input](networking::message message) {
message.content() >> game;
if (input == Mouse) {
double padPos = game.get_player(message.from()->connection_id())->pad.position.y;
double move = (mousePos - padPos) / 60.;
message.from()->write_message(networking::message(MovePad, networking::make_packet(move)));
return;
}
});
// client.on_message(Tick, [¤tPlayer, &player1PadPosition, &player2PadPosition, &mousePos](const networking::message &message) {
// float padPos = currentPlayer == 1 ? player1PadPosition : player2PadPosition;
// double move = (mousePos - padPos) / 60.;
// message.from()->write_message(networking::message(MovePad, networking::make_packet(move)));
// });
if (argc < 2)
start(client, "localhost");
else
start(client, argv[1]);
if (!client.running())
return -1;
sf::RenderWindow window{sf::VideoMode({800, 600}), "Pong!", sf::Style::Titlebar | sf::Style::Close};
sf::Event event{};
sf::Clock clock;
sf::RectangleShape ballShape({10, 10}), padShape({10, 80});
ballShape.setOrigin({5, 5});
padShape.setOrigin({5, 40});
sf::Text gameStartingText{openSans, "Game is starting, please wait...", 30};
gameStartingText.setPosition({200, 40});
bool wPressed = false, sPressed = false;
while (window.isOpen()){
while (window.pollEvent(event)){
if (event.type == sf::Event::Closed) window.close();
if (input == Keyboard) {
if (event.type == sf::Event::KeyPressed) {
if (event.key.code == sf::Keyboard::Key::W) {
wPressed = true;
gameMovePad(client, wPressed, sPressed);
}
if (event.key.code == sf::Keyboard::Key::S) {
sPressed = true;
gameMovePad(client, wPressed, sPressed);
}
}
if (event.type == sf::Event::KeyReleased) {
if (event.key.code == sf::Keyboard::Key::W) {
wPressed = false;
gameMovePad(client, wPressed, sPressed);
}
if (event.key.code == sf::Keyboard::Key::S) {
sPressed = false;
gameMovePad(client, wPressed, sPressed);
}
}
}
if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Key::I)
input = (InputType)((input + 1) % InputCount);
if (event.type == sf::Event::MouseMoved)
mousePos = (float)event.mouseMove.y;
}
if (!client.running()) {
window.close();
break;
}
while (client.read_message());
window.clear();
if (gameStarted) {
player1ScoreText.setString(std::to_string(game.scores[0]));
player2ScoreText.setString(std::to_string(game.scores[1]));
ballShape.setPosition({(float)game.ball.position.x, (float)game.ball.position.y});
for (const Player &player : game.players) {
padShape.setPosition({(float)player.pad.position.x, (float)player.pad.position.y});
window.draw(padShape);
}
window.draw(player1ScoreText);
window.draw(player2ScoreText);
window.draw(ballShape);
}else{
window.draw(gameStartingText);
}
window.display();
}
client.stop();
return 0;
}