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

#include <crepe/api/Animator.h>
#include <crepe/api/BehaviorScript.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 CollisionScript::init() {
	subscribe<CollisionEvent>([this](const CollisionEvent & ev) -> bool {
		return this->on_collision(ev);
	});
}

bool CollisionScript::on_collision(const CollisionEvent & ev) {
	RefVector<Animator> animators = this->get_components<Animator>();
	RefVector<Sprite> sprites = this->get_components<Sprite>();
	Rigidbody & rb = this->get_component<Rigidbody>();
	Transform & tr = this->get_component<Transform>();
	BehaviorScript & bs_panic = this->get_components<BehaviorScript>().front();

	if (ev.info.other.metadata.tag == "zapper") {
		for (Animator & anim : animators) {
			anim.active = false;
			anim.set_anim(3);
		}
		for (Sprite & sprite : sprites) {
			sprite.data.position_offset.x = 15;
		}
		rb.data.linear_velocity_coefficient = {0.5, 0.5};
		tr.rotation = 90;
		bs_panic.active = false;

		return false;
	} else if (ev.info.other.metadata.tag == "laser") {
		for (Animator & anim : animators) {
			anim.active = false;
			anim.set_anim(3);
		}
		for (Sprite & sprite : sprites) {
			sprite.data.position_offset.x = 15;
		}
		rb.data.linear_velocity_coefficient = {0.5, 0.5};
		tr.rotation = 90;
		bs_panic.active = false;

		return false;
	} else if (ev.info.other.metadata.tag == "missile") {
		for (Animator & anim : animators) {
			anim.active = false;
			anim.set_anim(3);
		}
		for (Sprite & sprite : sprites) {
			sprite.data.position_offset.x = 15;
		}
		rb.data.linear_velocity_coefficient = {0.5, 0.5};
		tr.rotation = 90;
		bs_panic.active = false;

		return false;
	}

	return false;
}