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

#include "../Config.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);

				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;
				} 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;
				}
			}
		} 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);

				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;
				} 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;
				}
			}
		}

		counter++;
	}
}