aboutsummaryrefslogtreecommitdiff
path: root/src/example/game.cpp
blob: 24aa13722d5594b63d78cfabbb73b895d9fe7cfb (plain)
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
#include "api/Animator.h"
#include "api/BehaviorScript.h"
#include "api/CircleCollider.h"
#include "api/Scene.h"
#include "manager/ComponentManager.h"
#include "manager/Mediator.h"
#include "types.h"
#include <crepe/api/Asset.h>
#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>
#include <iostream>

using namespace crepe;
using namespace std;

class Background {
public:
	Background(ComponentManager & mgr) {
		this->start(mgr);

		this->hallway(mgr, 1, Color::YELLOW);

		this->forest(mgr);

		this->aquarium(mgr);

		this->hallway(mgr, 2, Color::MAGENTA);
	}

	void start(ComponentManager & mgr) {
		GameObject begin = mgr.new_object("start_begin", "background", vec2(begin_x, 0));
		Asset begin_asset{"asset/jetpack_joyride/background/start/titleFG_1_TVOS.png"};
		begin.add_component<Sprite>(begin_asset, Sprite::Data{
													 .sorting_in_layer = 4,
													 .order_in_layer = 0,
													 .size = vec2(0, 800),
												 });
		begin_x += 700;

		GameObject end = mgr.new_object("start_end", "background", vec2(begin_x, 0));
		Asset end_asset{"asset/jetpack_joyride/background/start/titleFG_2_TVOS.png"};
		end.add_component<Sprite>(end_asset, Sprite::Data{
												 .sorting_in_layer = 4,
												 .order_in_layer = 1,
												 .size = vec2(0, 800),
											 });
		begin_x += 100;

		this->add_lamp_start(end, vec2(-350, -95));
	}

	void add_lamp_start(GameObject & obj, vec2 offset, unsigned int fps = 10) {
		Asset lamp_asset{"asset/jetpack_joyride/background/start/alarmLight_TVOS.png"};
		obj.add_component<Sprite>(lamp_asset, Sprite::Data{
												  .sorting_in_layer = 5,
												  .order_in_layer = 0,
												  .size = vec2(0, 100),
												  .position_offset = offset,
											  });
		Asset lamp_glow_asset{"asset/jetpack_joyride/background/start/alarmGlow_TVOS.png"};
		Sprite & lamp_glow_sprite = obj.add_component<Sprite>(
			lamp_glow_asset, Sprite::Data{
								 .sorting_in_layer = 5,
								 .order_in_layer = 1,
								 .size = vec2(0, 300),
								 .position_offset = offset - vec2(65, -55),
							 });
		obj.add_component<Animator>(lamp_glow_sprite, ivec2(422, 384), uvec2(6, 1),
									Animator::Data{
										.fps = fps,
										.looping = true,
									});
	}

