I'm working on a card game using Amethyst's Specs crate and am fairly new to Rust (therefore I'm learning through the fight with the compiler). My first System
is to deal the cards to each player. I have:
- a
Card
type that hasCopy
semantics - a
Cards
component that represents a set ofCard
the players or deck are holding
struct Deal {
deck: Entity,
players: Vec<Entity>,
}
impl<'a> System<'a> for Deal {
type SystemData = WriteStorage<'a, Cards>;
fn run(&mut self, mut cards: Self::SystemData) {
// code smell - can't use `get_mut`
let mut deck = cards.get(self.deck).unwrap().clone();
deck.shuffle();
for player in self.players.iter().cycle() {
let player_cards = cards.get_mut(*player).unwrap();
if let Some(card) = deck.take_top() {
player_cards.add(card);
} else {
break;
}
}
// code smell
let mut deck = cards.get_mut(self.deck).unwrap();
deck.clear();
}
}
The compiler complains that I can't take a mutable reference to the deck
's cards because the player_cards
is a mutable one (or vice versa).
As noted in the code above, I clone the Cards
for the deck
, shuffle that, deal to the mutable players' and then empty the deck.
I would have expected that I could get a mutable reference to the deck up front, shuffle that and remove it.
Is there something simple I am missing?
Aside, I do not know if it is convention to pass entities on the system struct it self or not. Is that wrong?