1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
|
use std::borrow::Borrow;
use gdnative::api::*;
use gdnative::prelude::*;
/// the input state for the player
enum InputState {
Default,
Shooting,
Moving
}
type SpatialRef = Option<Ref<Spatial>>;
/// the basic die used by the player
#[derive(NativeClass)]
#[inherit(Spatial)]
#[register_with(Self::register_builder)]
pub struct BasicDie {
#[property(path="camera/camera_clamp")]
camera_clamp: Vector2,
#[property(path="shooting/max_force")]
max_force: f32,
#[property(path="shooting/up_angle")]
up_angle: f32,
#[property(path="shooting/stopping_velocity")]
stopping_velocity: f32,
#[property(path="shooting/stopping_min_milliseconds")]
stopping_min_ms: i64,
#[property(path="input/camera_mouse_sensitivity")]
mouse_sensitivity: Vector2,
#[property(path="input/shoot_sensitivity")]
shoot_sensitivity: f32,
input_state: InputState,
current_force: f32,
last_shot_time: i64,
action_shooting: String,
node_die: SpatialRef,
node_camera_root: SpatialRef,
node_camera_arm_horizontal: SpatialRef,
node_camera_arm_vertical: SpatialRef,
node_camera: SpatialRef,
}
#[methods]
impl BasicDie {
// Register the builder for methods, properties and/or signals.
fn register_builder(_builder: &ClassBuilder<Self>) {
godot_print!("BasicDie builder is registered!");
}
fn new(_owner: &Spatial) -> Self {
BasicDie {
camera_clamp: Vector2 { x: 0.0, y: 0.0 },
max_force: 0.0,
up_angle: 5.0,
stopping_velocity: 0.0,
stopping_min_ms: 1000,
mouse_sensitivity: Vector2 { x: 1.0, y: 1.0 },
shoot_sensitivity: 1.0,
input_state: InputState::Default,
current_force: 0.0,
last_shot_time: 0,
action_shooting: String::from("mouse_btn_left"),
node_die: None,
node_camera_root: None,
node_camera_arm_horizontal: None,
node_camera_arm_vertical: None,
node_camera: None,
}
}
#[export]
unsafe fn _ready(&mut self, owner: &Spatial) {
Input::godot_singleton().set_mouse_mode(Input::MOUSE_MODE_CAPTURED);
owner.set_physics_process(true);
self.last_shot_time = OS::godot_singleton().get_ticks_msec();
let search_node = | parent: &SpatialRef, name: String | {
let parent_node = match parent {
Some(node) => node.assume_safe().as_ref(),
None => owner
};
match parent_node.get_node(NodePath::from_str(&name)) {
None => { godot_warn!("Could not find {}.", name); None },
Some(node) => {
let save_node = node.assume_safe();
match save_node.cast::<Spatial>() {
None => { godot_warn!("{} was not of type 'Spatial'.", name); None },
Some(casted) => Some(casted.claim())
}
}
}
};
// look for the camera and arm nodes
self.node_camera_root = search_node(&None, String::from("Camera"));
self.node_camera_arm_horizontal = search_node(&self.node_camera_root, String::from("CameraArmHorizontal"));
self.node_camera_arm_vertical = search_node(&self.node_camera_arm_horizontal, String::from("CameraArmVertical"));
self.node_camera = search_node(&self.node_camera_arm_vertical, String::from("Camera"));
godot_print!("{:?}", self.node_camera_root);
godot_print!("{:?}", self.node_camera_arm_horizontal);
godot_print!("{:?}", self.node_camera_arm_vertical);
godot_print!("{:?}", self.node_camera);
self.node_die = search_node(&None, String::from("W8"));
godot_print!("Player is ready");
}
#[export]
unsafe fn _physics_process(&mut self, _owner: &Spatial, _delta: f64) {
let die = match self.node_die {
None => { godot_warn!("No W8 assigned."); return; },
Some(node) => match node.assume_safe().cast::<RigidBody>() {
None => { godot_warn!("W8 is not a RigidBody."); return; },
Some(rb) => rb
}
};
// let the cam follow the die
match self.node_camera_root {
None => { godot_warn!("No W8 assigned."); },
Some(cam) => {
cam.assume_safe().set_translation(die.translation());
}
}
// detect if the die stops moving
let delta_ms = OS::godot_singleton().get_ticks_msec() - self.last_shot_time;
if matches!(self.input_state, InputState::Moving)
&& (delta_ms > self.stopping_min_ms) {
// check if the velocity is less than the threshold and change input state in that case
let current_vel = die.linear_velocity().length();
if current_vel <= self.stopping_velocity {
self.input_state = InputState::Default;
godot_print!("Die stopped moving at velocity {} after {} ms", current_vel, delta_ms);
}
};
}
#[export]
unsafe fn _input(&mut self, _owner: &Spatial, event: Ref<InputEvent>) {
self.general_input(event.borrow());
match self.input_state {
InputState::Default => self.default_input(event),
InputState::Shooting => self.shooting_input(event),
InputState::Moving => self.moving_input(event),
}
}
/// this input method will always be called, regardless of the input state
unsafe fn general_input(&mut self, event: &Ref<InputEvent>) {
let save_event = event.assume_safe();
// rotate camera horizontally
let mouse_event = save_event.cast::<InputEventMouseMotion>(); // get the input as mouse input
match mouse_event {
Some(motion_event) => {
let x_mov = motion_event.relative().x * self.mouse_sensitivity.x;
self.rotate_cam_horizontal(x_mov);
},
_ => {}
}
let key_event = save_event.cast::<InputEventKey>();
match key_event {
Some(key_event) => {
if key_event.scancode() == GlobalConstants::KEY_ESCAPE {
Input::godot_singleton().set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
}
},
_ => {}
}
}
/// this input method will be called when looking around, before taking a shot
unsafe fn default_input(&mut self, event: Ref<InputEvent>) {
let save_event = event.assume_safe();
// left mouse button was pressed => switch to shooting mode
if save_event.is_action_pressed(GodotString::from_str(&self.action_shooting), false, false) {
godot_print!("mouse_button, switching to shooting mode");
self.input_state = InputState::Shooting;
return;
}
// rotate camera vertically
let mouse_event = save_event.cast::<InputEventMouseMotion>(); // get the input as mouse input
match mouse_event {
Some(motion_event) => {
let y_mov = -motion_event.relative().y * self.mouse_sensitivity.y;
self.rotate_cam_vertical(y_mov);
},
_ => {}
};
}
/// this input method will be called when player is currently taking a shot
unsafe fn shooting_input(&mut self, event: Ref<InputEvent>) {
let save_event = event.assume_safe();
// mouse released, shoot
if save_event.is_action_released(GodotString::from_str(&self.action_shooting), false) {
self.input_state = InputState::Moving;
self.shoot();
self.current_force = 0.0;
return;
}
// charge shot with vertical mouse movement
let mouse_event = save_event.cast::<InputEventMouseMotion>(); // get the input as mouse input
match mouse_event {
Some(motion_event) => {
let y_mov = motion_event.relative().y * self.shoot_sensitivity;
self.current_force = match self.current_force + y_mov {
x if x < 0.0 => 0.0,
x if x > self.max_force => self.max_force,
x => x
};
godot_print!("current force: {}", self.current_force);
},
_ => {}
};
}
/// this input method will be called when player is moving
unsafe fn moving_input(&mut self, event: Ref<InputEvent>) {
let save_event = event.assume_safe();
// rotate camera vertically
let mouse_event = save_event.cast::<InputEventMouseMotion>(); // get the input as mouse input
match mouse_event {
Some(motion_event) => {
let y_mov = motion_event.relative().y * self.mouse_sensitivity.y;
self.rotate_cam_vertical(y_mov);
},
_ => {}
};
}
unsafe fn rotate_cam_horizontal(&mut self, input: f32) {
// make sure the arm exists
match self.node_camera_arm_horizontal {
Some(arm) => {
// rotate the horizontal camera arm
let save_arm = arm.assume_safe();
save_arm.rotate_object_local(Vector3 {x: 0.0, y: 1.0, z: 0.0}, input as f64)
},
_ => godot_warn!("No horizontal camera arm assigned.")
}
}
unsafe fn rotate_cam_vertical(&mut self, input: f32) {
// make sure the camera arm actually exists
match self.node_camera_arm_vertical {
Some(arm) => {
// check for the current rotation
let save_arm = arm.assume_safe();
let current_rot = save_arm.rotation();
// clamp the rotation
if current_rot.x + input > self.camera_clamp.x || current_rot.x + input <= self.camera_clamp.y {
return;
}
// actually rotate if possible
let save_arm = arm.assume_safe();
save_arm.rotate_object_local(Vector3 {x: 1.0, y: 0.0, z: 0.0}, input as f64)
},
_ => godot_warn!("No vertical camera arm assigned")
}
}
unsafe fn shoot(&mut self) {
// make sure the camera actually exists
let impulse_dir = match self.node_camera {
None => { godot_warn!("No camera assigned!"); return; },
Some(cam) => {
// get the forward vector of the camera setting the up angle to the defined value in the editor
let mut forward_vector = -cam.assume_safe().global_transform().basis.c().normalized();
forward_vector.y = self.up_angle;
// calculate the impulse force
forward_vector.normalized() * self.current_force
}
};
// get the die
let die = match self.node_die {
None => { godot_warn!("No W8 assigned."); return; },
Some(node) =>
match node.assume_safe().cast::<RigidBody>() {
None => { godot_warn!("W8 is not a RigidBody."); return; },
Some(rb) => rb
}
};
// actually add the force
die.apply_impulse(die.transform().origin, impulse_dir);
self.last_shot_time = OS::godot_singleton().get_ticks_msec();
}
}
|