-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.cpp
More file actions
170 lines (146 loc) · 4.37 KB
/
Game.cpp
File metadata and controls
170 lines (146 loc) · 4.37 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include "Game.h"
#include "Utils/Converter.h"
#include <iostream>
#include <thread>
#include <chrono>
#include <unistd.h>
#include <termios.h>
#define OK 0
#define ERROR -1
#define P1_SCORE 10
#define P2_SCORE 20
using namespace Toolbox;
Game::Game() : Game(64, 16)
{
}
Game::Game(int width, int height, std::string player1Name, std::string player2Name)
: scoreboard(Scoreboard(width, height))
{
field = Field(width, height);
ball = Ball();
player1 = Player(player1Name);
player2 = Player(player2Name);
scoreboard.AddEntry(player1.GetName(), 0);
scoreboard.AddEntry(player2.GetName(), 0);
}
#pragma region static functions
struct termios old_tio, new_tio;
static void __setup_console()
{
/* get the terminal settings for stdin */
tcgetattr(STDIN_FILENO, &old_tio);
/* we want to keep the old setting to restore them a the end */
new_tio = old_tio;
/* disable canonical mode (buffered i/o) and local echo */
new_tio.c_lflag &= (~ICANON & ~ECHO);
/* set the new settings immediately */
tcsetattr(STDIN_FILENO, TCSANOW, &new_tio);
}
static void __restore_console()
{
/* restore the former settings */
tcsetattr(STDIN_FILENO, TCSANOW, &old_tio);
}
#pragma endregion
void Game::Start()
{
__setup_console();
field.PlaceObject(ball, {field.GetWidth() / 2, field.GetHeight() / 2});
field.PlaceObject(player1, {1, field.GetHeight() / 2});
field.PlaceObject(player2, {field.GetWidth() - 2, field.GetHeight() / 2});
ball.State = MovingState::GoLeft;
auto input = '-';
auto exit = OK;
auto io_thread = std::thread([&] {
while (exit == OK && std::cin >> input);
});
exit = __game_loop(input);
system("clear");
switch (exit)
{
case P1_SCORE:
std::wcout << Converter::StringToWString(player1.GetName()) << " WIN!" << std::endl;
scoreboard.IncreaseCount(player1.GetName());
break;
case P2_SCORE:
std::wcout << Converter::StringToWString(player2.GetName()) << " WIN!" << std::endl;
scoreboard.IncreaseCount(player2.GetName());
break;
case ERROR:
std::wcout << L"Game crashed (" << exit << ")" << std::endl;
__restore_console();
return;
}
scoreboard.Draw();
io_thread.detach();
__restore_console();
}
Action Game::HandlePlayerAction(char &input)
{
switch (input)
{
case 'w': //p1 up
if (field.CanMove(player1, Up)) field.Move(player1, Up);
input = '?';
return MOVE;
case 's': //p1 down
if (field.CanMove(player1, Down)) field.Move(player1, Down);
input = '?';
return MOVE;
case '8': //p2 up
if (field.CanMove(player2, Up)) field.Move(player2, Up);
input = '?';
return MOVE;
case '5': //p2 down
if (field.CanMove(player2, Down)) field.Move(player2, Down);
input = '?';
return MOVE;
case 'q':
input = 'q';
return ESC;
}
return NONE;
}
int Game::__game_loop(char &input)
{
auto action = NONE;
do
{
switch (ball.State)
{
case GoDown:
if (field.CanMove(ball, Down))
field.Move(ball, Down);
else
ball.State = MovingState::GoUp;
break;
case GoUp:
if (field.CanMove(ball, Up))
field.Move(ball, Up);
else
ball.State = MovingState::GoDown;
break;
case GoLeft:
if (field.CanMove(ball, Left))
field.Move(ball, Left);
else
ball.State = MovingState::GoRight;
break;
case GoRight:
if (field.CanMove(ball, Right))
field.Move(ball, Right);
else
ball.State = MovingState::GoLeft;
break;
}
if (field.IsTouchingLeftBoundary(ball))
return P2_SCORE;
if (field.IsTouchingRightBoundary(ball))
return P1_SCORE;
action = HandlePlayerAction(input);
system("clear");
field.Draw();
std::this_thread::sleep_for(std::chrono::milliseconds(20));
} while (action != ESC);
return OK;
}