@@ 1,5 1,5 @@
use bevy_ecs::{bundle::Bundle, component::Component, event::Event, system::Resource};
-use nalgebra::{Matrix4, Rotation2, Scale3, Translation3};
+use nalgebra::{Matrix3, Matrix4, Rotation2, Scale2, Scale3, Translation2, Translation3};
use starkingdoms_common::packet::Packet;
#[derive(Component, Debug)]
@@ 33,6 33,25 @@ pub struct Camera {
pub width: u32, // screen width (these are for aspect ratio)
pub height: u32, // screen height
}
+impl Camera {
+ pub fn to_matrix(&self) -> Matrix4<f32> {
+ let x_scale = self.zoom / self.width as f32 * 2.0;
+ let y_scale = self.zoom / self.height as f32 * 2.0;
+ Matrix4::from_vec(vec![
+ x_scale, 0.0, 0.0, 0.0,
+ 0.0, y_scale, 0.0, 0.0,
+ 0.0, 0.0, 1.0, 0.0,
+ self.x*x_scale, self.y*y_scale, 0.0, 1.0,
+ ])
+ }
+ pub fn to_cursor_matrix(&self) -> Matrix3<f32> {
+ let x = -(self.width as f32 / 2.0);
+ let y = -(self.height as f32 / 2.0);
+ Translation2::new(-self.x, -self.y).to_homogeneous()
+ * Scale2::new(1.0/self.zoom, 1.0/self.zoom).to_homogeneous()
+ * Translation2::new(x, y).to_homogeneous()
+ }
+}
#[derive(Component, Debug, Clone, Copy)]
pub struct Player;
@@ 5,6 5,7 @@ use std::sync::Arc;
use assets::Assets;
use bevy_ecs::entity::Entity;
use bevy_ecs::event::Events;
+use bevy_ecs::query::With;
use bevy_ecs::world::World;
use egui::{Label, ProgressBar};
use egui_glow::EguiGlow;
@@ 15,20 16,22 @@ use glutin::surface::{Surface, WindowSurface, GlSurface, SwapInterval};
use glutin::{config::{ConfigTemplateBuilder, GlConfig}, context::{ContextApi, ContextAttributesBuilder, PossiblyCurrentContext}, display::GetGlDisplay, prelude::{GlDisplay, NotCurrentGlContext}};
#[cfg(not(target_arch = "wasm32"))]
use glutin_winit::{DisplayBuilder, GlWindow};
-use starkingdoms_common::packet::Packet;
+use nalgebra::{Vector3, Vector4};
+use starkingdoms_common::packet::{ButtonType, Packet};
use starkingdoms_common::PlanetType;
#[cfg(target_arch = "wasm32")]
use wasm_bindgen::{prelude::Closure, JsCast};
#[cfg(target_arch = "wasm32")]
use web_sys::{Event, HtmlCanvasElement};
-use winit::event::{ElementState, MouseScrollDelta};
+use winit::dpi::PhysicalPosition;
+use winit::event::{ElementState, MouseButton, MouseScrollDelta};
use winit::event_loop::ControlFlow;
use winit::keyboard::{KeyCode, PhysicalKey};
#[cfg(target_arch = "wasm32")]
use winit::platform::web::{WindowAttributesExtWebSys, WindowExtWebSys};
use winit::{application::ApplicationHandler, dpi::LogicalSize, event::WindowEvent, event_loop::ActiveEventLoop, raw_window_handle::HasWindowHandle, window::{Window, WindowAttributes}};
-use crate::components::{Camera, RecvPacket, SendPacket, Texture, Transform};
+use crate::components::{Camera, Player, RecvPacket, SendPacket, Texture, Transform};
use crate::networking::process_packets;
use crate::networking::ws::Ws;
#[cfg(not(target_arch="wasm32"))]
@@ 66,6 69,7 @@ pub struct App {
amount: u32,
max: u32,
+ mouse_pos: PhysicalPosition<f64>,
}
const VERTICES: [f32; 16] = [
@@ 326,6 330,27 @@ impl ApplicationHandler for App {
PhysicalKey::Unidentified(_) => {} // unsupported
}
}
+ WindowEvent::CursorMoved { position, .. } => self.mouse_pos = position,
+ WindowEvent::MouseInput { state, button, .. } => {
+ let button = match button {
+ MouseButton::Left => ButtonType::Left,
+ MouseButton::Middle => ButtonType::Middle,
+ MouseButton::Right => ButtonType::Right,
+ _ => return,
+ };
+ let camera = self.world.get_resource::<Camera>().unwrap();
+ tracing::info!("{}, {}", self.mouse_pos.x, self.mouse_pos.y);
+ let view = camera.to_cursor_matrix();
+ let pos = view * Vector3::new(self.mouse_pos.x as f32, self.mouse_pos.y as f32, 1.0);
+ let pos = pos / pos.z;
+ tracing::info!("{}, {}", pos.x, pos.y);
+ self.send_packet_events.send(SendPacket(Packet::PlayerMouseInput {
+ x: pos.x,
+ y: pos.y,
+ released: !state.is_pressed(),
+ button,
+ }));
+ }
_ => {}
}
let event_response = self.egui_glow.as_mut().unwrap()
@@ 351,6 376,8 @@ impl ApplicationHandler for App {
let window = self.window.as_ref().unwrap();
let gl = self.gl.as_ref().unwrap();
+ let mut player = self.world.query_filtered::<&Transform, With<Player>>();
+ let player = player.single(&self.world);
self.egui_glow.as_mut().unwrap().run(
self.window.as_ref().unwrap(),
|ctx| {
@@ 359,6 386,7 @@ impl ApplicationHandler for App {
ui.add(Label::new("Fuel:"));
ui.add(ProgressBar::new((self.amount as f32)/(self.max as f32)).corner_radius(0));
+ ui.add(Label::new(format!("Pos: {}, {}", player.translation.x as u32, player.translation.y as u32)));
});
},
);