Skip to main content

vitium_api/game/
level.rs

1use std::ops::{Add, Sub};
2
3use serde::{Deserialize, Serialize};
4
5#[derive(Serialize, Deserialize, Clone, Copy)]
6#[cfg_attr(target_family = "wasm", derive(tsify_next::Tsify))]
7#[cfg_attr(
8    target_family = "wasm",
9    tsify(into_wasm_abi, from_wasm_abi, large_number_types_as_bigints)
10)]
11pub struct Level {
12    /// Level when the character is created.
13    pub born: i16,
14    /// Level grown during the game.
15    pub growth: i16,
16    /// Bonus currently have.
17    pub bonus: i16,
18}
19
20impl Add<i16> for Level {
21    type Output = i16;
22
23    fn add(self, rhs: i16) -> Self::Output {
24        self.curr() + rhs
25    }
26}
27
28impl Sub<i16> for Level {
29    type Output = i16;
30
31    fn sub(self, rhs: i16) -> Self::Output {
32        self.curr() - rhs
33    }
34}
35
36impl Add for Level {
37    type Output = i16;
38
39    fn add(self, rhs: Self) -> Self::Output {
40        self.curr() + rhs.curr()
41    }
42}
43
44impl Sub for Level {
45    type Output = i16;
46
47    fn sub(self, rhs: Self) -> Self::Output {
48        self.curr() - rhs.curr()
49    }
50}
51
52impl Level {
53    pub fn new(born: i16) -> Self {
54        Self {
55            born,
56            growth: 0,
57            bonus: 0,
58        }
59    }
60    pub fn base(&self) -> i16 {
61        self.born + self.growth
62    }
63    pub fn curr(&self) -> i16 {
64        self.base() + self.bonus
65    }
66}