Skip to content

Commit

Permalink
Move assets and worldgen to separate crates
Browse files Browse the repository at this point in the history
  • Loading branch information
lunacys committed Aug 22, 2024
1 parent 75d395e commit f67b908
Show file tree
Hide file tree
Showing 25 changed files with 144 additions and 107 deletions.
24 changes: 24 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ members = [
"common",
"collisions",
"movement",
"vs-assets",
"worldgen",
"vs-rs"
]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#![allow(dead_code)]

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum BitMaskDirection {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
#![allow(dead_code)]

use bevy::prelude::*;
use common::math::almost_equal_vec2;

use super::room::WorldRoom;
use crate::{math::almost_equal_vec2, FRect};

#[derive(Default, Clone, Copy)]
pub struct Edge {
Expand Down Expand Up @@ -84,7 +81,7 @@ impl Delaunay2D {
res
}

pub fn triangulate_constraint(rooms: &[WorldRoom]) -> Delaunay2D {
pub fn triangulate_constraint(rooms: &[FRect]) -> Delaunay2D {
let mut res = Delaunay2D {
edges: vec![],
triangles: vec![],
Expand All @@ -93,8 +90,8 @@ impl Delaunay2D {

for i in 0..rooms.len() {
for j in (i + 1)..rooms.len() {
let a = rooms[i].rect;
let b = rooms[j].rect;
let a = rooms[i];
let b = rooms[j];

res.edges.push(Edge {
u: a.center(),
Expand Down
3 changes: 3 additions & 0 deletions common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use bevy::prelude::*;

pub mod bitmasking;
pub mod delaunay2d;
pub mod math;
pub mod prim;

#[derive(Component, Debug, Default, Clone, Copy, PartialEq, Reflect)]
pub struct Position(pub Vec2);
Expand Down
3 changes: 2 additions & 1 deletion vs-rs/src/worlds/worldgen/prim.rs → common/src/prim.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use bevy::prelude::*;
use common::math::almost_equal_f32;

use crate::math::almost_equal_f32;

#[derive(Default, Debug, Clone, Copy)]
pub struct PrimEdge {
Expand Down
13 changes: 13 additions & 0 deletions vs-assets/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "vs-assets"
version = "0.1.0"
edition = "2021"

[dependencies]
"common" = { path = "../common" }
"bevy" = { version = "0.14"}
tiled = "0.12"
thiserror = "1.0"
serde_json = "1.0"
serde = "1.0"
rand = "0.8.5"
5 changes: 5 additions & 0 deletions vs-assets/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub mod plugin;
pub mod prefabs;
pub mod rooms;
pub mod tilesheets;
pub mod prelude;
38 changes: 23 additions & 15 deletions vs-rs/src/assets.rs → vs-assets/src/plugin.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
#![allow(dead_code)]

use crate::AppState;
use bevy::{asset::LoadedFolder, prelude::*};
use rooms::{MapAsset, RoomStore};
use tilesheets::AssetTileSheet;

pub mod prefabs;
pub mod rooms;
pub mod tilesheets;
use crate::{
rooms::{MapAsset, RoomStore},
tilesheets::AssetTileSheet,
};

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, States)]
pub enum AssetLoadingState {
#[default]
LoadAssets,
SetupAssets,
Finished,
}

pub struct GameAssetsPlugin;

impl Plugin for GameAssetsPlugin {
fn build(&self, app: &mut App) {
app.init_state::<AssetLoadingState>();
app.insert_resource(RoomStore::default());
app.add_systems(OnEnter(AppState::LoadAssets), (start_loading,));
app.add_systems(OnEnter(AssetLoadingState::LoadAssets), (start_loading,));
app.add_systems(
Update,
check_asset_folders.run_if(in_state(AppState::LoadAssets)),
check_asset_folders.run_if(in_state(AssetLoadingState::LoadAssets)),
);
app.add_systems(
OnEnter(AssetLoadingState::SetupAssets),
(setup_game_assets,),
);
app.add_systems(OnEnter(AppState::SetupAssets), (setup_game_assets,));
}
}

Expand All @@ -40,7 +48,7 @@ pub struct GameAssetFolders {
}

fn check_asset_folders(
mut next_state: ResMut<NextState<AppState>>,
mut next_state: ResMut<NextState<AssetLoadingState>>,
mut game_assets: ResMut<GameAssetFolders>,
mut events: EventReader<AssetEvent<LoadedFolder>>,
) {
Expand All @@ -59,7 +67,7 @@ fn check_asset_folders(

if game_assets.tiles_loaded && game_assets.rooms_loaded {
info!("Finished loading");
next_state.set(AppState::SetupAssets);
next_state.set(AssetLoadingState::SetupAssets);
}
}

Expand All @@ -81,7 +89,7 @@ fn start_loading(mut commands: Commands, asset_server: Res<AssetServer>) {

fn setup_game_assets(
mut commands: Commands,
mut next_state: ResMut<NextState<AppState>>,
mut next_state: ResMut<NextState<AssetLoadingState>>,
asset_server: Res<AssetServer>,
mut layouts: ResMut<Assets<TextureAtlasLayout>>,
rooms: Res<Assets<MapAsset>>,
Expand Down Expand Up @@ -123,7 +131,7 @@ fn setup_game_assets(
}

info!("Finished setting up game assets");
next_state.set(AppState::Finished);
next_state.set(AssetLoadingState::Finished);
}

fn load_player(
Expand Down
File renamed without changes.
5 changes: 5 additions & 0 deletions vs-assets/src/prelude.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub use crate::plugin::{AssetLoadingState, GameAssetFolders, GameAssets, GameAssetsPlugin};
pub use crate::rooms::{MapAsset, MapAssetLoader, MapAssetLoaderError, RoomStore};
pub use crate::tilesheets::{
AssetTileSheet, TsxTilesetAsset, TsxTilesetAssetLoader, TsxTilesetAssetLoaderError,
};
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions vs-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ common = { path = "../common" }
collisions = { path = "../collisions" }
movement = { path = "../movement" }
worldgen = { path = "../worldgen" }
vs-assets = { path = "../vs-assets" }
bevy = { version = "0.14", features = ["dynamic_linking"] }
rand = "0.8.5"
serde_json = "1.0"
Expand Down
2 changes: 1 addition & 1 deletion vs-rs/src/enemy.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::assets::GameAssets;
#[cfg(debug_assertions)]
use crate::debug::DebugSettings;
use crate::player::*;
Expand All @@ -12,6 +11,7 @@ use movement::behaviors::SteerSeek;
use movement::prelude::*;
use rand::{thread_rng, Rng};
use serde::{Deserialize, Serialize};
use vs_assets::plugin::GameAssets;
use std::collections::HashMap;
use std::time::Duration;

Expand Down
28 changes: 19 additions & 9 deletions vs-rs/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
use assets::{
rooms::{MapAsset, MapAssetLoader},
tilesheets::{TsxTilesetAsset, TsxTilesetAssetLoader},
};
use bevy::{
diagnostic::FrameTimeDiagnosticsPlugin,
input::gamepad::{AxisSettings, GamepadSettings},
Expand All @@ -10,19 +6,21 @@ use bevy::{
use bevy_simple_tilemap::plugin::SimpleTileMapPlugin;
use collisions::plugin::CollisionPlugin;
use movement::plugin::SteeringPlugin;
use vs_assets::{
plugin::{AssetLoadingState, GameAssetsPlugin},
rooms::{MapAsset, MapAssetLoader},
tilesheets::{TsxTilesetAsset, TsxTilesetAssetLoader},
};
use worlds::WorldPlugin;

mod camera;
mod debug;
mod enemy;
mod input;
mod player;

mod assets;
mod enemy;
mod stats;
mod worlds;

use crate::assets::GameAssetsPlugin;
use crate::enemy::EnemyPlugin;
use camera::CameraMovementPlugin;
#[cfg(debug_assertions)]
Expand All @@ -36,7 +34,6 @@ pub const FIXED_TIMESTEP: f64 = 1.0 / FRAMERATE;
pub enum AppState {
#[default]
LoadAssets,
SetupAssets,
WorldGen,
Finished,
}
Expand Down Expand Up @@ -65,6 +62,10 @@ fn main() {
.add_plugins(EnemyPlugin)
.add_plugins(CollisionPlugin)
.add_systems(Startup, (spawn_camera, setup_gamepad))
.add_systems(
Update,
(monitor_asset_loading_state,).run_if(in_state(AppState::LoadAssets)),
)
.add_plugins(GameAssetsPlugin)
.add_plugins(WorldPlugin)
.insert_resource(Time::<Fixed>::from_seconds(FIXED_TIMESTEP))
Expand All @@ -90,3 +91,12 @@ fn setup_gamepad(mut gamepad_settings: ResMut<GamepadSettings>) {

gamepad_settings.default_axis_settings = settings.clone();
}

fn monitor_asset_loading_state(
asset_state: Res<State<AssetLoadingState>>,
mut next_state: ResMut<NextState<AppState>>,
) {
if asset_state.get() == &AssetLoadingState::Finished {
next_state.set(AppState::Finished);
}
}
2 changes: 1 addition & 1 deletion vs-rs/src/player.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::assets::GameAssets;
use crate::enemy::Enemy;
use crate::input::PlayerControls;
use crate::stats::*;
Expand All @@ -9,6 +8,7 @@ use collisions::prelude::*;
use common::Position;
use movement::behaviors::SteerSeek;
use movement::prelude::*;
use vs_assets::plugin::GameAssets;
use std::time::Duration;

pub struct PlayerPlugin;
Expand Down
20 changes: 3 additions & 17 deletions vs-rs/src/worlds.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,10 @@
use bevy::prelude::*;
use bevy_simple_tilemap::{prelude::TileMapBundle, Tile, TileMap};
use collisions::prelude::*;
use vs_assets::prelude::*;
use worldgen::{generation::{settings::WorldGeneratorSettings, WorldGenerator}, world::World};

use crate::{
assets::{
rooms::{MapAsset, RoomStore},
tilesheets::AssetTileSheet,
GameAssets,
},
AppState,
};

use self::{
world::World,
worldgen::{settings::WorldGeneratorSettings, WorldGenerator},
};

pub mod bitmasking;
pub mod world;
pub mod worldgen;
use crate::AppState;

#[derive(Component)]
pub struct WorldComponent {
Expand Down
8 changes: 8 additions & 0 deletions worldgen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,11 @@ version = "0.1.0"
edition = "2021"

[dependencies]
"common" = { path = "../common" }
"collisions" = { path = "../collisions" }
"vs-assets" = { path = "../vs-assets" }
"bevy" = { version = "0.14"}
bevy_simple_tilemap = "0.15"
tiled = "0.12"
pathfinding = "4.9"
rand = "0.8.5"
Loading

0 comments on commit f67b908

Please sign in to comment.