@@ -2,19 +2,13 @@ use crate::mob;
2
2
use crate :: player;
3
3
use crate :: scorelabel;
4
4
5
- use godot:: classes:: ColorRect ;
6
- use godot:: classes:: InputEvent ;
7
- use godot:: classes:: PathFollow3D ;
8
- use godot:: classes:: Timer ;
5
+ use godot:: classes:: { ColorRect , InputEvent , PathFollow3D , Timer } ;
9
6
use godot:: prelude:: * ;
10
7
use rand:: Rng ;
11
8
12
- // Deriving GodotClass makes the class available to Godot.
13
- // extends Node
14
9
#[ derive( GodotClass ) ]
15
10
#[ class( init, base=Node ) ]
16
11
pub struct MainScene {
17
- // export(PackedScene) var mob_scene
18
12
#[ export]
19
13
mob_scene : OnEditor < Gd < PackedScene > > ,
20
14
@@ -30,21 +24,17 @@ pub struct MainScene {
30
24
#[ godot_api]
31
25
impl INode for MainScene {
32
26
fn ready ( & mut self ) {
33
- // $UserInterface/Retry.hide()
34
27
self . base ( )
35
28
. get_node_as :: < ColorRect > ( "UserInterface/Retry" )
36
29
. hide ( ) ;
37
30
}
38
31
fn unhandled_input ( & mut self , event : Gd < InputEvent > ) {
39
- // if event.is_action_pressed("ui_accept") and $UserInterface/Retry.visible:
40
32
if event. is_action_pressed ( "ui_accept" )
41
33
&& self
42
34
. base ( )
43
35
. get_node_as :: < ColorRect > ( "UserInterface/Retry" )
44
36
. is_visible ( )
45
37
{
46
- // warning-ignore:return_value_discarded
47
- // get_tree().reload_current_scene()
48
38
self . base ( ) . get_tree ( ) . unwrap ( ) . reload_current_scene ( ) ;
49
39
}
50
40
}
@@ -54,42 +44,36 @@ impl MainScene {
54
44
#[ func]
55
45
fn on_mob_timer_timeout ( & mut self ) {
56
46
// Create mob instance
57
- // var mob_spawn_location = get_node("SpawnPath/SpawnLocation")
58
47
let mut mob_spawn_location = self
59
48
. base ( )
60
49
. get_node_as :: < PathFollow3D > ( "SpawnPath/SpawnLocation" ) ;
61
50
62
51
// Choose a random location on the SpawnPath.
63
52
// Set random progress using proper rng
64
- // mob_spawn_location.progress_ratio = randf()
65
53
mob_spawn_location. set_progress_ratio ( rand:: rng ( ) . random_range ( 0.0 ..=1.0 ) ) ;
66
54
67
55
// Communicate the spawn location and the player's location to the mob.
68
- // var player_position = $Player.position
69
56
let player_position = self
70
57
. base ( )
71
58
. get_node_as :: < player:: Player > ( "Player" )
72
59
. get_position ( ) ;
73
60
74
- // var mob = mob_scene.instantiate()
75
61
let mut mob = self . mob_scene . instantiate_as :: < mob:: Mob > ( ) ;
76
62
77
- // mob.initialize(mob_spawn_location.position, player_position)
78
63
mob. bind_mut ( )
79
64
. initialize ( mob_spawn_location. get_position ( ) , player_position) ;
80
65
81
- // mob.squashed.connect($UserInterface/ScoreLabel._on_mob_squashed.bind())
82
- mob. connect ( "squashed" , & self . user_interface . callable ( "on_mob_squashed" ) ) ;
83
-
84
- // add_child(mob)
66
+ // Spawn the mob by adding it to the Main scene.
85
67
self . base_mut ( ) . add_child ( & mob) ;
68
+
69
+ // We connect the mob to the score label to update the score upon squashing a mob.
70
+ mob. connect ( "squashed" , & self . user_interface . callable ( "on_mob_squashed" ) ) ;
86
71
}
87
72
88
73
#[ func]
89
74
pub fn on_player_hit ( & mut self ) {
90
- // $MobTimer.stop()
91
75
self . mob_timer . stop ( ) ;
92
- // $UserInterface/Retry.show()
76
+
93
77
self . base ( )
94
78
. get_node_as :: < ColorRect > ( "UserInterface/Retry" )
95
79
. show ( ) ;
0 commit comments