-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.cpp
171 lines (137 loc) · 4.52 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
#include "Supernova.h"
using namespace Supernova;
float lastX;
float lastY;
float lastPressX;
float lastPressY;
Scene scene;
Scene uiscene;
Camera camera(&scene);
Terrain terrain(&scene);
Light sun(&scene);
Text text(&uiscene);
Text text2(&uiscene);
Shape* cubes[25];
Shape* spheres[25];
void onViewLoaded();
void onUpdate();
void onKeyDown(int key, bool repeat, int mods);
void onKeyUp(int key, bool repeat, int mods);
void onTouchStart(int pointer, float x, float y);
void onTouchMove(int pointer, float x, float y);
void startPositions(){
for (int i = 0; i < 5; i++){
for (int j = 0; j < 5; j++){
cubes[(5*i)+j]->setPosition(10*i - 25, 15, 10*j - 25);
cubes[(5*i)+j]->getBody3D().setLinearVelocity(Vector3(0,0,0));
spheres[(5*i)+j]->setPosition(10*i - 20, 15, 10*j - 20);
spheres[(5*i)+j]->getBody3D().setLinearVelocity(Vector3(0,0,0));
}
}
}
void init(){
for (int i = 0; i < 5; i++){
for (int j = 0; j < 5; j++){
cubes[(5*i)+j] = new Shape(&scene);
cubes[(5*i)+j]->createBox(1.0, 1.0, 1.0);
cubes[(5*i)+j]->setTexture("block.png");
cubes[(5*i)+j]->setName("cube"+std::to_string((4*i)+j));
cubes[(5*i)+j]->getBody3D().createBoxShape(1, 1, 1);
cubes[(5*i)+j]->getBody3D().load();
spheres[(5*i)+j] = new Shape(&scene);
spheres[(5*i)+j]->createSphere(1.0);
spheres[(5*i)+j]->setTexture("block.png");
spheres[(5*i)+j]->setName("sphere"+std::to_string((4*i)+j));
spheres[(5*i)+j]->getBody3D().createSphereShape(1);
spheres[(5*i)+j]->getBody3D().load();
}
}
startPositions();
camera.setPosition(0, 15, -50);
scene.setCamera(camera.getEntity());
terrain.setName("terrain");
terrain.setHeightMap("heightmap.png");
terrain.setTexture("ground.png");
terrain.createTerrain();
Body3D bodyterrain = terrain.getBody3D();
bodyterrain.setType(BodyType::STATIC);
bodyterrain.createHeightFieldShape();
bodyterrain.load();
scene.setAmbientLight(0.4);
scene.setBackgroundColor(Vector4(0.5,0.5,0.5,1.0));
sun.setType(LightType::DIRECTIONAL);
sun.setDirection(0.0f, -0.3, -0.8);
sun.setShadows(true);
text.setAnchorPreset(AnchorPreset::TOP_LEFT);
text2.setAnchorPreset(AnchorPreset::CENTER_TOP);
text2.setText("Press space to reset");
Engine::setCanvasSize(1000,480);
Engine::setScene(&scene);
Engine::addSceneLayer(&uiscene);
Engine::setCallTouchInMouseEvent(true);
Engine::onViewLoaded = onViewLoaded;
Engine::onUpdate = onUpdate;
Engine::onKeyDown = onKeyDown;
Engine::onKeyUp = onKeyUp;
Engine::onTouchStart = onTouchStart;
Engine::onTouchMove = onTouchMove;
}
void onViewLoaded(){
}
void onUpdate(){
text.setText(std::string("FPS: ") + std::to_string(Engine::getFramerate()).c_str());
if (Input::isKeyPressed(S_KEY_UP) || Input::isKeyPressed(S_KEY_UP)){
camera.walkForward(1);
}
if (Input::isKeyPressed(S_KEY_DOWN) || Input::isKeyPressed(S_KEY_S)){
camera.walkForward(-1);
}
if (Input::isKeyPressed(S_KEY_LEFT)){
camera.rotateView(1);
}
if (Input::isKeyPressed(S_KEY_RIGHT)){
camera.rotateView(-1);
}
if (Input::isKeyPressed(S_KEY_A)){
camera.slide(-1);
}
if (Input::isKeyPressed(S_KEY_D)){
camera.slide(1);
}
}
void onKeyDown(int key, bool repeat, int mods){
if (key == S_KEY_X){
camera.setPosition(camera.getPosition().x, camera.getPosition().y+10, camera.getPosition().z);
camera.setView(camera.getView().x, camera.getView().y+10, camera.getView().z);
}
if (key == S_KEY_Z){
camera.setPosition(camera.getPosition().x, camera.getPosition().y-10, camera.getPosition().z);
camera.setView(camera.getView().x, camera.getView().y-10, camera.getView().z);
}
if (key == S_KEY_SPACE){
startPositions();
}
}
void onKeyUp(int key, bool repeat, int mods){
if (key == S_KEY_F){
if (!System::instance().isFullscreen()) {
System::instance().requestFullscreen();
}else{
System::instance().exitFullscreen();
}
}
}
void onTouchStart(int pointer, float x, float y) {
lastX = x;
lastY = y;
lastPressX = x;
lastPressY = y;
}
void onTouchMove(int pointer, float x, float y) {
float difX = lastX - x;
lastX = x;
camera.rotateView(0.1 * difX);
float difY = lastY - y;
lastY = y;
camera.elevateView(-0.1 * difY);
}