component factories + formatting

feature/battle
brnrs 5 years ago
parent 1a52695945
commit 28d5069b02
  1. 11
      src/battle/mod.rs
  2. 4
      src/contexts/component.rs
  3. 97
      src/contexts/component_factory.rs
  4. 8
      src/contexts/component_map.rs
  5. 4
      src/contexts/mod.rs
  6. 1
      src/main.rs

@ -161,16 +161,17 @@ fn check_result(context: &Context) -> Option<BattleResult> {
let team_db = context.get::<TeamDb>().unwrap();
let actor_state_db = context.get::<ActorStateDb>().unwrap();
let alive: Vec<TeamId> = team_db
.iter()
.filter_map(|(team_id, team)| {
if team.actors.iter().any(|actor_id| {
let is_alive = |actor_id: &u32| {
if let ActorState::Dead = actor_state_db.get(*actor_id).unwrap() {
false
} else {
true
}
}) {
};
let alive: Vec<TeamId> = team_db
.iter()
.filter_map(|(team_id, team)| {
if team.actors.iter().any(is_alive) {
Some(*team_id)
} else {
None

@ -1,6 +1,6 @@
use std::any::{Any, TypeId, type_name};
use std::convert::TryFrom;
use std::any::{type_name, Any, TypeId};
use std::cell::RefCell;
use std::convert::TryFrom;
use std::ops::Deref;
use std::rc::Rc;

@ -1,7 +1,7 @@
use super::component::Component;
use super::model_i::{Context, FromContext};
use std::any::{Any, type_name};
use std::any::{type_name, Any};
trait ComponentFactory {
type Input;
@ -14,11 +14,102 @@ trait ContextComponentFactory: ComponentFactory {
fn build_from_context(&self, context: &Context) -> Result<Self::Output, String>;
}
impl <T:Any + Sized + 'static, O, CF: ComponentFactory<Input=Component<T>, Output=O>> ContextComponentFactory for CF {
impl<T: Any + Sized + 'static, O, CF: ComponentFactory<Input = Component<T>, Output = O>>
ContextComponentFactory for CF
{
fn build_from_context(&self, context: &Context) -> Result<Self::Output, String> {
let input = FromContext::from(context).ok_or_else(|| format!("Could not find retrieve component of type {} from context", type_name::<T>()))?;
let input = FromContext::from(context).ok_or_else(|| {
format!(
"Could not find retrieve component of type {} from context",
type_name::<T>()
)
})?;
self.build(input)
}
}
#[macro_export]
macro_rules! derive_ccf {
($CF:ty; $( $T:ty ),* ) => {
impl ContextComponentFactory for $CF {
fn build_from_context(&self, context: &Context) -> Result<Self::Output, String> {
let input = from_context!(context; $($T),*);
self.build(input)
}
}
}
}
#[macro_export]
macro_rules! from_context {
($context:expr; $( $T:ty ),* ) => {
(
$(
FromContext::from($context).ok_or_else(|| {
format!(
"Could not find retrieve component of type {} from context",
type_name::<$T>()
)
})?,
)*
)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[derive(Debug, Eq, PartialEq)]
struct A(&'static str);
#[derive(Debug, Eq, PartialEq)]
struct B(u32);
#[test]
fn test_from_context_macro() {
// Given
fn get_input(context: &Context) -> Result<(Component<A>, Component<B>), String> {
Ok(from_context!(context; A, B))
}
let mut context = Context::new();
context.set(A("Test"));
context.set(B(67));
// When
let (comp_a, comp_b) = get_input(&context).unwrap();
// Then
assert_eq!(*comp_a.borrow(), A("Test"));
assert_eq!(*comp_b.borrow(), B(67));
}
#[derive(Debug, Eq, PartialEq)]
struct C(&'static str, u32);
struct CFactory;
impl ComponentFactory for CFactory {
type Input = (Component<A>, Component<B>);
type Output = C;
fn build(&self, (comp_a, comp_b): Self::Input) -> Result<Self::Output, String> {
Ok(C(comp_a.borrow().0, comp_b.borrow().0))
}
}
#[test]
fn test_derive_ccf_macro() {
// Given
derive_ccf!(CFactory; A, B);
let c_factory = CFactory;
let mut context = Context::new();
context.set(A("test"));
context.set(B(67));
// When
let c = c_factory.build_from_context(&context).unwrap();
assert_eq!(c, C("test", 67));
}
}

@ -19,10 +19,14 @@ impl ComponentMap {
}
pub fn get<T: Any + Sized + 'static>(&self) -> Option<Component<T>> {
self.0.get(&TypeId::of::<T>()).cloned().map(|c| c.try_into().unwrap())
self.0
.get(&TypeId::of::<T>())
.cloned()
.map(|c| c.try_into().unwrap())
}
pub fn set<T: Any + Sized + 'static>(&mut self, component: T) {
self.0.insert(TypeId::of::<T>(), Rc::new(RefCell::new(component)));
self.0
.insert(TypeId::of::<T>(), Rc::new(RefCell::new(component)));
}
}

@ -1,4 +1,4 @@
mod model_i;
mod component;
mod component_map;
mod component_factory;
mod component_map;
mod model_i;

@ -9,7 +9,6 @@ use battle::skill::*;
use battle::uic::Uic;
use context::Context;
fn main() {
println!("Hello, world!");

Loading…
Cancel
Save