Skip to content

Commit 3888ce7

Browse files
committed
refactor(main_scene): clean up code and improve readability
- Grouped imports for better organization - Removed redundant comments - Reordered mob connection logic for clarity
1 parent b965e31 commit 3888ce7

File tree

1 file changed

+6
-22
lines changed

1 file changed

+6
-22
lines changed

squash-the-creeps/rust/src/main_scene.rs

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,13 @@ use crate::mob;
22
use crate::player;
33
use crate::scorelabel;
44

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};
96
use godot::prelude::*;
107
use rand::Rng;
118

12-
// Deriving GodotClass makes the class available to Godot.
13-
// extends Node
149
#[derive(GodotClass)]
1510
#[class(init, base=Node)]
1611
pub struct MainScene {
17-
// export(PackedScene) var mob_scene
1812
#[export]
1913
mob_scene: OnEditor<Gd<PackedScene>>,
2014

@@ -30,21 +24,17 @@ pub struct MainScene {
3024
#[godot_api]
3125
impl INode for MainScene {
3226
fn ready(&mut self) {
33-
// $UserInterface/Retry.hide()
3427
self.base()
3528
.get_node_as::<ColorRect>("UserInterface/Retry")
3629
.hide();
3730
}
3831
fn unhandled_input(&mut self, event: Gd<InputEvent>) {
39-
// if event.is_action_pressed("ui_accept") and $UserInterface/Retry.visible:
4032
if event.is_action_pressed("ui_accept")
4133
&& self
4234
.base()
4335
.get_node_as::<ColorRect>("UserInterface/Retry")
4436
.is_visible()
4537
{
46-
// warning-ignore:return_value_discarded
47-
// get_tree().reload_current_scene()
4838
self.base().get_tree().unwrap().reload_current_scene();
4939
}
5040
}
@@ -54,42 +44,36 @@ impl MainScene {
5444
#[func]
5545
fn on_mob_timer_timeout(&mut self) {
5646
// Create mob instance
57-
// var mob_spawn_location = get_node("SpawnPath/SpawnLocation")
5847
let mut mob_spawn_location = self
5948
.base()
6049
.get_node_as::<PathFollow3D>("SpawnPath/SpawnLocation");
6150

6251
// Choose a random location on the SpawnPath.
6352
// Set random progress using proper rng
64-
// mob_spawn_location.progress_ratio = randf()
6553
mob_spawn_location.set_progress_ratio(rand::rng().random_range(0.0..=1.0));
6654

6755
// Communicate the spawn location and the player's location to the mob.
68-
// var player_position = $Player.position
6956
let player_position = self
7057
.base()
7158
.get_node_as::<player::Player>("Player")
7259
.get_position();
7360

74-
// var mob = mob_scene.instantiate()
7561
let mut mob = self.mob_scene.instantiate_as::<mob::Mob>();
7662

77-
// mob.initialize(mob_spawn_location.position, player_position)
7863
mob.bind_mut()
7964
.initialize(mob_spawn_location.get_position(), player_position);
8065

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.
8567
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"));
8671
}
8772

8873
#[func]
8974
pub fn on_player_hit(&mut self) {
90-
// $MobTimer.stop()
9175
self.mob_timer.stop();
92-
// $UserInterface/Retry.show()
76+
9377
self.base()
9478
.get_node_as::<ColorRect>("UserInterface/Retry")
9579
.show();

0 commit comments

Comments
 (0)