Files
systemic-game-design-course/course/01-handles/reference/rust/src/engine/card.rs
T
2026-10-29 00:17:31 +01:00

37 lines
930 B
Rust

//! Card definitions as the engine sees them. The definitions themselves live in `content`.
use std::collections::HashMap;
/// What the engine needs to know about a card. Module 1: only its name.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CardDef {
pub name: String,
}
impl CardDef {
pub fn new(name: &str) -> CardDef {
CardDef {
name: name.to_string(),
}
}
}
/// Every card the game can create, looked up by name.
#[derive(Debug, Default)]
pub struct CardLibrary {
cards: HashMap<String, CardDef>,
}
impl CardLibrary {
pub fn new(defs: Vec<CardDef>) -> CardLibrary {
let cards = defs.into_iter().map(|d| (d.name.clone(), d)).collect();
CardLibrary { cards }
}
pub fn get(&self, name: &str) -> &CardDef {
self.cards
.get(name)
.unwrap_or_else(|| panic!("unknown card {name:?}: define it in src/content"))
}
}