aboutsummaryrefslogtreecommitdiff
path: root/src/bullets.c
blob: bd7fc81466f9cf3071e2da64dee3b265585e7d81 (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
#include "bullets.h"
#include "game.h"

Bullet createBulletFromEntity(Entity entity, float damage) {
	return (Bullet){
		.ray = (Ray){
			entity.position,
			Vector3RotateByQuaternion((Vector3){0.0, 0.0, 1.0}, entity.rotation)
		},
		.fromId = entity.id,
		.fromFingerprint = entity.fingerprint,
		.damage = damage
	};
}


Bullet createBulletFromDirection(Entity entity, Vector3 direction, float damage) {
	return (Bullet){
		.ray = (Ray){
			entity.position,
			direction
		},
		.fromId = entity.id,
		.fromFingerprint = entity.fingerprint,
		.damage = damage
	};
}

BulletHitInfo handleBulletHit(Entity * entity, Bullet bullet) {
	// Handle health.
	entity->health -= bullet.damage;
	entity->health = Clamp(entity->health, ENTITY_MIN_HEALTH, ENTITY_MAX_HEALTH);

	// Check hit info.
	BulletHitInfo hitInfo = (BulletHitInfo){
		.hit = true,
		.killed = entity->health == 0.0,
		.hitId = entity->id,
		.hitFingerprint = entity->fingerprint
	};

	return hitInfo;
}

BulletHitInfo shootBullet(World * world, Bullet bullet) {
	EntityId hitId = traceRayToEntityInWorld(world, bullet.ray, bullet.fromFingerprint, true);
	Entity * hitEntity = getEntityFromWorld(*world, hitId);

	// We hit a fucker.
	if (hitEntity != NULL)
		return handleBulletHit(hitEntity, bullet);

	return (BulletHitInfo){
		.hit = false,
		.killed = false,
		.hitId = ENTITY_NONE,
	};
}

BulletHitInfo shootBulletAtEntity(Entity * entity, Bullet bullet) {
	RayCollision collision = traceRayToEntity(*entity, bullet.ray);

	if (collision.hit)
		return handleBulletHit(entity, bullet);

	return (BulletHitInfo){
		.hit = false,
		.killed = false,
		.hitId = ENTITY_NONE,
	};
}