forked from alexeykarnachev/gefest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprojectile.cpp
76 lines (57 loc) · 2.08 KB
/
projectile.cpp
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
#include "projectile.hpp"
#include "collider.hpp"
#include "constants.hpp"
#include "health.hpp"
#include "raylib/raylib.h"
#include "raylib/raymath.h"
#include "registry.hpp"
#include "resources.hpp"
#include "transform.hpp"
namespace gefest::projectile {
static float THICKNESS = 0.15f * constants::SCALE;
Matrix get_matrix(transform::Transform tr, float length) {
Matrix t = MatrixTranslate(tr.position.x, tr.position.y, tr.position.z);
Matrix r = MatrixMultiply(
MatrixRotateX(DEG2RAD * -90.0), QuaternionToMatrix(tr.rotation)
);
Matrix s = MatrixScale(THICKNESS, length, THICKNESS);
Matrix mat = MatrixMultiply(s, MatrixMultiply(r, t));
return mat;
}
Projectile::Projectile(entt::entity entity, entt::entity owner, float speed, float damage)
: entity(entity)
, owner(owner)
, speed(speed)
, damage(damage) {}
void Projectile::update() {
if (this->ttl <= 0.0) {
registry::registry.destroy(this->entity);
return;
}
auto &tr = registry::registry.get<transform::Transform>(this->entity);
Vector3 velocity = Vector3Scale(tr.get_forward(), this->speed);
Vector3 step = Vector3Scale(velocity, constants::DT);
float length = Vector3Length(step);
Matrix mat = get_matrix(tr, length);
Vector3 start = tr.position;
Vector3 end = Vector3Add(start, step);
auto view = registry::registry.view<collider::Collider>();
for (auto target : view) {
auto collider = registry::registry.get<collider::Collider>(target);
bool is_hit = collider.check_line_collision(start, end);
if (!is_hit) continue;
registry::registry.destroy(this->entity);
health::Health *health = registry::registry.try_get<health::Health>(target);
if (health) health->receive_damage(this->damage);
break;
}
this->ttl -= constants::DT;
this->matrix = mat;
tr.position = end;
}
void Projectile::draw() {
Mesh mesh = resources::CYLINDER_MESH;
Material material = resources::PROJECTILE_MATERIAL;
DrawMesh(mesh, material, this->matrix);
}
} // namespace gefest::projectile