Major tilemap/grid refactor, collision detection, interactable detection
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
package main
|
||||
|
||||
import "core:fmt"
|
||||
import "vendor:raylib"
|
||||
|
||||
TilemapSpritesheet :: struct {
|
||||
@@ -16,6 +15,12 @@ VisibleTileRange :: struct {
|
||||
end_x, end_y: int,
|
||||
}
|
||||
|
||||
Grid :: struct {
|
||||
width: int,
|
||||
height: int,
|
||||
tiles: []Tile,
|
||||
}
|
||||
|
||||
load_tilemap_sheet :: proc(path: cstring, tile_width, tile_height: i32) -> TilemapSpritesheet {
|
||||
tex := raylib.LoadTexture(path)
|
||||
|
||||
@@ -28,60 +33,52 @@ load_tilemap_sheet :: proc(path: cstring, tile_width, tile_height: i32) -> Tilem
|
||||
}
|
||||
}
|
||||
|
||||
create_tile_grid :: proc(width, height: i32, fill_tile: Tile) -> [][]Tile {
|
||||
grid: [][]Tile = make([][]Tile, height)
|
||||
for y := 0; y < int(height); y += 1 {
|
||||
grid[y] = make([]Tile, width)
|
||||
for x := 0; x < int(width); x += 1 {
|
||||
grid[y][x] = fill_tile
|
||||
}
|
||||
create_tile_grid :: proc(width, height: i32, fill_tile: Tile) -> Grid {
|
||||
w, h := int(width), int(height)
|
||||
grid_slice := make([]Tile, w * h)
|
||||
|
||||
for i := 0; i < len(grid_slice); i += 1 {
|
||||
grid_slice[i] = fill_tile
|
||||
}
|
||||
return grid
|
||||
|
||||
return Grid{width = w, height = h, tiles = grid_slice}
|
||||
}
|
||||
|
||||
update_tile_grid :: proc(
|
||||
grid: [][]Tile,
|
||||
camera: ^raylib.Camera2D,
|
||||
tile_w, tile_h: f32,
|
||||
delta: f32,
|
||||
) {
|
||||
update_tile_grid :: proc(grid: ^Grid, camera: ^raylib.Camera2D, tile_w, tile_h: f32, delta: f32) {
|
||||
range := get_visible_tile_range(grid, tile_w, tile_h, camera)
|
||||
|
||||
for y := range.start_y; y <= range.end_y; y += 1 {
|
||||
row := grid[y]
|
||||
for x := range.start_x; x <= range.end_x; x += 1 {
|
||||
update_tile_anim(&row[x], delta)
|
||||
tile := &grid.tiles[y * grid.width + x]
|
||||
update_tile_anim(tile, delta)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
draw_tile_grid :: proc(sheet: ^TilemapSpritesheet, grid: [][]Tile, camera: ^raylib.Camera2D) {
|
||||
draw_tile_grid :: proc(sheet: ^TilemapSpritesheet, grid: ^Grid, camera: ^raylib.Camera2D) {
|
||||
tile_w := f32(sheet.tile_width)
|
||||
tile_h := f32(sheet.tile_height)
|
||||
|
||||
range := get_visible_tile_range(grid, tile_w, tile_h, camera)
|
||||
|
||||
for y := range.start_y; y <= range.end_y; y += 1 {
|
||||
row := grid[y]
|
||||
for x := range.start_x; x <= range.end_x; x += 1 {
|
||||
tile := &row[x]
|
||||
if tile.type == TileType.NOTHING do continue
|
||||
tile := &grid.tiles[y * grid.width + x]
|
||||
|
||||
if tile.type == .NOTHING do continue
|
||||
|
||||
pos := raylib.Vector2{f32(x) * tile_w, f32(y) * tile_h}
|
||||
|
||||
draw_tile(sheet, tile, pos, raylib.WHITE)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
get_visible_tile_range :: proc(
|
||||
grid: [][]Tile,
|
||||
grid: ^Grid,
|
||||
tile_w, tile_h: f32,
|
||||
camera: ^raylib.Camera2D,
|
||||
padding: int = 1,
|
||||
) -> VisibleTileRange {
|
||||
|
||||
screen_w := f32(raylib.GetScreenWidth())
|
||||
screen_h := f32(raylib.GetScreenHeight())
|
||||
|
||||
@@ -98,12 +95,27 @@ get_visible_tile_range :: proc(
|
||||
end_x := int(max_x / tile_w) + padding
|
||||
end_y := int(max_y / tile_h) + padding
|
||||
|
||||
start_x = max(start_x, 0)
|
||||
start_y = max(start_y, 0)
|
||||
|
||||
end_x = min(end_x, len(grid[0]) - 1)
|
||||
end_y = min(end_y, len(grid) - 1)
|
||||
|
||||
return VisibleTileRange{start_x = start_x, start_y = start_y, end_x = end_x, end_y = end_y}
|
||||
return VisibleTileRange {
|
||||
start_x = clamp(start_x, 0, grid.width - 1),
|
||||
start_y = clamp(start_y, 0, grid.height - 1),
|
||||
end_x = clamp(end_x, 0, grid.width - 1),
|
||||
end_y = clamp(end_y, 0, grid.height - 1),
|
||||
}
|
||||
}
|
||||
|
||||
get_tile :: proc(grid: ^Grid, x, y: int) -> ^Tile {
|
||||
if x < 0 || x >= grid.width || y < 0 || y >= grid.height do return nil
|
||||
return &grid.tiles[y * grid.width + x]
|
||||
}
|
||||
|
||||
set_tile :: proc(grid: ^Grid, x: int, y: int, tile: Tile) {
|
||||
if x < 0 || x >= grid.width || y < 0 || y >= grid.height do return
|
||||
grid.tiles[y * grid.width + x] = tile
|
||||
}
|
||||
|
||||
delete_tile_grid :: proc(grid: ^Grid) {
|
||||
delete(grid.tiles)
|
||||
grid.width = 0
|
||||
grid.height = 0
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user