2024-07-06 20:23:25 +00:00
|
|
|
#![warn(rust_2018_idioms)]
|
|
|
|
#![deny(clippy::wildcard_imports)]
|
|
|
|
#![forbid(elided_lifetimes_in_paths, unsafe_code)]
|
|
|
|
|
|
|
|
mod assets {
|
|
|
|
include!(env!("ASSETS_RS"));
|
|
|
|
}
|
|
|
|
|
2024-07-06 13:33:58 +02:00
|
|
|
mod activities;
|
2024-07-05 23:10:25 +02:00
|
|
|
mod game;
|
2024-07-06 20:45:05 +00:00
|
|
|
mod ui;
|
2024-07-05 23:10:25 +02:00
|
|
|
|
2024-07-07 14:03:30 +00:00
|
|
|
use std::env::var;
|
|
|
|
|
2024-07-06 20:23:25 +00:00
|
|
|
use self::{
|
|
|
|
activities::{house::HouseState, overworld::worldgen::Overworld, Activity},
|
2024-07-06 20:45:05 +00:00
|
|
|
game::Ghost
|
2024-07-06 20:23:25 +00:00
|
|
|
};
|
|
|
|
use comfy::{
|
2024-07-07 14:03:30 +00:00
|
|
|
init_game_config, pollster, run_comfy_main_async, DevConfig, EngineContext,
|
|
|
|
EngineState, GameConfig, GameLoop, HashMap, IVec2
|
2024-07-06 20:23:25 +00:00
|
|
|
};
|
2024-07-05 23:10:25 +02:00
|
|
|
|
|
|
|
const GAME_NAME: &str = "Powercreep";
|
|
|
|
|
2024-07-07 14:03:30 +00:00
|
|
|
/// true if devtools should be enable
|
|
|
|
fn dev_from_env() -> bool {
|
|
|
|
match var("DEV").as_ref().map(|f| f.as_str()) {
|
|
|
|
Ok("false") => false,
|
|
|
|
Ok("true") => true,
|
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
_ => true,
|
|
|
|
#[cfg(not(debug_assertions))]
|
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-06 17:59:56 +00:00
|
|
|
#[derive(Debug, Default)]
|
2024-07-06 13:33:58 +02:00
|
|
|
struct State {
|
2024-07-06 20:23:25 +00:00
|
|
|
setup_called: bool,
|
2024-07-06 17:59:56 +00:00
|
|
|
activity: Activity,
|
2024-07-06 20:45:05 +00:00
|
|
|
ghost: Ghost,
|
2024-07-06 20:23:25 +00:00
|
|
|
overworld: Overworld,
|
2024-07-07 11:21:08 +00:00
|
|
|
houses: HashMap<IVec2, HouseState>,
|
2024-07-07 14:03:30 +00:00
|
|
|
score: f32,
|
|
|
|
/// show dev tools
|
|
|
|
dev: bool
|
2024-07-06 13:33:58 +02:00
|
|
|
}
|
2024-07-05 23:10:25 +02:00
|
|
|
|
2024-07-07 11:21:08 +00:00
|
|
|
impl State {
|
|
|
|
fn get_house_pos(&self) -> Option<IVec2> {
|
|
|
|
match self.activity {
|
|
|
|
Activity::House(pos) => Some(pos),
|
|
|
|
_ => None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn house(&self) -> Option<&HouseState> {
|
2024-07-07 11:54:42 +00:00
|
|
|
self.houses.get(&self.get_house_pos()?)
|
2024-07-07 11:21:08 +00:00
|
|
|
}
|
|
|
|
|
2024-07-07 11:54:42 +00:00
|
|
|
fn house_mut(&mut self, ctx: &mut EngineContext<'_>) -> &mut HouseState {
|
2024-07-07 11:21:08 +00:00
|
|
|
self.houses
|
|
|
|
.entry(self.get_house_pos().unwrap())
|
2024-07-07 11:54:42 +00:00
|
|
|
.or_insert_with(|| HouseState::generate_new_house(ctx))
|
2024-07-07 11:21:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-05 23:10:25 +02:00
|
|
|
impl GameLoop for State {
|
|
|
|
fn new(_c: &mut EngineState) -> Self {
|
2024-07-07 14:03:30 +00:00
|
|
|
Self {
|
|
|
|
dev: dev_from_env(),
|
|
|
|
..Default::default()
|
|
|
|
}
|
2024-07-05 23:10:25 +02:00
|
|
|
}
|
|
|
|
|
2024-07-07 11:54:42 +00:00
|
|
|
fn update(&mut self, ctx: &mut EngineContext<'_>) {
|
2024-07-06 20:23:25 +00:00
|
|
|
if !self.setup_called {
|
2024-07-07 14:13:52 +00:00
|
|
|
game::setup(self, ctx);
|
2024-07-07 11:54:42 +00:00
|
|
|
game::setup(self, ctx);
|
2024-07-06 20:23:25 +00:00
|
|
|
self.setup_called = true;
|
|
|
|
}
|
|
|
|
|
2024-07-07 11:54:42 +00:00
|
|
|
game::update(self, ctx);
|
|
|
|
game::draw(self, ctx);
|
2024-07-07 14:13:52 +00:00
|
|
|
game::update(self, ctx);
|
|
|
|
game::draw(self, ctx);
|
2024-07-05 23:10:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn config(config: GameConfig) -> GameConfig {
|
2024-07-07 14:03:30 +00:00
|
|
|
let dev = if dev_from_env() {
|
|
|
|
DevConfig {
|
|
|
|
show_fps: true,
|
|
|
|
..Default::default()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Default::default()
|
2024-07-07 14:59:39 +02:00
|
|
|
};
|
|
|
|
GameConfig {
|
|
|
|
tonemapping_enabled: true,
|
|
|
|
dev,
|
2024-07-07 14:03:30 +00:00
|
|
|
target_framerate: 165,
|
|
|
|
..config
|
2024-07-07 14:59:39 +02:00
|
|
|
}
|
2024-07-05 23:10:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn run() {
|
|
|
|
init_game_config(GAME_NAME.to_string(), env!("CARGO_PKG_VERSION"), config);
|
|
|
|
let mut engine = EngineState::new();
|
|
|
|
let game = State::new(&mut engine);
|
|
|
|
run_comfy_main_async(game, engine).await;
|
|
|
|
}
|
2024-07-05 19:01:09 +02:00
|
|
|
|
|
|
|
fn main() {
|
2024-07-05 23:10:25 +02:00
|
|
|
pollster::block_on(run());
|
|
|
|
}
|