Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pub fn handle_state<UI: GameUI, Res: ResourceContext>(
- &mut self,
- state: &State,
- ui: &mut UI,
- res: &Res,
- ) -> StateResult {
- match state {
- State::PushRegisterStack => {
- self.push_register_stack();
- completed![]
- }
- State::PopRegisterStack => {
- self.pop_register_stack();
- completed![]
- }
- State::TakeTurn => completed![
- State::DrawHand,
- State::ActionPhase,
- State::CleanUp,
- State::TakeTurn
- ],
- State::DrawHand => completed![State::DrawPlayerCards(5)],
- State::ActionPhase => {
- if self.actions > 0 {
- if let Some(res) = ui
- .get_card_or_button(&self.get_cards(Place::Hand), &[("Stop playing actions", ())])
- {
- match res {
- CardOrButton::Card(id) => {
- completed![State::ResolveAction(id), State::ActionPhase]
- }
- CardOrButton::Button(_) => completed!(),
- }
- } else {
- blocked!()
- }
- } else {
- if let Some(()) = ui.get_button(&[("Stop playing actions", ())]) {
- completed!()
- } else {
- blocked!()
- }
- }
- }
- State::CleanUp => completed![
- State::MoveAllCards(Place::Played, Place::Discard),
- State::MoveAllCards(Place::Hand, Place::Discard),
- ],
- State::CardAction(action) => match action {
- &CardAction::GainExperience(n) => {
- completed![State::ModifyExperience(self.get_number(n) as i32)]
- }
- &CardAction::GainActions(n) => {
- completed![State::ModifyActions(self.get_number(n) as i32)]
- }
- &CardAction::DrawCards(n) => completed![State::DrawPlayerCards(self.get_number(n))],
- &CardAction::SetVar(n, v) => {
- self.set_register_value(v.into(), self.get_number(n));
- completed![]
- }
- },
- &State::ResolveAction(card) => {
- if let CardType::Player(card_type) = self.get_card_type(res, card) {
- completed![
- State::PushRegisterStack,
- State::MoveCard(card, Place::Active),
- card_type.onplay.iter().map(|&a| State::CardAction(a)),
- State::MoveCard(card, Place::Played),
- State::PopRegisterStack,
- ]
- } else {
- //TODO handle this more nicely
- panic!("Tried to resolve a non-action card as an action");
- }
- }
- &State::MoveCard(card, to) => {
- let from = self.get_card_location(card);
- self.move_card(card, from, to);
- ui.move_card(card, from, to);
- completed![]
- }
- &State::MoveNCards(count, from, to) => {
- let count = u32::min(count, self.get_deck_length(from));
- for _ in 0..count {
- let card = self.get_top(from);
- self.move_card(card, from, to);
- ui.move_card(card, from, to);
- }
- completed![]
- }
- &State::MoveAllCards(from, to) => {
- completed![State::MoveNCards(self.get_deck_length(from), from, to)]
- }
- &State::Shuffle(place) => {
- self.shuffle(place);
- completed![]
- }
- &State::DrawPlayerCards(count) => {
- let deck_count = self.get_place_count(Place::Deck);
- if deck_count >= count {
- completed![State::MoveNCards(count, Place::Deck, Place::Hand)]
- } else {
- let remainder = count - deck_count;
- completed![
- State::MoveNCards(deck_count, Place::Deck, Place::Hand),
- State::MoveAllCards(Place::Discard, Place::Deck),
- State::Shuffle(Place::Deck),
- State::MoveNCards(remainder, Place::Deck, Place::Hand)
- ]
- }
- }
- &State::ModifyActions(n) => {
- self.actions += n;
- ui.set_counter(Counter::Actions, self.actions);
- completed![]
- }
- &State::ModifyExperience(n) => {
- self.experience += n;
- ui.set_counter(Counter::Experience, self.experience);
- completed![]
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement