-
Notifications
You must be signed in to change notification settings - Fork 0
/
State.cpp
142 lines (118 loc) · 2.78 KB
/
State.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
#include <cstring>
#include <cstdlib>
#include <iostream>
#ifdef __linux__
#include <unistd.h>
#endif
#include "State.hpp"
#include "Graphics.hpp"
#include "Snake.hpp"
#include "Wall.hpp"
#include "Fruit.hpp"
bool
State::pause = true,
State::running = false;
int
State::latency = 40,
State::latency_delta = 0,
State::skin_id = 0,
State::width = 50,
State::height = 50;
double
State::x_Display = double(WIDTH) * 1.4,
State::y_Display = double(HEIGHT) * 0.5;
std::string
State::folder = "";
Object
*WALLS,
*FRUITS,
*SNAKE;
#ifdef __linux__
static std::string get_executable_path() {
std::vector<char> buf(1000);
readlink("/proc/self/exe", buf.data(), 1000);
return std::string(buf.begin(), buf.end());
}
#endif
void State::Init(const int &argc, char **argv) {
WIDTH = (argc >= 3) ? atoi(argv[1]) : 50;
HEIGHT = (argc >= 3) ? atoi(argv[2]) : 50;
#ifdef __linux__
const std::string exec = get_executable_path();
#else
const std::string exec = argv[0];
#endif
std::string::size_type n = exec.rfind('/');
if(n == std::string::npos) {
folder = "./";
} else {
folder = exec.substr(0, n + 1);
}
x_Display = WIDTH * 1.4;
y_Display = HEIGHT + 0.5;
}
void State::Keyboard(char key) {
switch(key) {
case 27 :
Graphics::Quit();
break;
case 'p' :
pause = !pause;
break;
case 'r' :
running = !running;
break;
case 'o' :
if(latency_delta > 0)
latency_delta = 0;
latency = (latency > (abs(--latency_delta) >> 3) + 1) ? (latency - ((abs(latency_delta) >> 3) + 1)) : 0;
break;
case 'i' :
if(latency_delta < 0)
latency_delta = 0;
latency += ((++latency_delta) >> 3) + 1;
break;
}
if(strchr("0123456789", key)) {
latency = (key == '0') ? 40 : 0;
++skin_id %= NO_ICONSETS;
}
SNAKE->Keyboard(key);
if(running)
SSNAKE->DoStep();
WALLS->Keyboard(key);
FRUITS->Keyboard(key);
}
void State::Display() {
char text[20];
glColor3f(1.0f, 1.0f, 1.0f);
sprintf(text, "Latency: %d", State::latency);
Graphics::DisplayText(WIDTH + 1, HEIGHT * 0.89, text);
glColor3f(0.3f, 0.3f, 1.0f);
if(State::pause) {
sprintf(text, "[Paused]");
Graphics::DisplayText(WIDTH * 0.45, HEIGHT * 0.75, text);
}
glColor3f(1.0f, 0.3f, 0.3f);
if(State::running) {
sprintf(text, "[Running]");
Graphics::DisplayText(WIDTH * 0.45, HEIGHT * 0.25, text);
}
}
void State::Timer() {
if(!State::pause) {
if(!State::running)
SSNAKE->DoStep();
if(
Ball::InSegment(SNAKE->GetObjects().front(), WALLS->GetObjects())
|| Ball::InSegment(SNAKE->GetObjects().front(), SNAKE->GetObjects(), Ball(1, SNAKE->GetObjects().size()))
)
{
std::cout << std::to_string(FFRUITS->frufru) << std::endl;
Graphics::Quit();
}
}
if(Ball::InSegment(SNAKE->GetObjects().front(), FRUITS->GetObjects()))
FFRUITS->EatFruit();
SSNAKE->SetStep(SSNAKE->GetAutoStep());
}