aboutsummaryrefslogtreecommitdiff
path: root/src/example/Forest.cpp
blob: 3c45e0939e9c23b5a7d1c125a8a86abf7d3f78d0 (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
#include "Forest.h"

#include <crepe/api/Animator.h>
#include <crepe/api/BehaviorScript.h>
#include <crepe/api/GameObject.h>
#include <crepe/api/Scene.h>
#include <crepe/api/Script.h>
#include <crepe/api/Sprite.h>
#include <crepe/types.h>

using namespace crepe;
using namespace std;

class ParallaxScript : public Script {
public:
	ParallaxScript(float begin_x, float end_x, std::string unique_bg_name)
		: begin_x(begin_x),
		  end_x(end_x),
		  name(unique_bg_name) {}

	void update() {
		RefVector<Transform> vec_2
			= this->get_components_by_name<Transform>("forest_bg_2_" + name);
		RefVector<Transform> vec_3
			= this->get_components_by_name<Transform>("forest_bg_3_" + name);

		for (Transform & t : vec_2) {
			if (t.position.x > end_x - 400) {
				t.position.x = begin_x - 400;
			}
		}
		for (Transform & t : vec_3) {
			if (t.position.x > end_x - 400) {
				t.position.x = begin_x - 400;
			}
		}
	}

private:
	const float begin_x;
	const float end_x;
	const std::string name;
};

float Forest::create(Scene & scn, float begin_x, std::string unique_bg_name) {
	GameObject script = scn.new_object("forest_script", "background");
	script.add_component<BehaviorScript>().set_script<ParallaxScript>(
		begin_x - 400, begin_x + 3000 + 400, unique_bg_name);

	this->add_background(scn, begin_x, unique_bg_name);

	GameObject begin = scn.new_object("forest_begin", "background", vec2(begin_x, 0));
	Asset begin_asset{"asset/jetpack_joyride/background/forest/forestFG_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 += 800;

	this->add_background(scn, begin_x, unique_bg_name);

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

	this->add_background(scn, begin_x, unique_bg_name);

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

	this->add_background(scn, begin_x, unique_bg_name);

	GameObject end = scn.new_object("forest_end", "background", vec2(begin_x, 0));
	Asset end_asset{"asset/jetpack_joyride/background/forest/forestFG_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 += 600;

	this->add_background(scn, begin_x + 200, unique_bg_name);

	return begin_x;
}

void Forest::add_background(Scene & scn, float begin_x, std::string name) {
	GameObject bg_1
		= scn.new_object("forest_bg_1_" + name, "forest_background", vec2(begin_x, 0));
	Asset bg_1_asset{"asset/jetpack_joyride/background/forest/forestBG1_1_TVOS.png"};
	bg_1.add_component<Sprite>(bg_1_asset, Sprite::Data{
											   .sorting_in_layer = 3,
											   .order_in_layer = 2,
											   .size = vec2(0, 800),
										   });
	GameObject bg_2
		= scn.new_object("forest_bg_2_" + name, "forest_background", vec2(begin_x, 0));
	Asset bg_2_1_asset{"asset/jetpack_joyride/background/forest/forestBG2_1_TVOS.png"};
	bg_2.add_component<Sprite>(bg_2_1_asset, Sprite::Data{
												 .sorting_in_layer = 3,
												 .order_in_layer = 1,
												 .size = vec2(0, 400),
												 .position_offset = vec2(200, 0),
											 });
	Asset bg_2_2_asset{"asset/jetpack_joyride/background/forest/forestBG2_2_TVOS.png"};
	bg_2.add_component<Sprite>(bg_2_2_asset, Sprite::Data{
												 .sorting_in_layer = 3,
												 .order_in_layer = 1,
												 .size = vec2(0, 400),
												 .position_offset = vec2(-200, 0),
											 });
	GameObject bg_3
		= scn.new_object("forest_bg_3_" + name, "forest_background", vec2(begin_x, 0));
	Asset bg_3_1_asset{"asset/jetpack_joyride/background/forest/forestBG3_1_TVOS.png"};
	bg_3.add_component<Sprite>(bg_3_1_asset, Sprite::Data{
												 .sorting_in_layer = 3,
												 .order_in_layer = 0,
												 .size = vec2(0, 200),
												 .position_offset = vec2(300, 0),
											 });
	Asset bg_3_2_asset{"asset/jetpack_joyride/background/forest/forestBG3_2_TVOS.png"};
	bg_3.add_component<Sprite>(bg_3_2_asset, Sprite::Data{
												 .sorting_in_layer = 3,
												 .order_in_layer = 0,
												 .size = vec2(0, 200),
												 .position_offset = vec2(100, 0),
											 });
	Asset bg_3_3_asset{"asset/jetpack_joyride/background/forest/forestBG3_3_TVOS.png"};
	bg_3.add_component<Sprite>(bg_3_3_asset, Sprite::Data{
												 .sorting_in_layer = 3,
												 .order_in_layer = 0,
												 .size = vec2(0, 200),
												 .position_offset = vec2(-100, 0),
											 });
	Asset bg_3_4_asset{"asset/jetpack_joyride/background/forest/forestBG3_4_TVOS.png"};
	bg_3.add_component<Sprite>(bg_3_4_asset, Sprite::Data{
												 .sorting_in_layer = 3,
												 .order_in_layer = 0,
												 .size = vec2(0, 200),
												 .position_offset = vec2(-300, 0),
											 });

	bg_2.add_component<Rigidbody>(Rigidbody::Data{
		.linear_velocity = vec2(30, 0),
	});
	bg_3.add_component<Rigidbody>(Rigidbody::Data{
		.linear_velocity = vec2(40, 0),
	});
}