wolfrpg_map_parser/common/
move.rs1#[cfg(feature = "serde")]
2use serde::{Serialize, Deserialize};
3use move_type::MoveType;
4use crate::byte_utils::as_u16_le;
5use crate::common::r#move::state::State;
6
7pub mod move_type;
8pub mod state;
9
10#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
11#[derive(PartialEq, Clone)]
12pub struct Move {
13 move_type: MoveType,
14 state: State
15}
16
17impl Move {
18 pub(crate) fn parse(bytes: &[u8]) -> (usize, Self) {
19 let mut offset: usize = 0;
20
21 let move_type: u16 = as_u16_le(&bytes[offset..offset + 2]);
22 let move_type: MoveType = MoveType::new(move_type);
23 offset += 2;
24
25 let (bytes_read, state): (usize, State) = State::parse(&bytes[offset..], &move_type);
26 offset += bytes_read;
27
28 offset += 2; (offset, Move {
31 move_type,
32 state
33 })
34 }
35
36 pub(crate) fn parse_multiple(bytes: &[u8], move_count: u32) -> (usize, Vec<Move>) {
37 let mut offset: usize = 0;
38 let mut moves: Vec<Move> = Vec::with_capacity(move_count as usize);
39
40 for _ in 0..move_count {
41 let (bytes_read, mov): (usize, Move) = Move::parse(&bytes[offset..]);
42 offset += bytes_read;
43 moves.push(mov);
44 }
45
46 (offset, moves)
47 }
48
49 pub fn move_type(&self) -> &MoveType {
50 &self.move_type
51 }
52
53 pub fn move_type_mut(&mut self) -> &mut MoveType {
54 &mut self.move_type
55 }
56
57 pub fn state(&self) -> &State {
58 &self.state
59 }
60
61 pub fn state_mut(&mut self) -> &mut State {
62 &mut self.state
63 }
64}