summaryrefslogtreecommitdiff
path: root/src/render.rs
blob: 77075f75a95924dc35cef9f780b0f3f6d1025da1 (plain)
use alloc::format;
use hashbrown::HashSet;
use serde::{Deserialize, Serialize};
use tortuise_script::{
    buffer::{Cell, Color, WindowSize},
    state::ScriptState,
    ScriptContext, TortuiseScript,
};

use crate::{Position, APPLE_LOCATION, GRID_SIZE, IS_PAUSED, SAVE_DATA, SNAKE_CELLS};

#[derive(Serialize, Deserialize)]
struct OldState {
    old_snake_cells: HashSet<Position>,
    old_score: usize,
    old_high_score: usize,
    old_window_size: WindowSize,
}

static OLD_STATE: ScriptState<OldState> = ScriptState::new(|_| OldState {
    old_snake_cells: HashSet::new(),
    old_score: usize::MAX,
    old_high_score: usize::MAX,
    old_window_size: WindowSize {
        rows: 0,
        columns: 0,
    },
});

fn draw_text(cx: &mut ScriptContext, text: &str, x: u16, y: u16) {
    for (x, character) in (x..).zip(text.chars()) {
        cx.add_eternal_cell(Cell {
            character,
            x,
            y,
            ..Cell::default()
        });
    }
}

fn remove_text(cx: &mut ScriptContext, text: &str, x: u16, y: u16) {
    for (x, character) in (x..).zip(text.chars()) {
        cx.remove_eternal_cell(Cell {
            character,
            x,
            y,
            ..Cell::default()
        });
    }
}

fn draw_box(cx: &mut ScriptContext, char: char, x: u16, y: u16) {
    for i in 1..5 {
        cx.add_eternal_cell(Cell {
            character: '-',
            x: x + i,
            y,
            ..Default::default()
        });
        cx.add_eternal_cell(Cell {
            character: '-',
            x: x + i,
            y: y + 2,
            ..Default::default()
        });
    }
    cx.add_eternal_cell(Cell {
        character: '|',
        x,
        y: y + 1,
        ..Default::default()
    });
    cx.add_eternal_cell(Cell {
        character: '|',
        x: x + 5,
        y: y + 1,
        ..Default::default()
    });
    cx.add_eternal_cell(Cell {
        character: char,
        x: x + 3,
        y: y + 1,
        ..Default::default()
    });
}

fn draw_grid(context: &mut ScriptContext<'_>, offset_x: u16, offset_y: u16) {
    for i in 0..GRID_SIZE {
        context.add_eternal_cell(Cell {
            character: '|',
            x: offset_x.saturating_sub(1),
            y: offset_y + i,
            z: 0,
            ..Default::default()
        });
        context.add_eternal_cell(Cell {
            character: '|',
            x: offset_x + GRID_SIZE,
            y: offset_y + i,
            z: 0,
            ..Default::default()
        });
        context.add_eternal_cell(Cell {
            character: '-',
            x: offset_x + i,
            y: offset_y.saturating_sub(1),
            z: 0,
            ..Default::default()
        });
        context.add_eternal_cell(Cell {
            character: '-',
            x: offset_x + i,
            y: offset_y + GRID_SIZE,
            z: 0,
            ..Default::default()
        });
    }
}

fn delete_grid(context: &mut ScriptContext<'_>, offset_x: u16, offset_y: u16) {
    for i in 0..GRID_SIZE {
        context.remove_eternal_cell(Cell {
            character: '|',
            x: offset_x.saturating_sub(1),
            y: offset_y + i,
            z: 0,
            ..Default::default()
        });
        context.remove_eternal_cell(Cell {
            character: '|',
            x: offset_x + GRID_SIZE,
            y: offset_y + i,
            z: 0,
            ..Default::default()
        });
        context.remove_eternal_cell(Cell {
            character: '-',
            x: offset_x + i,
            y: offset_y.saturating_sub(1),
            z: 0,
            ..Default::default()
        });
        context.remove_eternal_cell(Cell {
            character: '-',
            x: offset_x + i,
            y: offset_y + GRID_SIZE,
            z: 0,
            ..Default::default()
        });
    }
}

