From: Ian Jackson Date: Sun, 12 Jul 2020 14:03:05 +0000 (+0100) Subject: make pieces by path X-Git-Tag: otter-0.2.0~1361 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=7d1b0aad891394990f1af1dade400ee06da95e51;p=otter.git make pieces by path --- diff --git a/src/gamestate.rs b/src/gamestate.rs index 242a39f8..b614abc2 100644 --- a/src/gamestate.rs +++ b/src/gamestate.rs @@ -174,7 +174,7 @@ pub fn make_pieceid_visible(p : PieceId) -> VisiblePieceId { pub fn xxx_gamestate_init() -> GameState { let mut pieces = DenseSlotMap::with_key(); let mut gen = Generation(0); - for (pos, p) in xxx_make_pieces() { + for (pos, p) in xxx_make_pieces().expect("make pieces") { let pr = PieceState { pos, p, face : 0.into(), diff --git a/src/pieces.rs b/src/pieces.rs index 1131db66..500c98ae 100644 --- a/src/pieces.rs +++ b/src/pieces.rs @@ -5,12 +5,15 @@ define_index_type! { pub struct FaceId = u8; } +type ColourMap = IndexVec; + #[derive(Debug)] struct SimpleShape { desc : String, path : String, +// scaled_path : String, approx_dia : Coord, - colours : IndexVec, + colours : ColourMap, } const SELECT_SCALE : f64 = 1.1; @@ -42,7 +45,10 @@ pub fn svg_rescale_path(input: &str, scale: f64) -> String { const fn new(bits: BM, len: BI) -> Self { Self{ bits, len, index:0 } } fn reset(&mut self) { self.index= 0; } fn next(&mut self) -> bool { - return false; + let r = (self.bits >> (self.len-1 - self.index)) & 1 != 0; + self.index += 1; + if self.index == self.len { self.index = 0; } + r } } const ALWAYS_MAP : RotatingBitmap = RotatingBitmap::new(0x01, 1); @@ -101,16 +107,33 @@ impl Piece for SimpleShape { } } -pub fn xxx_make_pieces() -> Vec<(Pos, Box)> { - vec![ +impl SimpleShape { + #[throws(SVGProcessError)] + fn new_from_path(desc: String, path: String, approx_dia: Coord, + colours: ColourMap) -> Self { + SimpleShape { + desc, approx_dia, path, colours, + } + } + #[throws(SVGProcessError)] + fn new_circle(approx_dia: Coord, colours: ColourMap) -> Self { + let unit_path = + "M 0 1 a 1 1 0 1 0 0 -2 \ + a 1 1 0 1 0 0 2 z"; + let scale = (approx_dia as f64) * 0.5; + let path = svg_rescale_path(&unit_path, scale)?; +eprintln!("rescaled by {}: {} as {}",scale,&unit_path,&path); + Self::new_from_path("circle".to_owned(), path, approx_dia, colours)? + } +} + +pub fn xxx_make_pieces() -> Result)>,SVGProcessError> { + Ok(vec![ ([ 90, 80 ], - Box::new(SimpleShape { - desc : "circle".to_owned(), - approx_dia : 20, - path : "M 0 10 a 10 10 0 1 0 0 -20\ - a 10 10 0 1 0 0 20 z".to_owned(), - colours : index_vec![ "red".to_string(), "grey".to_string() ], - })), + Box::new(SimpleShape::new_circle( + 20, + index_vec![ "red".to_string(), "grey".to_string() ], + )?)), ([ 90, 60 ], Box::new(SimpleShape { desc : "square".to_owned(), @@ -118,5 +141,5 @@ pub fn xxx_make_pieces() -> Vec<(Pos, Box)> { path : "M -10 -10 h 20 v 20 h -20 v -20 z".to_owned(), colours : index_vec![ "blue".to_string(), "grey".to_string() ], })), - ] + ]) }