diff options
| author | Xander Swan <[hidden email]> | 2025-12-23 21:35:30 -0500 |
|---|---|---|
| committer | Xander Swan <[hidden email]> | 2025-12-23 21:35:30 -0500 |
| commit | 1754937f0721f304f742575e9a27fc7ba10d8374 (patch) | |
| tree | df439bfd9733d9553ce867df6f69e5348161801c | |
| parent | 673c84b1e56f65bcda448b2304e98ff4831100fd (diff) | |
Add temp tile collisions
| -rwxr-xr-x | src.bin | bin | 0 -> 2391800 bytes | |||
| -rw-r--r-- | src/main.odin | 55 | ||||
| -rw-r--r-- | src/phys/body.odin | 4 | ||||
| -rw-r--r-- | src/phys/world.odin | 12 | ||||
| -rw-r--r-- | src/player.odin | 2 | ||||
| -rw-r--r-- | src/tiled/tiled.odin | 28 |
6 files changed, 54 insertions, 47 deletions
| Binary files differ diff --git a/src/main.odin b/src/main.odin index 88bf953..2e02f3c 100644 --- a/src/main.odin +++ b/src/main.odin @@ -43,48 +43,19 @@ init :: proc() { } state.room = room - make_platform(Rect{ - start = {50, 50}, - size = {64, 20} - }) - - i := f32(20) - for i < draw.SCREEN_WIDTH - 20 - 64 { - make_platform(Rect{ - start = {i, draw.SCREEN_HEIGHT - 20}, - size = {64, 20} + make_map_collisions :: proc( + x: i32, + y: i32, + tile: tiled.Tile, + tile_set: tiled.Tile_Set, + ) { + make_platform({ + {f32(x), f32(y)}, + {f32(tile_set.tile_width), f32(tile_set.tile_height)}, }) - i += 65 } - make_platform(Rect{ - start = {160, 320}, - size = {20, 20} - }) - make_platform(Rect{ - start = {300, 220}, - size = {40, 20} - }) - - make_platform(Rect{ - start = {240, 180}, - size = {40, 20} - }) - - make_platform(Rect{ - start = {200, 140}, - size = {40, 20} - }) - - make_platform(Rect{ - start = {180, 100}, - size = {20, 20} - }) - - make_platform(Rect{ - start = {140, 100}, - size = {16, 16} - }) + tiled.iterate_map_tiles(state.room, make_map_collisions) } frame :: proc() { @@ -121,7 +92,10 @@ main :: proc() { defer { if len(track.allocation_map) > 0 { - fmt.eprintf("=== %v allocations not freed: ===\n", len(track.allocation_map)) + fmt.eprintf( + "=== %v allocations not freed: ===\n", + len(track.allocation_map), + ) total := 0 for _, entry in track.allocation_map { fmt.eprintf( @@ -158,4 +132,3 @@ main :: proc() { cleanup() } - diff --git a/src/phys/body.odin b/src/phys/body.odin index 0256dee..0fe6c51 100644 --- a/src/phys/body.odin +++ b/src/phys/body.odin @@ -15,7 +15,7 @@ Layer :: enum(u16) { PLAYER, // player hitboxes } -Collision_Type :: enum(u16) { +Collision_Type :: enum(u8) { UP, DOWN, RIGHT, @@ -31,7 +31,7 @@ Body :: struct { active: bool, pos: Vec2, vel: Vec2, - collisions: bit_set[Collision_Type; u16], + collisions: bit_set[Collision_Type; u8], layers: bit_set[Layer; u16], mask: bit_set[Layer; u16], } diff --git a/src/phys/world.odin b/src/phys/world.odin index 0f03de7..8470872 100644 --- a/src/phys/world.odin +++ b/src/phys/world.odin @@ -195,15 +195,21 @@ update_body :: proc(h: Body_Handle) { b.vel.y = 0 } + set_body_position(h, res_pos) +} + +set_body_position :: proc(h: Body_Handle, new_pos: Vec2) { + b := get_body(h) + prev_bin := hash_bin(world_to_bin(b.pos + b.rect.start)) - res_bin := hash_bin(world_to_bin(res_pos + b.rect.start)) + res_bin := hash_bin(world_to_bin(new_pos + b.rect.start)) if prev_bin != res_bin { remove_from_bins(b^) - b.pos = res_pos + b.pos = new_pos add_to_bins(b^) } else { - b.pos = res_pos + b.pos = new_pos } } diff --git a/src/player.odin b/src/player.odin index 65e1b87..5d7ffb4 100644 --- a/src/player.odin +++ b/src/player.odin @@ -37,7 +37,7 @@ init_player :: proc(p: ^Player) { ) p.body_handle = handle - body.pos = Vec2{50, 0} + phys.set_body_position(handle, Vec2{50, 100}) draw.init_sprite(&p.sprite, p.anim.image_path, p.anim) diff --git a/src/tiled/tiled.odin b/src/tiled/tiled.odin index e1627a5..e01f6c5 100644 --- a/src/tiled/tiled.odin +++ b/src/tiled/tiled.odin @@ -344,6 +344,34 @@ convert_json_map :: proc(jmap: Json_Map, tmap: ^Map, path: string) -> Error { return .NONE } +Map_Iterate_Callback :: proc(i32, i32, Tile, Tile_Set) + +iterate_map_tiles :: proc(tmap: Map, callback: Map_Iterate_Callback) { + for layer in tmap.layers { + tile_layer, ok := layer.layer.(Tile_Layer) + if !ok { + continue + } + + x: i32 = 0 + y: i32 = 0 + + for cell in tile_layer.data { + if cell != 0 { + tile := tmap.tiles[cell - 1] + tile_set := tmap.tile_sets[tile.tile_set] + callback(x, y, tile, tile_set) + } + + x += tmap.tile_width + if x > (tmap.width - 1) * tmap.tile_width { + x = 0 + y += tmap.tile_height + } + } + } +} + draw_map :: proc(tmap: Map) { for layer in tmap.layers { tile_layer, ok := layer.layer.(Tile_Layer) |
