Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement controller support #65

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 64 additions & 1 deletion src/project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ _global_script_classes=[ {
"language": "GDScript",
"path": "res://spaceship/modules/fuel_tank.gd"
}, {
"base": "Reference",
"base": "CanvasLayer",
"class": "GameOver",
"language": "GDScript",
"path": "res://screens/game_over/GameOver.gd"
Expand Down Expand Up @@ -161,57 +161,120 @@ texture={

[input]

ui_focus_next={
"deadzone": 0.5,
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777218,"unicode":0,"echo":false,"script":null)
]
}
ui_focus_prev={
"deadzone": 0.5,
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":true,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777218,"unicode":0,"echo":false,"script":null)
]
}
ui_left={
"deadzone": 0.5,
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777231,"unicode":0,"echo":false,"script":null)
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":14,"pressure":0.0,"pressed":false,"script":null)
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":65,"unicode":0,"echo":false,"script":null)
, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":0,"axis":0,"axis_value":-1.0,"script":null)
]
}
ui_right={
"deadzone": 0.5,
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777233,"unicode":0,"echo":false,"script":null)
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":15,"pressure":0.0,"pressed":false,"script":null)
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":68,"unicode":0,"echo":false,"script":null)
, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":0,"axis":0,"axis_value":1.0,"script":null)
]
}
ui_up={
"deadzone": 0.5,
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777232,"unicode":0,"echo":false,"script":null)
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":12,"pressure":0.0,"pressed":false,"script":null)
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":87,"unicode":0,"echo":false,"script":null)
, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":0,"axis":1,"axis_value":-1.0,"script":null)
]
}
ui_down={
"deadzone": 0.5,
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777234,"unicode":0,"echo":false,"script":null)
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":13,"pressure":0.0,"pressed":false,"script":null)
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":83,"unicode":0,"echo":false,"script":null)
, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":0,"axis":1,"axis_value":1.0,"script":null)
]
}
ui_next_tab={
"deadzone": 0.5,
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777236,"unicode":0,"echo":false,"script":null)
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":5,"pressure":0.0,"pressed":false,"script":null)
]
}
ui_previous_tab={
"deadzone": 0.5,
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777235,"unicode":0,"echo":false,"script":null)
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":4,"pressure":0.0,"pressed":false,"script":null)
]
}
ui_quit={
"deadzone": 0.5,
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777217,"unicode":0,"echo":false,"script":null)
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":10,"pressure":0.0,"pressed":false,"script":null)
]
}
zoom_in={
"deadzone": 0.5,
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777235,"unicode":0,"echo":false,"script":null)
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":12,"pressure":0.0,"pressed":false,"script":null)
]
}
zoom_out={
"deadzone": 0.5,
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777236,"unicode":0,"echo":false,"script":null)
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":13,"pressure":0.0,"pressed":false,"script":null)
]
}
pause={
"deadzone": 0.5,
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":32,"unicode":0,"echo":false,"script":null)
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":11,"pressure":0.0,"pressed":false,"script":null)
]
}
ship_accelerate={
"deadzone": 0.5,
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":87,"unicode":0,"echo":false,"script":null)
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777232,"unicode":0,"echo":false,"script":null)
, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":0,"axis":7,"axis_value":1.0,"script":null)
]
}
ship_decelerate={
"deadzone": 0.5,
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":83,"unicode":0,"echo":false,"script":null)
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777234,"unicode":0,"echo":false,"script":null)
, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":0,"axis":6,"axis_value":1.0,"script":null)
]
}
ship_thrust_left={
"deadzone": 0.5,
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":81,"unicode":0,"echo":false,"script":null)
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":4,"pressure":0.0,"pressed":false,"script":null)
]
}
ship_thrust_right={
"deadzone": 0.5,
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":69,"unicode":0,"echo":false,"script":null)
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":5,"pressure":0.0,"pressed":false,"script":null)
]
}
ship_turn_left={
"deadzone": 0.5,
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":65,"unicode":0,"echo":false,"script":null)
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777231,"unicode":0,"echo":false,"script":null)
]
}
ship_turn_right={
"deadzone": 0.5,
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":68,"unicode":0,"echo":false,"script":null)
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777233,"unicode":0,"echo":false,"script":null)
]
}

Expand Down
4 changes: 3 additions & 1 deletion src/screens/destination_menu/DestinationMenu.tscn
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
[gd_scene load_steps=6 format=2]
[gd_scene load_steps=7 format=2]

[ext_resource path="res://screens/destination_menu/DestinationMenu.gd" type="Script" id=1]
[ext_resource path="res://screens/destination_menu/fuel_station/FuelStationContainer.tscn" type="PackedScene" id=2]
[ext_resource path="res://screens/destination_menu/ship_repairs/RepairOption.tscn" type="PackedScene" id=3]
[ext_resource path="res://assets/LeaveButtonHigherRes.png" type="Texture" id=4]
[ext_resource path="res://screens/destination_menu/passenger_pickup/PassengerPickupOption.tscn" type="PackedScene" id=5]
[ext_resource path="res://screens/destination_menu/TabContainer.gd" type="Script" id=6]

