|
@@ -20,30 +20,66 @@ impl From<Color> for ggez::graphics::Color {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-pub struct Board<Idx: Tile> {
|
|
|
- size: Size,
|
|
|
- contents: Vec<(Idx, SpriteIdx)>,
|
|
|
- tileset: Tileset<Idx>,
|
|
|
-}
|
|
|
|
|
|
-pub trait Tile: Copy {
|
|
|
+/// The `Tile` trait is used to identify which tile we care about on a
|
|
|
+/// tilesheet, and also to provide a sensible 'blank' tile. The tile
|
|
|
+/// identifiers we care about should at least be `Clone`, but often
|
|
|
+/// they'll be (wrappers over) small identifiers (like a `u8` or a
|
|
|
+/// `u16`) and could probably be `Copy`.
|
|
|
+pub trait Tile: Clone {
|
|
|
fn to_location(self) -> [f32;4];
|
|
|
fn blank() -> Self;
|
|
|
}
|
|
|
|
|
|
-impl Tile for u8 {
|
|
|
+/// This represents a character from DOS codepage 437
|
|
|
+#[derive(Debug, Copy, Clone)]
|
|
|
+pub struct CP437(u8);
|
|
|
+
|
|
|
+impl CP437 {
|
|
|
+ pub fn from_u8(ch: u8) -> CP437 {
|
|
|
+ CP437(ch)
|
|
|
+ }
|
|
|
+
|
|
|
+ pub fn from_char(ch: char) -> CP437 {
|
|
|
+ CP437(match ch as u32 {
|
|
|
+ // happy faces
|
|
|
+ 0x263A => 1,
|
|
|
+ 0x263B => 2,
|
|
|
+ // card suits
|
|
|
+ 0x2665 => 3,
|
|
|
+ 0x2666 => 4,
|
|
|
+ 0x2663 => 5,
|
|
|
+ 0x2660 => 6,
|
|
|
+ 0x2022 => 7,
|
|
|
+ // fill this in some time later
|
|
|
+ // standard ASCII mappings
|
|
|
+ 0x20 ..= 0x7e => ch as u8,
|
|
|
+ 0x2302 => 0x7f,
|
|
|
+ _ => panic!("Character {} does not have a CP437 equivalent", ch),
|
|
|
+ })
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+impl Tile for CP437 {
|
|
|
fn to_location(self) -> [f32;4] {
|
|
|
const TILE_SIZE: f32 = 1.0 / 16.0;
|
|
|
- let u = f32::from(self % 16) * TILE_SIZE;
|
|
|
- let v = f32::from(self / 16) * TILE_SIZE;
|
|
|
+ let u = f32::from(self.0 % 16) * TILE_SIZE;
|
|
|
+ let v = f32::from(self.0 / 16) * TILE_SIZE;
|
|
|
[u, v, TILE_SIZE, TILE_SIZE]
|
|
|
}
|
|
|
|
|
|
fn blank() -> Self {
|
|
|
- 0
|
|
|
+ CP437(0)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+pub struct Board<Idx: Tile> {
|
|
|
+ size: Size,
|
|
|
+ contents: Vec<(Idx, SpriteIdx)>,
|
|
|
+ tileset: Tileset<Idx>,
|
|
|
+}
|
|
|
+
|
|
|
pub struct Tileset<Idx: Tile> {
|
|
|
tile_size: Size,
|
|
|
batch: SpriteBatch,
|
|
@@ -157,7 +193,7 @@ impl<Idx: Tile> Board<Idx> {
|
|
|
pub fn get(&mut self, at: impl Into<Coord>) -> Idx {
|
|
|
let at = at.into();
|
|
|
let idx = at.x + at.y * self.size.width;
|
|
|
- self.contents[idx].0
|
|
|
+ self.contents[idx].0.clone()
|
|
|
}
|
|
|
|
|
|
pub fn clear(&mut self) {
|
|
@@ -173,11 +209,11 @@ impl<Idx: Tile> Board<Idx> {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-impl Board<u8> {
|
|
|
+impl Board<CP437> {
|
|
|
pub fn print(&mut self, at: impl Into<Coord>, msg: &str) {
|
|
|
let Coord { x, y } = at.into();
|
|
|
for (idx, ch) in msg.chars().enumerate() {
|
|
|
- self.set([idx + x, y], ch as u8);
|
|
|
+ self.set([idx + x, y], CP437::from_char(ch));
|
|
|
}
|
|
|
}
|
|
|
}
|