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 ecs::Shear; use egui::{Context, DragValue}; use rendering::assets::Assets; 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, shear_x: 0.0, shear_y: 0.0, zoom: 1.0, }); world.insert_resource(Assets::new()); 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 }, shear: Shear { x: 0.0, y: 0.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"); ui.separator(); let mut sprites = world.query::<(&mut Translation, &mut Shear, &mut Scale, &SpriteTexture, &mut Rotation)>(); for (mut pos, mut shear, mut scale, tex, mut rot) in sprites.iter_mut(world) { ui.heading(&tex.texture); egui::Grid::new("sprite_grid") .num_columns(2) .spacing([40.0, 4.0]) .striped(true) .show(ui, |ui| { ui.label("X"); ui.add(DragValue::new(&mut pos.x).speed(0.1)); ui.end_row(); ui.label("Y"); ui.add(DragValue::new(&mut pos.y).speed(0.1)); ui.end_row(); ui.label("Shear X"); ui.add(DragValue::new(&mut shear.x).speed(0.1)); ui.end_row(); ui.label("Shear Y"); ui.add(DragValue::new(&mut shear.y).speed(0.1)); ui.end_row(); ui.label("Width"); ui.add(DragValue::new(&mut scale.width).speed(0.1)); ui.end_row(); ui.label("Height"); ui.add(DragValue::new(&mut scale.height).speed(0.1)); ui.end_row(); ui.label("Rotation"); ui.add(DragValue::new(&mut rot.radians).speed(0.1)); ui.end_row(); }); } let mut camera = world.resource_mut::(); egui::Grid::new("camera_grid") .num_columns(2) .spacing([40.0, 4.0]) .show(ui, |ui| { ui.label("Camera X"); ui.add(DragValue::new(&mut camera.x).speed(0.1)); ui.end_row(); ui.label("Camera Y"); ui.add(DragValue::new(&mut camera.y).speed(0.1)); ui.end_row(); ui.label("Shear X"); ui.add(DragValue::new(&mut camera.shear_x).speed(0.1)); ui.end_row(); ui.label("Shear Y"); ui.add(DragValue::new(&mut camera.shear_y).speed(0.1)); ui.end_row(); ui.label("Camera Zoom"); ui.add(DragValue::new(&mut camera.zoom).speed(0.1)); ui.end_row(); }); }); } }