203 lines
3.7 KiB
Odin
203 lines
3.7 KiB
Odin
package main
|
|
|
|
import "base:runtime"
|
|
import hm "core:container/handle_map"
|
|
import "core:fmt"
|
|
import os "core:os"
|
|
import rl "vendor:raylib"
|
|
|
|
SCREEN_W :: 1280
|
|
SCREEN_H :: 720
|
|
|
|
|
|
MAX_ENEMIES :: 10
|
|
MAX_TILES :: 4096
|
|
|
|
Scene :: struct {
|
|
player: Player,
|
|
camera: rl.Camera2D,
|
|
enemies: hm.Static_Handle_Map(128, Enemy, ent_handle),
|
|
atlas: Atlas,
|
|
map_tex: rl.RenderTexture2D,
|
|
}
|
|
|
|
|
|
create_scene :: proc() -> Scene {
|
|
scene := Scene {
|
|
player = Player {
|
|
position = rl.Vector2{100, 100},
|
|
hp = 100,
|
|
walk_speed = 300,
|
|
run_speed = 500,
|
|
col = 32,
|
|
},
|
|
camera = rl.Camera2D {
|
|
zoom = 1,
|
|
offset = rl.Vector2{SCREEN_W / 2, SCREEN_H / 2},
|
|
},
|
|
}
|
|
|
|
|
|
// _ = hm.add(
|
|
// &scene.enemies,
|
|
// Enemy {
|
|
// position = rl.Vector2{500, 500},
|
|
// hp = 100,
|
|
// speed = 100,
|
|
// col = 32,
|
|
// },
|
|
// )
|
|
// _ = hm.add(
|
|
// &scene.enemies,
|
|
// Enemy {
|
|
// position = rl.Vector2{600, 600},
|
|
// hp = 100,
|
|
// speed = 100,
|
|
// col = 32,
|
|
// },
|
|
// )
|
|
|
|
scene.atlas = load_atlas()
|
|
|
|
|
|
// append(
|
|
// &scene.tiles,
|
|
// Tile{atlas = &scene.atlas, pos = rl.Vector2{0, 0}, frame = 3},
|
|
// )
|
|
return scene
|
|
}
|
|
|
|
draw_scene :: proc(scene: ^Scene) {
|
|
rl.BeginMode2D(scene.camera)
|
|
|
|
rl.DrawTexturePro(
|
|
scene.map_tex.texture,
|
|
rl.Rectangle {
|
|
0,
|
|
0,
|
|
cast(f32)scene.map_tex.texture.width,
|
|
-cast(f32)scene.map_tex.texture.height, // FIX: flip Y axis
|
|
},
|
|
rl.Rectangle {
|
|
0,
|
|
0,
|
|
cast(f32)scene.map_tex.texture.width * TILE_MULT,
|
|
cast(f32)scene.map_tex.texture.height * TILE_MULT,
|
|
},
|
|
rl.Vector2{0, 0},
|
|
0,
|
|
rl.WHITE,
|
|
)
|
|
|
|
// for tile in scene.tiles {
|
|
// if tile.atlas == nil do continue
|
|
// draw_tile(tile)
|
|
// }
|
|
|
|
rl.DrawCircle(
|
|
cast(i32)scene.player.position.x,
|
|
cast(i32)scene.player.position.y,
|
|
32,
|
|
rl.GREEN,
|
|
)
|
|
rl.DrawRectangle(300, 300, 100, 100, rl.BLUE)
|
|
|
|
it := hm.iterator_make(&scene.enemies)
|
|
for i, _ in hm.iterate(&it) {
|
|
rl.DrawCircle(cast(i32)i.position.x, cast(i32)i.position.y, 32, rl.RED)
|
|
}
|
|
|
|
|
|
rl.EndMode2D()
|
|
}
|
|
|
|
render_map :: proc(atlas: ^Atlas, map_data: Map) -> rl.RenderTexture2D {
|
|
tex := rl.LoadRenderTexture(map_data.width * 16, map_data.height * 16)
|
|
rl.BeginTextureMode(tex)
|
|
for layer in map_data.layers {
|
|
for row_data, row in layer.data {
|
|
for tile_id, col in row_data {
|
|
offset, empty := get_offset(atlas, tile_id)
|
|
if empty do continue
|
|
rl.DrawTexturePro(
|
|
atlas^.tex,
|
|
rl.Rectangle {
|
|
offset.x,
|
|
offset.y,
|
|
atlas.tile_size.x,
|
|
atlas.tile_size.y,
|
|
},
|
|
rl.Rectangle {
|
|
f32(col) * atlas.tile_size.x,
|
|
f32(row) * atlas.tile_size.y,
|
|
atlas.tile_size.x,
|
|
atlas.tile_size.y,
|
|
},
|
|
rl.Vector2{0, 0},
|
|
0,
|
|
rl.WHITE,
|
|
)
|
|
}
|
|
}
|
|
}
|
|
rl.EndTextureMode()
|
|
return tex
|
|
}
|
|
|
|
|
|
update_scene :: proc(scene: ^Scene, dt: f32) {
|
|
update_player(&scene.player, dt)
|
|
scene.camera.target = scene.player.position
|
|
|
|
it := hm.iterator_make(&scene.enemies)
|
|
for i, _ in hm.iterate(&it) {
|
|
dir := rl.Vector2Normalize(scene.player.position - i.position)
|
|
i.position += dir * i.speed * dt
|
|
}
|
|
|
|
resolve_collisions(scene)
|
|
}
|
|
|
|
Row :: distinct [16]i32
|
|
|
|
|
|
main :: proc() {
|
|
fmt.println("Hello, world!")
|
|
|
|
|
|
rl.InitWindow(SCREEN_W, SCREEN_H, "HUI")
|
|
defer rl.CloseWindow()
|
|
rl.SetTargetFPS(60)
|
|
|
|
|
|
arena: runtime.Arena
|
|
defer runtime.arena_destroy(&arena)
|
|
|
|
arena_alloc := runtime.arena_allocator(&arena)
|
|
|
|
map_data, err := load_map(
|
|
"./Tiled/map_file.json",
|
|
arena_alloc,
|
|
)
|
|
if err.type != .None {
|
|
|
|
fmt.println(err.msg)
|
|
os.exit(1)
|
|
}
|
|
|
|
//WARN: Add a destructor to free (handlemap dangling)
|
|
scene := create_scene()
|
|
scene.map_tex = render_map(&scene.atlas, map_data)
|
|
|
|
for !rl.WindowShouldClose() {
|
|
dt := rl.GetFrameTime()
|
|
update_scene(&scene, dt)
|
|
rl.BeginDrawing()
|
|
{
|
|
rl.ClearBackground(rl.BLACK)
|
|
draw_scene(&scene)
|
|
}
|
|
rl.EndDrawing()
|
|
}
|
|
}
|