@@ -5,21 +5,38 @@ use crate::player;
55use godot:: classes:: { Marker2D , PathFollow2D , RigidBody2D , Timer } ;
66use godot:: prelude:: * ;
77
8- use godot:: classes:: notify:: NodeNotification ;
98use rand:: Rng as _;
109use std:: f32:: consts:: PI ;
1110
12- // Deriving GodotClass makes the class available to Godot
11+ // Deriving GodotClass makes the class available to Godot.
1312#[ derive( GodotClass ) ]
1413#[ class( base=Node ) ]
1514pub struct Main {
16- mob_scene : Gd < PackedScene > ,
17- music : Option < Gd < AudioStreamPlayer > > ,
18- death_sound : Option < Gd < AudioStreamPlayer > > ,
15+ mob_scene : OnReady < Gd < PackedScene > > ,
16+ player : OnReady < Gd < player:: Player > > ,
17+ music : OnReady < Gd < AudioStreamPlayer > > ,
18+ death_sound : OnReady < Gd < AudioStreamPlayer > > ,
1919 score : i64 ,
2020 base : Base < Node > ,
2121}
2222
23+ #[ godot_api]
24+ impl INode for Main {
25+ fn init ( base : Base < Node > ) -> Self {
26+ // We could also initialize those manually inside ready(), but OnReady automatically defers initialization.
27+ Self {
28+ mob_scene : OnReady :: new ( || load ( "res://Mob.tscn" ) ) ,
29+ player : OnReady :: node ( "Player" ) ,
30+ music : OnReady :: node ( "Music" ) ,
31+ death_sound : OnReady :: node ( "DeathSound" ) ,
32+ score : 0 ,
33+ base,
34+ }
35+ }
36+
37+ fn ready ( & mut self ) { }
38+ }
39+
2340#[ godot_api]
2441impl Main {
2542 #[ func]
@@ -33,27 +50,26 @@ impl Main {
3350 let mut hud = self . base ( ) . get_node_as :: < Hud > ( "Hud" ) ;
3451 hud. bind_mut ( ) . show_game_over ( ) ;
3552
36- self . music ( ) . stop ( ) ;
37- self . death_sound ( ) . play ( ) ;
53+ self . music . stop ( ) ;
54+ self . death_sound . play ( ) ;
3855 }
3956
4057 #[ func]
4158 pub fn new_game ( & mut self ) {
4259 let start_position = self . base ( ) . get_node_as :: < Marker2D > ( "StartPosition" ) ;
43- let mut player = self . base ( ) . get_node_as :: < player:: Player > ( "Player" ) ;
4460 let mut start_timer = self . base ( ) . get_node_as :: < Timer > ( "StartTimer" ) ;
4561
4662 self . score = 0 ;
4763
48- player. bind_mut ( ) . start ( start_position. get_position ( ) ) ;
64+ self . player . bind_mut ( ) . start ( start_position. get_position ( ) ) ;
4965 start_timer. start ( ) ;
5066
5167 let mut hud = self . base ( ) . get_node_as :: < Hud > ( "Hud" ) ;
5268 let hud = hud. bind_mut ( ) ;
5369 hud. update_score ( self . score ) ;
5470 hud. show_message ( "Get Ready" . into ( ) ) ;
5571
56- self . music ( ) . play ( ) ;
72+ self . music . play ( ) ;
5773 }
5874
5975 #[ func]
@@ -105,36 +121,4 @@ impl Main {
105121 let mut hud = self . base ( ) . get_node_as :: < Hud > ( "Hud" ) ;
106122 hud. connect ( "start_game" , & mob. callable ( "on_start_game" ) ) ;
107123 }
108-
109- fn music ( & mut self ) -> & mut AudioStreamPlayer {
110- self . music . as_deref_mut ( ) . unwrap ( )
111- }
112-
113- fn death_sound ( & mut self ) -> & mut AudioStreamPlayer {
114- self . death_sound . as_deref_mut ( ) . unwrap ( )
115- }
116- }
117-
118- #[ godot_api]
119- impl INode for Main {
120- fn on_notification ( & mut self , _what : NodeNotification ) { }
121-
122- fn init ( base : Base < Node > ) -> Self {
123- Main {
124- mob_scene : PackedScene :: new_gd ( ) ,
125- score : 0 ,
126- base,
127- music : None ,
128- death_sound : None ,
129- }
130- }
131-
132- fn ready ( & mut self ) {
133- // Note: this is downcast during load() -- completely type-safe thanks to type inference!
134- // If the resource does not exist or has an incompatible type, this panics.
135- // There is also try_load() if you want to check whether loading succeeded.
136- self . mob_scene = load ( "res://Mob.tscn" ) ;
137- self . music = Some ( self . base ( ) . get_node_as ( "Music" ) ) ;
138- self . death_sound = Some ( self . base ( ) . get_node_as ( "DeathSound" ) ) ;
139- }
140124}
0 commit comments