-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
193 lines (147 loc) · 5.45 KB
/
main.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
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <ctime>
#include <cstdlib>
#include <thread>
#include "Utils.h"
#include "Player.h"
#include "Input.h"
#include "Camera2D.h"
#include "Bacterium.h"
using namespace std;
using namespace sf;
Utils tx;
Player pl;
Input in;
Event event;
LetterMap m(tx.W,tx.H,tx.S);
Camera2D c(tx.W,tx.H);
Entity *enemy = new Bacterium(m,20,1);
void update(RenderWindow &window);
int main(){
srand(static_cast<unsigned int>(time(NULL)));
RenderWindow window(VideoMode(tx.W,tx.H,32),tx.title,Style::None | Style::Close);
// window.setVerticalSyncEnabled(true);
// window.setFramerateLimit(120);
pair<int,int> matrixPos = m.getValidSpawn();
pair<int,int> enemyPos = m.getValidSpawn();
enemy->setRow(enemyPos.first);
enemy->setCol(enemyPos.second);
pl.setSpawn(matrixPos.second, matrixPos.first);
pl.respawn();
m.playerPos(matrixPos.first,matrixPos.second);
pl.setOnRoom(m.isPlayerOnRoom());
c.center(pl.getX(),pl.getY(),tx.S);
float oldZoom = 1.0F;
const Time delay = milliseconds(1000.0F/tx.FPS);
const Time step_delay = milliseconds(40.0F);
Clock clock,enemyClock;
clock.restart();
enemyClock.restart();
// if(!tx.setDefaultFont()) return EXIT_FAILURE;
enemy->targetPos(pl.getX(),pl.getY());
while (window.isOpen()){
while(window.pollEvent(event)){
in.update(event);
if (in.isEscape() || in.isClose(event)){
window.close();
break;
}
if (in.isSpace())
if (!pl.isPlaying())
pl.start(),clock.restart();
if(pl.isPlaying()){
// R
if (in.isKeyJustReleased(Keyboard::R))
pl.switchLight();
// W S
if(!in.isUp() && !in.isDown()) pl.setDirY(0.0F);
else if(in.isUp()) pl.setDirY(-1.0F);
else if(in.isDown()) pl.setDirY(+1.0F);
// A D
if(!in.isRight() && !in.isLeft()) pl.setDirX(0.0F);
else if(in.isRight()) pl.setDirX(+1.0F);
else if(in.isLeft()) pl.setDirX(-1.0F);
pl.incRay(in.mouseWheelScrool());
}
}
if(pl.isPlaying() && clock.getElapsedTime().asMilliseconds()>step_delay.asMilliseconds()){
int futureX = pl.getX() + pl.getDirX();
int futureY = pl.getY() + pl.getDirY();
// float deltaTime = fps.restart().asSeconds();
if(m.canGo(futureY,pl.getX())) {
clock.restart();
pl.setPos(pl.getX(),futureY);
m.playerPos(futureY,pl.getX());
}
if(m.canGo(pl.getY(),futureX)) {
clock.restart();
pl.setPos(futureX,pl.getY());
m.playerPos(pl.getY(),futureX);
}
//pl.setOnRoom(m.isPlayerOnRoom());
if(enemyClock.getElapsedTime().asMilliseconds() > milliseconds(70.0F).asMilliseconds()){
enemyClock.restart();
// enemy->searchTargetAround(pl.getY(),pl.getX());
enemy->searchTargetAround(pl.getX(),pl.getY());
//enemy->targetPos(pl.getX(),pl.getY());
// if(!enemy->nextStep()){
// //nothing
// }
// cout << " enemy pos ("<<enemy->getCol() << ";" << enemy->getRow() << ");\n";
// cout << " player pos ("<<pl.getY() << ";" << pl.getX() << ");\n";
}
enemy->slerpFollowPosition();
c.target(pl.getX(),pl.getY(),tx.S); // zoom is included in camera class
c.slerpFollow();
//c.zoomActived(pl.insideRoom(),deltaTime); // if is inside a room, zoom
//cout << "current zoom : " << c.getZoom() << "\n";
}
/*
if(pl.insideRoom() && oldZoom!=1.0F){
container.reset(rect_container);
container.zoom(.9F);
window.setView(container);
}else if(oldZoom!=1.0F){
container.reset(rect_container);
container.zoom(1.0F);
window.setView(container);
}*/
window.clear(Color::Black);
update(window);
window.display();
}
return EXIT_SUCCESS;
}
void drawHome(RenderWindow &window);
void drawDebug(RenderWindow &window);
void drawDisplay(RenderWindow &window);
void drawCrossHair(RenderWindow &window);
void update(RenderWindow &window){
if (pl.isPlaying()) drawDisplay(window);
else drawHome(window);
}
void drawHome(RenderWindow &window){
window.draw(tx.create(tx.S,tx.W/2.0F,tx.H/2.0F,Color::White,"press enter to start"));
}
void drawDebug(RenderWindow &window){
drawCrossHair(window);
vector<Text> letters = tx.genLetters(m);
for(Text letter : letters){
letter.move(-c.getX(),-c.getY()); // moves in opposite direction
window.draw(letter);
}
}
void drawDisplay(RenderWindow &window){
vector<RectangleShape> rects = tx.genRects(m,1,pl.isLightOn(),pl.getVisionRay());
for(RectangleShape rect : rects){
rect.move(-c.getX(),-c.getY()); // moves in opposite direction
window.draw(rect);
}
//window.draw(CircleShape(200));
enemy->draw(window,tx.S,c);
drawCrossHair(window);
}
void drawCrossHair(RenderWindow &window){
window.draw(tx.create(tx.S,tx.W/2 /*- tx.S/2*/,tx.H/2 /*- tx.S/2*/,Color::Green,"+"));
}