summaryrefslogtreecommitdiff
path: root/src/Api.h
blob: da763c2a1ffae101c8d0c150ae218b1870d4a41e (plain)
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
#ifndef DNG_API_H
#define DNG_API_H

#include "Level.h"
#include <lua.hpp>
#include <memory>

extern std::shared_ptr<Level> lvl;

static int
c_update_player_pos(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)) {
    lvl->player.x += dx;
    lvl->player.y += dy;
    res = true;
  }

  lua_pushboolean(L, res);

  return 1;
}

static int
c_player_can_move(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 = lvl->canStep(dx, dy);
  lua_pushboolean(L, res);

  return 1;
}

static int
c_enemy_can_move(lua_State* L)
{
  return 1;
}

static int
c_spawn_enemy(lua_State* L)
{
  return 1;
}

static int
c_destroy_enemy(lua_State* L)
{
  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);
  lua_register(L, "c_spawn_enemy", c_spawn_enemy);
  lua_register(L, "c_destroy_enemy", c_destroy_enemy);
}

#endif // DNG_API_H