}
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);
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;
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;
}
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 {
-
-}