aboutsummaryrefslogtreecommitdiff
path: root/src/muntik.odin
blob: 6abee4abe184cae38037ddca8f8b429ce3065182 (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
87
88
89
90
package demonchime

import "core:math"
import "core:math/linalg"
import "core:log"
import hm "core:container/handle_map"

import "fw"
import "phys"

muntik_stats := Enemy_Stats{
  max_health = 5,
}

Muntik :: struct {
  using enemy: Enemy,
  walk_dir: f32,
}

make_muntik :: proc(pos: Vec2, direction: f32) -> (int, ^Muntik) {
  sprite: Sprite
  init_sprite(&sprite, .Muntik)
  sprite.offset = {
    f32(sprite.width / 2),
    f32(sprite.height / 2),
  }

  body := phys.make_body(
    phys.Rect{-sprite.offset, {14, 10}},
    layers = {.Enemy},
    mask = {.Player, .Hard},
  )
  phys.set_position(body, pos)

  muntik := make_enemy(
    Muntik,
    &muntik_stats,
    sprite,
    body,
  )

  muntik.ai = .Muntik
  muntik.walk_dir = direction

  return muntik.handle, muntik
}

update_muntik :: proc(e: ^Enemy, dt: f32) {
  m, is_muntik := e.variant.(^Muntik)
  if !is_muntik {
    return
  }

  vel := phys.get_velocity(m.body)
  vel.x = dt_lerp(vel.x, m.walk_dir * MUNTIK_SPEED, MUNTIK_ACCEL)
  vel.y = math.min(vel.y + GRAVITY * dt, TERMINAL_VELOCITY)
  phys.set_velocity(m.body, vel)

  contact_damage(m.body, MUNTIK_DAMAGE)

  pos := phys.get_position(m.body)

  ground_rc_start := Vec2{pos.x + m.walk_dir * 4, pos.y}
  ground_rc := phys.make_raycast(
    ground_rc_start,
    ground_rc_start + {0, 16},
  )

  wall_rc_start := pos
  wall_rc := phys.make_raycast(
    wall_rc_start,
    wall_rc_start + {m.walk_dir * 8, 0},
  )

  if !phys.is_colliding(ground_rc) ||
     phys.is_colliding(wall_rc) {
    m.walk_dir *= -1
  }

  m.sprite.scale.x = -m.walk_dir
  if f32(fw.get_time()) - m.hit_timestamp < 0.1 {
    set_sprite_active_tag(&m.sprite, "hurt")
  } else {
    set_sprite_active_tag(&m.sprite, "walk")
  }
}

object_spawner_muntik :: proc(obj: Object_Resource) {
  make_muntik(obj.pos, 1)
}