car controlled by player
This commit is contained in:
parent
5310a1a763
commit
7db1a1b2b0
@ -26,9 +26,14 @@ scale = Vector2(1.99747, 0.147406)
|
|||||||
texture = ExtResource("4_fc0e3")
|
texture = ExtResource("4_fc0e3")
|
||||||
|
|
||||||
[node name="Car" parent="." instance=ExtResource("1_80nbo")]
|
[node name="Car" parent="." instance=ExtResource("1_80nbo")]
|
||||||
|
controlled_by_player = true
|
||||||
script = ExtResource("4_7jktm")
|
script = ExtResource("4_7jktm")
|
||||||
|
|
||||||
[node name="Camera2D" type="Camera2D" parent="Car"]
|
[node name="Camera2D" type="Camera2D" parent="Car"]
|
||||||
position = Vector2(0, -1)
|
position = Vector2(0, -1)
|
||||||
scale = Vector2(0.1, 0.1)
|
scale = Vector2(0.1, 0.1)
|
||||||
zoom = Vector2(0.36, 0.36)
|
zoom = Vector2(0.36, 0.36)
|
||||||
|
|
||||||
|
[node name="Car2" parent="." instance=ExtResource("1_80nbo")]
|
||||||
|
position = Vector2(512, -1536)
|
||||||
|
rotation = -1.0472
|
||||||
|
BIN
radio/Cassette FM/Groove Machine.mp3
Normal file
BIN
radio/Cassette FM/Groove Machine.mp3
Normal file
Binary file not shown.
BIN
radio/Cassette FM/Road Cassette.mp3
Normal file
BIN
radio/Cassette FM/Road Cassette.mp3
Normal file
Binary file not shown.
BIN
radio/Cassette FM/Trumpeteer.mp3
Normal file
BIN
radio/Cassette FM/Trumpeteer.mp3
Normal file
Binary file not shown.
BIN
radio/noir/Shadows in the Glow.mp3
Normal file
BIN
radio/noir/Shadows in the Glow.mp3
Normal file
Binary file not shown.
@ -28,6 +28,9 @@ struct Car {
|
|||||||
/// Unit: radians
|
/// Unit: radians
|
||||||
max_steering_angle: f32,
|
max_steering_angle: f32,
|
||||||
|
|
||||||
|
#[export]
|
||||||
|
controlled_by_player: bool,
|
||||||
|
|
||||||
base: Base<RigidBody2D>,
|
base: Base<RigidBody2D>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -47,6 +50,14 @@ impl Wheel {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct UserInput {
|
||||||
|
move_forward: bool,
|
||||||
|
move_backward: bool,
|
||||||
|
turn_left: bool,
|
||||||
|
turn_right: bool,
|
||||||
|
handbrake: bool,
|
||||||
|
}
|
||||||
|
|
||||||
#[godot_api]
|
#[godot_api]
|
||||||
impl IRigidBody2D for Car {
|
impl IRigidBody2D for Car {
|
||||||
fn init(base: Base<RigidBody2D>) -> Self {
|
fn init(base: Base<RigidBody2D>) -> Self {
|
||||||
@ -72,6 +83,9 @@ impl IRigidBody2D for Car {
|
|||||||
y_distance: 200.0,
|
y_distance: 200.0,
|
||||||
has_grip: true,
|
has_grip: true,
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// Exported
|
||||||
|
controlled_by_player: Default::default(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -90,14 +104,17 @@ impl IRigidBody2D for Car {
|
|||||||
self.base_mut().set_angular_velocity(0.0);
|
self.base_mut().set_angular_velocity(0.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
let input = Input::singleton();
|
let user_input = if self.controlled_by_player {
|
||||||
let move_forward = input.is_action_pressed("move_forward");
|
self.user_input()
|
||||||
let move_backward = input.is_action_pressed("move_backward");
|
} else {
|
||||||
let turn_left = input.is_action_pressed("turn_left");
|
UserInput {
|
||||||
let turn_right = input.is_action_pressed("turn_right");
|
move_forward: false,
|
||||||
|
move_backward: false,
|
||||||
// TODO: Handbrake
|
turn_left: false,
|
||||||
let _handbrake = input.is_action_pressed("handbrake");
|
turn_right: false,
|
||||||
|
handbrake: false,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
let rotation = self.base().get_rotation();
|
let rotation = self.base().get_rotation();
|
||||||
|
|
||||||
@ -106,7 +123,7 @@ impl IRigidBody2D for Car {
|
|||||||
let front_wheel_velocity = self.wheel_velocity(&self.front_wheel);
|
let front_wheel_velocity = self.wheel_velocity(&self.front_wheel);
|
||||||
let rear_wheel_velocity = self.wheel_velocity(&self.rear_wheel);
|
let rear_wheel_velocity = self.wheel_velocity(&self.rear_wheel);
|
||||||
|
|
||||||
let front_wheel_angle = match (turn_left, turn_right) {
|
let front_wheel_angle = match (user_input.turn_left, user_input.turn_right) {
|
||||||
(true, false) => -self.steering_angle(),
|
(true, false) => -self.steering_angle(),
|
||||||
(false, true) => self.steering_angle(),
|
(false, true) => self.steering_angle(),
|
||||||
(true, true) | (false, false) => 0.0,
|
(true, true) | (false, false) => 0.0,
|
||||||
@ -119,7 +136,7 @@ impl IRigidBody2D for Car {
|
|||||||
let rear_wheels_direction = forward;
|
let rear_wheels_direction = forward;
|
||||||
|
|
||||||
// Engine/thrust force
|
// Engine/thrust force
|
||||||
let thrust_force = match (move_forward, move_backward) {
|
let thrust_force = match (user_input.move_forward, user_input.move_backward) {
|
||||||
(true, false) => self.engine_force * front_wheels_direction,
|
(true, false) => self.engine_force * front_wheels_direction,
|
||||||
(false, true) => {
|
(false, true) => {
|
||||||
if forward.dot(self.base().get_linear_velocity()) > 0.0 {
|
if forward.dot(self.base().get_linear_velocity()) > 0.0 {
|
||||||
@ -176,6 +193,25 @@ impl IRigidBody2D for Car {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Car {
|
impl Car {
|
||||||
|
fn user_input(&self) -> UserInput {
|
||||||
|
let gd_input = Input::singleton();
|
||||||
|
let move_forward = gd_input.is_action_pressed("move_forward");
|
||||||
|
let move_backward = gd_input.is_action_pressed("move_backward");
|
||||||
|
let turn_left = gd_input.is_action_pressed("turn_left");
|
||||||
|
let turn_right = gd_input.is_action_pressed("turn_right");
|
||||||
|
|
||||||
|
// TODO: Handbrake
|
||||||
|
let _handbrake = gd_input.is_action_pressed("handbrake");
|
||||||
|
|
||||||
|
UserInput {
|
||||||
|
move_forward,
|
||||||
|
move_backward,
|
||||||
|
turn_left,
|
||||||
|
turn_right,
|
||||||
|
handbrake: false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn cornering_force(&mut self, slip_angle: f32) -> Vector2 {
|
fn cornering_force(&mut self, slip_angle: f32) -> Vector2 {
|
||||||
debug_assert!(slip_angle.is_finite());
|
debug_assert!(slip_angle.is_finite());
|
||||||
let right = self.right();
|
let right = self.right();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user