aboutsummaryrefslogtreecommitdiff
path: root/game/scheduler/ObjectsScheduler.cpp
blob: 3ce2018f3aebf66073eca697f64240dc9e239988 (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
#include "ObjectsScheduler.h"

#include "../Config.h"
#include "../Random.h"
#include "../missile/SpawnEvent.h"
#include "api/Rigidbody.h"
#include "api/Transform.h"
#include "enemy/BattleScript.h"
#include "prefab/ZapperPoolSubScene.h"

using namespace crepe;
void ObjectsScheduler::preset_0() {
	trigger_event<MissileSpawnEvent>(MissileSpawnEvent {});
	trigger_event<MissileSpawnEvent>(MissileSpawnEvent {});
}
void ObjectsScheduler::preset_1() { trigger_event<MissileSpawnEvent>(MissileSpawnEvent {}); }
void ObjectsScheduler::preset_2() { trigger_event<CreateZapperEvent>(CreateZapperEvent {}); }
void ObjectsScheduler::preset_3() { trigger_event<CreateZapperEvent>(CreateZapperEvent {}); }
void ObjectsScheduler::preset_4() {}
void ObjectsScheduler::boss_fight_1() {
	this->get_components_by_name<Rigidbody>("camera").front().get().data.linear_velocity.x = 0;
	this->get_components_by_name<Rigidbody>("player").front().get().data.linear_velocity.x = 0;
	this->trigger_event<BattleStartEvent>(BattleStartEvent {.num_enemies = 2});

	RefVector<Rigidbody> rb_back_forest
		= this->get_components_by_tag<Rigidbody>("forest_background");
	for (Rigidbody & rb : rb_back_forest) {
		rb.data.linear_velocity.x = 0;
	}
}

bool ObjectsScheduler::boss_fight_1_event() {
	this->get_components_by_name<Rigidbody>("camera").front().get().data.linear_velocity.x
		= PLAYER_SPEED * 0.02;
	this->get_components_by_name<Rigidbody>("player").front().get().data.linear_velocity.x
		= PLAYER_SPEED * 0.02;

	RefVector<Rigidbody> rb_back_forest
		= this->get_components_by_tag<Rigidbody>("forest_background");
	rb_back_forest.front().get().data.linear_velocity.x = 30;
	rb_back_forest.back().get().data.linear_velocity.x = 40;

	return false;
}

void ObjectsScheduler::init() {
	this->obstacles.push_back([this]() { preset_0(); });
	this->obstacles.push_back([this]() { preset_1(); });
	this->obstacles.push_back([this]() { preset_2(); });
	this->obstacles.push_back([this]() { preset_3(); });
	this->obstacles.push_back([this]() { preset_4(); });

	this->obstacles.push_back([this]() { boss_fight_1(); });

	// subscribe to battlewonevent
	this->subscribe<BattleWonEvent>([this](const BattleWonEvent & ev) -> bool {
		return this->boss_fight_1_event();
	});
}

void ObjectsScheduler::fixed_update(duration_t dt) {
	int pos_x
		= (int) this->get_components_by_name<Transform>("camera").front().get().position.x;

	int boss_check = (pos_x - this->start_offset) / this->boss_fight_interval;
	if (boss_check > this->last_boss_check) {
		this->obstacles.back()();
		this->last_boss_check = boss_check;
	}
	int obstacle_check = (pos_x - this->start_offset) / this->obstacle_interval;
	if (obstacle_check > this->last_obstacle_check) {
		this->obstacles[Random::i(this->obstacles.size() - 1, 0)]();
		this->last_obstacle_check = obstacle_check;
	}
}