Skip to content

Commit b965e31

Browse files
committed
refactor: consolidate imports and remove redundant comments
Consolidate multiple import statements into single lines for better readability and remove redundant comments to clean up the codebase. This improves maintainability and reduces clutter without altering the functionality.
1 parent 832f2bd commit b965e31

File tree

3 files changed

+5
-55
lines changed

3 files changed

+5
-55
lines changed

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
use godot::classes::AnimationPlayer;
2-
use godot::classes::CharacterBody3D;
3-
use godot::classes::ICharacterBody3D;
1+
use godot::classes::{AnimationPlayer, CharacterBody3D, ICharacterBody3D};
42
use godot::prelude::*;
53
use rand::Rng;
64
use std::f32::consts::PI;

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

Lines changed: 3 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
use crate::mob::Mob;
2-
use godot::classes::AnimationPlayer;
3-
use godot::classes::CharacterBody3D;
4-
use godot::classes::CollisionShape3D;
5-
use godot::classes::ICharacterBody3D;
6-
use godot::classes::Input;
2+
use godot::classes::{AnimationPlayer, CharacterBody3D, CollisionShape3D, ICharacterBody3D, Input};
73
use godot::prelude::*;
84
use std::f32::consts::FRAC_PI_6;
95

@@ -34,109 +30,74 @@ pub struct Player {
3430
#[godot_api]
3531
impl ICharacterBody3D for Player {
3632
fn physics_process(&mut self, _delta: f64) {
37-
/*Here, instead of _process(), we're going to make all
38-
calculations using the _physics_process() virtual function.
39-
It's designed specifically for physics-related code like
40-
moving a kinematic or rigid body.
41-
It updates the node using fixed time intervals.*/
42-
43-
// var direction = Vector3.ZERO
4433
let mut direction = Vector3::ZERO;
4534

4635
let input = Input::singleton();
4736

48-
// if Input.is_action_pressed("move_right"):
4937
if input.is_action_pressed("move_right") {
50-
//direction.x += 1
5138
direction += Vector3::RIGHT;
5239
}
53-
// if Input.is_action_pressed("move_left"):
5440
if input.is_action_pressed("move_left") {
55-
//direction.x -= 1
5641
direction += Vector3::LEFT;
5742
}
58-
// if Input.is_action_pressed("move_back"):
5943
if input.is_action_pressed("move_back") {
60-
// direction.z += 1
6144
direction += Vector3::BACK;
6245
}
63-
// if Input.is_action_pressed("move_forward"):
6446
if input.is_action_pressed("move_forward") {
65-
// direction.z -= 1
6647
direction += Vector3::FORWARD;
6748
}
6849

69-
// if direction != Vector3.ZERO:
7050
if direction != Vector3::ZERO {
7151
// In the lines below, we turn the character when moving and make the animation play faster.
72-
//direction = direction.normalized()
7352
direction = direction.normalized();
7453

7554
// take the pivot node and rotate it to face the direction we're moving in
7655
let mut pivot = self.base_mut().get_node_as::<Node3D>("Pivot");
77-
// $Pivot.basis = Basis.looking_at(direction) (GDScript)
56+
57+
// Setting the basis property will affect the rotation of the node
7858
pivot.set_basis(Basis::looking_at(-direction, Vector3::UP, true));
79-
// $AnimationPlayer.speed_scale = 4
8059
self.base()
8160
.get_node_as::<AnimationPlayer>("AnimationPlayer")
8261
.set_speed_scale(4.0);
8362
} else {
84-
// $AnimationPlayer.speed_scale = 1
8563
self.base()
8664
.get_node_as::<AnimationPlayer>("AnimationPlayer")
8765
.set_speed_scale(1.0);
8866
}
8967
// Ground Velocity
90-
// velocity.x = direction.x * speed
9168
self.target_velocity.x = direction.x * self.speed;
92-
93-
//velocity.z = direction.z * speed
9469
self.target_velocity.z = direction.z * self.speed;
9570

9671
// jumping
97-
// if is_on_floor() and Input.is_action_just_pressed("jump"):
9872
if self.base().is_on_floor() && input.is_action_just_pressed("jump") {
99-
//velocity.y += jump_impulse
10073
self.target_velocity.y = self.jump_impulse;
10174
}
10275
// We apply gravity every frame so the character always collides with the ground when moving.
10376
// This is necessary for the is_on_floor() function to work as a body can always detect
10477
// the floor, walls, etc. when a collision happens the same frame.
10578
if !self.base().is_on_floor() {
106-
// velocity.y -= fall_acceleration * delta
10779
self.target_velocity.y -= self.fall_acceleration * _delta as f32;
10880
}
10981
// moving the Character
11082
let velocity = self.target_velocity;
11183
self.base_mut().set_velocity(velocity);
112-
// move_and_slide()
11384
self.base_mut().move_and_slide();
11485

11586
// Here, we check if we landed on top of a mob and if so, we kill it and bounce.
11687
// With move_and_slide(), Godot makes the body move sometimes multiple times in a row to
11788
// smooth out the character's motion. So we have to loop over all collisions that may have
11889
// happened.
11990
// If there are no "slides" this frame, the loop below won't run.
120-
// for index in range(get_slide_collision_count()):
12191
for index in 0..self.base().get_slide_collision_count() {
122-
// var collision = get_slide_collision(index)
12392
let collision = self.base_mut().get_slide_collision(index).unwrap();
124-
125-
// if collision.get_collider().is_in_group("mob"):
12693
if let Some(collider) = collision.get_collider() {
12794
if let Ok(node) = collider.try_cast::<Node3D>() {
12895
if node.is_in_group("mob") {
129-
// var mob = collision.get_collider()
13096
let mut mob = collision.get_collider().unwrap().cast::<Mob>();
131-
132-
// if Vector3.UP.dot(collision.get_normal()) > 0.1:
13397
if Vector3::UP.dot(collision.get_normal()) > 0.1 {
134-
// mob.squash()
13598
mob.bind_mut().squash();
13699

137-
// velocity.y = bounce_impulse
138100
self.target_velocity.y = self.bounce_impulse;
139-
140101
// Prevent this block from running more than once,
141102
// which would award the player more than 1 point for squashing a single mob.
142103
break;
@@ -146,7 +107,6 @@ impl ICharacterBody3D for Player {
146107
}
147108
}
148109
// This makes the character follow a nice arc when jumping
149-
// $Pivot.rotation.x = PI / 6 * velocity.y / jump_impulse
150110
let mut pivot = self.base().get_node_as::<Node3D>("Pivot");
151111
let mut pivot_rotation = pivot.get_rotation();
152112
pivot_rotation.x = FRAC_PI_6 * self.base().get_velocity().y / self.jump_impulse;
@@ -156,16 +116,12 @@ impl ICharacterBody3D for Player {
156116

157117
#[godot_api]
158118
impl Player {
159-
// signal hit
160119
#[signal]
161120
pub fn hit();
162121

163122
#[func]
164123
pub fn die(&mut self) {
165-
// hit.emit()
166124
self.signals().hit().emit();
167-
168-
// queue_free()
169125
self.base_mut().queue_free();
170126
}
171127

@@ -176,7 +132,6 @@ impl Player {
176132
.get_node_as::<CollisionShape3D>("CollisionShape3D");
177133
collision_shape.set_deferred("disabled", &true.to_variant());
178134

179-
// die()
180135
self.die();
181136
}
182137
}

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
use godot::classes::Control;
2-
use godot::classes::Label;
1+
use godot::classes::{Control, Label};
32
use godot::prelude::*;
43

54
#[derive(GodotClass)]
@@ -13,10 +12,8 @@ pub struct UserInterface {
1312
impl UserInterface {
1413
#[func]
1514
pub fn on_mob_squashed(&mut self) {
16-
// score += 1
1715
self.score += 1.0;
1816

19-
// text = "Score: %s" % score
2017
let mut label = self.base().get_node_as::<Label>("ScoreLabel");
2118
label.set_text(format!("Score: {}", self.score).as_str());
2219
}

0 commit comments

Comments
 (0)