-
Notifications
You must be signed in to change notification settings - Fork 0
/
xi_systems.c
187 lines (175 loc) · 4.61 KB
/
xi_systems.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include "xi_systems.h"
#include "xi_components.h"
#include "xi.h"
#include "mathutils.h"
#include "graphicsutils.h"
#include "entities.h"
SYSTEM(forces_s){
ARG(v2* position, POSITION_C);
ARG(v2* forces, FORCES_C);
position->x += forces->x;
position->y += forces->y;
forces->x = 0;
forces->y = 0;
}
SYSTEM(blitable_s){
ARG(v2* position, POSITION_C);
ARG(Blitable* sprite, BLITABLE_C);
renderBlitableV2(xi->graphics, sprite, *position);
}
SYSTEM(behavior_s){
ARG(logic_t* behavior, BEHAVIOR_C);
behavior->f(SYSTEM_ARGS);
}
SYSTEM(text_s){
ARG(v2* position, POSITION_C);
ARG(text_node_t* node, TEXT_C);
drawTextC(xi->graphics, position->x, position->y, node->text,
node->red,
node->green,
node->blue,
node->alpha
);
}
SYSTEM(repeater_s){
ARG(repeater_t* wrapper, REPEATER_C);
wrapper->ticks += xi->ticks;
if (wrapper->ticks >= wrapper->trigger_time && wrapper->trigger_count > 0){
wrapper->ticks -= wrapper->trigger_time;
wrapper->f(SYSTEM_ARGS);
wrapper->trigger_count -- ;
if (wrapper->destroy_after && wrapper->trigger_count == 0){
entity_destroy(xi->ecs, id);
}
}
}
SYSTEM(clickable_s){
ARG(v2* position, POSITION_C);
ARG(clickable_t* button, CLICKABLE_C);
if (button->recharge_counter > 0){
button->recharge_counter -= xi->ticks;
}
v2 mouse = mousePos(xi->user_input);
v4 bounds = {
position->x,
position->y,
button->w,
button->h
};
if (
pointInRectV2(mouse, bounds) &&
mousePressed(xi->user_input, 1) &&
button->recharge_counter <= 0
){
button->toggle = !button->toggle;
button->recharge_counter = button->recharge_time;
button->f(SYSTEM_ARGS);
}
}
SYSTEM(draw_clickable_s){
ARG(v2* position, POSITION_C);
ARG(clickable_t* button, CLICKABLE_C);
view v = renderGetView(xi->graphics);
renderSetColor(xi->graphics, 0, 255, 0, 255);
drawRect(xi->graphics, position->x+v.x, position->y+v.y, button->w, button->h, OUTLINE);
renderSetColor(xi->graphics, 0, 0, 0, 255);
}
SYSTEM(animate_s){
ARG(Blitable* sprite, BLITABLE_C);
ARG(animator_t* animation, ANIMATOR_C);
sprite->drawBound.x = animation->pos_x*sprite->drawBound.w;
sprite->drawBound.y = animation->pos_y*sprite->drawBound.h;
animation->frame_time_counter += xi->ticks;
if (animation->frame_time_counter < animation->frame_time){
return;
}
animation->frame_time_counter -= animation->frame_time;
progress_animation(xi->graphics, animation);
}
SYSTEM(draw_entity_colliders_s){
ARG(v2* pos, POSITION_C);
ARG(v4* mask, COLLIDER_C);
v4 translated = {
mask->x + pos->x,
mask->y + pos->y,
mask->w,
mask->h
};
renderSetColor(xi->graphics, 255, 255, 255, 255);
drawRect(xi->graphics, pos->x-1, pos->y-1, 2, 2, FILL);
drawRectV4(xi->graphics, translated, OUTLINE);
renderSetColor(xi->graphics, 0, 0, 0, 255);
}
SYSTEM(draw_world_colliders_s){
renderSetColor(xi->graphics, 255, 255, 255, 255);
spacial_quadtree_node_t* root = xi->colliders;
vquadnode_t q = vquadnode_tInit();
vquadnode_tPushBack(&q, root);
while(q.size != 0){
root = vquadnode_tPop(&q);
if (root->state != INTERNAL_NODE){
if (root->state != 0){
renderSetColor(xi->graphics, 255, 0, 0, 128);
drawRectV4(xi->graphics, root->mask, OUTLINE);
renderSetColor(xi->graphics, 255, 255, 255, 255);
}
continue;
}
vquadnode_tInsert(&q, 0, root->a);
vquadnode_tInsert(&q, 0, root->b);
vquadnode_tInsert(&q, 0, root->c);
vquadnode_tInsert(&q, 0, root->d);
}
vquadnode_tFree(&q);
renderSetColor(xi->graphics, 0, 0, 0, 255);
}
SYSTEM(solid_collision_s){
ARG(v2* pos, POSITION_C);
ARG(v2* forces, FORCES_C);
ARG(v4* mask, COLLIDER_C);
v4 translated = {
mask->x + pos->x,
mask->y + pos->y,
mask->x + pos->x + mask->w,
mask->y + pos->y + mask->h
};
v4 t = translated;
t.x += forces->x;
t.w += forces->x;
vv4_t colliders = vv4_tInit();
find_colliding_partitions(xi->colliders, t, &colliders);
if (collides_with_mask(t, &colliders)){
int32_t increment = sign(forces->x);
int32_t count = 0;
t.x -= forces->x-increment;
t.w -= forces->x-increment;
while (!collides_with_mask(t, &colliders)){
t.x += increment;
t.w += increment;
count++;
}
forces->x = count*increment;
}
pos->x += forces->x;
forces->x = 0;
t = translated;
t.y += forces->y;
t.h += forces->y;
vv4_tClear(&colliders);
find_colliding_partitions(xi->colliders, t, &colliders);
if (collides_with_mask(t, &colliders)){
int32_t increment = sign(forces->y);
int32_t count = 0;
t.y -= forces->y-increment;
t.h -= forces->y-increment;
while (!collides_with_mask(t, &colliders)){
t.y += increment;
t.h += increment;
count++;
}
forces->y = count*increment;
}
pos->y += forces->y;
forces->y = 0;
vv4_tFree(&colliders);
}