~starkingdoms/starkingdoms

ref: 87db7538d05166cb8cdb701886b38b32de9bc708 starkingdoms/crates/server/src/crafting/mining.rs -rw-r--r-- 3.1 KiB
87db7538 — core cargo fmt 8 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
use crate::{module::component::Attach, planet::PlanetType};
use bevy::prelude::{Children, Entity, Query, Res};
use bevy_rapier2d::plugin::RapierContext;

use super::components::{IsMining, VarietyMaterialStorage};
pub fn mine_materials(
    rapier_context: Res<RapierContext>,
    planet_query: Query<(&PlanetType, &Children)>,
    mut mineable_query: Query<(
        Entity,
        &mut Attach,
        Option<&IsMining>,
        Option<&mut VarietyMaterialStorage>,
    )>,
) {
    for (planet_type, children) in &planet_query {
        for (entity1, entity2, intersecting) in
            rapier_context.intersection_pairs_with(*children.first().unwrap())
        {
            if !intersecting {
                continue;
            }
            let other = if *children.first().unwrap() == entity1 {
                entity2
            } else {
                entity1
            };
            let (entity, attach, mineable, _) = match mineable_query.get(other) {
                Ok(m) => m,
                Err(_) => continue,
            };
            let associated_player = match attach.associated_player {
                Some(e) => e,
                None => entity,
            };
            // is the module mining
            if let Some(mineable) = mineable {
                if mineable.0 {
                    if let Some(storage_entity) = find_storage(associated_player, &mineable_query) {
                        let (_, _, _, storage) = mineable_query.get_mut(storage_entity).unwrap();
                        if let Some(mut storage) = storage {
                            if let Ok(material) = planet_type.0.try_into() {
                                match storage.materials.get_mut(&material) {
                                    Some(v) => *v += 1,
                                    None => {
                                        storage.materials.insert(material, 1);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
pub fn find_storage(
    player: Entity,
    mineable_query: &Query<(
        Entity,
        &mut Attach,
        Option<&IsMining>,
        Option<&mut VarietyMaterialStorage>,
    )>,
) -> Option<Entity> {
    for (entity, attach, _, storage) in mineable_query.iter() {
        if let Some(storage) = storage {
            if attach.associated_player == Some(player) {
                // found a valid storage
                if storage.materials.values().sum::<u32>() > storage.capacity {
                    // cannot store more materials in a filled storage
                    continue;
                }
                return Some(entity);
            } else if attach.associated_player == None {
                // this is a player
                if storage.materials.values().sum::<u32>() > storage.capacity {
                    // cannot store more materials in a filled storage
                    continue;
                }
                return Some(entity);
            }
        }
    }
    return None;
}