-
Notifications
You must be signed in to change notification settings - Fork 0
/
TerminalEngine.cpp
230 lines (194 loc) · 7.71 KB
/
TerminalEngine.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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#include "TerminalEnginge.h"
#include "GameObject.h"
#include "Player.h"
#include "Enemy.h"
#include "Gun.h"
#include "Utils.h"
#include <algorithm>
#include <bitset>
short timer = 0;
short visObjectCount = 0;
short prevPrintMsgLength = 0;
short ext_scrW = 0;
short ext_scrH = 0;
TerminalEngine::TerminalEngine() {
CONSOLE_SCREEN_BUFFER_INFO csbi;
if (!GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi)) {
std::cout << "GetConsoleScreenBufferInfo error: " << GetLastError() << std::endl;
}
scr_W = csbi.srWindow.Right - csbi.srWindow.Left + 1;
scr_H = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
ext_scrH = scr_H;
ext_scrW = scr_W;
hConsole = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE, 2, NULL, CONSOLE_TEXTMODE_BUFFER, NULL);
SetConsoleActiveScreenBuffer(hConsole);
charsWritten = 0;
}
void TerminalEngine::init() {
timer = 0;
canvas = new wchar_t[scr_W*scr_H]();
scene.createPlayer();
visibleObjects = scene.get_PtrVisibleGameObjects();
}
void TerminalEngine::doEvents() {
// tutaj musi byc sprawdzane sprawdzanie size(), bo tu ciagle nastepuja zmiany w rozmiarze.
// Jeszcze na szybko testowalem z updatowaniem zmiennej recznie po scene.purge() i po usuwaniu normalnie obiektow.
// Pewnie gdzies tutaj jest vector subscript out of range error, ale nie mam VS, zeby to sprawdzic,
// a CodeBlocks nie ma debuggera w sobie i nie chce mi sie go instalowac
for(int i = 0; i < visibleObjects->size(); i++) {
VisualGameObject* obj = (*visibleObjects)[i];
if(obj->getDetailedName() == "Player" && obj->isDead())
{
scene.purge();
return;
}
if(obj->isDead()) {
delete obj;
visibleObjects->erase(visibleObjects->begin() + i);
continue;
}
if(obj->getDetailedName() == "Enemy") {
Enemy* enemy = (Enemy*)obj->refer();
enemy->shoot(visibleObjects);
}
}
if(timer % 75 == 0) {
scene.createEnemy();
}
}
void TerminalEngine::placePatternsOnCanvas() {
int visibleObjCount = visibleObjects->size();
for (int i = 0; i < visibleObjCount; i++) {
VisualGameObject* currentObject = (*visibleObjects)[i];
std::array<short, 2> objectCoordinates = currentObject->getPos();
short coord_1D = Utils::coord2DTo1D(objectCoordinates[0], objectCoordinates[1], scr_W);
std::wstring objectPattern = currentObject->getPattern();
size_t objectPatternLength = objectPattern.length();
std::array<short, 2> objectSizeXY = currentObject->getSize();
short rowCount = 0;
short colCount = 0;
// primitive clearing before rendering, so the terminal isn't gonna be cluttered with mess
// will make a better version if i'll have time
for (int Y = 0; Y <= objectSizeXY[1] + 1; Y++) {
for (int X = 0; X <= objectSizeXY[0] + 1; X++) {
canvas[coord_1D - scr_W - 1 + X + (scr_W * Y)] = L' ';
}
}
std::string currObjName = currentObject->getDetailedName();
if (currObjName == "Player" || currObjName == "Enemy") {
std::string healthDisplay = "|" + std::to_string(currentObject->getHealth()) + " HP|";
Utils::displayText2D(healthDisplay, canvas, objectCoordinates[0] + (objectSizeXY[0] / 2) - 2, objectCoordinates[1] - 1, scr_W);
}
if (currentObject->isDead()) {
std::string spaces(objectSizeXY[0] + 2, ' ');
Utils::displayText2D(spaces, canvas, objectCoordinates[0] - 2, objectCoordinates[1], scr_W);
continue;
}
for (size_t i = 0; i < objectPatternLength; i++) {
if (objectPattern[i] != L'`') {
canvas[coord_1D + rowCount + (scr_W * colCount)] = objectPattern[i];
rowCount++;
}
else
{
colCount++;
rowCount = 0;
}
}
}
}
void TerminalEngine::clearCanvas() {
for (int Y = 0; Y < scr_H; Y++) {
for (int X = 0; X < scr_W; X++) {
canvas[Utils::coord2DTo1D(X, Y, scr_W)] = L' ';
}
}
draw(false);
}
void TerminalEngine::handleKeys() {
playerControls = 0;
if (GetAsyncKeyState('W') & 0x8000) playerControls |= GameObject::controls::UP;
if (GetAsyncKeyState('A') & 0x8000) playerControls |= GameObject::controls::LEFT;
if (GetAsyncKeyState('S') & 0x8000) playerControls |= GameObject::controls::DOWN;
if (GetAsyncKeyState('D') & 0x8000) playerControls |= GameObject::controls::RIGHT;
if (GetAsyncKeyState(VK_SPACE) & 0x8000) playerControls |= GameObject::controls::SHOOT;
scene.forwardPlayerActions(playerControls);
}
void TerminalEngine::handleMovement(){
short direction = 0;
for(VisualGameObject* obj : *visibleObjects) {
if (obj->getDetailedName() == "Bullet") obj->move(direction, 1);
if (obj->getDetailedName() == "Enemy") obj->move(direction, 5);
}
// std::bitset<16> binDirection(playerControls);
// std::string debugStr = "Controls: " + binDirection.to_string() + " | Objects: " + std::to_string(visObjectCount);
//
// Utils::displayText2D(debugStr, canvas, 0, 1, scr_W);
}
void TerminalEngine::handleCollisions() {
short visibleObjCount = visibleObjects->size();
if (visibleObjCount == 0) return;
// Get player, enemies and bullets
std::vector<VisualGameObject*> shipsToCheck;
std::vector<Bullet*> bulletsChecking;
shipsToCheck.emplace_back((*visibleObjects)[0]);
for (int i = 0; i < visibleObjCount; i++) {
VisualGameObject* obj = (*visibleObjects)[i];
std::string objName = obj->getDetailedName();
std::array<short, 2> obj_XY = obj->getPos();
std::array<short, 2> objSize = obj->getSize();
if (obj_XY[0] > scr_W - objSize[0])
{
obj->setPos(obj_XY[0] - 1, obj_XY[1]);
if (objName == "Bullet") { obj->dealDamage(100); continue; }
}
if (obj_XY[0] < 0)
{
obj->setPos(obj_XY[0] + 1, obj_XY[1]);
if (objName == "Enemy") {
obj->dealDamage(100);
shipsToCheck[0]->dealDamage(25); continue;
}
if (objName == "Bullet") { obj->dealDamage(100); continue; }
}
if (obj_XY[1] >= scr_H - objSize[1]) obj->setPos(obj_XY[0], obj_XY[1] - 1);
if (obj_XY[1] <= 0) obj->setPos(obj_XY[0], obj_XY[1] + 1);
if (objName == "Enemy") shipsToCheck.emplace_back(obj);
if (objName == "Bullet") bulletsChecking.emplace_back((Bullet*)obj->refer());
}
// "ask" a bullet whether is it colliding with a player/enemy
// and deal damage if it's indeed colliding
for (Bullet* bullet : bulletsChecking) {
for(VisualGameObject* ship : shipsToCheck) {
bullet->tryDealingDamage(ship);
}
}
}
void TerminalEngine::tick() {
timer++;
if (timer >= 100) { timer = 0; }
visObjectCount = visibleObjects->size();
handleKeys();
handleMovement();
handleCollisions();
draw(true);
doEvents();
// death
if (visObjectCount == 0) {
bool keyPressed = false;
clearCanvas();
std::string urDead = "Your ship has been destroyed. Press [ENTER] to restart";
Utils::displayText2D(urDead, canvas, scr_W/2 - urDead.length()/2 - 1, scr_H/2 - 1, scr_W); draw(false);
while(!keyPressed) {
Sleep(1);
if (GetAsyncKeyState(VK_RETURN) & 0x8000) {
init();
return;
}
}
}
}
void TerminalEngine::draw(bool in_WithPatterns) {
if (in_WithPatterns) placePatternsOnCanvas();
WriteConsoleOutputCharacterW(hConsole, canvas, scr_H*scr_W, { 0,0 }, &charsWritten);
}