logo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
use std::rc::Rc;

use crate::engine::d2::{
    animation::AnimatedFloat,
    asset::Asset,
    sound::{Playback, Sound},
    util::{Disposable, Value},
};

use super::BasicAsset;

/// An empty sound used in environments that don't support audio.
#[derive(Clone)]
pub struct DummySound {
    pub inner: Rc<BasicAsset<DummySound>>,
    // static
    // _instance: DummySound,
    playback: Option<DummyPlayback>,
}

impl DummySound {
    pub fn new() -> Self {
        Self {
            inner: Rc::new(BasicAsset::<DummySound>::new()),
            playback: None,
        }
    }

    // override
    fn copy_from(&self, asset: DummySound) {
        // Nothing at all
    }

    // override
    fn on_disposed(&self) {
        // Nothing at all
    }

    // static
    pub fn instance() -> DummySound {
        // if _instance.is_none() {
        //     _instance = DummySound::new();
        // }

        // _instance
        unimplemented!()
    }
}

impl AsRef<BasicAsset<DummySound>> for DummySound {
    fn as_ref(&self) -> &BasicAsset<DummySound> {
        self.inner.as_ref()
    }
}

impl Sound for DummySound {
    // volume: f32 = 1.0
    fn play(&self, volume: f32) -> Rc<dyn Playback> {
        Rc::new(DummyPlayback::new(Rc::new(self.clone())))
    }

    // volume: f32 = 1.0
    fn play_with_loop(&self, volume: f32) -> Rc<dyn Playback> {
        Rc::new(DummyPlayback::new(Rc::new(self.clone())))
    }

    fn duration(&self) -> f32 {
        0.0
    }
}

impl Asset for DummySound {
    fn reload_count(&self) -> usize {
        self.inner.reload_count()
    }
}

impl Disposable for DummySound {
    fn dispose(&self) {
        self.inner.dispose()
    }
}

// This should be immutable too
#[derive(Clone)]
pub struct DummyPlayback {
    pub volume: AnimatedFloat,
    sound: Rc<dyn Sound>,
    complete: Value<bool>,
}

impl DummyPlayback {
    pub fn new(sound: Rc<dyn Sound>) -> Self {
        Self {
            sound,
            volume: AnimatedFloat::new(0.0, None), // A little quirky? All DummyPlaybacks share the same volume
            complete: Value::<bool>::new(true, None),
        }
    }

    pub fn set_paused(&self, paused: bool) {}
}

impl Playback for DummyPlayback {
    fn sound(&self) -> Rc<dyn Sound> {
        self.sound.clone()
    }

    fn volume(&self) -> AnimatedFloat {
        self.volume.clone()
    }

    fn paused(&self) -> bool {
        true
    }

    fn complete(&self) -> Value<bool> {
        self.complete.clone()
    }

    fn position(&self) -> f32 {
        0.0
    }

    fn set_complete(&self, val: Value<bool>) {
        todo!()
    }

    fn set_paused(&self, val: bool) {
        todo!()
    }

    fn set_volume(&self, val: AnimatedFloat) {
        todo!()
    }

    fn set_position(&self, pos: f32) {
        todo!()
    }
}

impl Disposable for DummyPlayback {
    fn dispose(&self) {
        // Nothing
    }
}