summaryrefslogtreecommitdiff
path: root/src/render.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/render.rs')
-rw-r--r--src/render.rs259
1 files changed, 195 insertions, 64 deletions
diff --git a/src/render.rs b/src/render.rs
index 80b06f7..77075f7 100644
--- a/src/render.rs
+++ b/src/render.rs
@@ -1,39 +1,82 @@
-use alloc::string::ToString;
+use alloc::format;
+use hashbrown::HashSet;
+use serde::{Deserialize, Serialize};
use tortuise_script::{
- buffer::{Cell, Color},
+ buffer::{Cell, Color, WindowSize},
+ state::ScriptState,
ScriptContext, TortuiseScript,
};
-use crate::{APPLE_LOCATION, GRID_SIZE, SNAKE_CELLS};
+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_cell(Cell {
+ cx.add_eternal_cell(Cell {
character: '-',
x: x + i,
y,
..Default::default()
});
- cx.add_cell(Cell {
+ cx.add_eternal_cell(Cell {
character: '-',
x: x + i,
y: y + 2,
..Default::default()
});
}
- cx.add_cell(Cell {
+ cx.add_eternal_cell(Cell {
character: '|',
x,
y: y + 1,
..Default::default()
});
- cx.add_cell(Cell {
+ cx.add_eternal_cell(Cell {
character: '|',
x: x + 5,
y: y + 1,
..Default::default()
});
- cx.add_cell(Cell {
+ cx.add_eternal_cell(Cell {
character: char,
x: x + 3,
y: y + 1,
@@ -41,71 +84,144 @@ fn draw_box(cx: &mut ScriptContext, char: char, x: u16, y: u16) {
});
}
+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;
+ let offset_x = (window_size.columns.saturating_sub(GRID_SIZE) / 2).max(24);
let offset_y = window_size.rows.saturating_sub(GRID_SIZE) / 2;
-
- for i in 0..GRID_SIZE {
- context.add_cell(Cell {
- character: '|',
- x: offset_x.saturating_sub(1),
- y: offset_y + i,
- z: 0,
- ..Default::default()
- });
- context.add_cell(Cell {
- character: '|',
- x: offset_x + GRID_SIZE,
- y: offset_y + i,
- z: 0,
- ..Default::default()
- });
- context.add_cell(Cell {
- character: '-',
- x: offset_x + i,
- y: offset_y.saturating_sub(1),
- z: 0,
- ..Default::default()
- });
- context.add_cell(Cell {
- character: '-',
- x: offset_x + i,
- y: offset_y + GRID_SIZE,
- z: 0,
- ..Default::default()
- });
+ 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);
}
- for (i, char) in "Score:".chars().enumerate() {
- context.add_cell(Cell {
- character: char,
- x: offset_x + GRID_SIZE - 10 + i as u16,
- y: offset_y.saturating_sub(2),
- ..Default::default()
- });
+ 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),
+ );
}
- 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);
+ 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),
+ );
+ }
- for (i, char) in (snake_cells.len() - 2)
- .to_string()
- .chars()
- .rev()
- .enumerate()
- {
- context.add_cell(Cell {
- character: char,
- x: offset_x + GRID_SIZE - i as u16,
- y: offset_y.saturating_sub(2),
- ..Default::default()
- });
+ if *is_paused {
+ draw_text(
+ &mut context,
+ "-- Paused --",
+ offset_x + 2,
+ offset_y + GRID_SIZE + 1,
+ );
}
context.add_cell(Cell {
@@ -116,8 +232,22 @@ pub const RENDER: TortuiseScript = TortuiseScript {
z: 0,
..Default::default()
});
- for cell in &*snake_cells {
- context.add_cell(Cell {
+
+ 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,
@@ -126,6 +256,7 @@ pub const RENDER: TortuiseScript = TortuiseScript {
..Default::default()
});
}
+ old_state.old_snake_cells = snake_cells;
},
..TortuiseScript::empty()
};