Skip to content

Instantly share code, notes, and snippets.

@fivemoreminix
Created February 28, 2022 17:00
Show Gist options
  • Save fivemoreminix/c283b7593a059c3268c26946faa0faa2 to your computer and use it in GitHub Desktop.
Save fivemoreminix/c283b7593a059c3268c26946faa0faa2 to your computer and use it in GitHub Desktop.
Hare
use std::rc::Rc;
use std::cell::RefCell;
pub struct Character {
pub display_name: String,
// TODO: ability to store arbitrary properties
}
impl Character {
pub fn new(name: impl Into<String>) -> Character {
Character { display_name: name.into() }
}
}
pub struct Message {
pub author: Rc<RefCell<Character>>,
pub content: String,
}
impl Message {
pub fn new(author: Rc<RefCell<Character>>, content: String) -> Message {
Message { author, content }
}
}
/// Container of scripted messages or actions performed by world
/// based on conditions.
pub struct Sequence {
pub messages: Vec<Message>,
}
impl Sequence {
pub fn add_message(mut self, msg: Message) -> Self {
self.messages.push(msg);
self
}
}
// All event conditions are tested every input.
pub struct Event<'game> {
pub condition: fn(state: &'game GameState) -> bool,
pub sequence: Sequence,
}
pub struct GameState<'game> {
pub characters: Vec<Rc<RefCell<Character>>>,
pub events: Vec<Event<'game>>,
}
impl<'game> GameState<'game> {
pub fn get_passing_events<I: Iterator<Item=Event<'game>>>(&self) {
self.events.iter().filter(|ev: &| (ev.condition)(self));
}
}
use std::rc::Rc;
use std::cell::RefCell;
mod game;
use game::*;
fn main() {
let mut game = initialize_game();
// Standard input loop
loop {
}
}
fn initialize_game<'state>() -> GameState<'state> {
let mut characters = Vec::new();
let bob = Rc::new(RefCell::new(Character::new("Bob")));
let alice = Rc::new(RefCell::new(Character::new("Alice")));
characters.push(bob.clone());
characters.push(alice.clone());
let mut events = Vec::new();
events.push(Event {
condition: |state| {
return true;
},
sequence: Sequence {
messages: Vec::new(),
}.add_message(Message::new(bob.clone(), String::from("yo")))
});
GameState{characters, events}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment