Add basic action card instructions

This commit is contained in:
Markus Wagner 2021-01-10 19:58:32 +01:00
parent ea94aa5230
commit 83d5062a84
2 changed files with 64 additions and 18 deletions

View File

@ -1,3 +1,4 @@
pub use super::Game;
use serde::{Serialize, Serializer}; use serde::{Serialize, Serializer};
use std::fmt; use std::fmt;
@ -57,9 +58,33 @@ pub fn curse() -> Card {
} }
} }
macro_rules! draw {
($g:ident, $e:expr) => {
$g.players[$g.active_player].draw($e)
};
}
macro_rules! action {
($g:ident, $e:expr) => {
$g.turn_state.actions += $e
};
}
macro_rules! buy {
($g:ident, $e:expr) => {
$g.turn_state.buys += $e
};
}
macro_rules! coin {
($g:ident, $e:expr) => {
$g.turn_state.coin += $e
};
}
#[derive(Clone)] #[derive(Clone)]
pub enum CardType { pub enum CardType {
Action(fn()), Action(fn(&mut Game)),
Curse, Curse,
Treasure(u32), Treasure(u32),
Victory(u32), Victory(u32),
@ -73,10 +98,10 @@ pub struct Card {
} }
impl Card { impl Card {
pub fn action(&self) -> Option<()> { pub fn action(&self) -> Option<fn(&mut Game)> {
for t in &self.types { for t in &self.types {
match t { match t {
CardType::Action(_) => return Some(()), CardType::Action(effects) => return Some(*effects),
_ => (), _ => (),
} }
} }

View File

@ -1,3 +1,4 @@
#[macro_use]
mod cards; mod cards;
use async_std::{prelude::*, sync::RwLock}; use async_std::{prelude::*, sync::RwLock};
@ -164,7 +165,7 @@ impl Default for GameSetup {
} }
} }
struct Game { pub struct Game {
players: Vec<Player>, players: Vec<Player>,
state: GameState, state: GameState,
setup: GameSetup, setup: GameSetup,
@ -216,7 +217,9 @@ impl Game {
Card { Card {
name: "Cellar".into(), name: "Cellar".into(),
cost: 2, cost: 2,
types: vec![CardType::Action(|| {})], types: vec![CardType::Action(|game| {
action!(game, 1);
})],
}, },
10, 10,
), ),
@ -224,7 +227,7 @@ impl Game {
Card { Card {
name: "Moat".into(), name: "Moat".into(),
cost: 2, cost: 2,
types: vec![CardType::Action(|| {})], types: vec![CardType::Action(|game| draw!(game, 2))],
}, },
10, 10,
), ),
@ -232,7 +235,10 @@ impl Game {
Card { Card {
name: "Village".into(), name: "Village".into(),
cost: 3, cost: 3,
types: vec![CardType::Action(|| {})], types: vec![CardType::Action(|game| {
draw!(game, 1);
action!(game, 2);
})],
}, },
10, 10,
), ),
@ -240,7 +246,10 @@ impl Game {
Card { Card {
name: "Merchant".into(), name: "Merchant".into(),
cost: 3, cost: 3,
types: vec![CardType::Action(|| {})], types: vec![CardType::Action(|game| {
draw!(game, 1);
action!(game, 1);
})],
}, },
10, 10,
), ),
@ -248,7 +257,7 @@ impl Game {
Card { Card {
name: "Workshop".into(), name: "Workshop".into(),
cost: 3, cost: 3,
types: vec![CardType::Action(|| {})], types: vec![CardType::Action(|game| {})],
}, },
10, 10,
), ),
@ -256,7 +265,7 @@ impl Game {
Card { Card {
name: "Smithy".into(), name: "Smithy".into(),
cost: 4, cost: 4,
types: vec![CardType::Action(|| {})], types: vec![CardType::Action(|game| draw!(game, 3))],
}, },
10, 10,
), ),
@ -264,7 +273,7 @@ impl Game {
Card { Card {
name: "Remodel".into(), name: "Remodel".into(),
cost: 4, cost: 4,
types: vec![CardType::Action(|| {})], types: vec![CardType::Action(|game| {})],
}, },
10, 10,
), ),
@ -272,7 +281,9 @@ impl Game {
Card { Card {
name: "Militia".into(), name: "Militia".into(),
cost: 4, cost: 4,
types: vec![CardType::Action(|| {})], types: vec![CardType::Action(|game| {
coin!(game, 2);
})],
}, },
10, 10,
), ),
@ -280,7 +291,12 @@ impl Game {
Card { Card {
name: "Market".into(), name: "Market".into(),
cost: 5, cost: 5,
types: vec![CardType::Action(|| {})], types: vec![CardType::Action(|game| {
draw!(game, 1);
action!(game, 1);
buy!(game, 1);
coin!(game, 1);
})],
}, },
10, 10,
), ),
@ -288,7 +304,7 @@ impl Game {
Card { Card {
name: "Mine".into(), name: "Mine".into(),
cost: 5, cost: 5,
types: vec![CardType::Action(|| {})], types: vec![CardType::Action(|game| {})],
}, },
10, 10,
), ),
@ -343,24 +359,29 @@ impl Game {
} }
} }
fn get_active_player(&mut self) -> &Player {
return &self.players[self.active_player];
}
pub fn trash_hand(&mut self, player_number: usize, index: usize) { pub fn trash_hand(&mut self, player_number: usize, index: usize) {
self.trash self.trash
.push(self.players[player_number].hand.remove(index)); .push(self.players[player_number].hand.remove(index));
} }
pub fn play_card(&mut self, player_number: usize, index: usize) { pub fn play_card(&mut self, player_number: usize, index: usize) {
let player = self.players.get_mut(player_number).unwrap(); let card = self.players[player_number].hand.remove(index);
let card = player.hand.remove(index);
if let Some(coin) = card.treasure() { if let Some(coin) = card.treasure() {
self.turn_state.coin += coin; self.turn_state.coin += coin;
} }
if let Some(_) = card.action() { if let Some(effect) = card.action() {
self.turn_state.actions -= 1; self.turn_state.actions -= 1;
effect(self);
} }
player.played_cards.push(card); self.players[player_number].played_cards.push(card);
} }
pub fn buy_card(&mut self, player_number: usize, index: usize) -> bool /*-> Result<(), &'static str>*/ pub fn buy_card(&mut self, player_number: usize, index: usize) -> bool /*-> Result<(), &'static str>*/