Skip to content

Commit

Permalink
add border too rooms on asset side instead of in code.
Browse files Browse the repository at this point in the history
add checks for level assets so they are valid before running app
simplify some weird code paths
update bevy_ecs_ldtk
  • Loading branch information
Hellzbellz123 committed Apr 20, 2024
1 parent 7b4e6d1 commit 461910b
Show file tree
Hide file tree
Showing 29 changed files with 9,257 additions and 26,407 deletions.
254 changes: 80 additions & 174 deletions assets/packs/asha/levels/aspenhallsLevelSet.ldtk

Large diffs are not rendered by default.

6,271 changes: 2,080 additions & 4,191 deletions assets/packs/asha/levels/aspenhallsLevelSet/BiGOpEnSpAcE.ldtkl

Large diffs are not rendered by default.

863 changes: 277 additions & 586 deletions assets/packs/asha/levels/aspenhallsLevelSet/BossRoomL1.ldtkl

Large diffs are not rendered by default.

1,501 changes: 463 additions & 1,038 deletions assets/packs/asha/levels/aspenhallsLevelSet/DahhButtrffreeee.ldtkl

Large diffs are not rendered by default.

714 changes: 189 additions & 525 deletions assets/packs/asha/levels/aspenhallsLevelSet/DungeonStartL1.ldtkl

Large diffs are not rendered by default.

473 changes: 11 additions & 462 deletions assets/packs/asha/levels/aspenhallsLevelSet/HideoutL1.ldtkl

Large diffs are not rendered by default.

979 changes: 4 additions & 975 deletions assets/packs/asha/levels/aspenhallsLevelSet/HideoutL2.ldtkl

Large diffs are not rendered by default.

2,303 changes: 6 additions & 2,297 deletions assets/packs/asha/levels/aspenhallsLevelSet/HideoutL3.ldtkl

Large diffs are not rendered by default.

1,545 changes: 469 additions & 1,076 deletions assets/packs/asha/levels/aspenhallsLevelSet/InchWormBB.ldtkl

Large diffs are not rendered by default.

3,197 changes: 1,055 additions & 2,142 deletions assets/packs/asha/levels/aspenhallsLevelSet/PlainOlSquare.ldtkl

Large diffs are not rendered by default.

447 changes: 72 additions & 375 deletions assets/packs/asha/levels/aspenhallsLevelSet/RewardTest.ldtkl

Large diffs are not rendered by default.

3,327 changes: 1,117 additions & 2,210 deletions assets/packs/asha/levels/aspenhallsLevelSet/RightHandBracket.ldtkl

Large diffs are not rendered by default.

1,652 changes: 538 additions & 1,114 deletions assets/packs/asha/levels/aspenhallsLevelSet/SailrmenSea_Men.ldtkl

Large diffs are not rendered by default.

923 changes: 299 additions & 624 deletions assets/packs/asha/levels/aspenhallsLevelSet/ShutesNLadders.ldtkl

Large diffs are not rendered by default.

940 changes: 307 additions & 633 deletions assets/packs/asha/levels/aspenhallsLevelSet/SmallShortDonutRoom.ldtkl

Large diffs are not rendered by default.

1,999 changes: 456 additions & 1,543 deletions assets/packs/asha/levels/aspenhallsLevelSet/SpokeyEyeBall.ldtkl

Large diffs are not rendered by default.

2,013 changes: 24 additions & 1,989 deletions assets/packs/asha/levels/aspenhallsLevelSet/TestingHalls.ldtkl

Large diffs are not rendered by default.

953 changes: 325 additions & 628 deletions assets/packs/asha/levels/aspenhallsLevelSet/TinyEyeBall.ldtkl

Large diffs are not rendered by default.

887 changes: 284 additions & 603 deletions assets/packs/asha/levels/aspenhallsLevelSet/TinySquare.ldtkl

Large diffs are not rendered by default.

