-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathplayer.rs
More file actions
57 lines (49 loc) Β· 1.39 KB
/
Copy pathplayer.rs
File metadata and controls
57 lines (49 loc) Β· 1.39 KB
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
use perro_api::prelude::*;
#[State]
struct PatternPlayer {
#[default = 0]
score: i32,
#[default = 0]
pub bonus: i32,
#[default = NodeID::nil()]
#[node_ref(Sprite2D)]
pub icon: NodeID,
}
lifecycle!({});
methods!({
pub fn set_icon_texture(
&self,
ctx: &mut ScriptContext<'_, API>,
texture: TextureID,
) -> bool {
let icon = with_state!(ctx.run, PatternPlayer, ctx.id, |state| state.icon).unwrap_or_default();
if icon.is_nil() || texture.is_nil() {
return false;
}
with_node_mut!(ctx.run, Sprite2D, icon, |sprite| {
sprite.texture = texture;
})
.is_some()
}
pub fn add_score(&self, ctx: &mut ScriptContext<'_, API>, amount: i32) -> i32 {
let result = with_state_mut!(ctx.run, PatternPlayer, ctx.id, |state| {
state.score += amount.max(0);
(state.score, state.icon)
});
let Some((score, icon)) = result else {
return 0;
};
if !icon.is_nil() {
with_node_mut!(ctx.run, Sprite2D, icon, |sprite| {
let scale = 0.75 + score.min(10) as f32 * 0.03;
sprite.transform.scale = Vector2::new(scale, scale);
});
}
signal_emit!(
ctx.run,
signal!("score_changed"),
params![score]
);
score
}
});