aboutsummaryrefslogtreecommitdiff
path: root/src/player.odin
diff options
context:
space:
mode:
Diffstat (limited to 'src/player.odin')
-rw-r--r--src/player.odin90
1 files changed, 45 insertions, 45 deletions
diff --git a/src/player.odin b/src/player.odin
index c758f31..abed174 100644
--- a/src/player.odin
+++ b/src/player.odin
@@ -30,7 +30,7 @@ Player_State :: enum {
// there will only ever be one player, so just make it a global :)
player: struct {
- body_handle: phys.Body_Handle,
+ body: phys.Body_Handle,
sprite: Sprite,
gun_sprite: Sprite,
jump_buffer: f32,
@@ -43,14 +43,12 @@ player: struct {
}
init_player :: proc() {
- handle, body := phys.make_body(
+ body := phys.make_body(
phys.Rect{{-4, -16}, {8, 16}},
layers = {.PLAYER},
mask = {.DEFAULT}
)
- player.body_handle = handle
-
- phys.set_body_position(handle, Vec2{50, 100})
+ player.body = body
init_sprite(&player.sprite, .PLAYER)
@@ -68,7 +66,7 @@ init_player :: proc() {
}
deinit_player :: proc() {
- phys.remove_body(player.body_handle)
+ phys.remove_body(player.body)
}
@(private = "file")
@@ -104,13 +102,16 @@ _default_state :: proc(dt: f32) {
return
}
- body := phys.get_body(player.body_handle)
+ // body := phys.get_body(player.body_handle)
+ pos := phys.get_position(player.body)
+ vel := phys.get_velocity(player.body)
+ collisions := phys.get_collisions(player.body)
- if .DOWN in body.collisions {
+ if .DOWN in collisions {
player.coyote_time = PLAYER_COYOTE_TIME
player.has_double_jumped = false
} else {
- switch body.vel.y {
+ switch vel.y {
case -math.INF_F32..<-50:
set_sprite_active_tag(&player.sprite, "jump_up")
case 50..<math.INF_F32:
@@ -120,12 +121,12 @@ _default_state :: proc(dt: f32) {
}
}
- rel_mouse_pos := get_mouse_pos() - body.pos
+ rel_mouse_pos := get_mouse_pos() - pos
if is_keybind_just_down(actions.shoot) {
mouse_dir := linalg.normalize0(rel_mouse_pos)
- bullet_pos := body.pos
+ bullet_pos := pos
bullet_pos.y -= PLAYER_GUN_HEIGHT
bullet_pos += mouse_dir * PLAYER_GUN_DIST
@@ -133,49 +134,49 @@ _default_state :: proc(dt: f32) {
bullet_pos,
mouse_dir * 200,
)
-
- // `make_bullet()` adds a new physics body, which could invalidate the
- // pointer to our body when the body array reallocs
- body = phys.get_body(player.body_handle)
}
player.sprite.scale.x = math.sign(rel_mouse_pos.x)
if player.jump_buffer > 0 {
if player.coyote_time > 0 {
- body.vel.y = -PLAYER_JUMP_FORCE
+ vel.y = -PLAYER_JUMP_FORCE
player.jump_buffer = 0
player.coyote_time = 0
} else if !player.has_double_jumped {
- body.vel.y = -PLAYER_DOUBLE_JUMP_FORCE
+ vel.y = -PLAYER_DOUBLE_JUMP_FORCE
player.has_double_jumped = true
player.jump_buffer = 0
}
}
- if .DOWN not_in body.collisions &&
+ if .DOWN not_in collisions &&
!is_keybind_down(actions.jump) &&
- body.vel.y < PLAYER_JUMP_RELEASE_CUT {
- body.vel.y = PLAYER_JUMP_RELEASE_CUT
+ vel.y < PLAYER_JUMP_RELEASE_CUT {
+ vel.y = PLAYER_JUMP_RELEASE_CUT
}
- body.vel.x = math.lerp(
- body.vel.x,
+ vel.x = math.lerp(
+ vel.x,
input * PLAYER_SPEED,
math.pow(0.5, dt * PLAYER_ACCEL),
)
- body.vel.y = math.min(body.vel.y + GRAVITY * dt, TERMINAL_VELOCITY)
+ vel.y = math.min(vel.y + GRAVITY * dt, TERMINAL_VELOCITY)
+
+ phys.set_velocity(player.body, vel)
- phys.update_body(player.body_handle)
+ phys.update_body(player.body)
}
@(private = "file")
_enter_dash :: proc() {
- body := phys.get_body(player.body_handle)
// the sprite x scale is the direction the player is facing :)
- body.vel = {math.sign(player.sprite.scale.x) * PLAYER_DASH_SPEED, 0}
+ phys.set_velocity(
+ player.body,
+ {math.sign(player.sprite.scale.x) * PLAYER_DASH_SPEED, 0},
+ )
player.dash_timer = PLAYER_DASH_TIME
player.state = .DASH
@@ -183,12 +184,12 @@ _enter_dash :: proc() {
@(private = "file")
_dash_state :: proc(dt: f32) {
- phys.update_body(player.body_handle)
+ phys.update_body(player.body)
player.dash_timer -= dt
- body := phys.get_body(player.body_handle)
+ // body := phys.get_body(player.body_handle)
- if player.dash_timer <= 0 || body.collisions != {} {
+ if player.dash_timer <= 0 || phys.get_collisions(player.body) != {} {
_exit_dash()
}
}
@@ -199,31 +200,30 @@ _exit_dash :: proc() {
player.dash_cooldown = PLAYER_DASH_COOLDOWN
- body := phys.get_body(player.body_handle)
- body.vel /= 2
+ vel := phys.get_velocity(player.body)
+ phys.set_velocity(player.body, vel / 2)
}
@(private = "file")
_change_rooms :: proc() {
- body := phys.get_body(player.body_handle)
-
width := f32(current_room.width)
height := f32(current_room.height)
- if body.pos.x < 0 ||
- body.pos.x > width ||
- body.pos.y < 0 ||
- body.pos.y > height {
+ pos := phys.get_position(player.body)
+
+ if pos.x < 0 ||
+ pos.x > width ||
+ pos.y < 0 ||
+ pos.y > height {
prev_room_pos := Vec2{f32(current_room.x), f32(current_room.y)}
- changed := open_room_at({i32(body.pos.x), i32(body.pos.y)})
+ changed := open_room_at({i32(pos.x), i32(pos.y)})
if changed {
new_room_pos := Vec2{f32(current_room.x), f32(current_room.y)}
diff := prev_room_pos - new_room_pos
- body = phys.get_body(player.body_handle)
- new_pos := body.pos + diff - {0, 0}
- phys.set_body_position(player.body_handle, new_pos)
+ new_pos := pos + diff - {0, 0}
+ phys.set_position(player.body, new_pos)
}
}
}
@@ -234,12 +234,12 @@ update_player :: proc(dt: f32) {
case .DASH: _dash_state(dt)
}
- body := phys.get_body(player.body_handle)
+ pos := phys.get_position(player.body)
- player.sprite.pos = body.pos
+ player.sprite.pos = pos
update_sprite(&player.sprite, dt)
- player.gun_sprite.pos = body.pos - Vec2{0, PLAYER_GUN_HEIGHT}
+ player.gun_sprite.pos = pos - Vec2{0, PLAYER_GUN_HEIGHT}
mouse_dir := get_mouse_pos() - player.gun_sprite.pos
player.gun_sprite.rotation = math.atan2(
@@ -270,5 +270,5 @@ object_spawner_player_spawn :: proc(obj: Object_Resource) {
}
already_spawned_player = true
- phys.set_body_position(player.body_handle, obj.pos)
+ phys.set_position(player.body, obj.pos)
}