Skip to main content

Module skin

Module skin 

Source
Expand description

A grid skin: how a MapScene looks (game ADR-0038). The skin paints the ground, the ranges, the path, the cursor and the threat arrows; the game’s UnitPainter paints its units, between the skin’s layers. What the skin paints with besides the scene (the palette, the game’s terrains, the lights) it is given as Looks.

§A game with its own painters and several looks

A game keeps its skins as Rc<dyn GridSkin<U>> and needs no skin trait of its own:

  • The painter is built each frame. GridSkin::paint takes the UnitPainter per call, so a screen builds it in its own draw from that frame’s context (its palette, its unit pictures) and passes a reference. The painter is told the skin’s Grid, whose zoom and art_tile say how big to paint.
  • What the painter needs is kept beside the skin. Which painter goes with which skin (a tileset of the game’s unit pictures, a one-bit painter) is the game’s: a small struct or enum of its own holding the skin and that, not a trait.
  • Zoom is the skin’s: zoom_levels, zoom and at_zoom, none for a skin that can’t.
  • Walk speeds are the skin’s too: WalkSpeeds, with a speed for while a key is held to hurry another side’s turn.
  • Another look of the same tiles (a one-bit screen’s) is the glyph skin with a GlyphStyle.
use std::rc::Rc;

use shuyi_ui::frame::glyph_buffer::{GlyphBuffer, Rect};
use shuyi_ui::grid::{Grid, GridSkin, MapScene, UnitPainter};
use shuyi_ui::toy::grid::{GLYPH, Kit, Piece};

/// The game's painter, borrowing what one frame paints with.
struct Pieces<'a> {
    kit: &'a Kit,
}

impl UnitPainter<Piece> for Pieces<'_> {
    fn paint(&self, scene: &MapScene<Piece>, grid: &Grid, buf: &mut GlyphBuffer) {
        // Paints `scene.units` in `self.kit`'s colors and pictures, at
        // `grid.zoom()`.
        let _ = (self.kit, scene, grid, buf);
    }
}

/// The game's look: a skin, and whatever its painter needs.
struct MapLook {
    skin: Rc<dyn GridSkin<Piece>>,
}

impl MapLook {
    /// What the screen's `draw` does each frame.
    fn draw(&self, kit: &Kit, scene: &MapScene<Piece>, buf: &mut GlyphBuffer) {
        let painter = Pieces { kit };
        let area = Rect::new(0, 0, 40, 20);
        self.skin.paint(&kit.looks(), scene, area, &painter, buf);
    }

    /// The same look at `zoom`, if its skin zooms to it.
    fn at_zoom(&self, zoom: i32) -> Option<Self> {
        Some(Self { skin: self.skin.at_zoom(zoom)? })
    }
}

let kit = Kit::new();
let look = MapLook { skin: Rc::new(GLYPH) };
let black = shuyi_ui::frame::Rgb::new(0, 0, 0);
let mut buf = GlyphBuffer::new(40, 20, shuyi_ui::frame::Cell::new(' ', black, black));
look.draw(&kit, &MapScene::new(shuyi_core::Pos::new(0, 0), (4, 4)), &mut buf);
// The glyph skin's tile is two font cells: it doesn't zoom.
assert!(look.at_zoom(2).is_none());

Structs§

Looks
What a skin paints a scene with besides the scene itself: the palette, the game’s terrains and the lights a map may be in. Build it with Looks::new (ADR-0039).
MapColors
The palette’s names of the colors a grid skin paints the path, the cursor and the threat arrows in: what the game’s palette calls them. The ranges are painted in the colors of their own names; a threat’s marks are the engine’s highlight color, and every dark edge (and what the cursor fades toward) its background color. Build it with MapColors::new (ADR-0039).
NoUnits
Paints no units: a map shown with nobody on it, or the ground alone.
TerrainGlyphs
How the glyph skin draws one terrain: its two glyphs, in the palette’s colors of these names. Build it with TerrainGlyphs::new: a struct literal doesn’t compile outside this crate, so a new field never breaks a game (ADR-0039).
WalkSpeeds
How fast a unit’s walk is shown, in tiles per second: how long the player watches a move, never what the move does. A skin’s setting.

Traits§

GridSkin
Paints grid maps of units U. The tile size is the skin’s: screens ask it how many tiles fit and where a tile is. A skin never changes the game: only the frame, how many tiles are on screen, and how fast a walk is shown.
Terrains
A game’s terrains, as far as its map’s look goes: what each is called, and how the glyph skin draws it. The game’s table, by TerrainId.
UnitPainter
Paints a game’s units on a grid skin’s tiles: its letters, bars, marks or pictures (game ADR-0038). A skin calls it between its own layers.

Functions§

cells_of
The cells the pixels px touch.