code cleanup using ENT_INFO macro
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 25 Feb 2022 19:23:29 +0000 (13:23 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 25 Feb 2022 19:23:29 +0000 (13:23 -0600)
run_tree/scenes/quick_save.scene
src/entity/editor.onyx
src/entity/manager.onyx
src/entity/player.onyx

index 5015cbbb7a9f9b21e20e1ca87160f7f8e5510ebb..180b2d36f2c601b5b3a68982ce1a87cea64ff951 100644 (file)
@@ -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
index 109c4063ae29935bccff514cec4bcd01801766e4..a94bdced9ed31824f853e4bee77f33cb7d890860 100644 (file)
@@ -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});
index e11bad36853ce8b086f9fc213a57383a84f0e605..89406bd8c4ce2cbe56730dd8feefd77ed13d97e9 100644 (file)
@@ -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.
index 560669f2a9da3a929e6a88243c2153dbd53ad40f..ea4df0162557aa46d37b1828b08f41cc4a3de38a 100644 (file)
@@ -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) => {