readded ability to read scenes from a file
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Sat, 26 Feb 2022 03:03:28 +0000 (21:03 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Sat, 26 Feb 2022 03:03:28 +0000 (21:03 -0600)
run_tree/scenes/quick_save.scene
src/entity/editor.onyx
src/entity/manager.onyx
src/entity/player.onyx
src/entity/store.onyx
src/game.onyx

index 180b2d36f2c601b5b3a68982ce1a87cea64ff951..8d0cfb42c4ad3d818f2ba732bb7589bfd61e45cb 100644 (file)
@@ -1,26 +1,27 @@
-:Wall
-entity.id = 10
-entity.flags = 2
-entity.pos.x = 168.0000
-entity.pos.y = 320.0000
-entity.size.x = 336.0000
-entity.size.y = 32.0000
+[Wall]
+id = 10
+flags = 2
+pos.x = 168.0000
+pos.y = 320.0000
+size.x = 336.0000
+size.y = 32.0000
 
-:Wall
-entity.id = 11
-entity.flags = 2
-entity.pos.x = 448.0000
-entity.pos.y = 168.0000
-entity.size.x = 32.0000
-entity.size.y = 336.0000
+[Wall]
+id = 11
+flags = 2
+pos.x = 448.0000
+pos.y = 168.0000
+size.x = 32.0000
+size.y = 336.0000
 
-:Player
-entity.id = 12
-entity.flags = 2
-entity.pos.x = 200.0000
-entity.pos.y = 152.0000
-entity.size.x = 48.0000
-entity.size.y = 48.0000
+[Player]
+id = 12
+flags = 2
+pos.x = 200.0000
+pos.y = 152.0000
+size.x = 48.0000
+size.y = 48.0000
+:PlayerComponent
 holding = 0
 controls.up = 87
 controls.down = 83
@@ -34,21 +35,23 @@ color.b = 1.0000
 color.a = 1.0000
 facing = 4
 
-:Door
-entity.id = 13
-entity.flags = 3
-entity.pos.x = 352.0000
-entity.pos.y = 320.0000
-entity.size.x = 160.0000
-entity.size.y = 32.0000
+[Door]
+id = 13
+flags = 3
+pos.x = 352.0000
+pos.y = 320.0000
+size.x = 160.0000
+size.y = 32.0000
+:DoorComponent
 target_openness = 0.0000
 
-:Item_Entity
-entity.id = 14
-entity.flags = 4
-entity.pos.x = 272.0000
-entity.pos.y = 144.0000
-entity.size.x = 32.0000
-entity.size.y = 32.0000
+[Item_Entity]
+id = 14
+flags = 4
+pos.x = 272.0000
+pos.y = 144.0000
+size.x = 32.0000
+size.y = 32.0000
+:ItemComponent
 item = "beer"
 
index 24473bada592284d85a397cf15482caf7958736b..49701c03f4cbd6cd1469cce135312e360f729de1 100644 (file)
@@ -318,12 +318,6 @@ editor_draw :: () {
             immediate_rectangle(x, y + Field_Height + 4, Field_Height / 2, new_y - y);
             y, i = new_y, new_i;
 
-        } elseif it.type == Entity_ID {
-            value_buf: [1024] u8;
-            // entity_type := (scene->get(*cast(^Entity_ID) member_any.data)).type; // Dereferencing null here
-            // value_str := conv.format_va(value_buf, "{} ({})", .[member_any, .{^entity_type, type_expr}]);
-            // font_print(editor_font, x + w - font_get_width(editor_font, value_str) - 2, y + Field_Height, value_str);
-
         } else {
             value_buf: [1024] u8;
             value_str := conv.format_va(value_buf, "{}", .[member_any]);
index cad011ee9efd9757f524cd72cee441741be89579..d69f3cf804a17a9cc2b3befcdcef56c3807a66f1 100644 (file)
@@ -46,6 +46,7 @@ InteractableComponent :: struct {
 Entity :: struct {
     id: Entity_ID;
     flags: Entity_Flags;
+    schematic := "Custom";
 
     pos:  Vector2;
 
index 308f7618e1cddf77ff6b69e04536adf9a145d492..64f74f293a4ed6b48129c90edd8a69f0337021c7 100644 (file)
@@ -221,9 +221,7 @@ player_assets: struct {
 
     update :: (use this: ^Entity, dt: f32) {
         door := this->get(DoorComponent);
-        if door.openness != door.target_openness {
-            move_towards(^door.openness, door.target_openness, dt * 2);
-        }
+        door->update(dt);
     }
 
     render :: (use this: ^Entity) {
@@ -251,11 +249,18 @@ player_assets: struct {
 DoorComponent :: struct {
     use base: Component;
 
+    max_openness    := 0.8f;
     target_openness := 0.0f;
     openness        := 0.0f;
 
+    update :: (use this: ^DoorComponent, dt: f32) {
+        if openness != target_openness {
+            move_towards(^openness, target_openness, dt * 2);
+        }
+    }
+
     toggle_open :: (use this: ^DoorComponent) {
-        target_openness = 0.8f - target_openness;
+        target_openness = max_openness - target_openness;
     }
 }
 
index f744d1f1c6a5be6d7bd173ebeef8aa2bc940825b..74bb53cf99d4138aa781e64855d5ebcf241aba80 100644 (file)
@@ -75,7 +75,6 @@ entity_manager_save_to_file :: (use this: ^Entity_Manager, filename: str) {
 }
 
 entity_manager_load_from_file :: (use this: ^Entity_Manager, filename: str) {
-    /*
     err, input_file := os.open(filename, .Read);
     if err != .None {
         debug_log(.Error, "Failed to open file: {}", filename);
@@ -84,7 +83,7 @@ entity_manager_load_from_file :: (use this: ^Entity_Manager, filename: str) {
     defer os.close(^input_file);
     reader := io.reader_make(^input_file);
 
-    for entities do raw_free(entity_allocator, it);
+    for entities do this->delete(it);
     map.clear(^entity_map);
     array.clear(^entities);
     next_entity_id = 0;
@@ -92,35 +91,56 @@ entity_manager_load_from_file :: (use this: ^Entity_Manager, filename: str) {
     use type_info;
 
     current_entity: ^Entity;
+    current_component: ^Component;
 
     line_number := 1;
     while !io.reader_empty(^reader) {
         defer line_number += 1;
 
-        line := io.read_line(^reader, inplace=true);
+        line := io.read_line(^reader, inplace=true) |> string.strip_whitespace();
         if line.count <= 1 do continue;
 
-        if line[0] == #char ":" {
+        if line[0] == #char "[" {
             if current_entity != null do this->add(current_entity);
+            current_component = null;
 
             entity_kind := string.advance(line)
-                        |> string.strip_whitespace();
-
-            entity_info : ^Entity_Info;
-            for^ entity_types.entries {
-                if (cast(^Type_Info_Struct) get_type_info(it.key)).name == entity_kind {
-                    entity_info = ^it.value;
-                    break;
-                }
-            }
-
-            if entity_info == null {
+                        |> (x => str.{x.data, x.count - 1})(); // In the grossest way possible remove one character from the end.
+            
+            schematic := schematics[entity_kind];
+            if schematic == null_proc {
                 debug_log(.Error, "Unknown entity kind '{}' on line {}.", entity_kind, line_number);
                 current_entity = null;
                 continue;
             }
 
-            current_entity = entity_info.create_default(this);
+            debug_log(.Debug, "Creating entity from schematic: {}.", entity_kind);
+            current_entity = schematic(this);
+            continue;
+        }
+
+        if line[0] == #char ":" {
+            if current_entity == null {
+                debug_log(.Error, "Attempt to create a component when no entity is specified on line {}.", line_number);
+                continue;
+            }
+
+            component_kind := string.advance(line);
+            component_type := get_struct_by_name(component_kind);
+            if current_entity->has(component_type) {
+                current_component = current_entity.components[component_type];
+
+            } else {
+                component_info := get_type_info(component_type);
+
+                current_component = raw_alloc(entity_allocator, component_info.size);
+                memory.set(current_component, 0, component_info.size);
+                current_component.type = component_type;
+
+                debug_log(.Debug, "Adding {} to entity.", component_type);
+                current_entity->add(current_component);
+            }
+
             continue;
         }
 
@@ -131,24 +151,27 @@ entity_manager_load_from_file :: (use this: ^Entity_Manager, filename: str) {
         string.advance(^line, 1);
         string.strip_whitespace(^line);
 
-        member := get_any_for_member(any.{current_entity, current_entity.type}, var_name);
-        if member.data == null {
-            entity_info := cast(^Type_Info_Struct) get_type_info(current_entity.type);
-            debug_log(.Warning, "'{}' is not a valid member of '{}' on line {}.", var_name, entity_info.name, line_number);
-            continue;
+        set_member :: macro (ptr: rawptr, type: type_expr) {
+            member := get_any_for_member(any.{ptr, type}, var_name);
+            if member.data == null {
+                t_info := cast(^Type_Info_Struct) get_type_info(type);
+                debug_log(.Warning, "'{}' is not a valid member of '{}' on line {}.", var_name, t_info.name, line_number);
+                continue;
+            }
+
+            if !conv.parse_any(member.data, member.type, line) {
+                debug_log(.Warning, "Unable to parse '{}' for type '{}' on line {}.", line, member.type, line_number);
+                continue;
+            }
         }
 
-        if !conv.parse_any(member.data, member.type, line) {
-            debug_log(.Warning, "Unable to parse '{}' for type '{}' on line {}.", line, member.type, line_number);
-            continue;
+        if current_component == null {
+            set_member(current_entity, Entity);
+        } else {
+            set_member(current_component, current_component.type);
         }
     }
 
     if current_entity != null do this->add(current_entity);
-    */
 }
 
-
-#local {
-
-}
index 83c1d9516a275649420d5a1782f337ee141167b9..aeab713ce20db1aa8b1fa5f757d953c33bf76e39 100644 (file)
@@ -17,7 +17,8 @@ game_init :: () {
     load_assets();
 
     scene = entity_manager_create();
-    // scene->load_from_file("scenes/quick_save.scene");
+    scene->load_from_file("scenes/quick_save.scene");
+    /*
     // player := player_create(^scene);
 
     player := scene.schematics["Player"](^scene);
@@ -42,6 +43,7 @@ game_init :: () {
     }
 
     scene->add(item);
+    */
 
     item_store = item_store_make();
     item_store->load_items_from_file("scenes/default.items");