aboutsummaryrefslogtreecommitdiff
path: root/game/enemy/EnemyBulletScript.cpp
blob: 65c0c23c1cf3748085da626342f3197a46bfe2e9 (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
#include "EnemyBulletScript.h"
#include <crepe/api/Camera.h>
#include <crepe/api/Metadata.h>
#include <crepe/api/Rigidbody.h>
#include <iostream>

#include "EnemyConfig.h"
using namespace crepe;
using namespace std;
void EnemyBulletScript::init() {
	this->subscribe<CollisionEvent>([this](const CollisionEvent & e) -> bool {
		return this->on_collide(e);
	});
}
void EnemyBulletScript::fixed_update(crepe::duration_t dt) {
	Transform & transform = this->get_component<Transform>();
	Camera & camera = this->get_components_by_name<Camera>("camera").front();
	Transform & cam_transform = this->get_components_by_name<Transform>("camera").front();
	Rigidbody & bullet_body = this->get_component<Rigidbody>();
	//move
	transform.position.x += bullet_body.data.linear_velocity.x * dt.count();
	vec2 half_screen = camera.viewport_size / 2;
	float despawn_location = cam_transform.position.x - half_screen.x - 50;
	if (transform.position.x < despawn_location) {
		this->despawn_bullet();
	}
}

void EnemyBulletScript::despawn_bullet() {
	Transform & transform = this->get_component<Transform>();
	Rigidbody & bullet_body = this->get_component<Rigidbody>();
	bullet_body.active = false;
	transform.position = ENEMY_BULLET_POOL_LOCATION;
}

bool EnemyBulletScript::on_collide(const CollisionEvent & e) {
	//cout << "collision happened with " << e.info.other.metadata.tag << endl;
	this->despawn_bullet();
	return false;
}