Crate termgame

Source
Expand description

TermGame is a small crate that extends tui-rs to make it easy to write TUI games.

Mainly used in COMP6991 at the University of New South Wales, the crate provides the Controller trait, which is accepted by the run_game function to start a game using a Crossterm TUI (provided by tui-rs).

It also wraps many tui features, like StyledCharacter, GameEvent, and GameStyle


use termgame::{SimpleEvent, Controller, Game, GameEvent, GameSettings, StyledCharacter, run_game, KeyCode};
use std::error::Error;
use std::time::Duration;

struct MyGame {}

impl Controller for MyGame {
    fn on_start(&mut self, game: &mut Game) {
    }

    fn on_event(&mut self, game: &mut Game, event: GameEvent) {
        match event.into() {
            SimpleEvent::Just(KeyCode::Char(ch)) => {
                game.set_screen_char(1, 1, Some(StyledCharacter::new(ch)))
            },
            _ => {}
        }

    }

    fn on_tick(&mut self, _game: &mut Game) {}
}

fn main() -> Result<(), Box<dyn Error>> {
    let mut controller = MyGame {};

    run_game(
        &mut controller,
        GameSettings::new()
            // The below are the defaults, but shown so you can edit them.
            .tick_duration(Duration::from_millis(50))
            .quit_event(Some(SimpleEvent::WithControl(KeyCode::Char('c')).into()))
    )?;

    println!("Game Ended!");

    Ok(())
}

Structs§

CharView
A widget that shows a small view into an infinitely sized map.
ChunkMap
A ChunkMap is an infinite 2D plane consisting of elements of type T. It’s basically a HashMap, but using a HashMap would have terrible iteration performance; so it stores values in “chunks”. Each Chunk is stored in a HashMap, and individual elements are accessed by finding the address of their chunk, then getting them by offset.
Font
Modifier changes the way a piece of text is displayed.
Game
The Game struct is passed to all of the Controller’s event methods, to allow the implementor to view and modify the state of the game.
GameSettings
This struct allows you to configure how run_game works.
GameStyle
This struct models how to show a character in Termgame.
KeyEvent
Represents a key event.
KeyEventState
Represents extra state about the key event.
KeyModifiers
Represents key modifiers (shift, control, alt, etc.).
Message
The Message struct is used when displaying a message to a user.
MouseEvent
Represents a mouse event.
StyledCharacter
A character with a given style.
ViewportLocation
The ViewportLocation describes the top-left

Enums§

GameColor
GameError
These are different reasons a game could end.
GameEvent
Represents an event.
KeyCode
Represents a key.
KeyEventKind
Represents a keyboard event kind.
SimpleEvent
This is an enum to make it easy to match on events.

Constants§

SCREEN_HEIGHT
The required screen height termgame can play at. Set to the size of a standard vt100
SCREEN_WIDTH
The required screen width termgame can play at.

Traits§

Controller
The Controller trait must be implemented on a struct in order to control a Termgame Game.

Functions§

run_game
Starts a game with a given Controller, which refreshes at the given tick_duration (a Duration).

Type Aliases§

CharChunkMap
This is a ChunkMap (an infinite 2D map) of the internal representation of characters.