aboutsummaryrefslogtreecommitdiff
path: root/src/objs/player.lua
blob: 74d28e817faa3e441b155bff51cbef09021c1dd7 (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
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

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)
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")
end