/* * This file is part of laurelin_client * Copyright (C) 2023 Jonni Liljamo * * Licensed under GPL-3.0-only. * See LICENSE for licensing information. */ #[macro_export] macro_rules! async_task_start_call { ($call_event_type:ty, $call_type:expr, |$ev:ident| $func:expr) => { pub fn start_call( mut commands: Commands, mut start_ev_r: EventReader<$call_event_type>, ) { for ev in start_ev_r.iter() { let thread_pool = AsyncComputeTaskPool::get(); let $ev = ev.clone(); let task = thread_pool.spawn(async move { $func }); commands.spawn($call_type(task)); } } }; ($call_event_type:ty, $call_type:expr, |$ev:ident, $no:ident| $func:expr) => { pub fn start_call( mut commands: Commands, mut start_ev_r: EventReader<$call_event_type>, no: Res ) { for ev in start_ev_r.iter() { let thread_pool = AsyncComputeTaskPool::get(); let $ev = ev.clone(); let $no = no.clone(); let task = thread_pool.spawn(async move { $func }); commands.spawn($call_type(task)); } } }; } #[macro_export] macro_rules! async_task_handle_call { ($call_type:ty, |$response:ident| $handler_func:expr) => { pub fn handle_call( mut commands: Commands, mut tasks: Query<(Entity, &mut $call_type)>, ) { match tasks.get_single_mut() { Ok((entity, mut task)) => { if let Some($response) = future::block_on(future::poll_once(&mut task.0)) { $handler_func; // remove the task commands.entity(entity).remove::<$call_type>(); commands.entity(entity).despawn_recursive(); } } // NOTE: don't do anything if the wanted thingy doesn't exist _ => {} } } }; ($call_type:ty, |$response:ident, $menu_data:ident| $handler_func:expr) => { pub fn handle_call( mut commands: Commands, mut tasks: Query<(Entity, &mut $call_type)>, mut $menu_data: ResMut, ) { match tasks.get_single_mut() { Ok((entity, mut task)) => { if let Some($response) = future::block_on(future::poll_once(&mut task.0)) { $handler_func; // remove the task commands.entity(entity).remove::<$call_type>(); commands.entity(entity).despawn_recursive(); } } // NOTE: don't do anything if the wanted thingy doesn't exist _ => {} } } }; ($call_type:ty, |$response:ident, $global:ident, $menu_data:ident, $no:ident| $handler_func:expr) => { pub fn handle_call( mut commands: Commands, mut tasks: Query<(Entity, &mut $call_type)>, mut $global: ResMut, mut $menu_data: ResMut, mut $no: ResMut, ) { match tasks.get_single_mut() { Ok((entity, mut task)) => { if let Some($response) = future::block_on(future::poll_once(&mut task.0)) { $handler_func; // remove the task commands.entity(entity).remove::<$call_type>(); commands.entity(entity).despawn_recursive(); } } // NOTE: don't do anything if the wanted thingy doesn't exist _ => {} } } }; ($call_type:ty, |$response:ident, $global:ident, $game_data:ident| $handler_func:expr) => { pub fn handle_call( mut commands: Commands, mut tasks: Query<(Entity, &mut $call_type)>, mut $game_data: ResMut, ) { match tasks.get_single_mut() { Ok((entity, mut task)) => { if let Some($response) = future::block_on(future::poll_once(&mut task.0)) { $handler_func; // remove the task commands.entity(entity).remove::<$call_type>(); commands.entity(entity).despawn_recursive(); } } // NOTE: don't do anything if the wanted thingy doesn't exist _ => {} } } }; ($call_type:ty, |$unused:ident, $ihatethis:ident, $response:ident, $game_data:ident, $parse_ev_w:ident| $handler_func:expr) => { pub fn handle_call( mut commands: Commands, mut tasks: Query<(Entity, &mut $call_type)>, mut $game_data: ResMut, mut $parse_ev_w: EventWriter, ) { match tasks.get_single_mut() { Ok((entity, mut task)) => { if let Some($response) = future::block_on(future::poll_once(&mut task.0)) { $handler_func; // remove the task commands.entity(entity).remove::<$call_type>(); commands.entity(entity).despawn_recursive(); } } // NOTE: don't do anything if the wanted thingy doesn't exist _ => {} } } }; }