aboutsummaryrefslogtreecommitdiff
path: root/src/objs/player.lua
blob: 4b75cb34f4bebf88d63ce73b87b365ab41c0c49a (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
72
73
74
PLAYER_SPEED = 100

register_comp("Body", function (ent, x, y, w, h, opts)
  ent.vx = 0
  ent.vy = 0
  ent.box = phys.Box.new(x, y, w, h, opts)
end)

register_comp("Player", TAGCOMP)

function body_sys(ent, dt)
  ent.vx, ent.vy = ent.box:update(ent.vx, ent.vy, dt)
  ent.x = ent.box.x
  ent.y = ent.box.y
end

local tile = 1

function player_movement_sys(player, dt)
  local inpx, inpy = input_direction("Left", "Right", "Up", "Down")
  inpx, inpy = normalize(inpx, inpy)
  player.vx = dlerp(player.vx, inpx * PLAYER_SPEED, 25 * dt)
  player.vy = dlerp(player.vy, inpy * PLAYER_SPEED, 25 * dt)

  -- Testicle stuff, remove when testising is no longer needed
  if not im.has_focus() then
    if is_input_pressed("Right_Click") then
      local scn = get_current_scene()
      assert(scn, "no scene set.")

      local mx, my = get_mouse_pos()
      local tx, ty = to_tile_coords(mx, my)
      set_tile(scn.tilemap, tx, ty, tile)
    end
    if is_input_pressed("Left_Click") then
      local scn = get_current_scene()
      assert(scn, "no scene set.")

      local mx, my = get_mouse_pos()
      local tx, ty = to_tile_coords(mx, my)
      remove_tile(scn.tilemap, tx, ty)
    end
  end
end

function player_ui_sys(_)
  im.begin_window("Room Editor", 120, 5, 180, 320, {})
    im.layout({0.5, 0.75, 1})
    im.text("Tile: " .. tostring(tile))
    if im.button(" - ") then
      tile = math.max(tile - 1, 0)
    end
    if im.button("+ ") then
      tile = tile + 1
    end
    im.layout()

    im.image(TILE_TEX, get_tileset_quad(tile))
  im.end_window()
end

function new_player(x, y)
  local ent = new_entity()
  add_comp(ent, "Body", x, y, 16, 16, {
    offsetx = -8,
    offsety = -8,
    layers = {},
    mask = {"hard"},
  })
  add_comp(ent, "Player")
  add_comp(ent, "Sprite", "res/img/player.ase")
  return ent
end