use crate::ecs::{Camera, Translation, Rotation, Scale, SpriteBundle, SpriteTexture}; use crate::input::MouseWheelEvent; use crate::rendering::ui::UiRenderable; use crate::rendering::App; use bevy_ecs::event::{EventReader, Events}; use bevy_ecs::schedule::Schedule; use bevy_ecs::system::ResMut; use bevy_ecs::world::World; use egui::Context; use tracing::info; use winit::event_loop::{ControlFlow, EventLoop}; #[cfg(target_arch = "wasm32")] #[path = "wasm/mod.rs"] pub mod platform; #[cfg(not(target_arch = "wasm32"))] #[path = "native/mod.rs"] pub mod platform; pub mod ecs; pub mod input; pub mod rendering; // Hi, you've found the real main function! This is called AFTER platform-specific initialization code. pub fn start() { info!( "Hello, world! StarKingdoms.TK v{} says hello, running on {}", env!("CARGO_PKG_VERSION"), if cfg!(target_arch = "wasm32") { "wasm" } else { "native" } ); info!("Creating the ECS world..."); let mut world = World::new(); world.insert_resource::>(Events::default()); world.insert_resource(Camera { x: 0.0, y: 0.0, zoom: 1.0, }); let mut start_schedule = Schedule::default(); // Add startup things here // Caution: This will run before there are things on-screen start_schedule.run(&mut world); let mut update_schedule = Schedule::default(); // Add things to run every frame here // Caution: This will run once before there are things on screen update_schedule.add_systems(zoom_camera_on_mouse_events); world.spawn(SpriteBundle { position: Translation { x: 100.0, y: 100.0 }, scale: Scale { width: 100.0, height: 100.0, }, rotation: Rotation { radians: 45.0_f32.to_radians() }, texture: SpriteTexture { texture: "happy-tree".to_string(), }, }); let event_loop = EventLoop::new().unwrap(); event_loop.set_control_flow(ControlFlow::Poll); event_loop .run_app(&mut App::new(world, update_schedule, Gui {})) .unwrap(); } fn zoom_camera_on_mouse_events(mut events: EventReader, mut camera: ResMut) { for event in events.read() { let raw_delta = match event { MouseWheelEvent::Line { y, .. } => *y, MouseWheelEvent::Pixel { y, ..} => *y, } as f32; let delta = if raw_delta < 0.0 { raw_delta * -0.9 } else { raw_delta * 1.1 }; if delta < 0.0 { camera.zoom *= 1.0 / delta; } else { camera.zoom *= delta; } } } pub struct Gui {} impl UiRenderable for Gui { fn render(&mut self, ctx: &Context, _world: &mut World) { egui::Window::new("Main Menu") .resizable(false) .show(ctx, |ui| { ui.heading("StarKingdoms.TK"); ui.label("A game about floating through space"); }); } }