aboutsummaryrefslogtreecommitdiff
path: root/game/workers/WorkerScript.cpp
blob: b0bfc4e1772983eb230de3fa6701f63913bd287d (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
#include "WorkerScript.h"

#include "../Config.h"
#include "api/BehaviorScript.h"

#include <crepe/api/Animator.h>
#include <crepe/api/Rigidbody.h>
#include <crepe/api/Sprite.h>
#include <crepe/api/Transform.h>
#include <crepe/types.h>
#include <cstdlib>

using namespace crepe;
using namespace std;

void WorkerScript::fixed_update(duration_t dt) {
	RefVector<Rigidbody> rb_workers = this->get_components_by_tag<Rigidbody>("worker");
	RefVector<Transform> trans_workers = this->get_components_by_tag<Transform>("worker");

	Rigidbody & rb_cam = this->get_components_by_name<Rigidbody>("camera").back();
	Transform & trans_cam = this->get_components_by_name<Transform>("camera").back();

	int counter = 0;
	for (Rigidbody & rb_worker : rb_workers) {
		Transform & trans_worker = trans_workers.at(counter);

		float result_x = rb_cam.data.linear_velocity.x - rb_worker.data.linear_velocity.x;

		if (result_x > 0) {
			float left_cam_pos_x = trans_cam.position.x - VIEWPORT_X / 2;
			if (trans_worker.position.x < left_cam_pos_x - 1000) {
				trans_worker.position.x = left_cam_pos_x + VIEWPORT_X + 1000;

				do {
					float min_value = -2500 * dt.count();
					float max_value = 2500 * dt.count();
					rb_worker.data.linear_velocity.x
						= min_value
						  + static_cast<float>(rand())
								/ (static_cast<float>(RAND_MAX / (max_value - min_value)));
				} while (rb_worker.data.linear_velocity.x < 500 * dt.count()
						 && rb_worker.data.linear_velocity.x > -500 * dt.count());

				RefVector<Sprite> sprite_worker
					= this->get_components_by_id<Sprite>(trans_worker.game_object_id);
				RefVector<Animator> animator_worker
					= this->get_components_by_id<Animator>(trans_worker.game_object_id);
				BehaviorScript & bs_panic
					= this->get_components_by_id<BehaviorScript>(trans_worker.game_object_id)
						  .front();

				if (rb_worker.data.linear_velocity.x < 0) {
					sprite_worker.front().get().data.flip.flip_x = true;
					sprite_worker.back().get().data.flip.flip_x = true;

					animator_worker.front().get().data.fps
						= -rb_worker.data.linear_velocity.x / 5;
					animator_worker.back().get().data.fps
						= -rb_worker.data.linear_velocity.x / 5;
					animator_worker.front().get().set_anim(0);
					animator_worker.back().get().set_anim(0);
					animator_worker.front().get().active = true;
					animator_worker.back().get().active = true;
				} else {
					sprite_worker.front().get().data.flip.flip_x = false;
					sprite_worker.back().get().data.flip.flip_x = false;

					animator_worker.front().get().data.fps
						= rb_worker.data.linear_velocity.x / 5;
					animator_worker.back().get().data.fps
						= rb_worker.data.linear_velocity.x / 5;
					animator_worker.front().get().set_anim(0);
					animator_worker.back().get().set_anim(0);
					animator_worker.front().get().active = true;
					animator_worker.back().get().active = true;
				}

				trans_worker.rotation = 0;
				bs_panic.active = true;
				rb_worker.data.linear_velocity_coefficient = {1, 1};
				for (Sprite & sprite : sprite_worker) {
					sprite.data.position_offset.x = 0;
				}
			}
		} else {
			float right_cam_pos_x = trans_cam.position.x + VIEWPORT_X / 2;
			if (trans_worker.position.x > right_cam_pos_x + 1000) {
				do {
					float min_value = -2500 * dt.count();
					float max_value = 2500 * dt.count();
					rb_worker.data.linear_velocity.x
						= min_value
						  + static_cast<float>(rand())
								/ (static_cast<float>(RAND_MAX / (max_value - min_value)));
				} while (rb_worker.data.linear_velocity.x < 500 * dt.count()
						 && rb_worker.data.linear_velocity.x > -500 * dt.count());

				RefVector<Sprite> sprite_worker
					= this->get_components_by_id<Sprite>(trans_worker.game_object_id);
				RefVector<Animator> animator_worker
					= this->get_components_by_id<Animator>(trans_worker.game_object_id);
				BehaviorScript & bs_panic
					= this->get_components_by_id<BehaviorScript>(trans_worker.game_object_id)
						  .front();

				if (rb_worker.data.linear_velocity.x < 0) {
					sprite_worker.front().get().data.flip.flip_x = true;
					sprite_worker.back().get().data.flip.flip_x = true;

					animator_worker.front().get().data.fps
						= -rb_worker.data.linear_velocity.x / 5;
					animator_worker.back().get().data.fps
						= -rb_worker.data.linear_velocity.x / 5;

					animator_worker.front().get().set_anim(0);
					animator_worker.back().get().set_anim(0);
					animator_worker.front().get().active = true;
					animator_worker.back().get().active = true;
				} else {
					sprite_worker.front().get().data.flip.flip_x = false;
					sprite_worker.back().get().data.flip.flip_x = false;

					animator_worker.front().get().data.fps
						= rb_worker.data.linear_velocity.x / 5;
					animator_worker.back().get().data.fps
						= rb_worker.data.linear_velocity.x / 5;

					animator_worker.front().get().set_anim(0);
					animator_worker.back().get().set_anim(0);
					animator_worker.front().get().active = true;
					animator_worker.back().get().active = true;
				}

				trans_worker.rotation = 0;
				bs_panic.active = true;
				rb_worker.data.linear_velocity_coefficient = {1, 1};
				for (Sprite & sprite : sprite_worker) {
					sprite.data.position_offset.x = 0;
				}
			}
		}

		counter++;
	}
}