Cleanup, rename player func and add map interaction.

This commit is contained in:
2026-07-16 07:49:47 +02:00
parent 433240f5b2
commit 99c32198ec
2 changed files with 36 additions and 25 deletions
+2 -3
View File
@@ -3,7 +3,7 @@ package game
import "core:log"
import "core:mem"
// Configuration Variables
// Global Config Variables
// -----------------------------------------------------------------------------
DEBUG_MODE :: true
@@ -15,6 +15,7 @@ GAME_HEIGHT :: 270
WINDOW_TITLE :: "Graveyard"
PLAYER_SPEED :: 180
TILE_SIZE :: 32
// -----------------------------------------------------------------------------
main :: proc() {
context.logger = log.create_console_logger()
@@ -51,5 +52,3 @@ main :: proc() {
}
}
}
+34 -22
View File
@@ -1,5 +1,6 @@
package game
import "core:fmt"
import rl "vendor:raylib"
Direction :: enum { LEFT, RIGHT }
@@ -8,58 +9,69 @@ Player_State :: enum { IDLE, WALK, ATTACK }
Player :: struct {
pos: rl.Vector2,
direction: Direction,
state: Player_State,
state: Player_State,
ani_state: AnimationState,
}
player_init :: proc(assets: ^Player_Assets) -> Player {
player := Player{}
// center for now not really needed
player.pos = {(WINDOW_WIDTH / 2), (WINDOW_HEIGHT / 2)}
player.pos = {320, 320}
player.state = .IDLE
animation_play(&player.ani_state, &assets.clips[.IDLE])
return player
}
update_player :: proc(player: ^Player, assets: ^Player_Assets) {
update_player :: proc(player: ^Player, assets: ^Player_Assets, camera: rl.Camera2D) {
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
if rl.IsKeyDown(.A) {
player.pos.x -= PLAYER_SPEED * dt
player.direction = .LEFT
moving = true
moving = true
}
if rl.IsKeyDown(.D) {
if rl.IsKeyDown(.D) {
player.pos.x += PLAYER_SPEED * dt
player.direction = .RIGHT
moving = true
moving = true
}
if rl.IsMouseButtonPressed(.LEFT) {
pixel_pos := rl.GetMousePosition()
//convert to render size
pixel_pos.x = pixel_pos.x / (WINDOW_WIDTH / GAME_WIDTH)
pixel_pos.y = pixel_pos.y / (WINDOW_HEIGHT / GAME_HEIGHT)
grid_space := world_to_grid(pixel_pos, camera)
fmt.printf("GRID SPACE: %v\n", grid_space)
world_space := grid_to_world(grid_space[0], grid_space[1])
fmt.printf("WORLD SPACE: %v\n", world_space)
}
player.state = moving ? .WALK : .IDLE
animation_play(&player.ani_state, &assets.clips[player.state])
animation_update(&player.ani_state, dt)
}
renderer_draw_player :: proc(player: ^Player) {
clip := player.ani_state.clip
src_rect := animation_frame_rect(&player.ani_state)
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
}
if player.direction == .LEFT {
src_rect.width = -src_rect.width
}
dst_rect := rl.Rectangle{
x = player.pos.x - f32(clip.frame_width) / 2,
y = player.pos.y - f32(clip.frame_height) / 2,
width = f32(clip.frame_width),
height = f32(clip.frame_height),
}
dst_rect := rl.Rectangle{
x = player.pos.x - f32(clip.frame_width) / 2,
y = player.pos.y - f32(clip.frame_height) / 2,
width = f32(clip.frame_width),
height = f32(clip.frame_height),
}
rl.DrawTexturePro(clip.texture, src_rect, dst_rect, rl.Vector2{}, 0, rl.WHITE)
rl.DrawTexturePro(clip.texture, src_rect, dst_rect, rl.Vector2{}, 0, rl.WHITE)
}