Cleanup, rename player func and add map interaction.
This commit is contained in:
+2
-3
@@ -3,7 +3,7 @@ package game
|
|||||||
import "core:log"
|
import "core:log"
|
||||||
import "core:mem"
|
import "core:mem"
|
||||||
|
|
||||||
// Configuration Variables
|
// Global Config Variables
|
||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
DEBUG_MODE :: true
|
DEBUG_MODE :: true
|
||||||
|
|
||||||
@@ -15,6 +15,7 @@ GAME_HEIGHT :: 270
|
|||||||
WINDOW_TITLE :: "Graveyard"
|
WINDOW_TITLE :: "Graveyard"
|
||||||
PLAYER_SPEED :: 180
|
PLAYER_SPEED :: 180
|
||||||
TILE_SIZE :: 32
|
TILE_SIZE :: 32
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
main :: proc() {
|
main :: proc() {
|
||||||
context.logger = log.create_console_logger()
|
context.logger = log.create_console_logger()
|
||||||
@@ -51,5 +52,3 @@ main :: proc() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+34
-22
@@ -1,5 +1,6 @@
|
|||||||
package game
|
package game
|
||||||
|
|
||||||
|
import "core:fmt"
|
||||||
import rl "vendor:raylib"
|
import rl "vendor:raylib"
|
||||||
|
|
||||||
Direction :: enum { LEFT, RIGHT }
|
Direction :: enum { LEFT, RIGHT }
|
||||||
@@ -8,58 +9,69 @@ Player_State :: enum { IDLE, WALK, ATTACK }
|
|||||||
Player :: struct {
|
Player :: struct {
|
||||||
pos: rl.Vector2,
|
pos: rl.Vector2,
|
||||||
direction: Direction,
|
direction: Direction,
|
||||||
state: Player_State,
|
state: Player_State,
|
||||||
ani_state: AnimationState,
|
ani_state: AnimationState,
|
||||||
}
|
}
|
||||||
|
|
||||||
player_init :: proc(assets: ^Player_Assets) -> Player {
|
player_init :: proc(assets: ^Player_Assets) -> Player {
|
||||||
player := Player{}
|
player := Player{}
|
||||||
// center for now not really needed
|
player.pos = {320, 320}
|
||||||
player.pos = {(WINDOW_WIDTH / 2), (WINDOW_HEIGHT / 2)}
|
|
||||||
player.state = .IDLE
|
player.state = .IDLE
|
||||||
animation_play(&player.ani_state, &assets.clips[.IDLE])
|
animation_play(&player.ani_state, &assets.clips[.IDLE])
|
||||||
|
|
||||||
return player
|
return player
|
||||||
}
|
}
|
||||||
|
|
||||||
update_player :: proc(player: ^Player, assets: ^Player_Assets) {
|
update_player :: proc(player: ^Player, assets: ^Player_Assets, camera: rl.Camera2D) {
|
||||||
moving := false
|
moving := false
|
||||||
dt := rl.GetFrameTime()
|
dt := rl.GetFrameTime()
|
||||||
|
|
||||||
if rl.IsKeyDown(.W) { player.pos.y -= PLAYER_SPEED * dt; moving = true }
|
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(.S) { player.pos.y += PLAYER_SPEED * dt; moving = true }
|
||||||
|
|
||||||
if rl.IsKeyDown(.A) {
|
if rl.IsKeyDown(.A) {
|
||||||
player.pos.x -= PLAYER_SPEED * dt
|
player.pos.x -= PLAYER_SPEED * dt
|
||||||
player.direction = .LEFT
|
player.direction = .LEFT
|
||||||
moving = true
|
moving = true
|
||||||
}
|
}
|
||||||
if rl.IsKeyDown(.D) {
|
if rl.IsKeyDown(.D) {
|
||||||
player.pos.x += PLAYER_SPEED * dt
|
player.pos.x += PLAYER_SPEED * dt
|
||||||
player.direction = .RIGHT
|
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
|
player.state = moving ? .WALK : .IDLE
|
||||||
|
|
||||||
animation_play(&player.ani_state, &assets.clips[player.state])
|
animation_play(&player.ani_state, &assets.clips[player.state])
|
||||||
animation_update(&player.ani_state, dt)
|
animation_update(&player.ani_state, dt)
|
||||||
}
|
}
|
||||||
|
|
||||||
renderer_draw_player :: proc(player: ^Player) {
|
draw_player :: proc(player: ^Player) {
|
||||||
clip := player.ani_state.clip
|
clip := player.ani_state.clip
|
||||||
src_rect := animation_frame_rect(&player.ani_state)
|
src_rect := animation_frame_rect(&player.ani_state)
|
||||||
|
|
||||||
if player.direction == .LEFT {
|
if player.direction == .LEFT {
|
||||||
src_rect.width = -src_rect.width
|
src_rect.width = -src_rect.width
|
||||||
}
|
}
|
||||||
|
|
||||||
dst_rect := rl.Rectangle{
|
dst_rect := rl.Rectangle{
|
||||||
x = player.pos.x - f32(clip.frame_width) / 2,
|
x = player.pos.x - f32(clip.frame_width) / 2,
|
||||||
y = player.pos.y - f32(clip.frame_height) / 2,
|
y = player.pos.y - f32(clip.frame_height) / 2,
|
||||||
width = f32(clip.frame_width),
|
width = f32(clip.frame_width),
|
||||||
height = f32(clip.frame_height),
|
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)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user