|
- use shrev::{EventChannel, ReaderId};
- use space_crush_common::{
- components::{MeetingPoint, Position},
- misc::WorldHelper,
- systems::FleetControlEvent,
- };
- use specs::{prelude::*, Entities, Entity, ReadStorage, System, World};
-
- use crate::{
- misc::MouseEvent,
- resources::{Camera, Config, GameState, InputState, Selection},
- Error,
- };
-
- pub struct FleetMove {
- mouse_event_id: ReaderId<MouseEvent>,
- target_meeting_point: Option<Entity>,
- }
-
- #[derive(SystemData)]
- pub struct FleetMoveData<'a> {
- game_state: WriteExpect<'a, GameState>,
- fleet_control: WriteExpect<'a, EventChannel<FleetControlEvent>>,
- mouse_events: ReadExpect<'a, EventChannel<MouseEvent>>,
- input_state: ReadExpect<'a, InputState>,
- camera: ReadExpect<'a, Camera>,
- config: ReadExpect<'a, Config>,
-
- entities: Entities<'a>,
- positions: ReadStorage<'a, Position>,
- meeting_points: ReadStorage<'a, MeetingPoint>,
- }
-
- impl FleetMove {
- pub fn new(world: &mut World) -> Result<Self, Error> {
- let mouse_event_id = world.register_event_reader::<MouseEvent>()?;
- let target_meeting_point = None;
-
- Ok(Self {
- mouse_event_id,
- target_meeting_point,
- })
- }
- }
-
- impl<'a> System<'a> for FleetMove {
- type SystemData = FleetMoveData<'a>;
-
- fn run(&mut self, data: Self::SystemData) {
- let FleetMoveData {
- mut game_state,
- mut fleet_control,
- mouse_events,
- input_state,
- camera,
- config,
- entities,
- positions,
- meeting_points,
- } = data;
-
- let events = mouse_events.read(&mut self.mouse_event_id);
- for event in events {
- match event {
- MouseEvent::ButtonDown(button) if button == &config.input.fleet_move_button => {
- let pos = camera.view_to_world(input_state.mouse_pos);
- for (id, position, meeting_point) in
- (&entities, &positions, &meeting_points).join()
- {
- let r = meeting_point.max() * meeting_point.max();
- if (position.get() - pos).length_sqr() <= r {
- self.target_meeting_point = Some(id);
-
- break;
- }
- }
- }
- MouseEvent::ButtonUp(button) if button == &config.input.fleet_move_button => {
- let Selection { fleet, count } = match game_state.selection_mut().take() {
- Some(selection) => selection,
- None => continue,
- };
- let target = match self.target_meeting_point {
- Some(target) => target,
- None => continue,
- };
- let event = FleetControlEvent::MoveToMeetingPoint {
- fleet,
- count,
- target,
- };
-
- fleet_control.single_write(event);
- }
- MouseEvent::Move(_, _) => {
- self.target_meeting_point = None;
- }
- _ => (),
- }
- }
- }
- }
|