Tilemap and grid

This commit is contained in:
2026-02-15 18:53:45 -06:00
parent 671a5172ab
commit b5e07700b0
4 changed files with 151 additions and 26 deletions

View File

@@ -4,6 +4,8 @@ import "core:fmt"
import "vendor:raylib"
player: Player
grid: [][]Tile
tilemap_sheet: TilemapSpritesheet
main :: proc() {
fmt.println("Brackey's Game Jam 2026 :) ")
@@ -11,6 +13,13 @@ main :: proc() {
raylib.InitWindow(1920, 1080, "Game")
raylib.SetTargetFPS(60)
tilemap_sheet = load_tilemap_sheet(
"assets/tiles/master_tilemap.png",
TILEMAP_TILE_SIZE,
TILEMAP_TILE_SIZE,
)
grid = create_tile_grid(100, 100)
player = {
position = {0, 0},
camera = {
@@ -28,7 +37,7 @@ main :: proc() {
delta := raylib.GetFrameTime()
raylib.BeginDrawing()
raylib.ClearBackground(raylib.DARKBLUE)
raylib.ClearBackground(raylib.BLACK)
raylib.BeginMode2D(player.camera)
raylib.DrawText("Ur mom", 100, 100, 50, raylib.BLACK)
@@ -47,10 +56,12 @@ main :: proc() {
@(private = "file")
update :: proc(delta: f32) {
update_player(&player, delta)
update_tile_grid(grid, delta)
}
@(private = "file")
draw :: proc() {
draw_tile_grid(&tilemap_sheet, grid)
draw_player(&player)
}

90
src/tile.odin Normal file
View File

@@ -0,0 +1,90 @@
package main
import "vendor:raylib"
TILEMAP_TILE_SIZE :: 16
Tile :: struct {
frame_index: i32,
color: raylib.Color,
type: TileType,
interaction: InteractionType,
resource: ResourceType,
animator: ^SpriteAnimator,
}
TileType :: enum {
NOTHING,
GROUND,
WALL,
DEPOSIT,
FLORA,
}
ResourceType :: enum {
NONE,
FLORA,
FAUNA,
MINERAL,
}
InteractionType :: enum {
NONE,
HARVEST,
DRILL,
EXAMINE,
}
nothing_tile := Tile {
type = .NOTHING,
frame_index = 0,
color = raylib.WHITE,
interaction = .NONE,
resource = .NONE,
animator = nil,
}
ground_tile := Tile {
type = .GROUND,
frame_index = 0,
color = raylib.WHITE,
interaction = .NONE,
resource = .NONE,
animator = nil,
}
draw_tile :: proc(
tilemap_sheet: ^TilemapSpritesheet,
tile: ^Tile,
pos: raylib.Vector2,
color: raylib.Color,
) {
frame_x := tile.frame_index % tilemap_sheet.tiles_x
frame_y := tile.frame_index / tilemap_sheet.tiles_x
src_rect := raylib.Rectangle {
x = f32(frame_x * tilemap_sheet.tile_width),
y = f32(frame_y * tilemap_sheet.tile_height),
width = f32(tilemap_sheet.tile_width),
height = f32(tilemap_sheet.tile_height),
}
dest_rect := raylib.Rectangle {
x = pos.x,
y = pos.y,
width = f32(tilemap_sheet.tile_width),
height = f32(tilemap_sheet.tile_height),
}
origin := raylib.Vector2{0, 0}
raylib.DrawTexturePro(tilemap_sheet.texture, src_rect, dest_rect, origin, 0.0, color)
}
update_tile_anim :: proc(tile: ^Tile, delta: f32) {
if (tile.animator == nil) do return
update_animator(tile.animator, delta)
tile.frame_index = tile.animator.current_frame
}

View File

@@ -1,34 +1,56 @@
package main
import rl "vendor:raylib"
import "vendor:raylib"
TILE_SIZE :: 16
TILE_IMAGE_PATH :: "assets/tiles.png"
tilemap_image: rl.Texture2D
tilesX: i32
tilesY: i32
load_tilemap :: proc() {
tilemap_image = rl.LoadTexture(TILE_IMAGE_PATH)
tilesX = tilemap_image.width / TILE_SIZE
tilesY = tilemap_image.height / TILE_SIZE
TilemapSpritesheet :: struct {
texture: raylib.Texture2D,
tile_width: i32,
tile_height: i32,
tiles_x: i32,
tiles_y: i32,
}
unload_tilemap :: proc() {
rl.UnloadTexture(tilemap_image)
}
draw_tile :: proc(tilemap_pos: rl.Vector2, draw_pos: rl.Vector2, color: rl.Color) {
source_rect := rl.Rectangle {
x = tilemap_pos.x * TILE_SIZE,
y = tilemap_pos.y * TILE_SIZE,
width = TILE_SIZE,
height = TILE_SIZE,
load_tilemap_sheet :: proc(path: cstring, tile_width, tile_height: i32) -> TilemapSpritesheet {
tex := raylib.LoadTexture(path)
return TilemapSpritesheet {
texture = tex,
tile_width = tile_width,
tile_height = tile_height,
tiles_x = tex.width / tile_width,
tiles_y = tex.height / tile_height,
}
}
create_tile_grid :: proc(width, height: i32) -> [][]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] = ground_tile
}
}
return grid
}
update_tile_grid :: proc(grid: [][]Tile, delta: f32) {
for y := 0; y < len(grid); y += 1 {
row := grid[y]
for x := 0; x < len(row); x += 1 {
update_tile_anim(&row[x], delta)
}
}
}
draw_tile_grid :: proc(sheet: ^TilemapSpritesheet, grid: [][]Tile) {
for y := 0; y < len(grid); y += 1 {
row := grid[y]
for x := 0; x < len(row); x += 1 {
tile := &row[x]
pos := raylib.Vector2{f32(x * int(sheet.tile_width)), f32(y * int(sheet.tile_height))}
draw_tile(sheet, tile, pos, raylib.Color{255, 136, 0, 255})
}
}
rl.DrawTextureRec(tilemap_image, source_rect, draw_pos, color)
}

2
src/world.odin Normal file
View File

@@ -0,0 +1,2 @@
package main