-
Notifications
You must be signed in to change notification settings - Fork 0
/
unit.c
executable file
·78 lines (66 loc) · 2.48 KB
/
unit.c
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
#include "unit.h"
#include "vector.h"
#include <math.h>
#include <stdlib.h>
void unit_initialize(Unit * unit) {
vector_initialize(&unit->position);
vector_initialize(&unit->target_position);
unit->direction = 0;
unit->angular_throttle = 0;
unit->throttle = 0;
unit->head_direction = 0;
unit->head_throttle = 0;
unit->next_fire_interval = INFINITY;
unit->health = UNIT_INITIAL_HEALTH;
unit->team_id = 0;
unit->closest_friendly_unit = NULL;
unit->closest_enemy_unit = NULL;
unit->closest_friendly_factory = NULL;
unit->closest_enemy_factory = NULL;
unit->mode = 0;
}
static void unit_update_straight_movement(Unit * unit, float delta) {
float forward_movement = unit->throttle * UNIT_PIXELS_PER_SECOND * delta;
unit->position.x -= forward_movement * sinf(unit->direction);
unit->position.y += forward_movement * cosf(unit->direction);
}
static void unit_update_angular_movement(Unit * unit, float delta) {
float angular_motion = unit->angular_throttle * UNIT_RADIANS_PER_SECOND;
float movement_radius = unit->throttle * UNIT_PIXELS_PER_SECOND / angular_motion;
float angular_movement = angular_motion * delta;
Vector movement = {
movement_radius * sinf(angular_movement),
movement_radius * cosf(angular_movement) - movement_radius
};
vector_rotate(&movement, unit->direction);
vector_add(&unit->position, movement);
unit->direction += angular_movement;
}
static void unit_update_head_direction(Unit * unit, float delta) {
unit->head_direction += unit->head_throttle * UNIT_HEAD_RADIANS_PER_SECOND * delta;
}
static void unit_update_health_regeneration(Unit * unit, float delta) {
unit->health += UNIT_HEALTH_REGENERATION * delta;
if(unit->health > UNIT_INITIAL_HEALTH)
unit->health = UNIT_INITIAL_HEALTH;
}
void unit_update(Unit * unit, float delta) {
if(unit->throttle > 0 && unit->angular_throttle == 0)
unit_update_straight_movement(unit, delta);
else if(unit->angular_throttle != 0)
unit_update_angular_movement(unit, delta);
if(unit->head_throttle != 0)
unit_update_head_direction(unit, delta);
if(unit->throttle <= 1.0f && unit->health > 0.0f && unit->health < UNIT_INITIAL_HEALTH)
unit_update_health_regeneration(unit, delta);
unit->next_fire_interval -= delta;
}
int unit_is_firing(Unit * unit) {
return isfinite(unit->next_fire_interval);
}
int unit_is_dead(Unit * unit) {
return unit->health <= 0;
}
float unit_health_percentage(Unit * unit) {
return unit->health <= 0 ? 0 : unit->health / UNIT_INITIAL_HEALTH;
}