pub const RENDER: TortuiseScript = TortuiseScript {
    start: |mut context| {
        draw_box(&mut context, '<', 4, 4);
        draw_box(&mut context, '>', 16, 4);
        draw_box(&mut context, '^', 10, 1);
        draw_box(&mut context, 'v', 10, 7);
    },
    update: |mut context: ScriptContext| {
        let mut old_state = OLD_STATE.get_mut(&context);
        let apple_location = APPLE_LOCATION.get(&context);
        let snake_cells = SNAKE_CELLS.get(&context);
        let is_paused = IS_PAUSED.get(&context);
        let high_score = SAVE_DATA.load(&context).high_score;

        let old_window_size = old_state.old_window_size;
        let old_offset_x = (old_window_size.columns.saturating_sub(GRID_SIZE) / 2).max(24);
        let old_offset_y = old_window_size.rows.saturating_sub(GRID_SIZE) / 2;
        let window_size = context.window_size();
        let offset_x = (window_size.columns.saturating_sub(GRID_SIZE) / 2).max(24);
        let offset_y = window_size.rows.saturating_sub(GRID_SIZE) / 2;
        if window_size != old_window_size {
            old_state.old_window_size = window_size;
            delete_grid(&mut context, old_offset_x, old_offset_y);
            draw_grid(&mut context, offset_x, offset_y);
        }

        let old_score = old_state.old_score;
        let score = snake_cells.len() - 2;
        if score != old_score || window_size != old_window_size {
            old_state.old_score = score;
            let score_counter = format!("Score: {score:>3}");
            let old_score_counter = format!("Score: {old_score:>3}");
            remove_text(
                &mut context,
                &old_score_counter,
                offset_x + GRID_SIZE - 10,
                offset_y.saturating_sub(2),
            );
            draw_text(
                &mut context,
                &score_counter,
                offset_x + GRID_SIZE - 10,
                offset_y.saturating_sub(2),
            );
        }

        let old_high_score = old_state.old_high_score;
        if high_score != old_high_score || window_size != old_window_size {
            old_state.old_high_score = high_score;
            let score_counter = format!("High Score: {high_score:>3}");
            let old_score_counter = format!("High Score: {old_high_score:>3}");
            remove_text(
                &mut context,
                &old_score_counter,
                offset_x + GRID_SIZE - 15,
                offset_y.saturating_sub(3),
            );
            draw_text(
                &mut context,
                &score_counter,
                offset_x + GRID_SIZE - 15,
                offset_y.saturating_sub(3),
            );
        }

        if *is_paused {
            draw_text(
                &mut context,
                "-- Paused --",
                offset_x + 2,
                offset_y + GRID_SIZE + 1,
            );
        }

        context.add_cell(Cell {
            character: '●',
            foreground_color: Color::Red,
            x: apple_location.x.unsigned_abs() + offset_x,
            y: apple_location.y.unsigned_abs() + offset_y,
            z: 0,
            ..Default::default()
        });

        let snake_cells = HashSet::from_iter(snake_cells.iter().cloned());
        let old_snake_cells = old_state.old_snake_cells.difference(&snake_cells);
        let new_snake_cells = snake_cells.difference(&old_state.old_snake_cells);
        for cell in old_snake_cells {
            context.remove_eternal_cell(Cell {
                character: '█',
                foreground_color: Color::Green,
                x: cell.x.unsigned_abs() + offset_x,
                y: cell.y.unsigned_abs() + offset_y,
                z: 1,
                ..Default::default()
            });
        }
        for cell in new_snake_cells {
            context.add_eternal_cell(Cell {
                character: '█',
                foreground_color: Color::Green,
                x: cell.x.unsigned_abs() + offset_x,
                y: cell.y.unsigned_abs() + offset_y,
                z: 1,
                ..Default::default()
            });
        }
        old_state.old_snake_cells = snake_cells;
    },
    ..TortuiseScript::empty()
};