-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.cpp
More file actions
224 lines (187 loc) · 7.16 KB
/
run.cpp
File metadata and controls
224 lines (187 loc) · 7.16 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
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
#include <nlohmann/json.hpp>
#include <fstream>
#include <iostream>
#include <chrono>
#include <cmath>
#include <limits>
#include <SFML/Graphics.hpp>
#include "headers/headers.cpp"
//runs the program :3
void game::run(){
returnXY p;
loadObjectsJSON(objects,"save.json");
LayerObjects();
using std::chrono::duration_cast;
using std::chrono::nanoseconds;
typedef std::chrono::high_resolution_clock clock;
float timediff=20;
sf::ContextSettings settings;
settings.antiAliasingLevel = 8.0;
sf::RenderWindow window(sf::VideoMode({static_cast<unsigned int>(W), static_cast<unsigned int>(H)}), "game"/*, sf::Style::Close, settings*/);
//recenters convex polygons
for(int i = 0;i<objects.size();i++){
float X=0;
float Y=0;
if(objects[i].mass<=0){
objects[i].mass=std::numeric_limits<float>::infinity();
}
for(int iP=0;iP<objects[i].points;iP++){
X += objects[i].pointList[iP*2];
Y += objects[i].pointList[iP*2+1];
//std::cout<<"iP: "<<iP<<"\npoints: "<<objects[i].points<<"\nX: "<<objects[i].pointList[iP*2+1]<<"\nY: "<<objects[i].pointList[iP*2]<<"\n";
}
X/=objects[i].points;
Y/=objects[i].points;
for(int iP=0;iP<objects[i].points;iP++){
//std::cout<<"Y: "<<objects[i].pointList[iP*2+1]<<"\n";
objects[i].pointList[iP*2]-=X;
objects[i].pointList[iP*2+1]-=Y;
//std::cout<<"Yc: "<<objects[i].pointList[iP*2+1]<<"\n\n";
}
if (!objects[i].texture.loadFromFile(objects[i].loc)){
//10/0;
//I'm too lazy to throw an error here
}
if (!objects[i].font.openFromFile(objects[i].fontLoc)){
//throw error or something
}
}
while (window.isOpen())
{
auto start = clock::now();
while (const std::optional event = window.pollEvent())
{
// "close requested" event: we close the window
if (event->is<sf::Event::Closed>())
window.close();
}
window.clear(sf::Color::Black);
p = Camera(p.x,p.y,timediff);
//draws all shapes+transformations
for(int i = 0; i<objects.size();i++){
drawShape(window,i);
if(debug==true){
debuger(window,i);
}
if(physics==true){
transform(i,timediff+1);
}
if(objects[i].objectType==-1){
sf::Vector2i position = sf::Mouse::getPosition(window);
objects[i].velX=((objects[i].velX*(1))+(-(objects[i].X)+(position.x-camOffsetX)));
objects[i].velY=((objects[i].velY*(1))+(-(objects[i].Y)+(position.y-camOffsetY)));
objects[i].X = position.x-camOffsetX;
objects[i].Y = position.y-camOffsetY;
if(objects[i].Y==std::numeric_limits<float>::infinity()||objects[i].X==std::numeric_limits<float>::infinity()){
objects[i].X=0;
objects[i].Y=0;
camOffsetX=0;
camOffsetY=0;
}
if(debug==true){
std::cout<<"vel:("<<(objects[i].velX)<<", "<<(objects[i].velY)<<")\n";
std::cout<<"pos:("<<objects[i].X<<", "<<objects[i].Y<<")\n";
}
mouseObject=i;
}
if(sf::Mouse::isButtonPressed(sf::Mouse::Button::Left)){
if(objects[i].grabbed==true){
objects[i].X=objects[mouseObject].X;
objects[i].Y=objects[mouseObject].Y;
//std::cout<<"VELWHX:"<<objects[mouseObject].velX<<"VELWHY: "<<objects[mouseObject].velY<<"\n";
objects[i].velX=objects[mouseObject].velX;
objects[i].velY=objects[mouseObject].velY;
//std::cout<<objects[i].velY<<","<<objects[i].velX<<"hmm...\n";
}
}else{
objects[i].grabbed=false;
}
while(objects[i].velRot>360){
if(debug==true){
std::cout<<"TOO FAR: "<<objects[i].velRot<<" > "<<objects[i].velRot-360<<"\n";
}
objects[i].velRot-=360;
}
while(objects[i].velRot<-360){
objects[i].velRot+=360;
}
if(objects[i].velRot==std::numeric_limits<float>::infinity()||objects[i].velRot==-std::numeric_limits<float>::infinity()){
objects[i].velRot=0;
}
if(objects[i].objectType==1){
inputs(i,timediff);
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::Escape)){
if(physics==true){
physics=false;
}else{
physics=true;
}
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::C)){
if(debug==true){
debug=false;
}else{
debug=true;
}
}
}
baseCollision(timediff);
//testingLayoutInf(window);
window.display();
auto end = clock::now();
//std::cout <<1000000000/duration_cast<nanoseconds>(end-start).count() << "\n";//fps
timediff=1000000000/duration_cast<nanoseconds>(end-start).count()/60;
jumpDown(timediff);
}
}
//loads in objects
int game::loadObjectsJSON(std::vector<object>& objectsVect, const std::string& filename) {
std::ifstream file(filename);
if (!file) {
std::cerr << "Error opening JSON file!\n";
return 0;
}
nlohmann::json j;
file >> j;
file.close();
objectsVect.clear();
for (const auto& item : j) {
object obj;
obj.X = item["X"];
obj.Y = item["Y"];
obj.velX = item["velX"];
obj.velY = item["velY"];
obj.velRot = item["velRot"];
obj.rotation = item["rotation"];
obj.sides = item["sides"];
obj.sizeModifier = item["sizeModifier"];
obj.objectType = item["objectType"];
obj.gravity = item["gravity"];
obj.airRes = item["airRes"];
obj.solid = item["solid"];
obj.mass = item["mass"];
obj.color = item["color"];
obj.width = item["width"];
obj.height = item["height"];
obj.points = item["points"];
obj.coefficentOfFriction = item["coefficentOfFriction"];
obj.loc = item["loc"];
obj.layer = item["layer"];
auto pointList = item["pointList"].get<std::vector<float>>();
std::copy(pointList.begin(), pointList.end(), obj.pointList);
obj.trigger.id = item["trigger"]["id"];
obj.trigger.event = item["trigger"]["event"];
obj.trigger.destroyO2 = item["trigger"]["destroyO2"];
obj.trigger.typeReq = item["trigger"]["typeReq"];
obj.text=item["text"];
obj.layer = item["layer"];
if (!obj.texture.loadFromFile(obj.loc)) {
std::cerr << "Failed to load texture: " << obj.loc << '\n';
}
obj.X*=baseUnit;
obj.Y*=baseUnit;
objectsVect.push_back(obj);
}
return objectsVect.size();
}