chiark / gitweb /
hand: wip piece implementation
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Fri, 12 Feb 2021 20:06:03 +0000 (20:06 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 13 Feb 2021 12:50:21 +0000 (12:50 +0000)
Doesn't do very much yet

Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
specs/demo.game.toml
src/pieces.rs
src/spec.rs

index c8b1028ec3cb54b633f4d19154127f24f0d8faa4..c3d6449a4e77ab86170dc779d197db5db0dc9cd5 100644 (file)
@@ -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"]
index 7d14655799ac069ea8365e2e12ce2938b67d6d1f..21fed5275a076df45459bd9eb79c3905eb596222 100644 (file)
@@ -273,3 +273,52 @@ impl PieceSpec for piece_specs::Square {
   #[throws(SpecError)]
   fn load(&self, _: usize) -> Box<dyn Piece> { 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<Html,IE>;
+      fn thresh_dragraise(&self, _pri: &PieceRenderInstructions)
+                          -> Result<Option<Coord>,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<FaceId>) -> 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<dyn Piece> {
+    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<dyn Piece>
+  }
+}
index 879cca400f4286ddc69538f53b59f09d2ea18e3e..aee67249cb9577b3efee5cfbe5b29a19a5f5764d 100644 (file)
@@ -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<dyn PieceSpec>,
-  }*/
+    pub shape: Box<dyn crate::pieces::SimplePieceSpec>,
+  }
 }
 
 //---------- Pos ----------