Skip to content

Commit

Permalink
hyperlinks move camera
Browse files Browse the repository at this point in the history
  • Loading branch information
Uriopass committed Aug 11, 2023
1 parent 1cf9288 commit 700a260
Show file tree
Hide file tree
Showing 15 changed files with 214 additions and 43 deletions.
11 changes: 11 additions & 0 deletions egregoria/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use geom::Vec3;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::any::Any;
use std::collections::BTreeMap;
use std::fmt::{Display, Formatter};
use std::hash::Hash;
use std::time::{Duration, Instant};
use utils::rand_provider::RandProvider;
Expand Down Expand Up @@ -67,6 +68,16 @@ pub enum SoulID {
FreightStation(FreightStationID),
}

impl Display for SoulID {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
SoulID::Human(id) => write!(f, "{:?}", id),
SoulID::GoodsCompany(id) => write!(f, "{:?}", id),
SoulID::FreightStation(id) => write!(f, "{:?}", id),
}
}
}

impl From<SoulID> for AnyEntity {
fn from(value: SoulID) -> Self {
match value {
Expand Down
2 changes: 1 addition & 1 deletion egregoria/src/map_dynamic/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use slotmapd::HopSlotMap;
pub struct Router {
steps: Vec<RoutingStep>,
cur_step: Option<RoutingStep>,
target_dest: Option<Destination>,
pub target_dest: Option<Destination>,
cur_dest: Option<Destination>,
vehicle: Option<VehicleID>,
pub personal_car: Option<VehicleID>,
Expand Down
2 changes: 1 addition & 1 deletion egregoria/src/souls/desire/buyfood.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ debug_inspect_impl!(BuyFoodState);

#[derive(Inspect, Clone, Serialize, Deserialize, Debug)]
pub struct BuyFood {
last_ate: GameInstant,
pub last_ate: GameInstant,
state: BuyFoodState,
bread: ItemID,
}
Expand Down
2 changes: 1 addition & 1 deletion egregoria/src/souls/desire/home.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use serde::{Deserialize, Serialize};

#[derive(Inspect, Clone, Serialize, Deserialize, Debug)]
pub struct Home {
house: BuildingID,
pub house: BuildingID,
}

impl Home {
Expand Down
14 changes: 14 additions & 0 deletions egregoria/src/utils/time.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use egui_inspect::Inspect;
use serde::{Deserialize, Serialize};
use std::fmt::{Display, Formatter};

pub const SECONDS_PER_REALTIME_SECOND: i32 = 15;
pub const SECONDS_PER_HOUR: i32 = 3600;
Expand Down Expand Up @@ -195,6 +196,19 @@ impl GameInstant {
}
}

impl Display for GameInstant {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let d = GameTime::new(0.0, self.timestamp);
write!(f, "{}", d.daytime)
}
}

impl Display for DayTime {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}d {:02}:{:02}", self.day, self.hour, self.second)
}
}

#[cfg(test)]
mod test {
use common::timestep::UP_DT;
Expand Down
14 changes: 14 additions & 0 deletions egregoria/src/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use geom::{Transform, Vec2, Vec3};
use serde::Deserialize;
use slotmapd::__impl::Serialize;
use slotmapd::{new_key_type, HopSlotMap};
use std::fmt::{Display, Formatter};

new_key_type! {
pub struct VehicleID;
Expand Down Expand Up @@ -429,3 +430,16 @@ impl<
.chain(self.4)
}
}

impl Display for AnyEntity {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
AnyEntity::HumanID(id) => write!(f, "{:?}", id),
AnyEntity::VehicleID(id) => write!(f, "{:?}", id),
AnyEntity::TrainID(id) => write!(f, "{:?}", id),
AnyEntity::WagonID(id) => write!(f, "{:?}", id),
AnyEntity::FreightStationID(id) => write!(f, "{:?}", id),
AnyEntity::CompanyID(id) => write!(f, "{:?}", id),
}
}
}
9 changes: 6 additions & 3 deletions native_app/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::audio::AudioContext;
use crate::game_loop;
use crate::init::SOUNDS_LIST;
use crate::input::InputContext;
use crate::rendering::CameraHandler3D;
use egregoria::utils::time::GameTime;
use geom::{vec2, vec3, LinearColor};
use std::time::Instant;
Expand Down Expand Up @@ -130,12 +131,14 @@ impl Context {
let params = self.gfx.render_params.value_mut();
params.time_always = (params.time_always + self.delta) % 3600.0;
params.sun_col = sun.z.max(0.0).sqrt().sqrt() * LinearColor::new(1.0, 0.95 + sun.z * 0.05, 0.95 + sun.z * 0.05, 1.0);
params.cam_pos = state.camera.camera.eye();
params.cam_dir = -state.camera.camera.dir();
let camera = state.uiw.read::<CameraHandler3D>();
params.cam_pos = camera.camera.eye();
params.cam_dir = -camera.camera.dir();
params.sun = sun;
params.viewport = vec2(self.gfx.size.0 as f32, self.gfx.size.1 as f32);
params.sun_shadow_proj =
state.camera.camera.build_sun_shadowmap_matrix(sun, params.shadow_mapping_resolution as f32, &state.camera.frustrum).try_into().unwrap();
camera.camera.build_sun_shadowmap_matrix(sun, params.shadow_mapping_resolution as f32, &camera.frustrum).try_into().unwrap();
drop(camera);
let c = egregoria::config();
params.grass_col = c.grass_col.into();
params.sand_col = c.sand_col.into();
Expand Down
31 changes: 17 additions & 14 deletions native_app/src/game_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ pub struct State {

pub game_schedule: SeqSchedule,

pub camera: CameraHandler3D,

egui_render: EguiWrapper,

instanced_renderer: InstancedRender,
Expand Down Expand Up @@ -74,6 +72,7 @@ impl State {

let gui: Gui = common::saveload::JSON::load("gui").unwrap_or_default();
uiworld.insert(camera.camera);
uiworld.insert(camera);

log::info!("version is {}", VERSION);

Expand All @@ -87,7 +86,6 @@ impl State {
let me = Self {
uiw: uiworld,
game_schedule,
camera,
egui_render,
instanced_renderer: InstancedRender::new(&mut ctx.gfx),
map_renderer: MapRenderer::new(&mut ctx.gfx, &goria),
Expand All @@ -111,9 +109,12 @@ impl State {
if !self.egui_render.last_mouse_captured {
let goria = self.goria.read().unwrap();
let map = goria.map();
let unproj = self.camera.unproject(ctx.input.mouse.screen, |p| {
map.terrain.height(p).map(|x| x + 0.01)
});
let unproj = self
.uiw
.read::<CameraHandler3D>()
.unproject(ctx.input.mouse.screen, |p| {
map.terrain.height(p).map(|x| x + 0.01)
});

self.uiw.write::<InputMap>().unprojected = unproj;
}
Expand Down Expand Up @@ -149,29 +150,30 @@ impl State {
.update(&self.goria.read().unwrap(), &mut self.uiw, &mut ctx.audio);

FollowEntity::update_camera(self);
self.camera.update(ctx);
self.uiw.camera_mut().update(ctx);
}

pub fn reset(&mut self, ctx: &mut Context) {
self.map_renderer = MapRenderer::new(&mut ctx.gfx, &self.goria.read().unwrap());
self.goria.write().unwrap().map().dispatch_all();
}

#[profiling::function]
pub fn render(&mut self, ctx: &mut FrameContext<'_>) {
profiling::scope!("main render");
let start = Instant::now();
let goria = self.goria.read().unwrap();

self.immtess.meshbuilder.clear();
self.camera.cull_tess(&mut self.immtess);
let camera = self.uiw.read::<CameraHandler3D>();
camera.cull_tess(&mut self.immtess);

let time: GameTime = *self.goria.read().unwrap().read::<GameTime>();

self.map_renderer.render(
&goria.map(),
time.seconds,
&self.camera.camera,
&self.camera.frustrum,
&camera.camera,
&camera.frustrum,
MapRenderOptions {
show_arrows: self.uiw.read::<Tool>().show_arrows(),
},
Expand Down Expand Up @@ -286,14 +288,14 @@ impl State {
let goria = self.goria.read().unwrap();
let map = goria.map();
// self.camera.movespeed = settings.camera_sensibility / 100.0;
self.camera.camera_movement(
self.uiw.camera_mut().camera_movement(
ctx,
ctx.delta,
&self.uiw.read::<InputMap>(),
&self.uiw.read::<Settings>(),
|p| map.terrain.height(p),
);
*self.uiw.write::<Camera>() = self.camera.camera;
*self.uiw.write::<Camera>() = self.uiw.read::<CameraHandler3D>().camera;

drop(map);
}
Expand All @@ -303,7 +305,8 @@ impl State {
}

pub fn resized(&mut self, ctx: &mut Context, size: PhysicalSize<u32>) {
self.camera
self.uiw
.write::<CameraHandler3D>()
.resize(ctx, size.width as f32, size.height as f32);
}
}
Expand Down
2 changes: 1 addition & 1 deletion native_app/src/gui/follow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl FollowEntity {

if let Some(e) = state.uiw.read::<FollowEntity>().0 {
if let Some(pos) = state.goria.read().unwrap().pos_any(e) {
state.camera.follow(pos);
state.uiw.camera_mut().follow(pos);
}
}
}
Expand Down
20 changes: 20 additions & 0 deletions native_app/src/gui/inspect.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::gui::follow::FollowEntity;
use crate::gui::{InspectedBuilding, InspectedEntity};
use crate::uiworld::UiWorld;
use egregoria::economy::{ItemRegistry, Market};
use egregoria::map::BuildingID;
use egregoria::transportation::Location;
use egregoria::{
AnyEntity, CompanyEnt, Egregoria, FreightStationEnt, HumanEnt, SoulID, TrainEnt, VehicleEnt,
Expand Down Expand Up @@ -147,3 +149,21 @@ impl InspectRenderer {
}
}
}

pub fn building_link(uiworld: &mut UiWorld, goria: &Egregoria, ui: &mut Ui, b: BuildingID) {
if ui.link(format!("{:?}", b)).clicked() {
uiworld.write::<InspectedBuilding>().e = Some(b);
if let Some(b) = goria.map().buildings().get(b) {
uiworld.camera_mut().targetpos = b.door_pos;
}
}
}

pub fn entity_link(uiworld: &mut UiWorld, goria: &Egregoria, ui: &mut Ui, e: AnyEntity) {
if ui.link(format!("{}", e)).clicked() {
uiworld.write::<InspectedEntity>().e = Some(e);
if let Some(pos) = goria.pos_any(e) {
uiworld.camera_mut().targetpos = pos
}
}
}
19 changes: 9 additions & 10 deletions native_app/src/gui/inspect_building.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use egregoria::engine_interaction::WorldCommand;
use egregoria::{Egregoria, SoulID};
use egui::{Context, Ui, Widget};

use crate::gui::{item_icon, InspectedEntity};
use crate::gui::inspect::entity_link;
use crate::gui::item_icon;
use egregoria::map::{Building, BuildingID, BuildingKind, Zone, MAX_ZONE_AREA};
use egregoria::map_dynamic::BuildingInfos;
use egregoria::souls::freight_station::FreightTrainState;
Expand Down Expand Up @@ -72,19 +73,17 @@ pub fn inspect_building(uiworld: &mut UiWorld, goria: &Egregoria, ui: &Context,
fn render_house(ui: &mut Ui, uiworld: &mut UiWorld, goria: &Egregoria, b: &Building) {
let binfos = goria.read::<BuildingInfos>();
let Some(info) = binfos.get(b.id) else { return; };
let Some(owner) = info.owner else { return; };
let Some(SoulID::Human(owner)) = info.owner else { return; };

let mut inspected = uiworld.write::<InspectedEntity>();

if ui.button(format!("Owner: {owner:?}")).clicked() {
inspected.e = Some(owner.into());
}
ui.horizontal(|ui| {
ui.label("Owner");
entity_link(uiworld, goria, ui, owner.into());
});

ui.label("Currently in the house:");
for &soul in info.inside.iter() {
if ui.button(format!("{soul:?}")).clicked() {
inspected.e = Some(soul.into());
}
let SoulID::Human(soul) = soul else { continue; };
entity_link(uiworld, goria, ui, soul.into());
}
}

Expand Down
Loading

0 comments on commit 700a260

Please sign in to comment.