Compare commits
6 Commits
refactor
...
087fc4a16c
| Author | SHA1 | Date | |
|---|---|---|---|
| 087fc4a16c | |||
| 9301fe2696 | |||
| c7cfd5eac0 | |||
| 4876b486a8 | |||
| 7390b1d6da | |||
|
|
352d69bdc6 |
Binary file not shown.
@@ -13,12 +13,16 @@ main :: proc() {
|
|||||||
raylib.SetConfigFlags(config_flags)
|
raylib.SetConfigFlags(config_flags)
|
||||||
|
|
||||||
raylib.InitWindow(1920, 1080, "Game")
|
raylib.InitWindow(1920, 1080, "Game")
|
||||||
|
defer raylib.CloseWindow()
|
||||||
raylib.SetTargetFPS(60)
|
raylib.SetTargetFPS(60)
|
||||||
|
|
||||||
|
raylib.SetExitKey(nil)
|
||||||
|
|
||||||
init_world()
|
init_world()
|
||||||
|
defer deinit_world()
|
||||||
|
|
||||||
player = {
|
player = {
|
||||||
position = {0, 0},
|
position = {300, 300},
|
||||||
camera = {
|
camera = {
|
||||||
zoom = 4,
|
zoom = 4,
|
||||||
offset = {f32(raylib.GetScreenWidth()) / 2, f32(raylib.GetScreenHeight()) / 2},
|
offset = {f32(raylib.GetScreenWidth()) / 2, f32(raylib.GetScreenHeight()) / 2},
|
||||||
@@ -48,8 +52,6 @@ main :: proc() {
|
|||||||
|
|
||||||
update(delta)
|
update(delta)
|
||||||
}
|
}
|
||||||
|
|
||||||
raylib.CloseWindow()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@(private = "file")
|
@(private = "file")
|
||||||
|
|||||||
@@ -3,12 +3,15 @@ package main
|
|||||||
import "core:math"
|
import "core:math"
|
||||||
import "vendor:raylib"
|
import "vendor:raylib"
|
||||||
|
|
||||||
PLAYER_SPRINT_SPEED :: 2.5
|
PLAYER_DEFAULT_SPRINT_SPEED :: 100
|
||||||
PLAYER_SPEED :: 1.5
|
PLAYER_DEFAULT_SPEED :: 60
|
||||||
PLAYER_WIDTH :: 32
|
PLAYER_WIDTH :: 32
|
||||||
PLAYER_HEIGHT :: 32
|
PLAYER_HEIGHT :: 32
|
||||||
PLAYER_SPRITE_PATH :: "assets/player/player.png"
|
PLAYER_SPRITE_PATH :: "assets/player/player.png"
|
||||||
|
|
||||||
|
player_speed := PLAYER_DEFAULT_SPEED
|
||||||
|
player_sprint_speed := PLAYER_DEFAULT_SPRINT_SPEED
|
||||||
|
|
||||||
spritesheet: raylib.Texture2D
|
spritesheet: raylib.Texture2D
|
||||||
framesX: i32
|
framesX: i32
|
||||||
framesY: i32
|
framesY: i32
|
||||||
@@ -48,43 +51,46 @@ handle_player_camera :: proc(p: ^Player, delta: f32) {
|
|||||||
|
|
||||||
@(private = "file")
|
@(private = "file")
|
||||||
handle_player_input :: proc(p: ^Player, delta: f32) {
|
handle_player_input :: proc(p: ^Player, delta: f32) {
|
||||||
dir: raylib.Vector2 = {0, 0}
|
|
||||||
|
|
||||||
|
if raylib.IsKeyPressed(.ESCAPE) do return
|
||||||
|
|
||||||
|
dir: raylib.Vector2 = {0, 0}
|
||||||
if raylib.IsKeyDown(.W) do dir.y -= 1
|
if raylib.IsKeyDown(.W) do dir.y -= 1
|
||||||
if raylib.IsKeyDown(.S) do dir.y += 1
|
if raylib.IsKeyDown(.S) do dir.y += 1
|
||||||
if raylib.IsKeyDown(.A) do dir.x -= 1
|
if raylib.IsKeyDown(.A) do dir.x -= 1
|
||||||
if raylib.IsKeyDown(.D) do dir.x += 1
|
if raylib.IsKeyDown(.D) do dir.x += 1
|
||||||
|
|
||||||
is_moving := dir.x != 0 || dir.y != 0
|
if dir.x != 0 || dir.y != 0 {
|
||||||
|
|
||||||
if is_moving {
|
|
||||||
is_sprinting := raylib.IsKeyDown(.LEFT_SHIFT)
|
is_sprinting := raylib.IsKeyDown(.LEFT_SHIFT)
|
||||||
dir = raylib.Vector2Normalize(dir)
|
dir = raylib.Vector2Normalize(dir)
|
||||||
|
|
||||||
speed: f32 = is_sprinting ? PLAYER_SPRINT_SPEED : PLAYER_SPEED
|
speed_val := f32(is_sprinting ? player_sprint_speed : player_speed)
|
||||||
p.animator.anim.fps = is_sprinting ? 11 : 6
|
p.animator.anim.fps = is_sprinting ? i32(player_sprint_speed / 10) : i32(player_speed / 10)
|
||||||
|
|
||||||
velocity := dir * speed
|
velocity := dir * speed_val * delta
|
||||||
|
|
||||||
new_pos_x := p.position
|
foot_y := p.position.y + f32(p.sprite.height)
|
||||||
new_pos_x.x += velocity.x
|
box_half_w: f32 = 4.0
|
||||||
|
center_offset_x := f32(p.sprite.width) * 0.5
|
||||||
|
|
||||||
foot_offset_y := f32(p.sprite.height)
|
next_x := p.position.x + velocity.x
|
||||||
foot_offset_x := f32(p.sprite.width) * 0.5
|
if !is_wall_at({next_x + center_offset_x - box_half_w, foot_y}) &&
|
||||||
|
!is_wall_at({next_x + center_offset_x + box_half_w, foot_y}) {
|
||||||
if !is_wall_at({new_pos_x.x + foot_offset_x, new_pos_x.y + foot_offset_y}) {
|
p.position.x = next_x
|
||||||
p.position.x = new_pos_x.x
|
|
||||||
}
|
}
|
||||||
|
|
||||||
new_pos_y := p.position
|
next_y := p.position.y + velocity.y
|
||||||
new_pos_y.y += velocity.y
|
if !is_wall_at(
|
||||||
if !is_wall_at({p.position.x + foot_offset_x, new_pos_y.y + foot_offset_y}) {
|
{p.position.x + center_offset_x - box_half_w, next_y + f32(p.sprite.height)},
|
||||||
p.position.y = new_pos_y.y
|
) &&
|
||||||
|
!is_wall_at(
|
||||||
|
{p.position.x + center_offset_x + box_half_w, next_y + f32(p.sprite.height)},
|
||||||
|
) {
|
||||||
|
p.position.y = next_y
|
||||||
}
|
}
|
||||||
|
|
||||||
if dir.x < 0 do p.facing_left = true
|
if dir.x < 0 do p.facing_left = true
|
||||||
if dir.x > 0 do p.facing_left = false
|
if dir.x > 0 do p.facing_left = false
|
||||||
|
|
||||||
p.state = .WALKING
|
p.state = .WALKING
|
||||||
} else {
|
} else {
|
||||||
p.state = .IDLE
|
p.state = .IDLE
|
||||||
@@ -113,6 +119,9 @@ update_player :: proc(p: ^Player, delta: f32) {
|
|||||||
handle_player_input(p, delta)
|
handle_player_input(p, delta)
|
||||||
handle_player_camera(p, delta)
|
handle_player_camera(p, delta)
|
||||||
|
|
||||||
|
// Remove this in release
|
||||||
|
handle_debug_inputs()
|
||||||
|
|
||||||
if (p.state == .IDLE) {
|
if (p.state == .IDLE) {
|
||||||
set_sprite_animation(&p.animator, &idle_animation)
|
set_sprite_animation(&p.animator, &idle_animation)
|
||||||
}
|
}
|
||||||
@@ -142,8 +151,25 @@ is_wall_at :: proc(world_pos: raylib.Vector2) -> bool {
|
|||||||
gx := int(math.floor(world_pos.x / tile_size))
|
gx := int(math.floor(world_pos.x / tile_size))
|
||||||
gy := int(math.floor(world_pos.y / tile_size))
|
gy := int(math.floor(world_pos.y / tile_size))
|
||||||
|
|
||||||
tile := get_tile(&ground_layer_grid, gx, gy)
|
if gx < 0 || gx >= ground_layer_grid.width || gy < 0 || gy >= ground_layer_grid.height {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
tile := get_tile(&ground_layer_grid, gx, gy)
|
||||||
return tile != nil && tile.type == .WALL
|
return tile != nil && tile.type == .WALL
|
||||||
}
|
}
|
||||||
|
|
||||||
|
handle_debug_inputs :: proc() {
|
||||||
|
if (raylib.IsKeyPressed(.KP_ADD)) do player_sprint_speed += 10
|
||||||
|
if (raylib.IsKeyPressed(.KP_SUBTRACT)) do player_sprint_speed -= 10
|
||||||
|
|
||||||
|
if (raylib.IsKeyDown(.LEFT_CONTROL) && raylib.IsKeyPressed(.EQUAL)) {
|
||||||
|
player.camera.zoom += 0.5
|
||||||
|
}
|
||||||
|
|
||||||
|
if (raylib.IsKeyDown(.LEFT_CONTROL) && raylib.IsKeyPressed(.MINUS)) {
|
||||||
|
player.camera.zoom -= 0.5
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ TILEMAP_TILE_SIZE :: 16
|
|||||||
|
|
||||||
Tile :: struct {
|
Tile :: struct {
|
||||||
frame_index: i32,
|
frame_index: i32,
|
||||||
color: raylib.Color,
|
|
||||||
type: TileType,
|
type: TileType,
|
||||||
interaction: InteractionType,
|
interaction: InteractionType,
|
||||||
resource: ResourceType,
|
resource: ResourceType,
|
||||||
@@ -38,7 +37,6 @@ InteractionType :: enum {
|
|||||||
nothing_tile := Tile {
|
nothing_tile := Tile {
|
||||||
type = .NOTHING,
|
type = .NOTHING,
|
||||||
frame_index = 0,
|
frame_index = 0,
|
||||||
color = raylib.WHITE,
|
|
||||||
interaction = .NONE,
|
interaction = .NONE,
|
||||||
resource = .NONE,
|
resource = .NONE,
|
||||||
animator = nil,
|
animator = nil,
|
||||||
@@ -47,7 +45,6 @@ nothing_tile := Tile {
|
|||||||
ground_tile := Tile {
|
ground_tile := Tile {
|
||||||
type = .GROUND,
|
type = .GROUND,
|
||||||
frame_index = 0,
|
frame_index = 0,
|
||||||
color = raylib.WHITE,
|
|
||||||
interaction = .NONE,
|
interaction = .NONE,
|
||||||
resource = .NONE,
|
resource = .NONE,
|
||||||
animator = nil,
|
animator = nil,
|
||||||
@@ -55,8 +52,7 @@ ground_tile := Tile {
|
|||||||
|
|
||||||
test_wall_tile := Tile {
|
test_wall_tile := Tile {
|
||||||
type = .WALL,
|
type = .WALL,
|
||||||
frame_index = 1,
|
frame_index = 5,
|
||||||
color = raylib.WHITE,
|
|
||||||
interaction = .NONE,
|
interaction = .NONE,
|
||||||
resource = .NONE,
|
resource = .NONE,
|
||||||
animator = nil,
|
animator = nil,
|
||||||
@@ -65,7 +61,6 @@ test_wall_tile := Tile {
|
|||||||
plant_tile := Tile {
|
plant_tile := Tile {
|
||||||
type = .FLORA,
|
type = .FLORA,
|
||||||
frame_index = 0,
|
frame_index = 0,
|
||||||
color = raylib.WHITE,
|
|
||||||
interaction = .HARVEST,
|
interaction = .HARVEST,
|
||||||
resource = .FLORA,
|
resource = .FLORA,
|
||||||
animator = nil,
|
animator = nil,
|
||||||
@@ -74,7 +69,6 @@ plant_tile := Tile {
|
|||||||
plant_2_tile := Tile {
|
plant_2_tile := Tile {
|
||||||
type = .FLORA,
|
type = .FLORA,
|
||||||
frame_index = 1,
|
frame_index = 1,
|
||||||
color = raylib.WHITE,
|
|
||||||
interaction = .HARVEST,
|
interaction = .HARVEST,
|
||||||
resource = .FLORA,
|
resource = .FLORA,
|
||||||
animator = nil,
|
animator = nil,
|
||||||
@@ -83,7 +77,6 @@ plant_2_tile := Tile {
|
|||||||
plant_3_tile := Tile {
|
plant_3_tile := Tile {
|
||||||
type = .FLORA,
|
type = .FLORA,
|
||||||
frame_index = 2,
|
frame_index = 2,
|
||||||
color = raylib.WHITE,
|
|
||||||
interaction = .HARVEST,
|
interaction = .HARVEST,
|
||||||
resource = .FLORA,
|
resource = .FLORA,
|
||||||
animator = nil,
|
animator = nil,
|
||||||
|
|||||||
@@ -55,7 +55,12 @@ update_tile_grid :: proc(grid: ^Grid, camera: ^raylib.Camera2D, tile_w, tile_h:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
draw_tile_grid :: proc(sheet: ^TilemapSpritesheet, grid: ^Grid, camera: ^raylib.Camera2D) {
|
draw_tile_grid :: proc(
|
||||||
|
sheet: ^TilemapSpritesheet,
|
||||||
|
grid: ^Grid,
|
||||||
|
camera: ^raylib.Camera2D,
|
||||||
|
color: raylib.Color,
|
||||||
|
) {
|
||||||
tile_w := f32(sheet.tile_width)
|
tile_w := f32(sheet.tile_width)
|
||||||
tile_h := f32(sheet.tile_height)
|
tile_h := f32(sheet.tile_height)
|
||||||
|
|
||||||
@@ -68,7 +73,7 @@ draw_tile_grid :: proc(sheet: ^TilemapSpritesheet, grid: ^Grid, camera: ^raylib.
|
|||||||
if tile.type == .NOTHING do continue
|
if tile.type == .NOTHING do continue
|
||||||
|
|
||||||
pos := raylib.Vector2{f32(x) * tile_w, f32(y) * tile_h}
|
pos := raylib.Vector2{f32(x) * tile_w, f32(y) * tile_h}
|
||||||
draw_tile(sheet, tile, pos, raylib.WHITE)
|
draw_tile(sheet, tile, pos, color)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,11 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
|
import "vendor:raylib"
|
||||||
WORLD_SIZE_X :: 1000
|
WORLD_SIZE_X :: 1000
|
||||||
WORLD_SIZE_Y :: 1000
|
WORLD_SIZE_Y :: 1000
|
||||||
|
|
||||||
|
world_base_color := raylib.WHITE
|
||||||
|
|
||||||
ground_layer_grid: Grid
|
ground_layer_grid: Grid
|
||||||
ground_tilemap_sheet: TilemapSpritesheet
|
ground_tilemap_sheet: TilemapSpritesheet
|
||||||
|
|
||||||
@@ -10,6 +13,9 @@ interactables_layer_grid: Grid
|
|||||||
interactables_tilemap_sheet: TilemapSpritesheet
|
interactables_tilemap_sheet: TilemapSpritesheet
|
||||||
|
|
||||||
init_world :: proc() {
|
init_world :: proc() {
|
||||||
|
|
||||||
|
// world_base_color = {74, 132, 74, 255}
|
||||||
|
|
||||||
ground_tilemap_sheet = load_tilemap_sheet(
|
ground_tilemap_sheet = load_tilemap_sheet(
|
||||||
"assets/tiles/master_tilemap.png",
|
"assets/tiles/master_tilemap.png",
|
||||||
TILEMAP_TILE_SIZE,
|
TILEMAP_TILE_SIZE,
|
||||||
@@ -59,7 +65,12 @@ update_world :: proc(delta: f32) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
draw_world :: proc() {
|
draw_world :: proc() {
|
||||||
draw_tile_grid(&ground_tilemap_sheet, &ground_layer_grid, &player.camera)
|
draw_tile_grid(&ground_tilemap_sheet, &ground_layer_grid, &player.camera, world_base_color)
|
||||||
draw_tile_grid(&interactables_tilemap_sheet, &interactables_layer_grid, &player.camera)
|
draw_tile_grid(
|
||||||
|
&interactables_tilemap_sheet,
|
||||||
|
&interactables_layer_grid,
|
||||||
|
&player.camera,
|
||||||
|
world_base_color,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user