summaryrefslogtreecommitdiffhomepage
path: root/src/bullet.c
blob: 7bd16c57638fc5b6b90826511629c5dadfeb427d (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include "bullet.h"
#include "world.h"
#include "raymath.h"

typedef
struct
{
	int team; // 0 means not in use
	Vector3 position;
	Vector3 direction;
	int lifetime;
}
Bullet;

float bullet_speed = 0.5;

Bullet bullets[100];

	void
spawn_bullet(int team, Vector3 position, Vector3 direction)
{
	if(team == 0)
	{
		return;
	}
	for(int i = 0; i < 100; ++i)
	{
		if(bullets[i].team != 0)
		{
			continue;
		}
		bullets[i].team = team;
		bullets[i].position = position;
		bullets[i].direction = direction;
		bullets[i].lifetime = 100;
		return;
	};
}

	void
render_bullets(void)
{
	for(int i = 0; i < 100; ++i)
	{
		if(bullets[i].team == 0)
		{
			continue;
		}
		else if(bullets[i].team == 1)
		{
			DrawCube(bullets[i].position, 0.25, 0.25, 0.25, BLUE);
		}
		else if(bullets[i].team == 2)
		{
			DrawCube(bullets[i].position, 0.25, 0.25, 0.25, VIOLET);
		}
	}
}

void
bullet_collision_check(void)
{
	for(int i = 0; i < 100; ++i)
	{
		if(bullets[i].team == 0)
		{
			continue;
		}
		bullets[i].lifetime -= 1;
		if(bullets[i].lifetime <= 0)
		{
			bullets[i].team = 0;
		}
		bullets[i].position.x += bullets[i].direction.x * bullet_speed;
		bullets[i].position.y += bullets[i].direction.y * bullet_speed;
		bullets[i].position.z += bullets[i].direction.z * bullet_speed;
		Vector3 enemypos = { 0 };
		if (bullets[i].team == 1)
		{
			enemypos = world.players[1].position;
			if (Vector3Distance(bullets[i].position, enemypos) < 1)
			{
				world.players[0].points++;
				bullets[i].team = 0;
			}
		}
		else if (bullets[i].team == 2)
		{
			enemypos = world.players[0].position;
			if (Vector3Distance(bullets[i].position, enemypos) < 1)
			{
				world.players[1].points++;
				bullets[i].team = 0;
			}
		}
	}

}