[node name="PlanetMenu" type="CanvasLayer"]
layer = 3
Expand Down Expand Up @@ -57,6 +58,7 @@ margin_top = -0.262001
margin_right = -31.0
margin_bottom = 0.231995
tab_align = 0
script = ExtResource( 6 )
__meta__ = {
"_edit_use_anchors_": false
}
Expand Down
13 changes: 13 additions & 0 deletions src/screens/destination_menu/TabContainer.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
extends TabContainer


func _input(event):
if event.is_action_pressed("ui_next_tab"):
current_tab = (get_child_count() + current_tab + 1) % get_child_count()
elif event.is_action_pressed("ui_previous_tab"):
current_tab = (get_child_count() + current_tab - 1) % get_child_count()


func cycle_tab(backwards = false):
var next_tab = current_tab + (-1 if backwards else 1)
current_tab = (get_child_count() + next_tab) % get_child_count()
2 changes: 1 addition & 1 deletion src/screens/main_game/MainGame.gd
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func open_pause_menu():

# Called on input events
func _unhandled_input(event):
if event.is_action_pressed("ui_cancel"):
if event.is_action_pressed("ui_quit"):
# Change to pause menu
open_pause_menu()

Expand Down
5 changes: 4 additions & 1 deletion src/screens/title/TitleScreen.gd
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ func _ready():
if is_pause_screen:
$CanvasLayer/CenterContainer/ColorRect/StartGameButton.text = "Continue game"

# Set focus to start button to allow control via keyboard/controller
$CanvasLayer/CenterContainer/ColorRect/StartGameButton.grab_focus()


func _unhandled_input(event):
if event.is_action_pressed("ui_cancel"):
if event.is_action_pressed("ui_quit"):
get_tree().quit()


Expand Down
35 changes: 31 additions & 4 deletions src/spaceship/spaceship.gd
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ signal fuel_changed(total_capacity, current_fuel)
const TORQUE_PER_THRUST = 35
const DISTANCE_AFTER_PLANET_MENU = 32
const VELOCITY_AFTER_PLANET_MENU = 200
const ANALOG_STICK_DEADZONE = 0.5
const ANALOG_ROTATION_DEADZONE = 0.2

# Define properties and internal variables
export var current_passenger = {"name": "nobody"}
Expand Down Expand Up @@ -155,9 +157,9 @@ func _integrate_forces(state):
# Thrust direction
var move_forwards = 0
var move_sideways = 0
if Input.is_action_pressed("ui_up"):
if Input.is_action_pressed("ship_accelerate"):
move_forwards = 1
if Input.is_action_pressed("ui_down"):
if Input.is_action_pressed("ship_decelerate"):
move_forwards = -1
if Input.is_action_pressed("ship_thrust_left"):
move_sideways = -1
Expand Down Expand Up @@ -193,11 +195,36 @@ func _integrate_forces(state):

# Rotation
var rotation_dir = 0
if Input.is_action_pressed("ui_left"):
if Input.is_action_pressed("ship_turn_left"):
rotation_dir -= 1
if Input.is_action_pressed("ui_right"):
if Input.is_action_pressed("ship_turn_right"):
rotation_dir += 1

# Rotation via left analog stick
if settings.camera_rotation:
# When camera rotation is on, just turn left/right if the stick is pushed left/right (like with keyboard controls)
var left_stick_x = Input.get_joy_axis(0, JOY_AXIS_0)

if abs(left_stick_x) > ANALOG_STICK_DEADZONE:
rotation_dir = sign(left_stick_x)
else:
# When camera rotation is off, rotate ship into the current direction of the stick
var left_stick_vector = Vector2(
-Input.get_joy_axis(0, JOY_AXIS_1),
Input.get_joy_axis(0, JOY_AXIS_0)
)

if left_stick_vector.length_squared() > ANALOG_STICK_DEADZONE:
var left_stick_angle = left_stick_vector.angle()
var rotation_delta = left_stick_angle - rotation

# Find the shortest rotation (e.g. rotate -40° instead of +320° to get from 160° to -160°)
if abs(rotation_delta) > PI:
rotation_delta -= sign(rotation_delta) * 2 * PI

if abs(rotation_delta) > ANALOG_ROTATION_DEADZONE:
rotation_dir = sign(rotation_delta)

if rotation_dir:
var ship_total_torque = ship_total_thrust * TORQUE_PER_THRUST
add_torque(rotation_dir * ship_total_torque)
Expand Down
4 changes: 1 addition & 3 deletions src/spaceship/spaceship.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
[ext_resource path="res://sound/exhaust.wav" type="AudioStream" id=3]
[ext_resource path="res://sound/explosion.wav" type="AudioStream" id=4]

[node name="Spaceship" type="RigidBody2D" groups=[
"Spaceship",
]]
[node name="Spaceship" type="RigidBody2D" groups=["Spaceship"]]
gravity_scale = 0.0
contacts_reported = 100
contact_monitor = true
Expand Down
Loading