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

using namespace crepe;
using namespace std;

void PanicFromPlayerScript::fixed_update(duration_t dt) {
	Animator & anim_player = this->get_components_by_name<Animator>("player").front();

	if (anim_player.data.col == 1) {
		Transform & trans_player = this->get_components_by_name<Transform>("player").back();
		Transform & trans_worker = this->get_components<Transform>().back();

		float result_x = trans_player.position.x - trans_worker.position.x;

		if (result_x < 100 && result_x > -20) {
			RefVector<Animator> anim_worker = this->get_components<Animator>();
			RefVector<Sprite> sprite_worker = this->get_components<Sprite>();
			Rigidbody & rb_worker = this->get_components<Rigidbody>().back();

			if (anim_worker.front().get().data.col != 1) {
				anim_worker.front().get().set_anim(1);
				anim_worker.back().get().set_anim(1);

				anim_worker.front().get().data.fps = 10;
				anim_worker.back().get().data.fps = 10;
			}

			if (result_x < 0) {
				float min_value = 8000;
				float max_value = 10000;
				float value = min_value
							  + static_cast<float>(rand())
									/ (static_cast<float>(RAND_MAX / (max_value - min_value)));
				rb_worker.data.linear_velocity.x = value * dt.count();
				sprite_worker.front().get().data.flip.flip_x = false;
				sprite_worker.back().get().data.flip.flip_x = false;
			} else {
				float min_value = -4000;
				float max_value = -5000;
				float value = min_value
							  + static_cast<float>(rand())
									/ (static_cast<float>(RAND_MAX / (max_value - min_value)));
				rb_worker.data.linear_velocity.x = value * dt.count();
				sprite_worker.front().get().data.flip.flip_x = true;
				sprite_worker.back().get().data.flip.flip_x = true;
			}
		}
	}
};