Initial player logic.
This commit is contained in:
@@ -0,0 +1,67 @@
|
|||||||
|
package game
|
||||||
|
|
||||||
|
import rl "vendor:raylib"
|
||||||
|
|
||||||
|
PLAYER_SPEED :: 180
|
||||||
|
Direction :: enum { LEFT, RIGHT }
|
||||||
|
Player_State :: enum { IDLE, WALK, ATTACK }
|
||||||
|
|
||||||
|
Player :: struct {
|
||||||
|
pos: rl.Vector2,
|
||||||
|
direction: Direction,
|
||||||
|
state: Player_State,
|
||||||
|
ani_state: AnimationState,
|
||||||
|
}
|
||||||
|
|
||||||
|
player_init :: proc(assets: ^Player_Assets) -> Player {
|
||||||
|
player := Player{}
|
||||||
|
player.pos = {(WINDOW_WIDTH / 2 - 16 * 4), (WINDOW_HEIGHT / 2 - 16 * 4)}
|
||||||
|
player.state = .IDLE
|
||||||
|
animation_play(&player.ani_state, &assets.clips[.IDLE])
|
||||||
|
|
||||||
|
return player
|
||||||
|
}
|
||||||
|
|
||||||
|
update_player :: proc(player: ^Player, assets: ^Player_Assets) {
|
||||||
|
moving := false
|
||||||
|
dt := rl.GetFrameTime()
|
||||||
|
|
||||||
|
if rl.IsKeyDown(.W) { player.pos.y -= PLAYER_SPEED * dt; moving = true }
|
||||||
|
if rl.IsKeyDown(.S) { player.pos.y += PLAYER_SPEED * dt; moving = true }
|
||||||
|
|
||||||
|
if rl.IsKeyDown(.A) {
|
||||||
|
player.pos.x -= PLAYER_SPEED * dt
|
||||||
|
player.direction = .LEFT
|
||||||
|
moving = true
|
||||||
|
}
|
||||||
|
if rl.IsKeyDown(.D) {
|
||||||
|
player.pos.x += PLAYER_SPEED * dt
|
||||||
|
player.direction = .RIGHT
|
||||||
|
moving = true
|
||||||
|
}
|
||||||
|
|
||||||
|
player.state = moving ? .WALK : .IDLE
|
||||||
|
|
||||||
|
animation_play(&player.ani_state, &assets.clips[player.state])
|
||||||
|
animation_update(&player.ani_state, dt)
|
||||||
|
}
|
||||||
|
|
||||||
|
// temp, remove once camera/zoom is in
|
||||||
|
SPRITE_SCALE :: 4
|
||||||
|
renderer_draw_player :: proc(player: ^Player) {
|
||||||
|
clip := player.ani_state.clip
|
||||||
|
src_rect := animation_frame_rect(&player.ani_state)
|
||||||
|
|
||||||
|
if player.direction == .LEFT {
|
||||||
|
src_rect.width = -src_rect.width
|
||||||
|
}
|
||||||
|
|
||||||
|
dst_rect := rl.Rectangle{
|
||||||
|
x = player.pos.x,
|
||||||
|
y = player.pos.y,
|
||||||
|
width = f32(clip.frame_width) * SPRITE_SCALE,
|
||||||
|
height = f32(clip.frame_height) * SPRITE_SCALE,
|
||||||
|
}
|
||||||
|
|
||||||
|
rl.DrawTexturePro(clip.texture, src_rect, dst_rect, rl.Vector2{}, 0, rl.WHITE)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user