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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
|
#include "api/Asset.h"
#include "api/BehaviorScript.h"
#include "api/CircleCollider.h"
#include "api/ParticleEmitter.h"
#include "api/Scene.h"
#include "manager/ComponentManager.h"
#include "manager/Mediator.h"
#include "types.h"
#include <cmath>
#include <crepe/api/BoxCollider.h>
#include <crepe/api/Camera.h>
#include <crepe/api/Color.h>
#include <crepe/api/Event.h>
#include <crepe/api/GameObject.h>
#include <crepe/api/LoopManager.h>
#include <crepe/api/Rigidbody.h>
#include <crepe/api/Script.h>
#include <crepe/api/Sprite.h>
#include <crepe/api/Transform.h>
#include <crepe/api/Vector2.h>
using namespace crepe;
using namespace std;
class ScriptBox : public Script {
public:
bool oncollision(const CollisionEvent & test) {
Log::logf("Box {} on_collision() with {}", test.info.self.metadata.game_object_id,
test.info.other.metadata.game_object_id);
return true;
}
void init() {
subscribe<CollisionEvent>(
[this](const CollisionEvent & ev) -> bool { return this->oncollision(ev); });
}
};
class ScriptCircle : public Script {
public:
bool oncollision(const CollisionEvent & test) {
Log::logf("Circle {} on_collision() with {}", test.info.self.metadata.game_object_id,
test.info.other.metadata.game_object_id);
return true;
}
void init() {
subscribe<CollisionEvent>(
[this](const CollisionEvent & ev) -> bool { return this->oncollision(ev); });
}
};
class ScriptMoveToLeft : public Script {
public:
void update() {
Transform & transform = this->get_component<Transform>();
transform.position.x -= 0.02;
}
};
class ScriptMoveToRight : public Script {
public:
void update() {
Transform & transform = this->get_component<Transform>();
transform.position.x += 0.02;
}
};
class ConcreteScene1 : public Scene {
public:
void load_scene() {
GameObject camera = this->new_object("camera");
camera.add_component<Camera>(ivec2(1080, 720), vec2(10, 10),
Camera::Data{.bg_color = Color::WHITE, .zoom = 1});
GameObject reference = this->new_object("reference", "tag", vec2(0, 0), 0, 1);
Asset reference_asset = Asset("asset/texture/square.png");
reference.add_component<Sprite>(reference_asset, Sprite::Data{
.color = Color::RED,
.sorting_in_layer = 10,
.order_in_layer = 0,
.size = vec2(0.1, 0.1),
.angle_offset = 0,
.scale_offset = 1,
.position_offset = vec2(0, 0),
});
/*GameObject box_1 = this->new_object("box_1", "tag", vec2(0, 0), 0, 1);
Asset box_1_asset = Asset("asset/texture/square.png");
box_1.add_component<Sprite>(box_1_asset, Sprite::Data{
.sorting_in_layer = 0,
.order_in_layer = 0,
.size = vec2(1, 1),
.angle_offset = 0,
.scale_offset = 1,
.position_offset = vec2(0, 0.5),
});
Asset box_1_asset2 = Asset("asset/texture/test_ap43.png");
box_1.add_component<Sprite>(box_1_asset2, Sprite::Data{
.sorting_in_layer = 0,
.order_in_layer = 0,
.size = vec2(1, 1),
.angle_offset = 0,
.scale_offset = 1,
.position_offset = vec2(0, -0.5),
});*/
/*GameObject particles = this->new_object("particles", "tag", vec2(-1, 0), 180, 1);
Asset particles_asset = Asset("asset/texture/test_ap43.png");
Sprite & particles_sprite = particles.add_component<Sprite>(particles_asset, Sprite::Data{
.sorting_in_layer = 0,
.order_in_layer = 0,
.size = vec2(1, 1),
.angle_offset = 0,
.scale_offset = 1,
.position_offset = vec2(0, 0),
});
ParticleEmitter & particles_emitter = particles.add_component<ParticleEmitter>(particles_sprite, ParticleEmitter::Data{
.offset = vec2(0, 0),
.emission_rate = 1,
.min_speed = 1,
.max_speed = 1,
.min_angle = 0,
.max_angle = 0,
.end_lifespan = 4,
});*/
const bool SCRIPT = false;
const Rigidbody::BodyType BODYTYPE_LEFT = Rigidbody::BodyType::DYNAMIC;
const Rigidbody::BodyType BODYTYPE_RIGHT = Rigidbody::BodyType::DYNAMIC;
const bool BOUNCE_LEFT = false;
const bool BOUNCE_RIGHT = false;
const bool KINEMATIC_COLLISION = true;
const bool ONTOP = false;
const float SCALE = 0.5;
const float OFFSET_X_LEFT = 0;
const float OFFSET_X_RIGHT = 0;
const float OFFSET_Y_LEFT = 0;
const float OFFSET_Y_RIGHT = 0;
GameObject box_1 = this->new_object("box_1", "tag",
ONTOP ? vec2(-0.25, -4) : vec2(-2, -4), 0, SCALE);
Asset box_1_asset = Asset("asset/texture/square.png");
box_1.add_component<Sprite>(box_1_asset,
Sprite::Data{
.sorting_in_layer = 0,
.order_in_layer = 0,
.size = vec2(1, 1),
.angle_offset = 0,
.scale_offset = 1,
.position_offset = vec2(OFFSET_X_LEFT, OFFSET_Y_LEFT),
});
box_1.add_component<Rigidbody>(Rigidbody::Data{
.gravity_scale = 0,
.body_type = BODYTYPE_LEFT,
.linear_velocity = SCRIPT ? vec2{0, 0} : vec2{1, 0},
.elasticity_coefficient = BOUNCE_LEFT ? 0.5 : 0,
.kinematic_collision = KINEMATIC_COLLISION,
});
box_1.add_component<BehaviorScript>().set_script<ScriptMoveToRight>().active = SCRIPT;
box_1.add_component<BoxCollider>(vec2(1, 1), vec2(OFFSET_X_LEFT, OFFSET_Y_LEFT));
box_1.add_component<BehaviorScript>().set_script<ScriptBox>();
GameObject circle_1 = this->new_object("ricle_1", "tag",
ONTOP ? vec2(0.25, -4) : vec2(2, -4), 0, SCALE);
Asset circle_1_asset = Asset("asset/texture/circle.png");
circle_1.add_component<Sprite>(
circle_1_asset, Sprite::Data{
.sorting_in_layer = 0,
.order_in_layer = 0,
.size = vec2(1, 1),
.angle_offset = 0,
.scale_offset = 1,
.position_offset = vec2(OFFSET_X_RIGHT, OFFSET_Y_RIGHT),
});
circle_1.add_component<Rigidbody>(Rigidbody::Data{
.gravity_scale = 0,
.body_type = BODYTYPE_RIGHT,
.linear_velocity = SCRIPT ? vec2{0, 0} : vec2{-1, 0},
.elasticity_coefficient = BOUNCE_RIGHT ? 0.5 : 0,
.kinematic_collision = KINEMATIC_COLLISION,
});
circle_1.add_component<BehaviorScript>().set_script<ScriptMoveToLeft>().active
= SCRIPT;
circle_1.add_component<CircleCollider>(0.5, vec2(OFFSET_X_RIGHT, OFFSET_Y_RIGHT));
circle_1.add_component<BehaviorScript>().set_script<ScriptCircle>();
GameObject circle_2 = this->new_object(
"ricle_2", "tag", ONTOP ? vec2(-0.25, -1.5) : vec2(-2.5, -1.5), 0, SCALE);
Asset circle_2_asset = Asset("asset/texture/circle.png");
circle_2.add_component<Sprite>(
circle_2_asset, Sprite::Data{
.sorting_in_layer = 0,
.order_in_layer = 0,
.size = vec2(1, 1),
.angle_offset = 0,
.scale_offset = 1,
.position_offset = vec2(OFFSET_X_LEFT, OFFSET_Y_LEFT),
});
circle_2.add_component<Rigidbody>(Rigidbody::Data{
.gravity_scale = 0,
.body_type = BODYTYPE_LEFT,
.linear_velocity = SCRIPT ? vec2{0, 0} : vec2{1, 0},
.elasticity_coefficient = BOUNCE_LEFT ? 0.5 : 0,
.kinematic_collision = KINEMATIC_COLLISION,
});
circle_2.add_component<BehaviorScript>().set_script<ScriptMoveToRight>().active
= SCRIPT;
circle_2.add_component<CircleCollider>(0.5, vec2(OFFSET_X_LEFT, OFFSET_Y_LEFT));
circle_2.add_component<BehaviorScript>().set_script<ScriptCircle>();
GameObject box_2 = this->new_object(
"box_2", "tag", ONTOP ? vec2(0.25, -1.5) : vec2(2.5, -1.5), 0, SCALE);
Asset box_2_asset = Asset("asset/texture/square.png");
box_2.add_component<Sprite>(
box_2_asset, Sprite::Data{
.sorting_in_layer = 0,
.order_in_layer = 0,
.size = vec2(1, 1),
.angle_offset = 0,
.scale_offset = 1,
.position_offset = vec2(OFFSET_X_RIGHT, OFFSET_Y_RIGHT),
});
box_2.add_component<Rigidbody>(Rigidbody::Data{
.gravity_scale = 0,
.body_type = BODYTYPE_RIGHT,
.linear_velocity = SCRIPT ? vec2{0, 0} : vec2{-1, 0},
.elasticity_coefficient = BOUNCE_RIGHT ? 0.5 : 0,
.kinematic_collision = KINEMATIC_COLLISION,
});
box_2.add_component<BehaviorScript>().set_script<ScriptMoveToLeft>().active = SCRIPT;
box_2.add_component<BoxCollider>(vec2(1, 1), vec2(OFFSET_X_RIGHT, OFFSET_Y_RIGHT));
box_2.add_component<BehaviorScript>().set_script<ScriptBox>();
GameObject box_3 = this->new_object(
"box_3", "tag", ONTOP ? vec2(-0.25, 1.5) : vec2(-3, 1.5), 0, SCALE);
Asset box_3_asset = Asset("asset/texture/square.png");
box_3.add_component<Sprite>(box_3_asset,
Sprite::Data{
.sorting_in_layer = 0,
.order_in_layer = 0,
.size = vec2(1, 1),
.angle_offset = 0,
.scale_offset = 1,
.position_offset = vec2(OFFSET_X_LEFT, OFFSET_Y_LEFT),
});
box_3.add_component<Rigidbody>(Rigidbody::Data{
.gravity_scale = 0,
.body_type = BODYTYPE_LEFT,
.linear_velocity = SCRIPT ? vec2{0, 0} : vec2{1, 0},
.elasticity_coefficient = BOUNCE_LEFT ? 0.5 : 0,
.kinematic_collision = KINEMATIC_COLLISION,
});
box_3.add_component<BehaviorScript>().set_script<ScriptMoveToRight>().active = SCRIPT;
box_3.add_component<BoxCollider>(vec2(1, 1), vec2(OFFSET_X_LEFT, OFFSET_Y_LEFT));
box_3.add_component<BehaviorScript>().set_script<ScriptBox>();
GameObject box_4 = this->new_object("box_4", "tag",
ONTOP ? vec2(0.25, 1.5) : vec2(3, 1.5), 0, SCALE);
Asset box_4_asset = Asset("asset/texture/square.png");
box_4.add_component<Sprite>(
box_4_asset, Sprite::Data{
.sorting_in_layer = 0,
.order_in_layer = 0,
.size = vec2(1, 1),
.angle_offset = 0,
.scale_offset = 1,
.position_offset = vec2(OFFSET_X_RIGHT, OFFSET_Y_RIGHT),
});
box_4.add_component<Rigidbody>(Rigidbody::Data{
.gravity_scale = 0,
.body_type = BODYTYPE_RIGHT,
.linear_velocity = SCRIPT ? vec2{0, 0} : vec2{-1, 0},
.elasticity_coefficient = BOUNCE_RIGHT ? 0.5 : 0,
.kinematic_collision = KINEMATIC_COLLISION,
});
box_4.add_component<BehaviorScript>().set_script<ScriptMoveToLeft>().active = SCRIPT;
box_4.add_component<BoxCollider>(vec2(1, 1), vec2(OFFSET_X_RIGHT, OFFSET_Y_RIGHT));
box_4.add_component<BehaviorScript>().set_script<ScriptBox>();
GameObject circle_3 = this->new_object(
"ricle_3", "tag", ONTOP ? vec2(-0.25, 4) : vec2(-3.5, 4), 0, SCALE);
Asset circle_3_asset = Asset("asset/texture/circle.png");
circle_3.add_component<Sprite>(
circle_3_asset, Sprite::Data{
.sorting_in_layer = 0,
.order_in_layer = 0,
.size = vec2(1, 1),
.angle_offset = 0,
.scale_offset = 1,
.position_offset = vec2(OFFSET_X_LEFT, OFFSET_Y_LEFT),
});
circle_3.add_component<Rigidbody>(Rigidbody::Data{
.gravity_scale = 0,
.body_type = BODYTYPE_LEFT,
.linear_velocity = SCRIPT ? vec2{0, 0} : vec2{1, 0},
.elasticity_coefficient = BOUNCE_LEFT ? 0.5 : 0,
.kinematic_collision = KINEMATIC_COLLISION,
});
circle_3.add_component<BehaviorScript>().set_script<ScriptMoveToRight>().active
= SCRIPT;
circle_3.add_component<CircleCollider>(0.5, vec2(OFFSET_X_LEFT, OFFSET_Y_LEFT));
circle_3.add_component<BehaviorScript>().set_script<ScriptCircle>();
GameObject circle_4 = this->new_object("ricle_4", "tag",
ONTOP ? vec2(0.25, 4) : vec2(3.5, 4), 0, SCALE);
Asset circle_4_asset = Asset("asset/texture/circle.png");
circle_4.add_component<Sprite>(
circle_4_asset, Sprite::Data{
.sorting_in_layer = 0,
.order_in_layer = 0,
.size = vec2(1, 1),
.angle_offset = 0,
.scale_offset = 1,
.position_offset = vec2(OFFSET_X_RIGHT, OFFSET_Y_RIGHT),
});
circle_4.add_component<Rigidbody>(Rigidbody::Data{
.gravity_scale = 0,
.body_type = BODYTYPE_RIGHT,
.linear_velocity = SCRIPT ? vec2{0, 0} : vec2{-1, 0},
.elasticity_coefficient = BOUNCE_RIGHT ? 0.5 : 0,
.kinematic_collision = KINEMATIC_COLLISION,
});
circle_4.add_component<BehaviorScript>().set_script<ScriptMoveToLeft>().active
= SCRIPT;
circle_4.add_component<CircleCollider>(0.5, vec2(OFFSET_X_RIGHT, OFFSET_Y_RIGHT));
circle_4.add_component<BehaviorScript>().set_script<ScriptCircle>();
}
string get_name() const { return "scene1"; }
};
int main(int argc, char * argv[]) {
LoopManager gameloop;
gameloop.add_scene<ConcreteScene1>();
gameloop.start();
return 0;
}
|