Skip to content
Snippets Groups Projects
mod.rs 3.48 KiB
Newer Older
use bevy::app::{App, CoreStage};
use bevy::prelude::{
	Commands, Events, IntoSystemDescriptor, Plugin, ResMut, SystemSet, SystemStage,
};
use iyes_loopless::prelude::{AppLooplessStateExt, ConditionSet};
Louis's avatar
Louis committed

mod encounters;
mod generators;
mod specialism;
Louis's avatar
Louis committed
mod trading;
mod travel;
mod utils;
mod world_query;
Louis's avatar
Louis committed

pub use encounters::{
	Encounter, EncounterOption, EncounterOutcome, EncounterRequirement, EncounterState,
	EncounterType, SelectEncounterOptionEvent, WorldZones,
};
pub use hunger::{HungerState, StarvationMarker};
pub use spawning::PendingLoadState;
pub use specialism::CraftSpecialism;
Louis's avatar
Louis committed
pub use towns::{CurrentResidence, PathingResult, TownName, TownPaths, TravelPath, TravelTarget};
pub use trading::{
	ItemName, RosterEntry, TradeGood, TradeManifest, TradeManifestTickState, TradeRoster,
	TradingState,
Louis's avatar
Louis committed
};
pub use travel::DistanceTravelled;
Louis's avatar
Louis committed
pub use utils::{grid_to_px, px_to_grid, ActiveLevel, WorldLinked};
pub use world_query::{CameraBounds, MapQuery};

use crate::system::flow::AppState;
Louis's avatar
Louis committed
use crate::world::hunger::PlayerStarvedEvent;
use crate::world::spawning::PopulateWorldEvent;
Louis's avatar
Louis committed
use crate::world::travel::WorldTickEvent;
Louis's avatar
Louis committed
pub struct WorldPlugin;
impl Plugin for WorldPlugin {
	fn build(&self, app: &mut App) {
		app.add_stage_after(CoreStage::PreUpdate, "travel", SystemStage::parallel());
		app.add_stage_after("travel", "trade", SystemStage::parallel());
		app.add_stage_after("trade", "hunger", SystemStage::parallel());
		app.add_stage_after(CoreStage::Update, "cleanup", SystemStage::parallel());

		app.init_resource::<TownPaths>()
			.init_resource::<WorldZones>()
			.init_resource::<EncounterState>()
Louis's avatar
Louis committed
			.add_event::<PopulateWorldEvent>()
Louis's avatar
Louis committed
			.add_event::<WorldTickEvent>()
Louis's avatar
Louis committed
			.add_event::<PlayerStarvedEvent>()
			.add_event::<SelectEncounterOptionEvent>()
			.add_enter_system(AppState::InGame, |mut commands: Commands| {
				commands.insert_resource(ActiveLevel {
					map: String::from("Grantswaith"),
					dirty: true,
				});
			.add_enter_system(AppState::Menu, spawning::clean_game_state)
			.add_system_set_to_stage(
				"travel",
				ConditionSet::new()
					.label("tick_travelling_merchant")
					.run_in_state(AppState::InGame)
					.with_system(travel::tick_travelling_merchant)
					.into(),
			)
			.add_system_set_to_stage(
				"travel",
				ConditionSet::new()
					.after("tick_travelling_merchant")
					.run_in_state(AppState::InGame)
Louis's avatar
Louis committed
					.with_system(travel::track_travel)
					.into(),
			)
			.add_system_set_to_stage(
				"trade",
				ConditionSet::new()
					.run_in_state(AppState::InGame)
Louis's avatar
Louis committed
					.with_system(trading::tick_manifests)
					.with_system(specialism::check_specialisms)
			)
			.add_system_set_to_stage(
				"hunger",
				ConditionSet::new()
					.run_in_state(AppState::InGame)
					.with_system(hunger::process_player_hunger_ticks)
					.with_system(hunger::process_towns_hunger_ticks)
					.into(),
			)
			.add_system_set_to_stage(
				"cleanup",
				ConditionSet::new()
					.run_in_state(AppState::InGame)
					.with_system(encounters::notify_new_zone)
					.with_system(hunger::handle_entity_starvation)
					.with_system(encounters::handle_encounter_option_event)
Louis's avatar
Louis committed
					.with_system(hunger::handle_player_starved)
					.with_system(|mut events: ResMut<Events<WorldTickEvent>>| {
						events.clear();
					})
					.into(),
			)
			.add_system_set(
				ConditionSet::new()
					.run_in_state(AppState::InGame)
					.with_system(spawning::spawn_world_data)
					.with_system(spawning::populate_world)
					.into(),
Louis's avatar
Louis committed
	}
}