-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpentity.cpp
219 lines (195 loc) · 6.6 KB
/
pentity.cpp
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include "pentity.h"
extern "C"
{
#include <dolpmodule.h>
#include "py/obj.h"
#include "py/runtime.h"
}
mp_obj_t dolp_pentity_obj_get_id(mp_obj_t self_in)
{
dolp_pentity_obj_t *self = (dolp_pentity_obj_t *)MP_OBJ_TO_PTR(self_in);
return mp_obj_new_int(self->pentity.getId());
}
mp_obj_t dolp_pentity_obj_set_type(mp_obj_t self_in, mp_obj_t typ)
{
dolp_pentity_obj_t *self = (dolp_pentity_obj_t *)MP_OBJ_TO_PTR(self_in);
int t = mp_obj_get_int(typ);
self->pentity.setType(t);
return mp_const_none;
}
mp_obj_t dolp_pentity_obj_get_type(mp_obj_t self_in)
{
dolp_pentity_obj_t *self = (dolp_pentity_obj_t *)MP_OBJ_TO_PTR(self_in);
return mp_obj_new_int(self->pentity.getType());
}
mp_obj_t dolp_pentity_obj_configure(size_t n_args, const mp_obj_t *args)
{
dolp_pentity_obj_t *self = (dolp_pentity_obj_t *)MP_OBJ_TO_PTR(args[0]);
int x = mp_obj_get_int(args[1]);
int y = mp_obj_get_int(args[2]);
int w = mp_obj_get_int(args[3]);
int h = mp_obj_get_int(args[4]);
self->pentity.configure(x, y, w, h);
return mp_const_none;
}
mp_obj_t dolp_pentity_obj_move_to(size_t n_args, const mp_obj_t *args)
{
dolp_pentity_obj_t *self = (dolp_pentity_obj_t *)MP_OBJ_TO_PTR(args[0]);
int x = mp_obj_get_int(args[1]);
int y = mp_obj_get_int(args[2]);
int v = mp_obj_get_int(args[3]);
self->pentity.moveTo(x, y, v);
return mp_const_none;
}
mp_obj_t dolp_pentity_obj_is_moving(mp_obj_t self_in)
{
dolp_pentity_obj_t *self = (dolp_pentity_obj_t *)MP_OBJ_TO_PTR(self_in);
return mp_obj_new_bool(self->pentity.isMoving());
}
mp_obj_t dolp_pentity_obj_update(mp_obj_t self_in, mp_obj_t cback)
{
// TODO: check if the function params are correct
if (!mp_obj_is_fun(cback))
{
mp_raise_TypeError("passed parameter is not a function");
}
dolp_pentity_obj_t *self = (dolp_pentity_obj_t *)MP_OBJ_TO_PTR(self_in);
self->pentity.update(cback);
return mp_const_none;
}
mp_obj_t dolp_pentity_obj_collision(size_t n_args, const mp_obj_t *args)
{
dolp_pentity_obj_t *self = (dolp_pentity_obj_t *)MP_OBJ_TO_PTR(args[0]);
if (n_args < 2)
{
self->pentity.collision();
return mp_const_none;
}
auto cback = args[1];
if (!mp_obj_is_fun(cback))
{
mp_raise_TypeError("passed parameter is not a function");
}
self->pentity.collision(cback);
return mp_const_none;
}
mp_obj_t dolp_pentity_obj_collided(mp_obj_t self_in, mp_obj_t other_in)
{
dolp_pentity_obj_t *self = (dolp_pentity_obj_t *)MP_OBJ_TO_PTR(self_in);
dolp_pentity_obj_t *other = (dolp_pentity_obj_t *)MP_OBJ_TO_PTR(other_in);
return mp_obj_new_bool(self->pentity.collided(other->pentity));
}
mp_obj_t dolp_pentity_obj_set_state(mp_obj_t self_in, mp_obj_t index, mp_obj_t value)
{
dolp_pentity_obj_t *self = (dolp_pentity_obj_t *)MP_OBJ_TO_PTR(self_in);
int idx = mp_obj_get_int(index);
int val = mp_obj_get_int(value);
self->pentity.setState(idx, val);
return mp_const_none;
}
mp_obj_t dolp_pentity_obj_get_state(mp_obj_t self_in, mp_obj_t index)
{
dolp_pentity_obj_t *self = (dolp_pentity_obj_t *)MP_OBJ_TO_PTR(self_in);
int idx = mp_obj_get_int(index);
return mp_obj_new_int(self->pentity.getState(idx));
}
mp_obj_t dolp_pentity_obj_set_image(size_t n_args, const mp_obj_t *args)
{
dolp_pentity_obj_t *self = (dolp_pentity_obj_t *)MP_OBJ_TO_PTR(args[0]);
mp_buffer_info_t imgbuf;
mp_get_buffer_raise(args[1], &imgbuf, 1);
const uint8_t *b = (const uint8_t *)imgbuf.buf;
self->pentity.setImage(b);
if (n_args > 2)
{
mp_buffer_info_t mskbuf;
mp_get_buffer_raise(args[2], &mskbuf, 1);
const uint8_t *m = (const uint8_t *)mskbuf.buf;
self->pentity.setImage(b, m);
}
return mp_const_none;
}
mp_obj_t dolp_pentity_obj_set_animation(mp_obj_t self_in, mp_obj_t animation_in)
{
if (!mp_obj_is_type(animation_in, &dolp_animation_type))
{
mp_raise_TypeError("you should pass an animation here");
}
dolp_pentity_obj_t *self = (dolp_pentity_obj_t *)MP_OBJ_TO_PTR(self_in);
dolp_animation_obj_t *animation = (dolp_animation_obj_t *)MP_OBJ_TO_PTR(animation_in);
self->pentity.setAnimation(animation->animation);
return mp_const_none;
}
mp_obj_t dolp_pentity_obj_new_timeout(mp_obj_t self_in, mp_obj_t index)
{
dolp_pentity_obj_t *self = (dolp_pentity_obj_t *)MP_OBJ_TO_PTR(self_in);
int idx = mp_obj_get_int(index);
return mp_obj_new_int(self->pentity.newTimeout(idx));
}
mp_obj_t dolp_pentity_obj_delete_timeout(mp_obj_t self_in, mp_obj_t index)
{
dolp_pentity_obj_t *self = (dolp_pentity_obj_t *)MP_OBJ_TO_PTR(self_in);
int idx = mp_obj_get_int(index);
self->pentity.deleteTimeout(idx);
return mp_const_none;
}
mp_obj_t dolp_pentity_obj_get_timeout(mp_obj_t self_in, mp_obj_t index)
{
dolp_pentity_obj_t *self = (dolp_pentity_obj_t *)MP_OBJ_TO_PTR(self_in);
int idx = mp_obj_get_int(index);
return mp_obj_new_int(self->pentity.getTimeout(idx));
}
mp_obj_t dolp_pentity_obj_get_x(mp_obj_t self_in)
{
dolp_pentity_obj_t *self = (dolp_pentity_obj_t *)MP_OBJ_TO_PTR(self_in);
return mp_obj_new_int(self->pentity.getX());
}
mp_obj_t dolp_pentity_obj_set_x(mp_obj_t self_in, mp_obj_t value)
{
dolp_pentity_obj_t *self = (dolp_pentity_obj_t *)MP_OBJ_TO_PTR(self_in);
int val = mp_obj_get_int(value);
self->pentity.setX(val);
return mp_const_none;
}
mp_obj_t dolp_pentity_obj_get_y(mp_obj_t self_in)
{
dolp_pentity_obj_t *self = (dolp_pentity_obj_t *)MP_OBJ_TO_PTR(self_in);
return mp_obj_new_int(self->pentity.getY());
}
mp_obj_t dolp_pentity_obj_set_y(mp_obj_t self_in, mp_obj_t value)
{
dolp_pentity_obj_t *self = (dolp_pentity_obj_t *)MP_OBJ_TO_PTR(self_in);
int val = mp_obj_get_int(value);
self->pentity.setY(val);
return mp_const_none;
}
mp_obj_t dolp_pentity_obj_delete(mp_obj_t self_in)
{
dolp_pentity_obj_t *self = (dolp_pentity_obj_t *)MP_OBJ_TO_PTR(self_in);
dolp.deleteEntity(self->pentity);
return mp_const_none;
}
mp_obj_t pentity_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args)
{
mp_arg_check_num(n_args, n_kw, 0, 0, true);
dolp_pentity_obj_t *p = m_new_obj(dolp_pentity_obj_t);
auto ent = dolp.newEntity();
p->base.type = &dolp_pentity_type;
p->pentity = ent;
return MP_OBJ_FROM_PTR(p);
}
void pentity_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind)
{
(void)kind;
dolp_pentity_obj_t *self = (dolp_pentity_obj_t *)MP_OBJ_TO_PTR(self_in);
mp_print_str(print, "entity(");
mp_print_str(print, "id: ");
mp_obj_print_helper(print, mp_obj_new_int(self->pentity.getId()), PRINT_REPR);
mp_print_str(print, ", type: ");
mp_obj_print_helper(print, mp_obj_new_int(self->pentity.getType()), PRINT_REPR);
mp_print_str(print, ", x: ");
mp_obj_print_helper(print, mp_obj_new_int(self->pentity.getX()), PRINT_REPR);
mp_print_str(print, ", y: ");
mp_obj_print_helper(print, mp_obj_new_int(self->pentity.getY()), PRINT_REPR);
mp_print_str(print, ")");
}