Skip to content

Commit

Permalink
Rename control schemes
Browse files Browse the repository at this point in the history
  • Loading branch information
jspahrsummers committed Jun 25, 2024
1 parent 9092fc8 commit f4e991d
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 31 deletions.
6 changes: 3 additions & 3 deletions gui/control_scheme_button.gd
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ func _ready() -> void:

func _on_preferences_updated() -> void:
var popup := get_popup()
popup.set_item_checked(0, !UserPreferencesSystem.use_joystick)
popup.set_item_checked(1, UserPreferencesSystem.use_joystick)
for value in UserPreferencesSystem.ControlScheme.values():
popup.set_item_checked(value, UserPreferencesSystem.control_scheme == value)

func _on_menu_index_pressed(index: int) -> void:
UserPreferencesSystem.set_use_joystick(index == 1)
UserPreferencesSystem.set_control_scheme(index)
4 changes: 2 additions & 2 deletions main.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,11 @@ layout_mode = 2
text = "Control scheme"
flat = false
item_count = 2
popup/item_0/text = "Default"
popup/item_0/text = "Relative"
popup/item_0/checkable = 2
popup/item_0/checked = true
popup/item_0/id = 0
popup/item_1/text = "Joystick"
popup/item_1/text = "Absolute"
popup/item_1/checkable = 2
popup/item_1/id = 1
script = ExtResource("11_b5oou")
Expand Down
49 changes: 26 additions & 23 deletions ships/player.gd
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
extends Ship
class_name Player

const JOYSTICK_THRUST_WITHIN_RAD = 0.1745
const ABSOLUTE_DIRECTION_TOLERANCE_RAD = 0.1745

func _ready() -> void:
super()
Expand Down Expand Up @@ -53,21 +53,24 @@ func _process(_delta: float) -> void:
if Input.is_action_just_pressed("cycle_target"):
CombatSystem.set_targeted_ship(_next_ship_target())

func _joystick_input_direction() -> Vector3:
func _absolute_input_direction() -> Vector3:
var input_direction := Input.get_vector("move_left", "move_right", "move_up", "move_down")
return Vector3(input_direction.x, 0, input_direction.y)

func _physics_process(_delta: float) -> void:
if HyperspaceSystem.jumping:
return

if UserPreferencesSystem.use_joystick:
var desired_direction := self._joystick_input_direction()
var current_direction := - self.transform.basis.z
if desired_direction != Vector3.ZERO and desired_direction.angle_to(current_direction) <= JOYSTICK_THRUST_WITHIN_RAD:
apply_central_force(self.transform.basis * Vector3.FORWARD * self.ship_def.thrust * desired_direction.length())
elif Input.is_action_pressed("thrust"):
apply_central_force(self.transform.basis * Vector3.FORWARD * self.ship_def.thrust)
match UserPreferencesSystem.control_scheme:
UserPreferencesSystem.ControlScheme.RELATIVE:
if Input.is_action_pressed("thrust"):
apply_central_force(self.transform.basis * Vector3.FORWARD * self.ship_def.thrust)

UserPreferencesSystem.ControlScheme.ABSOLUTE:
var desired_direction := self._absolute_input_direction()
var current_direction := - self.transform.basis.z
if desired_direction != Vector3.ZERO and desired_direction.angle_to(current_direction) <= ABSOLUTE_DIRECTION_TOLERANCE_RAD:
apply_central_force(self.transform.basis * Vector3.FORWARD * self.ship_def.thrust * desired_direction.length())

if Input.is_action_pressed("fire"):
self.fire()
Expand All @@ -79,17 +82,17 @@ func _integrate_forces(state: PhysicsDirectBodyState3D) -> void:
if HyperspaceSystem.jumping:
return

if UserPreferencesSystem.use_joystick:
var desired_direction := self._joystick_input_direction()
if desired_direction == Vector3.ZERO:
return

var desired_basis = Basis.looking_at(desired_direction)
state.transform.basis = state.transform.basis.slerp(desired_basis, self.ship_def.torque * state.get_step())
else:
if Input.is_action_pressed("turn_left"):
state.angular_velocity = Vector3.ZERO
state.transform.basis = state.transform.basis.rotated(Vector3.DOWN, -self.ship_def.torque * state.get_step())
if Input.is_action_pressed("turn_right"):
state.angular_velocity = Vector3.ZERO
state.transform.basis = state.transform.basis.rotated(Vector3.DOWN, self.ship_def.torque * state.get_step())
match UserPreferencesSystem.control_scheme:
UserPreferencesSystem.ControlScheme.RELATIVE:
if Input.is_action_pressed("turn_left"):
state.angular_velocity = Vector3.ZERO
state.transform.basis = state.transform.basis.rotated(Vector3.DOWN, -self.ship_def.torque * state.get_step())
if Input.is_action_pressed("turn_right"):
state.angular_velocity = Vector3.ZERO
state.transform.basis = state.transform.basis.rotated(Vector3.DOWN, self.ship_def.torque * state.get_step())

UserPreferencesSystem.ControlScheme.ABSOLUTE:
var desired_direction := self._absolute_input_direction()
if desired_direction != Vector3.ZERO:
var desired_basis = Basis.looking_at(desired_direction)
state.transform.basis = state.transform.basis.slerp(desired_basis, self.ship_def.torque * state.get_step())
8 changes: 5 additions & 3 deletions systems/user_preferences_system.gd
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
extends Node

enum ControlScheme {RELATIVE = 0, ABSOLUTE = 1}

signal preferences_updated

var use_joystick: bool = false
var control_scheme := ControlScheme.RELATIVE

func set_use_joystick(value: bool) -> void:
self.use_joystick = value
func set_control_scheme(value: ControlScheme) -> void:
self.control_scheme = value
self.emit_signal("preferences_updated")

0 comments on commit f4e991d

Please sign in to comment.