	void hallway(ComponentManager & mgr, unsigned int sector_num, Color sector_color) {
		GameObject begin = mgr.new_object("hallway_begin", "background", vec2(begin_x, 0));
		Asset begin_asset{"asset/jetpack_joyride/background/hallway/hallway1FG_1_TVOS.png"};
		begin.add_component<Sprite>(begin_asset, Sprite::Data{
													 .sorting_in_layer = 4,
													 .order_in_layer = 0,
													 .size = vec2(0, 800),
												 });
		begin_x += 600;

		this->add_sector_number(begin, vec2(-200, 0), sector_num, sector_color);
		this->add_lamp_hallway(begin, vec2(-70, -120), 11);
		this->add_lamp_hallway(begin, vec2(30, -120), 9);

		GameObject middle_1 = mgr.new_object("hallway_middle", "background", vec2(begin_x, 0));
		Asset middle_asset{"asset/jetpack_joyride/background/hallway/hallway1FG_2_TVOS.png"};
		middle_1.add_component<Sprite>(middle_asset, Sprite::Data{
														 .sorting_in_layer = 4,
														 .order_in_layer = 2,
														 .size = vec2(0, 800),
													 });
		begin_x += 600;

		GameObject middle_2 = mgr.new_object("hallway_middle", "background", vec2(begin_x, 0));
		Asset middle_asset_2{"asset/jetpack_joyride/background/hallway/hallway1FG_2_TVOS.png"};
		middle_2.add_component<Sprite>(middle_asset_2, Sprite::Data{
														   .sorting_in_layer = 4,
														   .order_in_layer = 3,
														   .size = vec2(0, 800),
													   });
		begin_x += 200;

		GameObject middle_3 = mgr.new_object("hallway_middle", "background", vec2(begin_x, 0));
		Asset middle_asset_3{"asset/jetpack_joyride/background/hallway/hallway1FG_2_TVOS.png"};
		middle_3.add_component<Sprite>(middle_asset_3, Sprite::Data{
														   .sorting_in_layer = 4,
														   .order_in_layer = 4,
														   .size = vec2(0, 800),
													   });
		begin_x += 400;

		this->add_lamp_hallway(middle_3, vec2(0, -120));

		GameObject middle_4 = mgr.new_object("hallway_middle", "background", vec2(begin_x, 0));
		Asset middle_asset_4{"asset/jetpack_joyride/background/hallway/hallway1FG_2_TVOS.png"};
		middle_4.add_component<Sprite>(middle_asset_4, Sprite::Data{
														   .sorting_in_layer = 4,
														   .order_in_layer = 5,
														   .size = vec2(0, 800),
													   });
		begin_x += 600;

		GameObject end = mgr.new_object("hallway_end", "background", vec2(begin_x, 0));
		Asset end_asset{"asset/jetpack_joyride/background/hallway/hallway1FG_1_TVOS.png"};
		end.add_component<Sprite>(end_asset, Sprite::Data{
												 .sorting_in_layer = 4,
												 .order_in_layer = 1,
												 .size = vec2(0, 800),
											 });
		begin_x += 600;
	}

	void add_lamp_hallway(GameObject & obj, vec2 offset, unsigned int fps = 10) {
		Asset lamp_asset{"asset/jetpack_joyride/background/hallway/alarmLight_TVOS.png"};
		obj.add_component<Sprite>(lamp_asset, Sprite::Data{
												  .sorting_in_layer = 5,
												  .order_in_layer = 0,
												  .size = vec2(0, 100),
												  .position_offset = offset,
											  });
		Asset lamp_glow_asset{"asset/jetpack_joyride/background/hallway/alarmGlow_TVOS.png"};
		Sprite & lamp_glow_sprite = obj.add_component<Sprite>(
			lamp_glow_asset, Sprite::Data{
								 .sorting_in_layer = 5,
								 .order_in_layer = 1,
								 .size = vec2(0, 300),
								 .position_offset = offset - vec2(65, -30),
							 });
		obj.add_component<Animator>(lamp_glow_sprite, ivec2(422, 384), uvec2(6, 1),
									Animator::Data{
										.fps = fps,
										.looping = true,
									});
	}

	void add_sector_number(GameObject & obj, vec2 offset, unsigned int sector_num,
						   Color sector_color) {
		Asset sector_text_asset{
			"asset/jetpack_joyride/background/hallway/sectorText_TVOS.png"};
		obj.add_component<Sprite>(sector_text_asset, Sprite::Data{
														 .color = sector_color,
														 .sorting_in_layer = 5,
														 .order_in_layer = 0,
														 .size = vec2(0, 100),
														 .position_offset = offset,
													 });
		Asset sector_num_asset{
			"asset/jetpack_joyride/background/hallway/sectorNumbers_TVOS.png"};
		Sprite & sector_num_sprite = obj.add_component<Sprite>(
			sector_num_asset, Sprite::Data{
								  .color = sector_color,
								  .sorting_in_layer = 5,
								  .order_in_layer = 0,
								  .size = vec2(0, 100),
								  .position_offset = offset + vec2(200, 0),
							  });
		Animator & sector_num_anim = obj.add_component<Animator>(
			sector_num_sprite, ivec2(256, 128), uvec2(4, 4), Animator::Data{});
		int column = (sector_num - 1) / 4;
		int row = (sector_num - 1) % 4;
		sector_num_anim.set_anim(column);
		for (int i = 0; i < row; i++) {
			sector_num_anim.next_anim();
		}
		sector_num_anim.pause();
	}

