In an RTS game, I have multiple types of storages systems that need to store "metal" in different ways in my game. Two examples are a metal quarry and a conveyor belt.
"Drones" need to be able to take metal and give metal too storage systems. I want the drones to be able to take from and put on metal at any point on the conveyor belt.
I made a Storage trait, Drones should use the trait to interact with a storage device.
A metal quarry stores metal, and allows drones to take from it, but not give as shown by this code here.
pub trait Storage{
fn try_give_amount(&mut self, amount: u16)->u16;
fn try_take_amount(&mut self, amount: u16)->u16;
}
#[derive(Component)]
pub struct Quarry{
current: u16 //amount of metal the quarry is currently holding
}
impl Storage for Quarry{
fn try_give_amount(&mut self, _amount: u16)->u16 {
0
}
fn try_take_amount(&mut self, amount: u16)->u16 {
let amount_taken = amount.clamp(0, self.current); //ensures a drone cant take more metal than there is
self.current -= amount_taken;
amount_taken
}
}
A conveyor belt has a single parent "belt" and then many "beltchild" children along the length of the belt. I want drones to be able to interact with each "beltchild" so I was going to implement storage for beltchild
#[derive(Component)]
pub struct Belt{
pub queue: VecDeque<bool>, //each bool is weather or not that "slot" on the belt is holding metal
}
#[derive(Component)]
pub struct BeltChild; //each belt child is an entity to represent a "slot" on the belt.
impl Storage for BeltChild{
fn try_give_amount(&mut self, amount: u16)->u16 {}
fn try_take_amount(&mut self, amount: u16)->u16 {}
}
The problem is that the functions dont have acess to the queue of bools that represent the metal on the belt.
Originally I wanted to try something like this
#[derive(Component)]
pub struct BeltChild{
index_along_belt: u16,
parent_belt: Entity
}