aboutsummaryrefslogtreecommitdiff
path: root/src/player.odin
blob: e098167c2d6716508777d20ea0142f3c3c64b7a3 (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
75
76
77
78
79
80
81
82
83
84
85
86
package demonchime

import "core:fmt"
import "core:math"
import "core:math/linalg"

import sapp "shared:sokol/app"

import "draw"

Player :: struct {
  pos: Vec2,
  vel: Vec2,
  anim: draw.Animation,
  sprite: draw.Sprite,
}

PLAYER_SPEED :: 100
PLAYER_ACCEL :: 10
PLAYER_JUMP_FORCE :: 300

init_player :: proc(p: ^Player) {
  p.pos = Vec2{50, 50}

  anim_ok := draw.init_anim_data(&p.anim, "res/robot2.json")
  if !anim_ok {
    fmt.println("coult not load animation")
    sapp.quit()
    return
  }

  draw.init_sprite(&p.sprite, p.anim.image_path, p.anim)

  p.sprite.offset = Vec2{
    -math.floor(f32(p.sprite.width / 2)),
    -f32(p.sprite.height)
  }
}

deinit_player :: proc(p: ^Player) {
  draw.delete_anim_data(p.anim)
}

update_player :: proc(p: ^Player, dt: f32) {
  input: f32

  if is_keybind_down(state.input, state.input.move_left) {
    input -= 1
  }
  if is_keybind_down(state.input, state.input.move_right) {
    input += 1
  }

  if input != 0 {
    draw.set_sprite_active_tag(&p.sprite, "down_run")
    p.sprite.scale.x = math.sign(input)
  } else {
    draw.set_sprite_active_tag(&p.sprite, "down_idle")
  }

  if is_keybind_down(state.input, state.input.jump) && p.pos.y >= 256 {
    p.vel.y = -PLAYER_JUMP_FORCE
  }

  p.vel.x = math.lerp(
    p.vel.x,
    input * PLAYER_SPEED,
    math.pow(0.5, dt * PLAYER_ACCEL),
  )
  p.vel.y = math.min(p.vel.y + GRAVITY * dt, TERMINAL_VELOCITY)

  p.pos += p.vel * dt
  
  if p.pos.y > 256 {
    p.pos.y = 256
    p.vel.y = 0
  }

  p.sprite.pos = p.pos

  draw.update_sprite(&p.sprite, dt)
}

draw_player :: proc(p: Player) {
  draw.draw_sprite(&state.renderer, p.sprite)
}