	void forest(ComponentManager & mgr) {
		GameObject forest_begin
			= mgr.new_object("forest_begin", "background", vec2(begin_x, 0));
		Asset forest_begin_asset{
			"asset/jetpack_joyride/background/forest/forestFG_1_TVOS.png"};
		forest_begin.add_component<Sprite>(forest_begin_asset, Sprite::Data{
																   .sorting_in_layer = 4,
																   .order_in_layer = 0,
																   .size = vec2(0, 800),
															   });
		begin_x += 800;

		GameObject forest_middle
			= mgr.new_object("forest_middle", "background", vec2(begin_x, 0));
		Asset forest_middle_asset{
			"asset/jetpack_joyride/background/forest/forestFG_3_TVOS.png"};
		forest_middle.add_component<Sprite>(forest_middle_asset, Sprite::Data{
																	 .sorting_in_layer = 4,
																	 .order_in_layer = 2,
																	 .size = vec2(0, 800),
																 });
		begin_x += 800;

		GameObject forest_end = mgr.new_object("forest_end", "background", vec2(begin_x, 0));
		Asset forest_end_asset{"asset/jetpack_joyride/background/forest/forestFG_2_TVOS.png"};
		forest_end.add_component<Sprite>(forest_end_asset, Sprite::Data{
															   .sorting_in_layer = 4,
															   .order_in_layer = 1,
															   .size = vec2(0, 800),
														   });
		begin_x += 600;
	}

	void aquarium(ComponentManager & mgr) {
		GameObject aquarium_begin
			= mgr.new_object("aquarium_begin", "background", vec2(begin_x, 0));
		Asset aquarium_begin_asset{
			"asset/jetpack_joyride/background/aquarium/glassTubeFG_1_TVOS.png"};
		aquarium_begin.add_component<Sprite>(aquarium_begin_asset, Sprite::Data{
																	   .sorting_in_layer = 4,
																	   .order_in_layer = 0,
																	   .size = vec2(0, 800),
																   });
		begin_x += 600;

		GameObject aquarium_middle
			= mgr.new_object("aquarium_middle", "background", vec2(begin_x, 0));
		Asset aquarium_middle_asset{
			"asset/jetpack_joyride/background/aquarium/glassTubeFG_3_TVOS.png"};
		aquarium_middle.add_component<Sprite>(aquarium_middle_asset, Sprite::Data{
																		 .sorting_in_layer = 4,
																		 .order_in_layer = 2,
																		 .size = vec2(0, 800),
																	 });
		begin_x += 600;

		GameObject aquarium_end
			= mgr.new_object("aquarium_end", "background", vec2(begin_x, 0));
		Asset aquarium_end_asset{
			"asset/jetpack_joyride/background/aquarium/glassTubeFG_2_TVOS.png"};
		aquarium_end.add_component<Sprite>(aquarium_end_asset, Sprite::Data{
																   .sorting_in_layer = 4,
																   .order_in_layer = 1,
																   .size = vec2(0, 800),
															   });
		begin_x += 600;
	}

private:
	float begin_x = 0;
};

class MoveCameraScript : public Script {
public:
	void init() {
		subscribe<KeyPressEvent>(
			[this](const KeyPressEvent & ev) -> bool { return this->keypressed(ev); });
	}

private:
	bool keypressed(const KeyPressEvent & event) {
		if (event.key == Keycode::RIGHT) {
			Transform & cam = this->get_components_by_name<Transform>("camera").front();
			cam.position.x += 100;
		} else if (event.key == Keycode::LEFT) {
			Transform & cam = this->get_components_by_name<Transform>("camera").front();
			cam.position.x -= 100;
		}
		return true;
	}
};

class Scene1 : public Scene {
public:
	void load_scene() {
		Mediator & m = this->mediator;
		ComponentManager & mgr = m.component_manager;

		Background background(mgr);

		GameObject camera = mgr.new_object("camera", "camera", vec2(600, 0));
		camera.add_component<Camera>(ivec2(1700, 720), vec2(2000, 800),
									 Camera::Data{
										 .bg_color = Color::RED,
									 });
		camera.add_component<BehaviorScript>().set_script<MoveCameraScript>();
	}

	string get_name() const { return "scene1"; }
};

int main(int argc, char * argv[]) {
	LoopManager gameloop;
	gameloop.add_scene<Scene1>();
	gameloop.start();
	return 0;
}