]> git.lizzy.rs Git - nothing.git/blob - src/game/level_script.c
(#513) Introduce (hide-label) and (show-label)
[nothing.git] / src / game / level_script.c
1 #include <assert.h>
2
3 #include "ebisp/gc.h"
4 #include "ebisp/interpreter.h"
5 #include "ebisp/scope.h"
6 #include "game/level/player/rigid_rect.h"
7 #include "level.h"
8 #include "level_script.h"
9 #include "system/log.h"
10
11 struct EvalResult
12 hide_goal(void *param, Gc *gc, struct Scope *scope, struct Expr args)
13 {
14     assert(param);
15     assert(gc);
16     assert(scope);
17
18     Level * const level = (Level*)param;
19     const char * const goal_id = NULL;
20
21     struct EvalResult result = unpack_args(gc, "s", args, &goal_id);
22     if (result.is_error) {
23         return result;
24     }
25
26     level_hide_goal(level, goal_id);
27
28     return eval_success(NIL(gc));
29 }
30
31 struct EvalResult
32 show_goal(void *param, Gc *gc, struct Scope *scope, struct Expr args)
33 {
34     assert(param);
35     assert(gc);
36     assert(scope);
37
38     Level * const level = (Level*)param;
39     const char *goal_id = NULL;
40
41     struct EvalResult result = unpack_args(gc, "s", args, &goal_id);
42     if (result.is_error) {
43         return result;
44     }
45
46     level_show_goal(level, goal_id);
47
48     return eval_success(NIL(gc));
49 }
50
51 struct EvalResult rect_apply_force(void *param, Gc *gc, struct Scope *scope, struct Expr args)
52 {
53     assert(gc);
54     assert(scope);
55     assert(param);
56
57     /* TODO(#401): rect_apply_force doesn't sanitize it's input */
58
59     Level *level = (Level*) param;
60     const char *rect_id = CAR(args).atom->str;
61     struct Expr vector_force_expr = CAR(CDR(args));
62     const float force_x = (float) CAR(vector_force_expr).atom->num;
63     const float force_y = (float) CDR(vector_force_expr).atom->num;
64
65     print_expr_as_sexpr(stdout, args); printf("\n");
66
67     Rigid_rect *rigid_rect = level_rigid_rect(level, rect_id);
68     if (rigid_rect != NULL) {
69         log_info("Found rect `%s`\n", rect_id);
70         log_info("Applying force (%f, %f)\n", force_x, force_y);
71         rigid_rect_apply_force(rigid_rect, vec(force_x, force_y));
72     } else {
73         log_fail("Couldn't find rigid_rect `%s`\n", rect_id);
74     }
75
76     return eval_success(NIL(gc));
77 }
78
79 struct EvalResult
80 hide_label(void *param, Gc *gc, struct Scope *scope, struct Expr args)
81 {
82     assert(param);
83     assert(gc);
84     assert(scope);
85     (void) args;
86
87     /* TODO: hide-label is not implemented */
88
89     return not_implemented(gc);
90 }
91
92 struct EvalResult
93 show_label(void *param, Gc *gc, struct Scope *scope, struct Expr args)
94 {
95     assert(param);
96     assert(gc);
97     assert(scope);
98     (void) args;
99
100     /* TODO: show-label is not implemented */
101
102     return not_implemented(gc);
103 }
104
105 void load_level_library(Gc *gc, struct Scope *scope, Level *level)
106 {
107     set_scope_value(
108         gc,
109         scope,
110         SYMBOL(gc, "rect-apply-force"),
111         NATIVE(gc, rect_apply_force, level));
112     set_scope_value(
113         gc,
114         scope,
115         SYMBOL(gc, "hide-goal"),
116         NATIVE(gc, hide_goal, level));
117     set_scope_value(
118         gc,
119         scope,
120         SYMBOL(gc, "show-goal"),
121         NATIVE(gc, show_goal, level));
122     set_scope_value(
123         gc,
124         scope,
125         SYMBOL(gc, "show-label"),
126         NATIVE(gc, show_label, level));
127     set_scope_value(
128         gc,
129         scope,
130         SYMBOL(gc, "hide-label"),
131         NATIVE(gc, hide_label, level));
132 }