summaryrefslogtreecommitdiff
path: root/src/Api.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/Api.h')
-rw-r--r--src/Api.h93
1 files changed, 68 insertions, 25 deletions
diff --git a/src/Api.h b/src/Api.h
index da763c2..f9e7e8d 100644
--- a/src/Api.h
+++ b/src/Api.h
@@ -7,16 +7,24 @@
extern std::shared_ptr<Level> lvl;
-static int
-c_update_player_pos(lua_State* L)
-{
+static int c_get_player_position(lua_State *L) {
+ lua_createtable(L, 0, 2);
+ lua_pushnumber(L, lvl->player.y + 1);
+ lua_setfield(L, -2, "y");
+ lua_pushnumber(L, lvl->player.x + 1);
+ lua_setfield(L, -2, "x");
+
+ return 1;
+}
+
+static int c_move_player(lua_State *L) {
// stack ordering
int dy = static_cast<int>(lua_tonumber(L, -1));
int dx = static_cast<int>(lua_tonumber(L, -2));
bool res = false;
- if (lvl->canStep(dx, dy)) {
+ if (lvl->playerCanStep(dx, dy)) {
lvl->player.x += dx;
lvl->player.y += dy;
res = true;
@@ -27,45 +35,80 @@ c_update_player_pos(lua_State* L)
return 1;
}
-static int
-c_player_can_move(lua_State* L)
-{
+static int c_move_enemy(lua_State *L) {
// stack ordering
int dy = static_cast<int>(lua_tonumber(L, -1));
int dx = static_cast<int>(lua_tonumber(L, -2));
+ int id = static_cast<int>(lua_tonumber(L, -3));
+
+ int i = lvl->getEnemyIndex(id);
+ // guard against enemy not found
+ if (i == -1) {
+ lua_pushboolean(L, false);
+ return 1;
+ }
+
+ bool res = false;
+ if (lvl->enemyCanStep(lvl->enemyPositions[i], dx, dy)) {
+ lvl->enemyPositions[i].x += dx;
+ lvl->enemyPositions[i].y += dy;
+ res = true;
+ }
- bool res = lvl->canStep(dx, dy);
lua_pushboolean(L, res);
return 1;
}
-static int
-c_enemy_can_move(lua_State* L)
-{
- return 1;
-}
+static int c_get_enemies(lua_State *L) {
+ lua_createtable(L, int(lvl->enemyPositions.size()), 0);
+
+ int idx = 0;
+
+ for (auto &pos : lvl->enemyPositions) {
+ lua_pushnumber(L, ++idx);
+ lua_createtable(L, 0, 3);
+ lua_pushnumber(L, pos.id);
+ lua_setfield(L, -2, "id");
+ lua_pushnumber(L, pos.x + 1);
+ lua_setfield(L, -2, "x");
+ lua_pushnumber(L, pos.y + 1);
+ lua_setfield(L, -2, "y");
+ lua_settable(L, -3);
+ }
-static int
-c_spawn_enemy(lua_State* L)
-{
return 1;
}
-static int
-c_destroy_enemy(lua_State* L)
-{
+static int c_spawn_enemy(lua_State *L) { return 1; }
+
+static int c_destroy_enemy(lua_State *L) { return 1; }
+
+static int c_get_map(lua_State *L) {
+ lua_createtable(L, int(lvl->map.size()), 0);
+ int idx = 0;
+ for (auto &vec : lvl->map) {
+ lua_pushnumber(L, ++idx);
+ lua_createtable(L, int(vec.size()), 0);
+ int inner_idx = 0;
+ for (auto &c : vec) {
+ lua_pushnumber(L, ++inner_idx);
+ lua_pushnumber(L, c == WALL_SPACE ? 1 : 0);
+ lua_rawset(L, -3);
+ }
+ lua_rawset(L, -3);
+ }
return 1;
}
-static void
-init_c_api(lua_State* L)
-{
- lua_register(L, "c_update_player_pos", c_update_player_pos);
- lua_register(L, "c_player_can_move", c_player_can_move);
- lua_register(L, "c_enemy_can_move", c_enemy_can_move);
+static void init_c_api(lua_State *L) {
+ lua_register(L, "c_move_player", c_move_player);
+ lua_register(L, "c_move_enemy", c_move_enemy);
lua_register(L, "c_spawn_enemy", c_spawn_enemy);
lua_register(L, "c_destroy_enemy", c_destroy_enemy);
+ lua_register(L, "c_get_enemies", c_get_enemies);
+ lua_register(L, "c_get_player_position", c_get_player_position);
+ lua_register(L, "c_get_map", c_get_map);
}
#endif // DNG_API_H \ No newline at end of file