wolfrpg_map_parser/command/
transfer_command.rs

1#[cfg(feature = "serde")]
2use serde::{Serialize, Deserialize};
3use crate::byte_utils::as_u32_le;
4use crate::command::transfer_command::options::Options;
5use crate::command::transfer_command::target::Target;
6
7pub mod target;
8pub mod options;
9pub mod transition;
10
11#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
12#[derive(PartialEq, Clone)]
13pub struct TransferCommand {
14    target: Target,
15    db_variable: Option<u32>,
16    destination_x: u32,
17    destination_y: u32,
18    destination_map: Option<u32>,
19    options: Options
20}
21
22impl TransferCommand {
23    pub(crate) fn parse(bytes: &[u8]) -> (usize, Self) {
24        let mut offset: usize = 0;
25
26        let target: u32 = as_u32_le(&bytes[offset..offset+4]);
27        let target: Target = Target::new(target);
28        offset += 4;
29
30        let db_variable: Option<u32> = match target {
31            Target::SavedPosition => {
32                let db_variable: u32 = as_u32_le(&bytes[offset..offset+4]);
33                offset += 4;
34
35                Some(db_variable)
36            },
37            _ => None
38        };
39
40        let destination_x: u32 = as_u32_le(&bytes[offset..offset+4]);
41        offset += 4;
42
43        let destination_y: u32 = as_u32_le(&bytes[offset..offset+4]);
44        offset += 4;
45
46        let destination_map: Option<u32> = match target {
47            Target::SavedPosition => None,
48            _ => {
49                let destination_map: u32 = as_u32_le(&bytes[offset..offset+4]);
50                offset += 4;
51
52                Some(destination_map)
53            }
54        };
55
56        let options: u32 = as_u32_le(&bytes[offset..offset+4]);
57        let options: Options = Options::new(options);
58        offset += 4;
59
60        offset += 3; // Command end signature
61
62        (offset, Self {
63            target,
64            db_variable,
65            destination_x,
66            destination_y,
67            destination_map,
68            options
69        })
70    }
71
72    pub fn target(&self) -> &Target {
73        &self.target
74    }
75    
76    pub fn target_mut(&mut self) -> &mut Target {
77        &mut self.target
78    }
79
80    pub fn db_variable(&self) -> Option<u32> {
81        self.db_variable
82    }
83    
84    pub fn db_variable_mut(&mut self) -> &mut Option<u32> {
85        &mut self.db_variable
86    }
87
88    pub fn destination_x(&self) -> u32 {
89        self.destination_x
90    }
91    
92    pub fn destination_x_mut(&mut self) -> &mut u32 {
93        &mut self.destination_x
94    }
95
96    pub fn destination_y(&self) -> u32 {
97        self.destination_y
98    }
99    
100    pub fn destination_y_mut(&mut self) -> &mut u32 {
101        &mut self.destination_y
102    }
103
104    pub fn destination_map(&self) -> Option<u32> {
105        self.destination_map
106    }
107    
108    pub fn destination_map_mut(&mut self) -> &mut Option<u32> {
109        &mut self.destination_map
110    }
111
112    pub fn options(&self) -> &Options {
113        &self.options
114    }
115    
116    pub fn options_mut(&mut self) -> &mut Options {
117        &mut self.options
118    }
119}