Fixed serialization bug with duplicate entity ids

This commit is contained in:
2024-10-22 23:38:36 +02:00
parent c2504ec624
commit 74675a4c77
2 changed files with 117 additions and 12 deletions

View File

@@ -14,14 +14,14 @@ GAME_NAME :: "OCT24";
// String added via add_build_string() from c:/Personal/games/onegameamonth/oct24/modules/Coven/metaprogram.jai:248.
//
init_scene :: (scene: *Scene) {
scene.by_type._Block.allocator = scene.allocator;scene.by_type._Character.allocator = scene.allocator;scene.by_type._Item.allocator = scene.allocator;
scene.by_type._Block.allocator = scene.allocator;scene.by_type._Character.allocator = scene.allocator;scene.by_type._Crab.allocator = scene.allocator;scene.by_type._Item.allocator = scene.allocator;
}
//
// String added via add_build_string() from c:/Personal/games/onegameamonth/oct24/modules/Coven/metaprogram.jai:260.
//
editor_ui_entity_creation :: () -> *Entity {
if ui_clickable_label("New Block") return new_block();if ui_clickable_label("New Character") return new_character();if ui_clickable_label("New Item") return new_item();
if ui_clickable_label("New Block") return new_block();if ui_clickable_label("New Character") return new_character();if ui_clickable_label("New Crab") return new_crab();if ui_clickable_label("New Item") return new_item();
return null;
}
@@ -37,7 +37,7 @@ delete_entity :: (e: *Entity) {
destroy_entity(e);
if e.type == {
case Block; bucket_array_remove(*e.scene.by_type._Block, e._locator); case Character; bucket_array_remove(*e.scene.by_type._Character, e._locator); case Item; bucket_array_remove(*e.scene.by_type._Item, e._locator);
case Block; bucket_array_remove(*e.scene.by_type._Block, e._locator); case Character; bucket_array_remove(*e.scene.by_type._Character, e._locator); case Crab; bucket_array_remove(*e.scene.by_type._Crab, e._locator); case Item; bucket_array_remove(*e.scene.by_type._Item, e._locator);
}
}
@@ -54,6 +54,7 @@ serialize_entity :: (e: *Entity, path: string) {
if e.type == {
case Block; serialize_entity(cast(*Block)e, *builder);
case Character; serialize_entity(cast(*Character)e, *builder);
case Crab; serialize_entity(cast(*Crab)e, *builder);
case Item; serialize_entity(cast(*Item)e, *builder);
}
@@ -80,6 +81,7 @@ deserialize_entity :: (scene: *Scene, id: Entity_Id, path: string) -> *Entity {
if type == {
case "Block"; p, locator := find_and_occupy_empty_slot(*scene.by_type._Block); p._locator = locator; e = p; register_entity(scene, p, id); init_entity(p); deserialize_entity(scene, lines, cast(*Block)e); update_matrix(*e.transform);
case "Character"; p, locator := find_and_occupy_empty_slot(*scene.by_type._Character); p._locator = locator; e = p; register_entity(scene, p, id); init_entity(p); deserialize_entity(scene, lines, cast(*Character)e); update_matrix(*e.transform);
case "Crab"; p, locator := find_and_occupy_empty_slot(*scene.by_type._Crab); p._locator = locator; e = p; register_entity(scene, p, id); init_entity(p); deserialize_entity(scene, lines, cast(*Crab)e); update_matrix(*e.transform);
case "Item"; p, locator := find_and_occupy_empty_slot(*scene.by_type._Item); p._locator = locator; e = p; register_entity(scene, p, id); init_entity(p); deserialize_entity(scene, lines, cast(*Item)e); update_matrix(*e.transform);
}
@@ -98,11 +100,91 @@ new_character :: (scene: *Scene = null) -> *Character { _scene := scene;
if _scene == null {
_scene = engine.current_scene; }
p, locator := find_and_occupy_empty_slot(*_scene.by_type._Character); p._locator = locator; register_entity(_scene, p); p.transform = create_identity_transform(); init_entity(p); return p; }
new_crab :: (scene: *Scene = null) -> *Crab { _scene := scene;
if _scene == null {
_scene = engine.current_scene; }
p, locator := find_and_occupy_empty_slot(*_scene.by_type._Crab); p._locator = locator; register_entity(_scene, p); p.transform = create_identity_transform(); init_entity(p); return p; }
new_item :: (scene: *Scene = null) -> *Item { _scene := scene;
if _scene == null {
_scene = engine.current_scene; }
p, locator := find_and_occupy_empty_slot(*_scene.by_type._Item); p._locator = locator; register_entity(_scene, p); p.transform = create_identity_transform(); init_entity(p); return p; }
//
// String added via add_build_string() from c:/Personal/games/onegameamonth/oct24/modules/Coven/metaprogram.jai:307.
//
serialize_entity :: (e: *Block, builder: *String_Builder) {
print_to_builder(builder, "type: Block\n");
print_to_builder(builder, "entity.enabled: %\n", e.entity.enabled);
print_to_builder(builder, "entity.flags: %\n", e.entity.flags);
print_to_builder(builder, "entity.transform.position.x: %\n", e.entity.transform.position.x);
print_to_builder(builder, "entity.transform.position.y: %\n", e.entity.transform.position.y);
print_to_builder(builder, "entity.transform.position.z: %\n", e.entity.transform.position.z);
print_to_builder(builder, "entity.transform.orientation.x: %\n", e.entity.transform.orientation.x);
print_to_builder(builder, "entity.transform.orientation.y: %\n", e.entity.transform.orientation.y);
print_to_builder(builder, "entity.transform.orientation.z: %\n", e.entity.transform.orientation.z);
print_to_builder(builder, "entity.transform.orientation.w: %\n", e.entity.transform.orientation.w);
print_to_builder(builder, "entity.transform.scale.x: %\n", e.entity.transform.scale.x);
print_to_builder(builder, "entity.transform.scale.y: %\n", e.entity.transform.scale.y);
print_to_builder(builder, "entity.transform.scale.z: %\n", e.entity.transform.scale.z);
print_to_builder(builder, "entity.snap_offset.x: %\n", e.entity.snap_offset.x);
print_to_builder(builder, "entity.snap_offset.y: %\n", e.entity.snap_offset.y);
print_to_builder(builder, "entity.snap_offset.z: %\n", e.entity.snap_offset.z);
print_to_builder(builder, "entity.snap_intervals.x: %\n", e.entity.snap_intervals.x);
print_to_builder(builder, "entity.snap_intervals.y: %\n", e.entity.snap_intervals.y);
print_to_builder(builder, "entity.snap_intervals.z: %\n", e.entity.snap_intervals.z);
print_to_builder(builder, "block_type: %\n", e.block_type);
print_to_builder(builder, "debug_id: %\n", e.debug_id);
}
//
// String added via add_build_string() from c:/Personal/games/onegameamonth/oct24/modules/Coven/metaprogram.jai:307.
//
deserialize_entity :: (scene: *Scene, lines: [] string, e: *Block) {
for line: lines {
values := split(line, ":");
if values.count == 2 {
if trim(values[0], " ") == {
case "entity.enabled";
scan2(values[1], "%", *e.entity.enabled);
case "entity.transform.position.x";
scan2(values[1], "%", *e.entity.transform.position.x);
case "entity.transform.position.y";
scan2(values[1], "%", *e.entity.transform.position.y);
case "entity.transform.position.z";
scan2(values[1], "%", *e.entity.transform.position.z);
case "entity.transform.orientation.x";
scan2(values[1], "%", *e.entity.transform.orientation.x);
case "entity.transform.orientation.y";
scan2(values[1], "%", *e.entity.transform.orientation.y);
case "entity.transform.orientation.z";
scan2(values[1], "%", *e.entity.transform.orientation.z);
case "entity.transform.orientation.w";
scan2(values[1], "%", *e.entity.transform.orientation.w);
case "entity.transform.scale.x";
scan2(values[1], "%", *e.entity.transform.scale.x);
case "entity.transform.scale.y";
scan2(values[1], "%", *e.entity.transform.scale.y);
case "entity.transform.scale.z";
scan2(values[1], "%", *e.entity.transform.scale.z);
case "entity.snap_offset.x";
scan2(values[1], "%", *e.entity.snap_offset.x);
case "entity.snap_offset.y";
scan2(values[1], "%", *e.entity.snap_offset.y);
case "entity.snap_offset.z";
scan2(values[1], "%", *e.entity.snap_offset.z);
case "entity.snap_intervals.x";
scan2(values[1], "%", *e.entity.snap_intervals.x);
case "entity.snap_intervals.y";
scan2(values[1], "%", *e.entity.snap_intervals.y);
case "entity.snap_intervals.z";
scan2(values[1], "%", *e.entity.snap_intervals.z);
case "debug_id";
scan2(values[1], "%", *e.debug_id);
}
}
}
}
//
// String added via add_build_string() from c:/Personal/games/onegameamonth/oct24/modules/Coven/metaprogram.jai:307.
//
@@ -178,8 +260,8 @@ deserialize_entity :: (scene: *Scene, lines: [] string, e: *Item) {
//
// String added via add_build_string() from c:/Personal/games/onegameamonth/oct24/modules/Coven/metaprogram.jai:307.
//
serialize_entity :: (e: *Block, builder: *String_Builder) {
print_to_builder(builder, "type: Block\n");
serialize_entity :: (e: *Crab, builder: *String_Builder) {
print_to_builder(builder, "type: Crab\n");
print_to_builder(builder, "entity.enabled: %\n", e.entity.enabled);
print_to_builder(builder, "entity.flags: %\n", e.entity.flags);
print_to_builder(builder, "entity.transform.position.x: %\n", e.entity.transform.position.x);
@@ -198,14 +280,19 @@ serialize_entity :: (e: *Block, builder: *String_Builder) {
print_to_builder(builder, "entity.snap_intervals.x: %\n", e.entity.snap_intervals.x);
print_to_builder(builder, "entity.snap_intervals.y: %\n", e.entity.snap_intervals.y);
print_to_builder(builder, "entity.snap_intervals.z: %\n", e.entity.snap_intervals.z);
print_to_builder(builder, "block_type: %\n", e.block_type);
print_to_builder(builder, "debug_id: %\n", e.debug_id);
print_to_builder(builder, "current_yaw: %\n", e.current_yaw);
print_to_builder(builder, "target_look_direction.x: %\n", e.target_look_direction.x);
print_to_builder(builder, "target_look_direction.y: %\n", e.target_look_direction.y);
print_to_builder(builder, "target_look_direction.z: %\n", e.target_look_direction.z);
print_to_builder(builder, "current_direction.x: %\n", e.current_direction.x);
print_to_builder(builder, "current_direction.y: %\n", e.current_direction.y);
print_to_builder(builder, "current_direction.z: %\n", e.current_direction.z);
}
//
// String added via add_build_string() from c:/Personal/games/onegameamonth/oct24/modules/Coven/metaprogram.jai:307.
//
deserialize_entity :: (scene: *Scene, lines: [] string, e: *Block) {
deserialize_entity :: (scene: *Scene, lines: [] string, e: *Crab) {
for line: lines {
values := split(line, ":");
if values.count == 2 {
@@ -244,8 +331,20 @@ deserialize_entity :: (scene: *Scene, lines: [] string, e: *Block) {
scan2(values[1], "%", *e.entity.snap_intervals.y);
case "entity.snap_intervals.z";
scan2(values[1], "%", *e.entity.snap_intervals.z);
case "debug_id";
scan2(values[1], "%", *e.debug_id);
case "current_yaw";
scan2(values[1], "%", *e.current_yaw);
case "target_look_direction.x";
scan2(values[1], "%", *e.target_look_direction.x);
case "target_look_direction.y";
scan2(values[1], "%", *e.target_look_direction.y);
case "target_look_direction.z";
scan2(values[1], "%", *e.target_look_direction.z);
case "current_direction.x";
scan2(values[1], "%", *e.current_direction.x);
case "current_direction.y";
scan2(values[1], "%", *e.current_direction.y);
case "current_direction.z";
scan2(values[1], "%", *e.current_direction.z);
}
}
}
@@ -370,14 +469,15 @@ deserialize_entity :: (scene: *Scene, lines: [] string, e: *Character) {
//
// NUM_ENTITY_TYPES tells the target program how many entity types there are.
NUM_ENTITY_TYPES :: 3;
NUM_ENTITY_TYPES :: 4;
// entity_types is an array containing all the entity types.
entity_types : [3] Type : .[ Block, Character, Item ];
entity_types : [4] Type : .[ Block, Character, Crab, Item ];
Entity_Storage :: struct {
_Block: Bucket_Array(Block, 20, true);
_Character: Bucket_Array(Character, 20, true);
_Crab: Bucket_Array(Crab, 20, true);
_Item: Bucket_Array(Item, 20, true);
}

View File

@@ -54,10 +54,15 @@ load_scene :: (name: string) -> *Scene {
files.allocator = temp;
visit_files(path, true, *files, visitor);
highest : Entity_Id = 0;
for file: files {
deserialize_entity(scene, file.id, file.full_path);
if file.id > highest {
highest = file.id;
}
}
next_entity_id = cast(Entity_Id)(highest + 1);
calculate_aabbs(scene);
make_sure_nothing_collides(scene);