Implement setter functions for symbol selection
This commit is contained in:
parent
18a0abcd62
commit
fe2f998810
@ -46,6 +46,9 @@ pub enum Error {
|
||||
#[error("Failed to serialize savefile")]
|
||||
SerializationFailed(binrw::Error),
|
||||
|
||||
#[error("Symbol id is out of range")]
|
||||
SymbolIdOutOfRange,
|
||||
|
||||
#[error("Failed to read file")]
|
||||
FileReadingFailed(io::Error),
|
||||
}
|
||||
|
@ -3,7 +3,10 @@ use core::fmt;
|
||||
use binrw::{BinRead, BinWrite};
|
||||
use substring::Substring;
|
||||
|
||||
use crate::{Error, Result};
|
||||
|
||||
|
||||
const MAX_SYMBOL_ID: u32 = 20;
|
||||
const SYMBOL_PARTS: &str = include_str!("symbol_parts.txt");
|
||||
const SYMBOL_PART_WIDTH: usize = 6;
|
||||
const SYMBOL_PART_HEIGTH: usize = 3;
|
||||
@ -11,7 +14,42 @@ const SYMBOL_PART_HEIGTH: usize = 3;
|
||||
|
||||
#[derive(Debug, Clone, Copy, BinRead, BinWrite)]
|
||||
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 {
|
||||
|
@ -16,7 +16,7 @@ fn general_info() {
|
||||
assert_eq!(savefile.robe_color(), RobeColor::Red);
|
||||
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.current_level, 1);
|
||||
assert_eq!(savefile.total_collected_symbols, 107);
|
||||
|
@ -63,7 +63,7 @@ fn edit_file(cur_savefile: &Savefile, args: &Args) -> Result<Savefile> {
|
||||
}
|
||||
|
||||
if let Some(val) = args.symbol {
|
||||
savefile.symbol.id = val;
|
||||
savefile.symbol.set_by_id(val)?;
|
||||
}
|
||||
|
||||
if let Some(color) = &args.robe_color {
|
||||
|
@ -187,15 +187,7 @@ impl State {
|
||||
|
||||
savefile.scarf_length = new_length;
|
||||
}
|
||||
6 => {
|
||||
let next_symbol_id = value.parse()?;
|
||||
|
||||
if next_symbol_id > 20 {
|
||||
bail!("Symbol id out of range");
|
||||
}
|
||||
|
||||
savefile.symbol.id = next_symbol_id;
|
||||
}
|
||||
6 => savefile.symbol.set_by_id(value.parse()?)?,
|
||||
7 => {
|
||||
let new_color = match value {
|
||||
"Red" | "red" => RobeColor::Red,
|
||||
@ -244,14 +236,7 @@ impl State {
|
||||
savefile.scarf_length += 1;
|
||||
}
|
||||
}
|
||||
6 => {
|
||||
let next_symbol = savefile.symbol.id + 1;
|
||||
savefile.symbol.id = if next_symbol > 20 {
|
||||
0
|
||||
} else {
|
||||
savefile.symbol.id + 1
|
||||
};
|
||||
}
|
||||
6 => savefile.symbol = savefile.symbol.wrapping_next(),
|
||||
7 => {
|
||||
savefile.set_robe_color(match savefile.robe_color() {
|
||||
RobeColor::Red => RobeColor::White,
|
||||
@ -304,14 +289,7 @@ impl State {
|
||||
savefile.scarf_length = savefile.scarf_length.saturating_sub(1);
|
||||
}
|
||||
}
|
||||
6 => {
|
||||
let next_symbol = savefile.symbol.id as i32 - 1;
|
||||
savefile.symbol.id = if next_symbol < 0 {
|
||||
20
|
||||
} else {
|
||||
next_symbol as u32
|
||||
};
|
||||
}
|
||||
6 => savefile.symbol = savefile.symbol.wrapping_previous(),
|
||||
7 => {
|
||||
savefile.set_robe_color(match savefile.robe_color() {
|
||||
RobeColor::Red => RobeColor::White,
|
||||
|
@ -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()),
|
||||
("Companions Met", savefile.companions_met.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 Tier", savefile.robe_tier().to_string()),
|
||||
("Last Played", savefile.last_played.to_string()),
|
||||
|
Loading…
Reference in New Issue
Block a user