Compare commits
18 Commits
master
...
53d717dee4
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
53d717dee4 | ||
|
|
a26ec7afed | ||
|
|
5c60c9b74d | ||
| 175f44692a | |||
| 7e8ab5e180 | |||
|
|
678c14b680 | ||
| 7f7cbc567b | |||
| aaf04c9e1a | |||
| b5e07700b0 | |||
|
|
671a5172ab | ||
|
|
a8a0126d11 | ||
| cc479131d8 | |||
| 7b19fbcf53 | |||
|
|
997aa3f16b | ||
|
|
5c5a77ea32 | ||
|
|
1d6d3726c6 | ||
|
|
1ae587e7ff | ||
|
|
3b6277fce0 |
BIN
builds/linux/assets/player/player.png
Normal file
BIN
builds/linux/assets/player/player.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.5 KiB |
BIN
builds/linux/assets/player/player.pxo
Normal file
BIN
builds/linux/assets/player/player.pxo
Normal file
Binary file not shown.
BIN
builds/linux/assets/tiles/master_tilemap.png
Normal file
BIN
builds/linux/assets/tiles/master_tilemap.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.2 KiB |
BIN
builds/linux/assets/tiles/master_tilemap.pxo
Normal file
BIN
builds/linux/assets/tiles/master_tilemap.pxo
Normal file
Binary file not shown.
9
builds/linux/assets/tiles/master_tilemap_addresses.txt
Normal file
9
builds/linux/assets/tiles/master_tilemap_addresses.txt
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
0 - martian ground
|
||||||
|
1 - martian ground barrier UP
|
||||||
|
2 - martian ground barrier DOWN
|
||||||
|
3 - martian ground barrier LEFT
|
||||||
|
4 - martian ground barrier RIGHT
|
||||||
|
5 - martian ground barrier TOP RIGHT CORNER
|
||||||
|
6 - martian ground barrier TOP LEFT CORNER
|
||||||
|
7 - martian ground barrier BOTTOM RIGHT CORNER
|
||||||
|
8 - martian ground barrier BOTTOM LEFT CORNER
|
||||||
BIN
builds/linux/game
Executable file
BIN
builds/linux/game
Executable file
Binary file not shown.
BIN
src/assets/interactables/interactables_spritesheet.png
Normal file
BIN
src/assets/interactables/interactables_spritesheet.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.0 KiB |
BIN
src/assets/interactables/interactables_spritesheet.pxo
Normal file
BIN
src/assets/interactables/interactables_spritesheet.pxo
Normal file
Binary file not shown.
BIN
src/assets/player/player.png
Normal file
BIN
src/assets/player/player.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.5 KiB |
BIN
src/assets/player/player.pxo
Normal file
BIN
src/assets/player/player.pxo
Normal file
Binary file not shown.
BIN
src/assets/tiles/master_tilemap.png
Normal file
BIN
src/assets/tiles/master_tilemap.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.2 KiB |
BIN
src/assets/tiles/master_tilemap.pxo
Normal file
BIN
src/assets/tiles/master_tilemap.pxo
Normal file
Binary file not shown.
9
src/assets/tiles/master_tilemap_addresses.txt
Normal file
9
src/assets/tiles/master_tilemap_addresses.txt
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
0 - martian ground
|
||||||
|
1 - martian ground barrier UP
|
||||||
|
2 - martian ground barrier DOWN
|
||||||
|
3 - martian ground barrier LEFT
|
||||||
|
4 - martian ground barrier RIGHT
|
||||||
|
5 - martian ground barrier TOP RIGHT CORNER
|
||||||
|
6 - martian ground barrier TOP LEFT CORNER
|
||||||
|
7 - martian ground barrier BOTTOM RIGHT CORNER
|
||||||
|
8 - martian ground barrier BOTTOM LEFT CORNER
|
||||||
@@ -4,35 +4,52 @@ import "core:fmt"
|
|||||||
import "vendor:raylib"
|
import "vendor:raylib"
|
||||||
|
|
||||||
player: Player
|
player: Player
|
||||||
|
grid: [][]Tile
|
||||||
|
tilemap_sheet: TilemapSpritesheet
|
||||||
|
|
||||||
main :: proc() {
|
main :: proc() {
|
||||||
fmt.println("Brackey's Game Jam 2026 :) ")
|
fmt.println("Brackey's Game Jam 2026 :) ")
|
||||||
|
|
||||||
|
config_flags: raylib.ConfigFlags = {.WINDOW_RESIZABLE}
|
||||||
|
raylib.SetConfigFlags(config_flags)
|
||||||
|
|
||||||
raylib.InitWindow(1920, 1080, "Game")
|
raylib.InitWindow(1920, 1080, "Game")
|
||||||
raylib.SetTargetFPS(60)
|
raylib.SetTargetFPS(60)
|
||||||
|
|
||||||
|
tilemap_sheet = load_tilemap_sheet(
|
||||||
|
"assets/tiles/master_tilemap.png",
|
||||||
|
TILEMAP_TILE_SIZE,
|
||||||
|
TILEMAP_TILE_SIZE,
|
||||||
|
)
|
||||||
|
grid = create_tile_grid(5000, 5000)
|
||||||
|
|
||||||
player = {
|
player = {
|
||||||
position = {0, 0},
|
position = {1500, 500},
|
||||||
camera = {
|
camera = {
|
||||||
zoom = 2,
|
zoom = 4,
|
||||||
offset = {f32(raylib.GetScreenWidth()) / 2, f32(raylib.GetScreenHeight()) / 2},
|
offset = {f32(raylib.GetScreenWidth()) / 2, f32(raylib.GetScreenHeight()) / 2},
|
||||||
target = {player.position.x + (32 / 2), player.position.y + (32 / 2)},
|
target = {player.position.x + (32 / 2), player.position.y + (32 / 2)},
|
||||||
},
|
},
|
||||||
|
sprite = load_sprite(PLAYER_SPRITE_PATH, PLAYER_WIDTH, PLAYER_HEIGHT),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
set_tile(grid, 10, 10, nothing_tile)
|
||||||
|
|
||||||
for (!raylib.WindowShouldClose()) {
|
for (!raylib.WindowShouldClose()) {
|
||||||
|
|
||||||
delta := raylib.GetFrameTime()
|
delta := raylib.GetFrameTime()
|
||||||
|
|
||||||
raylib.BeginDrawing()
|
raylib.BeginDrawing()
|
||||||
raylib.ClearBackground(raylib.DARKBLUE)
|
raylib.ClearBackground(raylib.BLACK)
|
||||||
raylib.BeginMode2D(player.camera)
|
raylib.BeginMode2D(player.camera)
|
||||||
|
|
||||||
raylib.DrawText("Ur mom", 100, 100, 50, raylib.BLACK)
|
raylib.DrawText("Ur mom", 100, 100, 50, raylib.BLACK)
|
||||||
|
|
||||||
|
|
||||||
draw()
|
draw()
|
||||||
|
|
||||||
raylib.EndMode2D()
|
raylib.EndMode2D()
|
||||||
|
raylib.DrawFPS(20, 20)
|
||||||
raylib.EndDrawing()
|
raylib.EndDrawing()
|
||||||
|
|
||||||
update(delta)
|
update(delta)
|
||||||
@@ -44,10 +61,18 @@ main :: proc() {
|
|||||||
@(private = "file")
|
@(private = "file")
|
||||||
update :: proc(delta: f32) {
|
update :: proc(delta: f32) {
|
||||||
update_player(&player, delta)
|
update_player(&player, delta)
|
||||||
|
update_tile_grid(
|
||||||
|
grid,
|
||||||
|
&player.camera,
|
||||||
|
f32(tilemap_sheet.tile_width),
|
||||||
|
f32(tilemap_sheet.tile_height),
|
||||||
|
delta,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@(private = "file")
|
@(private = "file")
|
||||||
draw :: proc() {
|
draw :: proc() {
|
||||||
|
draw_tile_grid(&tilemap_sheet, grid, &player.camera)
|
||||||
draw_player(&player)
|
draw_player(&player)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,20 +1,41 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import "core:fmt"
|
import "core:fmt"
|
||||||
|
import "core:strings"
|
||||||
import "vendor:raylib"
|
import "vendor:raylib"
|
||||||
|
|
||||||
PLAYER_SPEED :: 5
|
PLAYER_SPRINT_SPEED :: 2.5
|
||||||
|
PLAYER_SPEED :: 1.5
|
||||||
|
PLAYER_WIDTH :: 32
|
||||||
|
PLAYER_HEIGHT :: 32
|
||||||
|
PLAYER_SPRITE_PATH :: "assets/player/player.png"
|
||||||
|
|
||||||
|
spritesheet: raylib.Texture2D
|
||||||
|
framesX: i32
|
||||||
|
framesY: i32
|
||||||
|
|
||||||
Player :: struct {
|
Player :: struct {
|
||||||
|
sprite: Sprite,
|
||||||
health: int,
|
health: int,
|
||||||
current_world: string,
|
current_world: string,
|
||||||
camera: raylib.Camera2D,
|
camera: raylib.Camera2D,
|
||||||
position: raylib.Vector2,
|
position: raylib.Vector2,
|
||||||
|
animator: SpriteAnimator,
|
||||||
|
state: PlayerState,
|
||||||
|
facing_left: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
PlayerState :: enum {
|
||||||
|
IDLE,
|
||||||
|
WALKING,
|
||||||
}
|
}
|
||||||
|
|
||||||
@(private = "file")
|
@(private = "file")
|
||||||
handle_player_camera :: proc(p: ^Player, delta: f32) {
|
handle_player_camera :: proc(p: ^Player, delta: f32) {
|
||||||
player_center := raylib.Vector2{p.position.x + 16, p.position.y + 16}
|
player_center := raylib.Vector2 {
|
||||||
|
p.position.x + (PLAYER_WIDTH / 2),
|
||||||
|
p.position.y + (PLAYER_HEIGHT / 2),
|
||||||
|
}
|
||||||
|
|
||||||
smooth_speed :: 10.0
|
smooth_speed :: 10.0
|
||||||
|
|
||||||
@@ -24,12 +45,6 @@ handle_player_camera :: proc(p: ^Player, delta: f32) {
|
|||||||
if (raylib.IsWindowResized()) {
|
if (raylib.IsWindowResized()) {
|
||||||
p.camera.offset = {f32(raylib.GetScreenWidth()) * 0.5, f32(raylib.GetScreenHeight()) * 0.5}
|
p.camera.offset = {f32(raylib.GetScreenWidth()) * 0.5, f32(raylib.GetScreenHeight()) * 0.5}
|
||||||
}
|
}
|
||||||
|
|
||||||
// p.camera.target = {p.position.x + (32 / 2), p.position.y + (32 / 2)}
|
|
||||||
|
|
||||||
// if raylib.IsWindowResized() {
|
|
||||||
// p.camera.offset = {f32(raylib.GetScreenWidth()) / 2, f32(raylib.GetScreenHeight()) / 2}
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@(private = "file")
|
@(private = "file")
|
||||||
@@ -41,20 +56,80 @@ handle_player_input :: proc(p: ^Player, delta: f32) {
|
|||||||
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
|
||||||
|
|
||||||
if dir.x != 0 || dir.y != 0 {
|
is_moving := dir.x != 0 || dir.y != 0
|
||||||
|
|
||||||
|
if (is_moving) {
|
||||||
|
is_sprinting := false
|
||||||
|
|
||||||
|
if raylib.IsKeyDown(.LEFT_SHIFT) {
|
||||||
|
is_sprinting = true
|
||||||
|
} else {
|
||||||
|
is_sprinting = false
|
||||||
|
}
|
||||||
|
|
||||||
dir = raylib.Vector2Normalize(dir)
|
dir = raylib.Vector2Normalize(dir)
|
||||||
dir = dir * PLAYER_SPEED
|
|
||||||
|
if is_sprinting {
|
||||||
|
dir = dir * PLAYER_SPRINT_SPEED
|
||||||
|
p.animator.anim.fps = 11
|
||||||
|
} else {
|
||||||
|
dir = dir * PLAYER_SPEED
|
||||||
|
p.animator.anim.fps = 6
|
||||||
|
}
|
||||||
|
|
||||||
p.position = p.position + dir
|
p.position = p.position + dir
|
||||||
|
|
||||||
|
if dir.x < 0 {
|
||||||
|
p.facing_left = true
|
||||||
|
}
|
||||||
|
|
||||||
|
if dir.x > 0 {
|
||||||
|
p.facing_left = false
|
||||||
|
}
|
||||||
|
|
||||||
|
p.state = .WALKING
|
||||||
|
} else {
|
||||||
|
p.state = .IDLE
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
idle_animation: SpriteAnimation = {
|
||||||
|
start_frame = 0,
|
||||||
|
end_frame = 5,
|
||||||
|
fps = 6,
|
||||||
|
loop = true,
|
||||||
|
}
|
||||||
|
|
||||||
|
player_walk_anim: SpriteAnimation = {
|
||||||
|
start_frame = 6,
|
||||||
|
end_frame = 11,
|
||||||
|
fps = 6,
|
||||||
|
loop = true,
|
||||||
}
|
}
|
||||||
|
|
||||||
draw_player :: proc(p: ^Player) {
|
draw_player :: proc(p: ^Player) {
|
||||||
raylib.DrawRectangle(i32(p.position.x), i32(p.position.y), 32, 32, raylib.BLACK)
|
// raylib.DrawRectangle(i32(p.position.x), i32(p.position.y), 32, 32, raylib.BLACK)
|
||||||
|
|
||||||
|
// draw_sprite_frame(&p.sprite, {0, 0}, p.position, raylib.WHITE)
|
||||||
|
|
||||||
|
draw_sprite_animated(&p.sprite, &p.animator, p.position, p.facing_left, false, raylib.WHITE)
|
||||||
}
|
}
|
||||||
|
|
||||||
update_player :: proc(p: ^Player, delta: f32) {
|
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)
|
||||||
// fmt.println(p.position)
|
|
||||||
|
if (p.state == .IDLE) {
|
||||||
|
set_sprite_animation(&p.animator, &idle_animation)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (p.state == .WALKING) {
|
||||||
|
set_sprite_animation(&p.animator, &player_walk_anim)
|
||||||
|
}
|
||||||
|
|
||||||
|
update_animator(&p.animator, delta)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
118
src/sprite.odin
Normal file
118
src/sprite.odin
Normal file
@@ -0,0 +1,118 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "vendor:raylib"
|
||||||
|
|
||||||
|
Sprite :: struct {
|
||||||
|
texture: raylib.Texture2D,
|
||||||
|
width: i32,
|
||||||
|
height: i32,
|
||||||
|
framesX: i32,
|
||||||
|
framesY: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
SpriteAnimation :: struct {
|
||||||
|
start_frame: i32,
|
||||||
|
end_frame: i32,
|
||||||
|
fps: i32,
|
||||||
|
loop: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
SpriteAnimator :: struct {
|
||||||
|
anim: ^SpriteAnimation,
|
||||||
|
current_frame: i32,
|
||||||
|
timer: f32,
|
||||||
|
}
|
||||||
|
|
||||||
|
load_sprite :: proc(image_path: cstring, w: i32, h: i32) -> Sprite {
|
||||||
|
new_texture: raylib.Texture2D = raylib.LoadTexture(image_path)
|
||||||
|
|
||||||
|
new_sprite: Sprite = {
|
||||||
|
texture = new_texture,
|
||||||
|
width = w,
|
||||||
|
height = h,
|
||||||
|
framesX = new_texture.width / w,
|
||||||
|
framesY = new_texture.height / h,
|
||||||
|
}
|
||||||
|
|
||||||
|
return new_sprite
|
||||||
|
}
|
||||||
|
|
||||||
|
draw_sprite_frame :: proc(
|
||||||
|
sprite: ^Sprite,
|
||||||
|
frame_pos: raylib.Vector2,
|
||||||
|
draw_pos: raylib.Vector2,
|
||||||
|
flipX: bool,
|
||||||
|
flipY: bool,
|
||||||
|
color: raylib.Color,
|
||||||
|
) {
|
||||||
|
widthf := f32(sprite.width)
|
||||||
|
heightf := f32(sprite.height)
|
||||||
|
|
||||||
|
source_rect := raylib.Rectangle {
|
||||||
|
x = frame_pos.x * widthf,
|
||||||
|
y = frame_pos.y * heightf,
|
||||||
|
width = widthf,
|
||||||
|
height = heightf,
|
||||||
|
}
|
||||||
|
|
||||||
|
if flipX do source_rect.width *= -1
|
||||||
|
if flipY do source_rect.height *= -1
|
||||||
|
|
||||||
|
origin := raylib.Vector2{widthf / 2, heightf / 2}
|
||||||
|
|
||||||
|
dest_rect := raylib.Rectangle {
|
||||||
|
x = draw_pos.x + origin.x,
|
||||||
|
y = draw_pos.y + origin.y,
|
||||||
|
width = widthf,
|
||||||
|
height = heightf,
|
||||||
|
}
|
||||||
|
|
||||||
|
raylib.DrawTexturePro(sprite.texture, source_rect, dest_rect, origin, 0.0, color)
|
||||||
|
}
|
||||||
|
|
||||||
|
set_sprite_animation :: proc(animator: ^SpriteAnimator, new_anim: ^SpriteAnimation) {
|
||||||
|
if (animator.anim == new_anim) do return
|
||||||
|
|
||||||
|
animator.anim = new_anim
|
||||||
|
animator.timer = 0
|
||||||
|
animator.current_frame = new_anim.start_frame
|
||||||
|
}
|
||||||
|
|
||||||
|
draw_sprite_animated :: proc(
|
||||||
|
sprite: ^Sprite,
|
||||||
|
animator: ^SpriteAnimator,
|
||||||
|
draw_pos: raylib.Vector2,
|
||||||
|
flipX: bool,
|
||||||
|
flipY: bool,
|
||||||
|
color: raylib.Color,
|
||||||
|
) {
|
||||||
|
frame_pos := frame_to_xy(sprite, animator.current_frame)
|
||||||
|
draw_sprite_frame(sprite, frame_pos, draw_pos, flipX, flipY, color)
|
||||||
|
}
|
||||||
|
|
||||||
|
update_animator :: proc(a: ^SpriteAnimator, delta: f32) {
|
||||||
|
if a.anim == nil do return
|
||||||
|
|
||||||
|
frame_time := 1.0 / f32(a.anim.fps)
|
||||||
|
a.timer += delta
|
||||||
|
|
||||||
|
if (a.timer >= frame_time) {
|
||||||
|
a.timer -= frame_time
|
||||||
|
a.current_frame += 1
|
||||||
|
|
||||||
|
if (a.current_frame > a.anim.end_frame) {
|
||||||
|
if (a.anim.loop) {
|
||||||
|
a.current_frame = a.anim.start_frame
|
||||||
|
} else {
|
||||||
|
a.current_frame = a.anim.end_frame
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
frame_to_xy :: proc(sprite: ^Sprite, frame: i32) -> raylib.Vector2 {
|
||||||
|
x := frame % sprite.framesX
|
||||||
|
y := frame / sprite.framesX
|
||||||
|
return raylib.Vector2{f32(x), f32(y)}
|
||||||
|
}
|
||||||
|
|
||||||
94
src/tile.odin
Normal file
94
src/tile.odin
Normal file
@@ -0,0 +1,94 @@
|
|||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
set_tile :: proc(grid: [][]Tile, x: int, y: int, tile: Tile) {
|
||||||
|
grid[x][y] = tile
|
||||||
|
}
|
||||||
|
|
||||||
109
src/tilemap.odin
Normal file
109
src/tilemap.odin
Normal file
@@ -0,0 +1,109 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "core:fmt"
|
||||||
|
import "vendor:raylib"
|
||||||
|
|
||||||
|
TilemapSpritesheet :: struct {
|
||||||
|
texture: raylib.Texture2D,
|
||||||
|
tile_width: i32,
|
||||||
|
tile_height: i32,
|
||||||
|
tiles_x: i32,
|
||||||
|
tiles_y: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
VisibleTileRange :: struct {
|
||||||
|
start_x, start_y: int,
|
||||||
|
end_x, end_y: int,
|
||||||
|
}
|
||||||
|
|
||||||
|
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,
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
draw_tile_grid :: proc(sheet: ^TilemapSpritesheet, grid: [][]Tile, 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
|
||||||
|
|
||||||
|
pos := raylib.Vector2{f32(x) * tile_w, f32(y) * tile_h}
|
||||||
|
|
||||||
|
draw_tile(sheet, tile, pos, raylib.Color{0, 136, 200, 255})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
get_visible_tile_range :: proc(
|
||||||
|
grid: [][]Tile,
|
||||||
|
tile_w, tile_h: f32,
|
||||||
|
camera: ^raylib.Camera2D,
|
||||||
|
padding: int = 1,
|
||||||
|
) -> VisibleTileRange {
|
||||||
|
|
||||||
|
screen_w := f32(raylib.GetScreenWidth())
|
||||||
|
screen_h := f32(raylib.GetScreenHeight())
|
||||||
|
|
||||||
|
world_w := screen_w / camera.zoom
|
||||||
|
world_h := screen_h / camera.zoom
|
||||||
|
|
||||||
|
min_x := camera.target.x - world_w * 0.5
|
||||||
|
min_y := camera.target.y - world_h * 0.5
|
||||||
|
max_x := min_x + world_w
|
||||||
|
max_y := min_y + world_h
|
||||||
|
|
||||||
|
start_x := int(min_x / tile_w) - padding
|
||||||
|
start_y := int(min_y / tile_h) - padding
|
||||||
|
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}
|
||||||
|
}
|
||||||
|
|
||||||
2
src/world.odin
Normal file
2
src/world.odin
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
Reference in New Issue
Block a user