Skip to main content

rgc_chart/models/fluxis/
hitobjects.rs

1use serde::{Deserialize, Serialize};
2use crate::models::generic::sound;
3use crate::utils::serde::{is_default_f32};
4
5#[derive(Debug, Serialize, Deserialize)]
6pub struct HitObject {
7    pub time: f32,
8    pub lane: isize,
9    
10    #[serde(rename = "visual-lane", default, skip_serializing_if = "is_default_f32")]
11    pub visual_lane: f32,
12    
13    #[serde(default, skip_serializing_if = "is_default_f32")]
14    pub holdtime: f32,
15    
16    pub hitsound: String,
17    
18    #[serde(default, skip_serializing_if = "Option::is_none")]
19    pub group: Option<String>,
20    
21    #[serde(rename = "type")]
22    pub is_tick: i8,
23}
24
25impl Default for HitObject {
26    fn default() -> Self {
27        Self {
28            time: 0.0,
29            lane: 0,
30            visual_lane: 0.0,
31            holdtime: 0.0,
32            hitsound: ":normal".to_string(),
33            group: None,
34            is_tick: 0,
35        }
36    }
37}
38
39impl HitObject {
40    pub fn is_normal(&self) -> bool {
41        self.holdtime <= 0.0 && self.is_tick == 0
42    }
43
44    pub fn is_ln(&self) -> bool {
45        self.holdtime > 0.0 && self.is_tick == 0
46    }
47    
48    pub fn is_tick(&self) -> bool {
49        self.is_tick == 1
50    }
51    
52    pub fn end_time(&self) -> f32 {
53        self.time + self.holdtime
54    }
55    
56    pub fn set_end_time(&mut self, end_time: f32) {
57        self.holdtime = end_time - self.time;
58    }
59
60    pub fn get_generic_keysound(&self) -> sound::KeySound {
61        let hitsound_type = match self.hitsound.to_lowercase().as_str() {
62            ":clap" => sound::HitSoundType::Clap,
63            ":whistle" => sound::HitSoundType::Whistle,
64            ":finish" => sound::HitSoundType::Finish,
65            _ => sound::HitSoundType::Normal,
66        };
67
68        sound::KeySound::of_type(100, hitsound_type)
69    }
70    
71    pub fn new_normal_note(time: f32, lane: isize ) -> Self {
72        HitObject {
73            time,
74            lane,
75            visual_lane: 0.0,
76            holdtime: 0.0,
77            hitsound: ":normal".to_string(),
78            group: None,
79            is_tick: 0,
80        }
81    }
82    
83    pub fn new_long_note(time: f32, lane: isize, holdtime: f32) -> Self {
84        HitObject {
85            time,
86            lane,
87            visual_lane: 0.0,
88            holdtime,
89            hitsound: ":normal".to_string(),
90            group: None,
91            is_tick: 0,
92        }
93    }
94    
95    pub fn new_tick_note(time: f32, lane: isize, visual_lane: f32) -> Self {
96        HitObject {
97            time,
98            lane,
99            visual_lane,
100            holdtime: 0.0,
101            hitsound: ":normal".to_string(),
102            group: None,
103            is_tick: 1,
104        }
105    }
106}