]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/ecs/_lib.inc
Move PM_walk landing event to ecs
[xonotic/xonotic-data.pk3dir.git] / qcsrc / ecs / _lib.inc
1 /** Components always interpolate from the previous state */
2 #define COMPONENT(com) \
3         void com_##com##_interpolate(entity it, float a); \
4         .bool com_##com
5
6 #define FOREACH_COMPONENT(com, body) FOREACH_ENTITY_FLOAT(com_##com, true, body)
7
8
9 #define EVENT(T, args) .bool evt_##T##_listener; .void args evt_##T
10
11 #define emit(T, ...) \
12         MACRO_BEGIN \
13         FOREACH_ENTITY_FLOAT_ORDERED(evt_##T##_listener, true, it.evt_##T(__VA_ARGS__)); \
14         MACRO_END
15
16 #define subscribe(listener, T, fn) \
17         MACRO_BEGIN \
18         listener.evt_##T = (fn); \
19         listener.evt_##T##_listener = true; \
20         MACRO_END
21
22
23 /**
24  * framelimit 0 is no limit, interpolation does not apply
25  * framerate below minfps will result in less than 100% speed
26  */
27 #define SYSTEM(sys, frameLimit, minfps) \
28         void sys_##sys##_update(entity this, float dt); \
29         float autocvar_xon_sys_##sys##_dt = ((frameLimit) ? (1 / (frameLimit)) : 0); \
30         float autocvar_xon_sys_##sys##_minfps = (1 / (1 / (minfps)))
31
32 #define SYSTEM_UPDATE(sys) \
33         MACRO_BEGIN \
34         static float t = 0; \
35         float dt = autocvar_xon_sys_##sys##_dt; \
36         float minfps = autocvar_xon_sys_##sys##_minfps; \
37         static float accumulator = 0; \
38         float a = 0; \
39         if (dt) { \
40                 accumulator += min(frametime, 1 / (minfps)); \
41         } else { \
42                 accumulator += frametime; \
43                 dt = accumulator; \
44                 a = 1; \
45         } \
46         while (accumulator >= dt) \
47         { \
48                 time = t; \
49                 FOREACH_COMPONENT(sys, sys_##sys##_update(it, dt)); \
50                 t += dt; \
51                 accumulator -= dt; \
52         } \
53         if (!a) a = accumulator / dt; \
54         FOREACH_COMPONENT(sys, com_##sys##_interpolate(it, a)); \
55         MACRO_END
56
57
58 #include "_mod.inc"
59 #include "components/_mod.inc"
60 #include "events/_mod.inc"
61 #include "systems/_mod.inc"