1
1
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 } ;
7
3
use godot:: prelude:: * ;
8
4
use std:: f32:: consts:: FRAC_PI_6 ;
9
5
@@ -34,109 +30,74 @@ pub struct Player {
34
30
#[ godot_api]
35
31
impl ICharacterBody3D for Player {
36
32
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
44
33
let mut direction = Vector3 :: ZERO ;
45
34
46
35
let input = Input :: singleton ( ) ;
47
36
48
- // if Input.is_action_pressed("move_right"):
49
37
if input. is_action_pressed ( "move_right" ) {
50
- //direction.x += 1
51
38
direction += Vector3 :: RIGHT ;
52
39
}
53
- // if Input.is_action_pressed("move_left"):
54
40
if input. is_action_pressed ( "move_left" ) {
55
- //direction.x -= 1
56
41
direction += Vector3 :: LEFT ;
57
42
}
58
- // if Input.is_action_pressed("move_back"):
59
43
if input. is_action_pressed ( "move_back" ) {
60
- // direction.z += 1
61
44
direction += Vector3 :: BACK ;
62
45
}
63
- // if Input.is_action_pressed("move_forward"):
64
46
if input. is_action_pressed ( "move_forward" ) {
65
- // direction.z -= 1
66
47
direction += Vector3 :: FORWARD ;
67
48
}
68
49
69
- // if direction != Vector3.ZERO:
70
50
if direction != Vector3 :: ZERO {
71
51
// In the lines below, we turn the character when moving and make the animation play faster.
72
- //direction = direction.normalized()
73
52
direction = direction. normalized ( ) ;
74
53
75
54
// take the pivot node and rotate it to face the direction we're moving in
76
55
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
78
58
pivot. set_basis ( Basis :: looking_at ( -direction, Vector3 :: UP , true ) ) ;
79
- // $AnimationPlayer.speed_scale = 4
80
59
self . base ( )
81
60
. get_node_as :: < AnimationPlayer > ( "AnimationPlayer" )
82
61
. set_speed_scale ( 4.0 ) ;
83
62
} else {
84
- // $AnimationPlayer.speed_scale = 1
85
63
self . base ( )
86
64
. get_node_as :: < AnimationPlayer > ( "AnimationPlayer" )
87
65
. set_speed_scale ( 1.0 ) ;
88
66
}
89
67
// Ground Velocity
90
- // velocity.x = direction.x * speed
91
68
self . target_velocity . x = direction. x * self . speed ;
92
-
93
- //velocity.z = direction.z * speed
94
69
self . target_velocity . z = direction. z * self . speed ;
95
70
96
71
// jumping
97
- // if is_on_floor() and Input.is_action_just_pressed("jump"):
98
72
if self . base ( ) . is_on_floor ( ) && input. is_action_just_pressed ( "jump" ) {
99
- //velocity.y += jump_impulse
100
73
self . target_velocity . y = self . jump_impulse ;
101
74
}
102
75
// We apply gravity every frame so the character always collides with the ground when moving.
103
76
// This is necessary for the is_on_floor() function to work as a body can always detect
104
77
// the floor, walls, etc. when a collision happens the same frame.
105
78
if !self . base ( ) . is_on_floor ( ) {
106
- // velocity.y -= fall_acceleration * delta
107
79
self . target_velocity . y -= self . fall_acceleration * _delta as f32 ;
108
80
}
109
81
// moving the Character
110
82
let velocity = self . target_velocity ;
111
83
self . base_mut ( ) . set_velocity ( velocity) ;
112
- // move_and_slide()
113
84
self . base_mut ( ) . move_and_slide ( ) ;
114
85
115
86
// Here, we check if we landed on top of a mob and if so, we kill it and bounce.
116
87
// With move_and_slide(), Godot makes the body move sometimes multiple times in a row to
117
88
// smooth out the character's motion. So we have to loop over all collisions that may have
118
89
// happened.
119
90
// If there are no "slides" this frame, the loop below won't run.
120
- // for index in range(get_slide_collision_count()):
121
91
for index in 0 ..self . base ( ) . get_slide_collision_count ( ) {
122
- // var collision = get_slide_collision(index)
123
92
let collision = self . base_mut ( ) . get_slide_collision ( index) . unwrap ( ) ;
124
-
125
- // if collision.get_collider().is_in_group("mob"):
126
93
if let Some ( collider) = collision. get_collider ( ) {
127
94
if let Ok ( node) = collider. try_cast :: < Node3D > ( ) {
128
95
if node. is_in_group ( "mob" ) {
129
- // var mob = collision.get_collider()
130
96
let mut mob = collision. get_collider ( ) . unwrap ( ) . cast :: < Mob > ( ) ;
131
-
132
- // if Vector3.UP.dot(collision.get_normal()) > 0.1:
133
97
if Vector3 :: UP . dot ( collision. get_normal ( ) ) > 0.1 {
134
- // mob.squash()
135
98
mob. bind_mut ( ) . squash ( ) ;
136
99
137
- // velocity.y = bounce_impulse
138
100
self . target_velocity . y = self . bounce_impulse ;
139
-
140
101
// Prevent this block from running more than once,
141
102
// which would award the player more than 1 point for squashing a single mob.
142
103
break ;
@@ -146,7 +107,6 @@ impl ICharacterBody3D for Player {
146
107
}
147
108
}
148
109
// This makes the character follow a nice arc when jumping
149
- // $Pivot.rotation.x = PI / 6 * velocity.y / jump_impulse
150
110
let mut pivot = self . base ( ) . get_node_as :: < Node3D > ( "Pivot" ) ;
151
111
let mut pivot_rotation = pivot. get_rotation ( ) ;
152
112
pivot_rotation. x = FRAC_PI_6 * self . base ( ) . get_velocity ( ) . y / self . jump_impulse ;
@@ -156,16 +116,12 @@ impl ICharacterBody3D for Player {
156
116
157
117
#[ godot_api]
158
118
impl Player {
159
- // signal hit
160
119
#[ signal]
161
120
pub fn hit ( ) ;
162
121
163
122
#[ func]
164
123
pub fn die ( & mut self ) {
165
- // hit.emit()
166
124
self . signals ( ) . hit ( ) . emit ( ) ;
167
-
168
- // queue_free()
169
125
self . base_mut ( ) . queue_free ( ) ;
170
126
}
171
127
@@ -176,7 +132,6 @@ impl Player {
176
132
. get_node_as :: < CollisionShape3D > ( "CollisionShape3D" ) ;
177
133
collision_shape. set_deferred ( "disabled" , & true . to_variant ( ) ) ;
178
134
179
- // die()
180
135
self . die ( ) ;
181
136
}
182
137
}
0 commit comments