From: Ian Jackson Date: Fri, 12 Feb 2021 20:06:03 +0000 (+0000) Subject: hand: wip piece implementation X-Git-Tag: otter-0.4.0~517 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=3c546f9ecd64797aee40548eb367175361546f40;p=otter.git hand: wip piece implementation Doesn't do very much yet Signed-off-by: Ian Jackson --- diff --git a/specs/demo.game.toml b/specs/demo.game.toml index c8b1028e..c3d6449a 100644 --- a/specs/demo.game.toml +++ b/specs/demo.game.toml @@ -39,3 +39,11 @@ angle.Compass = 1 faces = [ ] edges = ["yellow", "orange"] edge_width = 0.5 + +[[pieces]] +pos = [40, 40] +type = "Hand" +shape.type = "Square" +shape.size = [70,20] +shape.faces = ["grey"] +shape.edges = ["white"] diff --git a/src/pieces.rs b/src/pieces.rs index 7d146557..21fed527 100644 --- a/src/pieces.rs +++ b/src/pieces.rs @@ -273,3 +273,52 @@ impl PieceSpec for piece_specs::Square { #[throws(SpecError)] fn load(&self, _: usize) -> Box { SimplePieceSpec::load(self)? } } + +#[derive(Debug,Serialize,Deserialize)] +struct Hand { + shape: SimpleShape, +} + +#[typetag::serde] +impl Outline for Hand { + delegate!{ + to self.shape { + fn surround_path(&self, _pri: &PieceRenderInstructions) + -> Result; + fn thresh_dragraise(&self, _pri: &PieceRenderInstructions) + -> Result,IE>; + fn bbox_approx(&self) -> [Pos;2]; + } + } +} + +#[typetag::serde] +impl Piece for Hand { + delegate!{ + to self.shape { + fn svg_piece(&self, f: &mut Html, pri: &PieceRenderInstructions) + -> Result<(),IE>; + fn describe_html(&self, face: Option) -> Html; + fn itemname(&self) -> &str; + } + } + fn nfaces(&self) -> RawFaceId { 1 } +} + +#[typetag::serde] +impl PieceSpec for piece_specs::Hand { + #[throws(SpecError)] + fn load(&self, _: usize) -> Box { + let (mut shape, common) = self.shape.load_raw()?; + if common.itemname.is_some() { + throw!(SpecError::ItemnameSpecifiedWhereForbidden); + } + if shape.nfaces() != 1 { + throw!(SpecError::MultifacetedMagic); + } + shape.itemname = "magic-hand".to_string(); + Box::new(Hand { + shape + }) as Box + } +} diff --git a/src/spec.rs b/src/spec.rs index 879cca40..aee67249 100644 --- a/src/spec.rs +++ b/src/spec.rs @@ -80,6 +80,8 @@ pub enum SpecError { ZeroFaces, InconsistentFacesEdgecoloursCount, SpecifiedWidthOfNoEdges, + ItemnameSpecifiedWhereForbidden, + MultifacetedMagic, } display_as_debug!{SpecError} @@ -218,10 +220,11 @@ pub mod piece_specs { #[serde(flatten)] pub common: SimpleCommon, } -/* + + #[derive(Debug,Serialize,Deserialize)] pub struct Hand { - pub shape: Box, - }*/ + pub shape: Box, + } } //---------- Pos ----------