aboutsummaryrefslogtreecommitdiff
path: root/src/objs/player.lua
blob: 1d89f8a3dd881db0b141d42b3703158e683ff6ea (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
PLAYER_SPEED = 100
PLAYER_ACCEL = 30
PLAYER_JUMP_FORCE = 350

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 inputx = bton(is_input_pressed("Right")) - bton(is_input_pressed("Left"))

  player.vx = dlerp(player.vx, inputx * PLAYER_SPEED, PLAYER_ACCEL * dt)
  player.vy = math.min(player.vy + GRAVITY * dt, TERMINAL_VELOCITY)

  if player.box:touching_down() and is_input_just_pressed("Jump") then
    player.vy = -PLAYER_JUMP_FORCE
  end
end

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