Implement setter functions for symbol selection

This commit is contained in:
Patrick Auernig 2023-08-16 14:48:01 +02:00
parent 18a0abcd62
commit fe2f998810
6 changed files with 48 additions and 29 deletions

View File

@ -46,6 +46,9 @@ pub enum Error {
#[error("Failed to serialize savefile")] #[error("Failed to serialize savefile")]
SerializationFailed(binrw::Error), SerializationFailed(binrw::Error),
#[error("Symbol id is out of range")]
SymbolIdOutOfRange,
#[error("Failed to read file")] #[error("Failed to read file")]
FileReadingFailed(io::Error), FileReadingFailed(io::Error),
} }

View File

@ -3,7 +3,10 @@ use core::fmt;
use binrw::{BinRead, BinWrite}; use binrw::{BinRead, BinWrite};
use substring::Substring; use substring::Substring;
use crate::{Error, Result};
const MAX_SYMBOL_ID: u32 = 20;
const SYMBOL_PARTS: &str = include_str!("symbol_parts.txt"); const SYMBOL_PARTS: &str = include_str!("symbol_parts.txt");
const SYMBOL_PART_WIDTH: usize = 6; const SYMBOL_PART_WIDTH: usize = 6;
const SYMBOL_PART_HEIGTH: usize = 3; const SYMBOL_PART_HEIGTH: usize = 3;
@ -11,7 +14,42 @@ const SYMBOL_PART_HEIGTH: usize = 3;
#[derive(Debug, Clone, Copy, BinRead, BinWrite)] #[derive(Debug, Clone, Copy, BinRead, BinWrite)]
pub struct Symbol { pub struct Symbol {
pub id: u32, #[br(assert(id < MAX_SYMBOL_ID))]
id: u32,
}
impl Symbol {
pub fn set_by_id(&mut self, id: u32) -> Result<()> {
if id > MAX_SYMBOL_ID {
return Err(Error::SymbolIdOutOfRange);
}
self.id = id;
Ok(())
}
pub fn wrapping_next(&self) -> Self {
let id = self.id + 1;
if id > MAX_SYMBOL_ID {
Self { id: 0 }
} else {
Self { id }
}
}
pub fn wrapping_previous(&self) -> Self {
match self.id.checked_sub(1) {
Some(id) => Self { id },
None => Self { id: MAX_SYMBOL_ID },
}
}
}
impl AsRef<u32> for Symbol {
fn as_ref(&self) -> &u32 {
&self.id
}
} }
impl fmt::Display for Symbol { impl fmt::Display for Symbol {

View File

@ -16,7 +16,7 @@ fn general_info() {
assert_eq!(savefile.robe_color(), RobeColor::Red); assert_eq!(savefile.robe_color(), RobeColor::Red);
assert_eq!(savefile.robe_tier(), 4); assert_eq!(savefile.robe_tier(), 4);
assert_eq!(savefile.symbol.id, 7); assert_eq!(savefile.symbol.as_ref(), &7);
assert_eq!(savefile.scarf_length, 27); assert_eq!(savefile.scarf_length, 27);
assert_eq!(savefile.current_level, 1); assert_eq!(savefile.current_level, 1);
assert_eq!(savefile.total_collected_symbols, 107); assert_eq!(savefile.total_collected_symbols, 107);

View File

@ -63,7 +63,7 @@ fn edit_file(cur_savefile: &Savefile, args: &Args) -> Result<Savefile> {
} }
if let Some(val) = args.symbol { if let Some(val) = args.symbol {
savefile.symbol.id = val; savefile.symbol.set_by_id(val)?;
} }
if let Some(color) = &args.robe_color { if let Some(color) = &args.robe_color {

View File

@ -187,15 +187,7 @@ impl State {
savefile.scarf_length = new_length; savefile.scarf_length = new_length;
} }
6 => { 6 => savefile.symbol.set_by_id(value.parse()?)?,
let next_symbol_id = value.parse()?;
if next_symbol_id > 20 {
bail!("Symbol id out of range");
}
savefile.symbol.id = next_symbol_id;
}
7 => { 7 => {
let new_color = match value { let new_color = match value {
"Red" | "red" => RobeColor::Red, "Red" | "red" => RobeColor::Red,
@ -244,14 +236,7 @@ impl State {
savefile.scarf_length += 1; savefile.scarf_length += 1;
} }
} }
6 => { 6 => savefile.symbol = savefile.symbol.wrapping_next(),
let next_symbol = savefile.symbol.id + 1;
savefile.symbol.id = if next_symbol > 20 {
0
} else {
savefile.symbol.id + 1
};
}
7 => { 7 => {
savefile.set_robe_color(match savefile.robe_color() { savefile.set_robe_color(match savefile.robe_color() {
RobeColor::Red => RobeColor::White, RobeColor::Red => RobeColor::White,
@ -304,14 +289,7 @@ impl State {
savefile.scarf_length = savefile.scarf_length.saturating_sub(1); savefile.scarf_length = savefile.scarf_length.saturating_sub(1);
} }
} }
6 => { 6 => savefile.symbol = savefile.symbol.wrapping_previous(),
let next_symbol = savefile.symbol.id as i32 - 1;
savefile.symbol.id = if next_symbol < 0 {
20
} else {
next_symbol as u32
};
}
7 => { 7 => {
savefile.set_robe_color(match savefile.robe_color() { savefile.set_robe_color(match savefile.robe_color() {
RobeColor::Red => RobeColor::White, RobeColor::Red => RobeColor::White,

View File

@ -55,7 +55,7 @@ pub(super) fn render<'a>(state: &mut State, frame: &mut Frame, area: Rect) {
("Current Level", savefile.current_level_name().to_string()), ("Current Level", savefile.current_level_name().to_string()),
("Companions Met", savefile.companions_met.to_string()), ("Companions Met", savefile.companions_met.to_string()),
("Scarf Length", savefile.scarf_length.to_string()), ("Scarf Length", savefile.scarf_length.to_string()),
("Symbol Number", savefile.symbol.id.to_string()), ("Symbol Number", savefile.symbol.as_ref().to_string()),
("Robe Color", savefile.robe_color().to_string()), ("Robe Color", savefile.robe_color().to_string()),
("Robe Tier", savefile.robe_tier().to_string()), ("Robe Tier", savefile.robe_tier().to_string()),
("Last Played", savefile.last_played.to_string()), ("Last Played", savefile.last_played.to_string()),