pub fn try_move_weapon_w_player(
    delta_x: i32,
    delta_y: i32,
    ecs: &mut World,
    player_x: i32,
    player_y: i32,
) -> bool {
    let player_entity = ecs.fetch::<Entity>();

    let mut positions = ecs.write_storage::<Position>();
    let weapon_stats = ecs.read_storage::<WeaponStats>();
    let combat_stats = ecs.read_storage::<CombatStats>();
    let entities = ecs.entities();
    let map = ecs.fetch::<Map>();
    let mut wants_to_melee = ecs.write_storage::<WantsToMelee>();

    for (entity, _weaponstat, pos) in (&entities, &weapon_stats, &mut positions)
        .join()
        .filter(|item| item.1.owner == *player_entity)
    {
        if pos.x + delta_x < 0
            || pos.x + delta_x > map.width - 1
            || pos.y + delta_y < 0
            || pos.y + delta_y > map.height - 1
        {
            if player_x == pos.x && player_y == pos.y {
                return true;
            }
            return false;
        }

        let destination_idx = map.xy_idx(pos.x + delta_x, pos.y + delta_y);

        for potential_target in map.tile_content[destination_idx].iter() {
            if *potential_target == *player_entity {
                if player_x == pos.x + delta_x && player_y == pos.y + delta_y {
                    return false;
                } else {
                    continue;
                }
            }

            let target = combat_stats.get(*potential_target);
            if let Some(_target) = target {
                wants_to_melee
                    .insert(entity, WantsToMelee { target: *potential_target })
                    .expect("Add target failed");
                return player_x == pos.x && player_y == pos.y;
            }
        }

        if !map.blocked[destination_idx] {
            pos.x = min(79, max(0, pos.x + delta_x));
            pos.y = min(49, max(0, pos.y + delta_y));

            return false;
        }

        return player_x == pos.x && player_y == pos.y;
    }

    return false;
}
/// TODO: use A* pathing to follow player if we can't move the weapon or we get too
/// far away and the player moved
use A* pathing to follow player if we can't move the weapon or we get too
far away and the player moved
use A * pathing to follow player if we can ' t move the weapon or we get too far away and the player moved
pub fn try_move_weapon_w_player(
    delta_x: i32,
    delta_y: i32,
    ecs: &mut World,
    player_x: i32,
    player_y: i32,
) -> bool {
    let player_entity = ecs.fetch::<Entity>();
    let mut positions = ecs.write_storage::<Position>();
    let weapon_stats = ecs.read_storage::<WeaponStats>();
    let combat_stats = ecs.read_storage::<CombatStats>();
    let entities = ecs.entities();
    let map = ecs.fetch::<Map>();
    let mut wants_to_melee = ecs.write_storage::<WantsToMelee>();
    for (entity, _weaponstat, pos) in (&entities, &weapon_stats, &mut positions)
        .join()
        .filter(|item| item.1.owner == *player_entity)
    {
        if pos.x + delta_x < 0
            || pos.x + delta_x > map.width - 1
            || pos.y + delta_y < 0
            || pos.y + delta_y > map.height - 1
        {
            if player_x == pos.x && player_y == pos.y {
                return true;
            }
            return false;
        }
        let destination_idx = map.xy_idx(pos.x + delta_x, pos.y + delta_y);
        for potential_target in map.tile_content[destination_idx].iter() {
            if *potential_target == *player_entity {
                if player_x == pos.x + delta_x && player_y == pos.y + delta_y {
                    return false;
                } else {
                    continue;
                }
            }
            let target = combat_stats.get(*potential_target);
            if let Some(_target) = target {
                wants_to_melee
                    .insert(entity, WantsToMelee { target: *potential_target })
                    .expect("Add target failed");
                return player_x == pos.x && player_y == pos.y;
            }
        }
        if !map.blocked[destination_idx] {
            pos.x = min(79, max(0, pos.x + delta_x));
            pos.y = min(49, max(0, pos.y + delta_y));
            return false;
        }
        return player_x == pos.x && player_y == pos.y;
    }
    return false;
}
pub fn try_move_weapon_w_player ( delta_x : i32 , delta_y : i32 , ecs : & mut World , player_x : i32 , player_y : i32 , ) -> bool { let player_entity = ecs . fetch :: < Entity > ( ) ; let mut positions = ecs . write_storage :: < Position > ( ) ; let weapon_stats = ecs . read_storage :: < WeaponStats > ( ) ; let combat_stats = ecs . read_storage :: < CombatStats > ( ) ; let entities = ecs . entities ( ) ; let map = ecs . fetch :: < Map > ( ) ; let mut wants_to_melee = ecs . write_storage :: < WantsToMelee > ( ) ; for ( entity , _weaponstat , pos ) in ( & entities , & weapon_stats , & mut positions ) . join ( ) . filter ( | item | item . 1 . owner == * player_entity ) { if pos . x + delta_x < 0 || pos . x + delta_x > map . width - 1 || pos . y + delta_y < 0 || pos . y + delta_y > map . height - 1 { if player_x == pos . x && player_y == pos . y { return true ; } return false ; } let destination_idx = map . xy_idx ( pos . x + delta_x , pos . y + delta_y ) ; for potential_target in map . tile_content [ destination_idx ] . iter ( ) { if * potential_target == * player_entity { if player_x == pos . x + delta_x && player_y == pos . y + delta_y { return false ; } else { continue ; } } let target = combat_stats . get ( * potential_target ) ; if let Some ( _target ) = target { wants_to_melee . insert ( entity , WantsToMelee { target : * potential_target } ) . expect ( " " ) ; return player_x == pos . x && player_y == pos . y ; } } if ! map . blocked [ destination_idx ] { pos . x = min ( 79 , max ( 0 , pos . x + delta_x ) ) ; pos . y = min ( 49 , max ( 0 , pos . y + delta_y ) ) ; return false ; } return player_x == pos . x && player_y == pos . y ; } return false ; }
TODO: use A* pathing to follow player if we can't move the weapon or we get too
far away and the player moved
TODO : use A * pathing to follow player if we can ' t move the weapon or we get too far away and the player moved
delta_x
i32
delta_y
i32
ecs
&mut World
player_x
i32
player_y
i32