From d4a0cd88bc090bc11ee22973e58171bf35716746 Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Fri, 25 Feb 2022 21:03:28 -0600 Subject: [PATCH] readded ability to read scenes from a file --- run_tree/scenes/quick_save.scene | 73 ++++++++++++++-------------- src/entity/editor.onyx | 6 --- src/entity/manager.onyx | 1 + src/entity/player.onyx | 13 +++-- src/entity/store.onyx | 81 ++++++++++++++++++++------------ src/game.onyx | 4 +- 6 files changed, 103 insertions(+), 75 deletions(-) diff --git a/run_tree/scenes/quick_save.scene b/run_tree/scenes/quick_save.scene index 180b2d3..8d0cfb4 100644 --- a/run_tree/scenes/quick_save.scene +++ b/run_tree/scenes/quick_save.scene @@ -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" diff --git a/src/entity/editor.onyx b/src/entity/editor.onyx index 24473ba..49701c0 100644 --- a/src/entity/editor.onyx +++ b/src/entity/editor.onyx @@ -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]); diff --git a/src/entity/manager.onyx b/src/entity/manager.onyx index cad011e..d69f3cf 100644 --- a/src/entity/manager.onyx +++ b/src/entity/manager.onyx @@ -46,6 +46,7 @@ InteractableComponent :: struct { Entity :: struct { id: Entity_ID; flags: Entity_Flags; + schematic := "Custom"; pos: Vector2; diff --git a/src/entity/player.onyx b/src/entity/player.onyx index 308f761..64f74f2 100644 --- a/src/entity/player.onyx +++ b/src/entity/player.onyx @@ -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; } } diff --git a/src/entity/store.onyx b/src/entity/store.onyx index f744d1f..74bb53c 100644 --- a/src/entity/store.onyx +++ b/src/entity/store.onyx @@ -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 { - -} diff --git a/src/game.onyx b/src/game.onyx index 83c1d95..aeab713 100644 --- a/src/game.onyx +++ b/src/game.onyx @@ -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"); -- 2.25.1