tr_thing.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. #include "eruta.h"
  2. #include "toruby.h"
  3. #include "rh.h"
  4. #include "state.h"
  5. #include "image.h"
  6. #include "fifi.h"
  7. #include "store.h"
  8. #include "scegra.h"
  9. #include "sound.h"
  10. #include <mruby/hash.h>
  11. #include <mruby/class.h>
  12. #include <mruby/data.h>
  13. #include <mruby/array.h>
  14. #include "tr_macro.h"
  15. #include "tr_thing.h"
  16. static mrb_value tr_newthing(mrb_state * mrb, mrb_value self) {
  17. int thing = -1;
  18. State * state = state_get();
  19. int result;
  20. mrb_int kind, x, y, z, w, h;
  21. (void) self;
  22. mrb_get_args(mrb, "iiiiii", &kind, &x, &y, &z, &w, &h);
  23. if (kind < 0) {
  24. return mrb_nil_value();
  25. }
  26. thing = state_newthingindex(state, kind, x, y, z, w, h);
  27. return mrb_fixnum_value(thing);
  28. }
  29. static mrb_value tr_thing_sprite_(mrb_state * mrb, mrb_value self) {
  30. State * state = state_get();
  31. int result;
  32. mrb_int thing, sprite;
  33. (void) self;
  34. mrb_get_args(mrb, "ii", &thing, &sprite);
  35. result = state_thing_sprite_(state, thing, sprite);
  36. return mrb_fixnum_value(result);
  37. }
  38. static mrb_value tr_thing_pose_(mrb_state * mrb, mrb_value self) {
  39. State * state = state_get();
  40. int result;
  41. mrb_int thing, pose;
  42. (void) self;
  43. mrb_get_args(mrb, "ii", &thing, &pose);
  44. result = state_thing_pose_(state, thing, pose);
  45. return mrb_fixnum_value(result);
  46. }
  47. static mrb_value tr_thing_direction_(mrb_state * mrb, mrb_value self) {
  48. State * state = state_get();
  49. int result;
  50. mrb_int thing, direction;
  51. (void) self;
  52. mrb_get_args(mrb, "ii", &thing, &direction);
  53. result = state_thing_direction_(state, thing, direction);
  54. return mrb_fixnum_value(result);
  55. }
  56. static mrb_value tr_thing_v_(mrb_state * mrb, mrb_value self) {
  57. State * state = state_get();
  58. Thing * thing = NULL;
  59. int result;
  60. mrb_int thingid; mrb_float x, y;
  61. (void) self;
  62. mrb_get_args(mrb, "iff", &thingid, &x, &y);
  63. thing = state_thing(state, thingid);
  64. if (!thing) {
  65. return mrb_nil_value();
  66. }
  67. thing_v_(thing, bevec(x , y));
  68. return mrb_fixnum_value(thingid);
  69. }
  70. struct tr_thing_find_helper {
  71. mrb_state * mrb;
  72. mrb_value * results;
  73. };
  74. static int tr_thing_find_callback(Thing * thing, void * extra) {
  75. struct tr_thing_find_helper * helper = extra;
  76. mrb_value id = mrb_fixnum_value(thing_id(thing));
  77. mrb_ary_push(helper->mrb, (*helper->results), id);
  78. return 0;
  79. }
  80. static mrb_value tr_thing_find_in_rectangle
  81. (mrb_state * mrb, mrb_value self) {
  82. mrb_value results;
  83. int x, y, w, h;
  84. struct tr_thing_find_helper helper;
  85. State * state = state_get();
  86. Area * area = state_area(state);
  87. (void) self;
  88. mrb_get_args(mrb, "iiii", &x, &y, &w, &h);
  89. results = mrb_ary_new(mrb);
  90. helper.mrb = mrb;
  91. helper.results = &results;
  92. area_find_things(area, x, y, w, h, &helper, tr_thing_find_callback);
  93. return results;
  94. }
  95. /* Converts a bevec to an array of 2 floats */
  96. mrb_value bevec2mrb(mrb_state * mrb, BeVec vec) {
  97. mrb_value vals[2];
  98. mrb_value marr;
  99. vals[0] = mrb_fixnum_value(vec.x);
  100. vals[1] = mrb_fixnum_value(vec.y);
  101. marr = mrb_ary_new_from_values(mrb, 2, vals);
  102. return marr;
  103. }
  104. static mrb_value tr_thing_v(mrb_state * mrb, mrb_value self) {
  105. State * state = state_get();
  106. Thing * thing = NULL;
  107. BeVec result;
  108. mrb_int thingid; mrb_float x, y;
  109. (void) self;
  110. mrb_get_args(mrb, "i", &thingid);
  111. thing = state_thing(state, thingid);
  112. if (!thing) {
  113. return mrb_nil_value();
  114. }
  115. result = thing_v(thing);
  116. return bevec2mrb(mrb, result);
  117. }
  118. static mrb_value tr_thing_tint(mrb_state * mrb, mrb_value self) {
  119. State * state = state_get();
  120. int result = 0;
  121. mrb_int rindex = -1;
  122. mrb_int rlayer = -1;
  123. mrb_int rr = 255;
  124. mrb_int rg = 255;
  125. mrb_int rb = 255;
  126. mrb_int ra = 255;
  127. (void) self;
  128. mrb_get_args(mrb, "iiiiii", &rindex, &rlayer, &rr, &rg, &rb, &ra);
  129. if ((rindex<0) || (rlayer<0)) {
  130. return mrb_nil_value();
  131. }
  132. result =
  133. state_thing_tint_layer(state, rindex, rlayer, rr, rg, rb, ra);
  134. return mrb_fixnum_value(result);
  135. }
  136. TR_THING_II_INT(tr_thing_hide_layer, thing_hide_layer)
  137. TR_THING_I_INT(tr_thing_is_layer_hidden, thing_is_layer_hidden)
  138. TR_THING_II_INT(tr_thing_set_action_loop, thing_set_action_loop)
  139. TR_THING_I_INT(tr_thing_get_action_loop, thing_get_action_loop)
  140. TR_THING_I_BOOL(tr_thing_is_action_done, thing_is_action_done)
  141. TR_THING_III_INT(tr_thing_set_pose_direction_loop, thing_set_pose_direction_loop)
  142. TR_THING_II_INT(tr_thing_get_pose_direction_loop, thing_get_pose_direction_loop)
  143. /* Define getters for various dimensions of thing's bounds box. */
  144. TR_PAIR_DO(TR_THING_IGETTER, thing_w)
  145. TR_PAIR_DO(TR_THING_IGETTER, thing_h)
  146. TR_PAIR_DO(TR_THING_IGETTER, thing_x)
  147. TR_PAIR_DO(TR_THING_IGETTER, thing_y)
  148. TR_PAIR_DO(TR_THING_IGETTER, thing_cx)
  149. TR_PAIR_DO(TR_THING_IGETTER, thing_cy)
  150. TR_PAIR_DO(TR_THING_IGETTER, thing_z)
  151. TR_PAIR_DO(TR_THING_IGETTER, thing_direction)
  152. TR_PAIR_DO(TR_THING_IGETTER, thing_pose)
  153. TR_WRAP_I_INT(tr_thing_get_unused_id, state_get_unused_thing_id);
  154. TR_AREA_I_INT(tr_area_hull_flags, area_hull_flags);
  155. TR_AREA_II_INT(tr_area_hull_flags_, area_hull_flags_);
  156. TR_AREA_II_INT(tr_area_set_hull_flag, area_set_hull_flag);
  157. TR_AREA_II_INT(tr_area_unset_hull_flag, area_unset_hull_flag);
  158. TR_AREA_I_INT(tr_area_thing_hull_flags, area_thing_hull_flags);
  159. TR_AREA_II_INT(tr_area_thing_hull_flags_, area_thing_hull_flags_);
  160. TR_AREA_II_INT(tr_area_set_thing_hull_flag, area_set_thing_hull_flag);
  161. TR_AREA_II_INT(tr_area_unset_thing_hull_flag, area_unset_thing_hull_flag);
  162. TR_AREA_I_INT(tr_area_thing_hull_group, area_thing_hull_group);
  163. TR_AREA_II_INT(tr_area_thing_hull_group_, area_thing_hull_group_);
  164. TR_WRAP_I_INT(tr_delete_thing, state_delete_thing);
  165. /** Initialize mruby bindings to the physics engine and physical thing
  166. * functionality. Eru is the parent module, which is normally named "Eruta" on the
  167. * ruby side. */
  168. int tr_thing_init(mrb_state * mrb, struct RClass * eru) {
  169. struct RClass *thi;
  170. struct RClass *krn;
  171. struct RClass *kin;
  172. struct RClass *fla;
  173. /* Thing class/module and class/module methods. */
  174. thi = mrb_define_class_under(mrb, eru, "Thing" , mrb->object_class);
  175. krn = mrb_module_get(mrb, "Kernel");
  176. kin = mrb_define_module_under(mrb, thi, "Kind");
  177. fla = mrb_define_module_under(mrb, thi, "Flag");
  178. TR_METHOD_ARGC(mrb, krn, "thing_new" , tr_newthing, 7);
  179. TR_CLASS_METHOD_ARGC(mrb, thi, "thing_new", tr_newthing, 7);
  180. TR_CLASS_METHOD_ARGC(mrb, thi, "v" , tr_thing_v , 1);
  181. TR_CLASS_METHOD_ARGC(mrb, thi, "v_" , tr_thing_v_, 3);
  182. TR_CLASS_METHOD_ARGC(mrb, thi, "find_in_rectangle", tr_thing_find_in_rectangle, 4);
  183. TR_CLASS_METHOD_ARGC(mrb, thi, "sprite_", tr_thing_sprite_, 2);
  184. TR_CLASS_METHOD_ARGC(mrb, thi, "pose_" , tr_thing_pose_, 2);
  185. TR_CLASS_METHOD_ARGC(mrb, thi, "direction_", tr_thing_direction_, 2);
  186. TR_CLASS_METHOD_NOARG(mrb, thi, "x" , tr_thing_x);
  187. TR_CLASS_METHOD_NOARG(mrb, thi, "y" , tr_thing_y);
  188. TR_CLASS_METHOD_NOARG(mrb, thi, "z" , tr_thing_z);
  189. TR_CLASS_METHOD_NOARG(mrb, thi, "cx", tr_thing_cx);
  190. TR_CLASS_METHOD_NOARG(mrb, thi, "cy", tr_thing_cy);
  191. TR_CLASS_METHOD_NOARG(mrb, thi, "h" , tr_thing_h);
  192. TR_CLASS_METHOD_NOARG(mrb, thi, "w" , tr_thing_w);
  193. TR_CLASS_METHOD_NOARG(mrb, thi, "direction", tr_thing_direction);
  194. TR_CLASS_METHOD_NOARG(mrb, thi, "pose" , tr_thing_pose);
  195. TR_CLASS_METHOD_ARGC(mrb, thi, "get_unused_id" , tr_thing_get_unused_id, 1);
  196. TR_CLASS_METHOD_ARGC(mrb, thi, "tint_rgba" , tr_thing_tint, 6);
  197. TR_CLASS_METHOD_ARGC(mrb, thi, "hide_layer" , tr_thing_hide_layer, 3);
  198. TR_CLASS_METHOD_ARGC(mrb, thi, "layer_hidden?" , tr_thing_is_layer_hidden, 2);
  199. TR_CLASS_METHOD_ARGC(mrb, thi, "set_action_loop", tr_thing_set_action_loop, 3);
  200. TR_CLASS_METHOD_ARGC(mrb, thi, "get_action_loop", tr_thing_get_action_loop, 2);
  201. TR_CLASS_METHOD_ARGC(mrb, thi, "action_done?" , tr_thing_is_action_done, 3);
  202. TR_CLASS_METHOD_ARGC(mrb, thi, "set_pose_direction_loop", tr_thing_set_pose_direction_loop, 4);
  203. TR_CLASS_METHOD_ARGC(mrb, thi, "get_pose_direction_loop", tr_thing_get_pose_direction_loop, 3);
  204. TR_CLASS_METHOD_ARGC(mrb, thi, "set_hull_flag" , tr_area_set_thing_hull_flag, 2);
  205. TR_CLASS_METHOD_ARGC(mrb, thi, "unset_hull_flag", tr_area_unset_thing_hull_flag, 2);
  206. TR_CLASS_METHOD_ARGC(mrb, thi, "hull_flags" , tr_area_thing_hull_flags, 1);
  207. TR_CLASS_METHOD_ARGC(mrb, thi, "hull_flags_" , tr_area_thing_hull_flags_, 2);
  208. TR_CLASS_METHOD_ARGC(mrb, thi, "group" , tr_area_thing_hull_group, 1);
  209. TR_CLASS_METHOD_ARGC(mrb, thi, "group_" , tr_area_thing_hull_group_, 2);
  210. TR_CLASS_METHOD_ARGC(mrb, thi, "delete" , tr_delete_thing, 1);
  211. /* Collision types of a thing */
  212. TR_CONST_INT_EASY(mrb, fla, BUMP_FLAG_, NORMAL);
  213. TR_CONST_INT_EASY(mrb, fla, BUMP_FLAG_, SENSOR);
  214. TR_CONST_INT_EASY(mrb, fla, BUMP_FLAG_, DISABLED);
  215. TR_CONST_INT_EASY(mrb, fla, BUMP_FLAG_, USER1);
  216. TR_CONST_INT_EASY(mrb, fla, BUMP_FLAG_, USER2);
  217. TR_CONST_INT_EASY(mrb, fla, BUMP_FLAG_, USER3);
  218. TR_CONST_INT_EASY(mrb, fla, BUMP_FLAG_, USER4);
  219. TR_CONST_INT_EASY(mrb, kin, BUMP_KIND_, PLAYER);
  220. TR_CONST_INT_EASY(mrb, kin, BUMP_KIND_, NPC);
  221. TR_CONST_INT_EASY(mrb, kin, BUMP_KIND_, FOE);
  222. TR_CONST_INT_EASY(mrb, kin, BUMP_KIND_, ATTACK);
  223. TR_CONST_INT_EASY(mrb, kin, BUMP_KIND_, ART);
  224. TR_CONST_INT_EASY(mrb, kin, BUMP_KIND_, SEARCH);
  225. return 0;
  226. }