From: Brendan Hansen Date: Fri, 25 Feb 2022 19:23:29 +0000 (-0600) Subject: code cleanup using ENT_INFO macro X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=d8d08255f0a01c50ef44cec4f9acde5d22ac0410;p=bar-game.git code cleanup using ENT_INFO macro --- diff --git a/run_tree/scenes/quick_save.scene b/run_tree/scenes/quick_save.scene index 5015cbb..180b2d3 100644 --- a/run_tree/scenes/quick_save.scene +++ b/run_tree/scenes/quick_save.scene @@ -36,7 +36,7 @@ facing = 4 :Door entity.id = 13 -entity.flags = 7 +entity.flags = 3 entity.pos.x = 352.0000 entity.pos.y = 320.0000 entity.size.x = 160.0000 diff --git a/src/entity/editor.onyx b/src/entity/editor.onyx index 109c406..a94bdce 100644 --- a/src/entity/editor.onyx +++ b/src/entity/editor.onyx @@ -72,12 +72,11 @@ editor_update :: (dt: f32) { selected_entity_id = Entity_Nothing; for scene.entities { - get_rect := scene.entity_types[it.type].get_rect; - if get_rect == null_proc do continue; - - if get_rect(it) |> Rect.contains(mouse_pos) { - selected_entity_id = it.id; - break; + if get_rect := ENT_INFO(it).get_rect; get_rect != null_proc { + if get_rect(it) |> Rect.contains(mouse_pos) { + selected_entity_id = it.id; + break; + } } } @@ -103,7 +102,7 @@ editor_update :: (dt: f32) { new_top_left.x = editor_grid_size * math.floor(new_top_left.x / editor_grid_size); new_top_left.y = editor_grid_size * math.floor(new_top_left.y / editor_grid_size); - rect := scene.entity_types[selected_entity.type].get_rect(selected_entity); + rect := ENT_INFO(selected_entity).get_rect(selected_entity); new_rect := Rect.{ new_top_left.x, new_top_left.y, rect.w, rect.h }; selected_entity.pos = Rect.center(new_rect); @@ -120,9 +119,7 @@ editor_update :: (dt: f32) { if resizing { if editor_grid_shown { - E :: macro (e: ^Entity) => scene.entity_types[e.type]; - - rect := E(selected_entity).get_rect(selected_entity); + rect := ENT_INFO(selected_entity).get_rect(selected_entity); new_size := mouse_get_position_vector() - Rect.top_left(rect); new_size.x = editor_grid_size * math.floor(new_size.x / editor_grid_size); new_size.y = editor_grid_size * math.floor(new_size.y / editor_grid_size); @@ -166,7 +163,7 @@ editor_draw :: () { if selected_entity_id != Entity_Nothing { selected_entity := scene->get(selected_entity_id); - get_rect := scene.entity_types[selected_entity.type].get_rect; + get_rect := ENT_INFO(selected_entity).get_rect; r := get_rect(selected_entity); immediate_set_color(.{1,1,0,0.5}); diff --git a/src/entity/manager.onyx b/src/entity/manager.onyx index e11bad3..89406bd 100644 --- a/src/entity/manager.onyx +++ b/src/entity/manager.onyx @@ -76,6 +76,15 @@ Entity_Manager :: struct { save_to_file :: entity_manager_save_to_file; } +// This assumes that the main entity manager is called "scene". +ENT_INFO :: macro (e: ^Entity) => { + @CompilerBug // Why does the following line break the program? + // It's like its declaring a new null scene...? + // scene :: scene; + + return ^scene.entity_types[e.type]; +} + entity_manager_create :: () -> Entity_Manager { em: Entity_Manager; em.entity_allocator = context.allocator; @TODO // Replace the allocator here. diff --git a/src/entity/player.onyx b/src/entity/player.onyx index 560669f..ea4df01 100644 --- a/src/entity/player.onyx +++ b/src/entity/player.onyx @@ -98,9 +98,9 @@ Player :: struct { if holding != Entity_Nothing { holding_object := scene->get(holding); holding_object.flags |= .Carryable; - vtable := ^scene.entity_types[holding_object.type]; - if vtable.get_rect != null_proc { - r := vtable.get_rect(holding_object); + obj_get_rect := ENT_INFO(holding_object).get_rect; + if obj_get_rect != null_proc { + r := get_rect(holding_object); d := Vector2.{(size.x + r.w) / 2 + 4, (size.y + r.h) / 2 + 4}; holding_object.pos = pos + facing_to_direction_vector(facing) * d; @@ -136,9 +136,9 @@ Player :: struct { defer memory.free_slice(^objects); for objects { - vtable := ^scene.entity_types[it.type]; - if vtable.interact == null_proc do continue; - vtable.interact(it, this); + if interact := ENT_INFO(it).interact; interact != null_proc { + interact(it, this); + } } } @@ -148,9 +148,9 @@ Player :: struct { // if holding != Entity_Nothing { holding_object := scene->get(holding); - vtable := ^scene.entity_types[holding_object.type]; - if vtable.get_rect != null_proc { - r := vtable.get_rect(holding_object); + obj_get_rect := ENT_INFO(holding_object).get_rect; + if obj_get_rect != null_proc { + r := obj_get_rect(holding_object); holding_object.pos = pos - .{0, (size.y + r.h) / 2}; } } @@ -166,8 +166,8 @@ Player :: struct { if it == this do continue; if it.id == this.holding do continue; - vtable := ^scene.entity_types[it.type]; - if Rect.intersects(ent_rect, vtable.get_rect(~~ it)) { + vtable := ENT_INFO(it); + if Rect.intersects(ent_rect, vtable.get_rect(it)) { pos -= delta; break; } @@ -229,7 +229,6 @@ Door :: struct { this.flags |= .Interactable; this.flags |= .Solid; - this.flags |= .Carryable; } get_rect :: (use this: ^Door) => {