4,069 changes: 987 additions & 3,082 deletions assets/packs/asha/levels/aspenhallsLevelSet/WierdTeleportyThing.ldtkl

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,9 @@ pub struct DungeonRoomDatabase {
pub struct DungeonSettings {
/// dungeon max size / 2.0
pub map_halfsize: f32,
/// minimum space between dungeon rooms, in tiles
pub tiles_between_rooms: i32,
// TODO: readd, disabled because it breaks maths
// /// minimum space between dungeon rooms, in tiles
// pub tiles_between_rooms: i32,
/// amount of rooms inside dungeon
pub distribution: RoomDistribution,
/// percentage of paths between
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,16 @@ pub fn build_hallways(
let graph_data = (tile_graph.center, tile_graph.size);

for (_hallway_eid, mut hallway, _) in &mut hallways {
if hallway.built {
continue;
}

info!("generating path for hallway: {:?}", hallway.connected_rooms);
let hallway_path = create_path_simple(tile_graph, &hallway);
let Some(hallway_path) = create_path_simple(tile_graph, &hallway) else {
hallway.built = true;
error!("could not generate path for {:?}", hallway);
continue;
};

mark_path_as_hallway_tiles(&hallway_path, tile_graph);

Expand Down Expand Up @@ -98,34 +106,42 @@ pub struct HallwayPoints {
fn create_path_simple(
tile_graph: &TileGraph,
hallway: &Mut<HallWayBlueprint>,
) -> VecDeque<NodeIndex> {
) -> Option<VecDeque<NodeIndex>> {
info!("getting hallway side nodes");
let start_pos = tile_graph.get_node_at_translation(hallway.start_pos);
let end_pos = tile_graph.get_node_at_translation(hallway.end_pos);

let bad_pos_msg = |pos: IVec2| -> String { format!("node position should exist: {pos}") };
let Some(start) = start_pos else {
error!("Could not get start translation from tile position");
return None;
};
let Some(end) = end_pos else {
error!("Could not get end translation from tile position");
return None;
};

let first_side = HallwayPoints {
start: get_node_for_position(tile_graph, hallway.start_pos).unwrap_or_else(|| { panic!("{}", bad_pos_msg(hallway.start_pos)) }),
end: get_node_for_position(tile_graph, hallway.end_pos).unwrap_or_else(|| { panic!("{}", bad_pos_msg(hallway.end_pos)) })
};
let first_side = HallwayPoints { start, end };

info!("calculating first sides path");
dijkstra_path(tile_graph, first_side)
Some(dijkstra_path(tile_graph, first_side))
}

/// calculates dijkstra path between nodes
fn dijkstra_path(tile_graph: &TileGraph, hallway: HallwayPoints) -> VecDeque<NodeIndex> {
info!("getting path list");

let edge_cost = |edge: EdgeReference<TileGraphEdge>| {
let mut cost =edge.weight().cost; // 1.0
let (a,b) = tile_graph.edge_endpoints(edge.id()).expect("this edge should exist");
let mut cost = edge.weight().cost; // 1.0
let (a, b) = tile_graph
.edge_endpoints(edge.id())
.expect("this edge should exist");
let mut neighbor_count = 0;
tile_graph.neighbors(a).for_each(|f| {
neighbor_count += tile_graph.neighbors(f).count();
}); // 4 neighbors if valid, each neighbor has 4 if valid, 16 total
tile_graph.neighbors(b).for_each(|f| {
neighbor_count += tile_graph.neighbors(f).count();
});// 4 neighbors if valid, each neighbor has 4 if valid, 16 total
}); // 4 neighbors if valid, each neighbor has 4 if valid, 16 total

if neighbor_count == 32 {
cost += 0.0;
Expand All @@ -140,7 +156,7 @@ fn dijkstra_path(tile_graph: &TileGraph, hallway: HallwayPoints) -> VecDeque<Nod
&tile_graph.graph,
hallway.start,
Some(hallway.end),
edge_cost
edge_cost,
);

// Reconstruct the shortest path to the end node
Expand Down Expand Up @@ -169,9 +185,23 @@ fn dijkstra_path(tile_graph: &TileGraph, hallway: HallwayPoints) -> VecDeque<Nod
break;
}
}

if path_node_indecies.is_empty() {
warn!("could not generate path");
let path_start_pos = tile_graph
.node_weight(hallway.start)
.expect("node should exist")
.tile;
let path_end_pos = tile_graph
.node_weight(hallway.end)
.expect("node should exist")
.tile;
let path_start_debug = tile_graph.get_tiles_translation(path_start_pos);
let path_end_debug = tile_graph.get_tiles_translation(path_end_pos);
info!("path start debug: {:?}", path_start_debug);
info!("path end debug: {:?}", path_end_debug);
panic!("could not generate path");
}

path_node_indecies
}

Expand All @@ -189,15 +219,6 @@ fn mark_path_as_hallway_tiles(expanded_path: &VecDeque<NodeIndex>, tile_graph: &
}
}

/// finds `node_index` for givent tile position
pub fn get_node_for_position(tile_graph: &TileGraph, position: IVec2) -> Option<NodeIndex> {
tile_graph.node_indices().find(|&node_index| {
let node = tile_graph.graph[node_index];
let node_pos = get_tile_translation(tile_graph.center, tile_graph.size, node.tile);
node_pos.as_ivec2() == position
})
}

/// calculates distance between 2 uvecs
pub const fn manhattan_distance_tiles(v1: UVec2, v2: UVec2) -> u32 {
let dx = v2.x.saturating_sub(v1.x);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ fn fix_graph(room_graph: &mut Graph<DungeonGraphNode, DungeonGraphEdge, Undirect
petgraph::algo::kosaraju_scc(&*room_graph);

if connected_components.len() > 1 {
warn!("graph is bad, fixing graph");
warn!("graph is bad, fixing graph. groups : {}", connected_components.len());
warn!("connected nodes: {:?}", connected_components);
connected_components.sort_by_key(Vec::len);

Expand Down
7 changes: 5 additions & 2 deletions crates/aspenlib/src/game/game_world/dungeonator_v2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,14 @@ fn spawn_dungeon_root(mut cmds: Commands, ldtk_project_handles: Res<AspenMapHand
dungeon: Dungeon {
rooms: Vec::new(),
hallways: Vec::new(),
tile_graph: TileGraph::new(4000 / 32, position.translation.truncate()),
// TODO: fix this
// border is applied too each room asset so 0 here
tile_graph: TileGraph::new(4000 / 32, 0, position.translation.truncate()),
settings: DungeonSettings {
// room placing settings
map_halfsize: 2000.0,
tiles_between_rooms: 4,
// TODO: use this but make it working
// tiles_between_rooms: 4,
distribution: RoomDistribution {
small_short: 6,
small_long: 4,
Expand Down
48 changes: 30 additions & 18 deletions crates/aspenlib/src/game/game_world/dungeonator_v2/rooms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ use bevy::{
use bevy_ecs_ldtk::{assets::LdtkExternalLevel, prelude::LdtkProject};

use crate::{
consts::TILE_SIZE,
game::game_world::dungeonator_v2::{
components::{
try_get_roomlevel, try_get_roomshape, try_get_roomtype,
DungeonRoomBundle, DungeonRoomDatabase, RoomBlueprint,
RoomDescriptor, RoomLevel, RoomPreset, RoomShape, RoomType, Dungeon,
try_get_roomlevel, try_get_roomshape, try_get_roomtype, Dungeon, DungeonRoomBundle,
DungeonRoomDatabase, RoomBlueprint, RoomDescriptor, RoomLevel, RoomPreset, RoomShape,
RoomType,
},
utils::{choose_filler_presets, get_leveled_preset, random_room_positon},
GeneratorState,
Expand Down Expand Up @@ -52,6 +53,21 @@ pub fn generate_room_database(
dungeon_project
.iter_external_levels(&level_assets)
.for_each(|level_def| {
// check if room assets are right size,
// if they arent the correct size we get annoying panics elsewhere
if ["DungeonStartL1", "TestingHalls"].iter().all(|f| f != level_def.identifier())
&& ((level_def.px_wid() / TILE_SIZE as i32) % 2 != 0
|| (level_def.px_hei() / TILE_SIZE as i32) % 2 != 0)
{
panic!("Dungeon filler room MUST be even number of tiles in size for x AND y: {}", level_def.identifier());
}
if ["DungeonStartL1"].iter().all(|f| f == level_def.identifier())
&& ((level_def.px_wid() / TILE_SIZE as i32) / 2 == 0
|| (level_def.px_hei() / TILE_SIZE as i32) / 2 == 0)
{
panic!("ONLY Dungeon Start room MUST be odd number of tiles in size for x AND y: {}", level_def.identifier());
}

let field_instances = level_def.field_instances();
let room_shape = try_get_roomshape(field_instances)
.expect("No size ident on room definiton. Check Ldtk Editor for errors");
Expand Down Expand Up @@ -131,9 +147,7 @@ pub fn select_presets(
/// spawns possible dungeon rooms from `dungeon_root.dungeon_settings.useable_dungeons`
pub fn spawn_presets(
mut cmds: Commands,
dungeon_root: Query<
(Entity, &Dungeon, &Handle<LdtkProject>),
>,
dungeon_root: Query<(Entity, &Dungeon, &Handle<LdtkProject>)>,
) {
let (dungeon_root, dungeon_settings, _proj_handle) = dungeon_root.single();
if dungeon_settings.rooms.is_empty() {
Expand All @@ -144,18 +158,16 @@ pub fn spawn_presets(
cmds.entity(dungeon_root).with_children(|rooms| {
for room in &dungeon_settings.rooms {
let name = format!("DungeonRoom-{}", room.name);
rooms.spawn((
DungeonRoomBundle {
room: room.clone(),
name: Name::new(name),
id: room.asset_id.clone(),
spatial: SpatialBundle::from_transform(Transform::from_xyz(
room.position.x as f32,
room.position.y as f32,
0.0,
)),
},
));
rooms.spawn((DungeonRoomBundle {
room: room.clone(),
name: Name::new(name),
id: room.asset_id.clone(),
spatial: SpatialBundle::from_transform(Transform::from_xyz(
room.position.x as f32,
room.position.y as f32,
0.0,
)),
},));
}
});
}
Loading

0 comments on commit 461910b

Please sign in to comment.