Skip to content

Commit 2afbeef

Browse files
committed
Allow using WASD and controllers in Truck Town
1 parent 9c17c25 commit 2afbeef

File tree

5 files changed

+69
-30
lines changed

5 files changed

+69
-30
lines changed

3d/truck_town/car_select.gd

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,26 @@ extends Control
22

33
var town = null
44

5-
func _back():
6-
town.queue_free()
7-
show()
5+
func _process(_delta):
6+
if Input.is_action_just_pressed("back"):
7+
_on_Back_pressed()
88

99

1010
func _load_scene(car):
1111
var tt = load(car).instance()
1212
tt.set_name("car")
1313
town = load("res://town_scene.tscn").instance()
14-
town.get_node("instance_pos").add_child(tt)
15-
town.get_node("back").connect("pressed", self, "_back")
14+
town.get_node("InstancePos").add_child(tt)
15+
town.get_node("Back").connect("pressed", self, "_on_Back_pressed")
1616
get_parent().add_child(town)
1717
hide()
1818

1919

20+
func _on_Back_pressed():
21+
town.queue_free()
22+
show()
23+
24+
2025
func _on_MiniVan_pressed():
2126
_load_scene("res://car_base.tscn")
2227

3d/truck_town/follow_camera.gd

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,27 @@
11
extends Camera
22

3-
var collision_exception = []
43
export var min_distance = 0.5
54
export var max_distance = 4.0
65
export var angle_v_adjust = 0.0
6+
7+
var collision_exception = []
78
var max_height = 2.0
89
var min_height = 0
910

11+
func _ready():
12+
# Find collision exceptions for ray.
13+
var node = self
14+
while(node):
15+
if (node is RigidBody):
16+
collision_exception.append(node.get_rid())
17+
break
18+
else:
19+
node = node.get_parent()
20+
21+
# This detaches the camera transform from the parent spatial node.
22+
set_as_toplevel(true)
23+
24+
1025
func _physics_process(_delta):
1126
var target = get_parent().get_global_transform().origin
1227
var pos = get_global_transform().origin
@@ -33,17 +48,3 @@ func _physics_process(_delta):
3348
var t = get_transform()
3449
t.basis = Basis(t.basis[0], deg2rad(angle_v_adjust)) * t.basis
3550
set_transform(t)
36-
37-
38-
func _ready():
39-
# Find collision exceptions for ray.
40-
var node = self
41-
while(node):
42-
if (node is RigidBody):
43-
collision_exception.append(node.get_rid())
44-
break
45-
else:
46-
node = node.get_parent()
47-
48-
# This detaches the camera transform from the parent spatial node.
49-
set_as_toplevel(true)

3d/truck_town/project.godot

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,43 @@ window/stretch/aspect="expand"
2626
window/height=720
2727
window/width=1280
2828

29+
[input]
30+
31+
accelerate={
32+
"deadzone": 0.5,
33+
"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)
34+
, 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)
35+
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":0,"pressure":0.0,"pressed":false,"script":null)
36+
]
37+
}
38+
reverse={
39+
"deadzone": 0.5,
40+
"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)
41+
, 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)
42+
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":1,"pressure":0.0,"pressed":false,"script":null)
43+
]
44+
}
45+
turn_left={
46+
"deadzone": 0.5,
47+
"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)
48+
, 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)
49+
, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":0,"axis":0,"axis_value":-1.0,"script":null)
50+
]
51+
}
52+
turn_right={
53+
"deadzone": 0.5,
54+
"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)
55+
, 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)
56+
, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":0,"axis":0,"axis_value":1.0,"script":null)
57+
]
58+
}
59+
back={
60+
"deadzone": 0.5,
61+
"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)
62+
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":11,"pressure":0.0,"pressed":false,"script":null)
63+
]
64+
}
65+
2966
[rasterizer]
3067

3168
shadow_filter=3

3d/truck_town/town_scene.tscn

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66

77
[node name="TownMesh" parent="." instance=ExtResource( 1 )]
88

9-
[node name="instance_pos" type="Position3D" parent="."]
9+
[node name="InstancePos" type="Position3D" parent="."]
1010
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 13.2039, 6.67095, -37.6042 )
1111

12-
[node name="back" type="Button" parent="."]
12+
[node name="Back" type="Button" parent="."]
1313
margin_left = 17.0
1414
margin_top = 9.0
1515
margin_right = 85.0

3d/truck_town/vehicle.gd

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,15 @@ export var engine_force_value = 40
1111
func _physics_process(delta):
1212
var fwd_mps = transform.basis.xform_inv(linear_velocity).x
1313

14-
if Input.is_action_pressed("ui_left"):
15-
steer_target = STEER_LIMIT
16-
elif Input.is_action_pressed("ui_right"):
17-
steer_target = -STEER_LIMIT
18-
else:
19-
steer_target = 0
14+
steer_target = Input.get_action_strength("turn_left") - Input.get_action_strength("turn_right")
15+
steer_target *= STEER_LIMIT
2016

21-
if Input.is_action_pressed("ui_up"):
17+
if Input.is_action_pressed("accelerate"):
2218
engine_force = engine_force_value
2319
else:
2420
engine_force = 0
2521

26-
if Input.is_action_pressed("ui_down"):
22+
if Input.is_action_pressed("reverse"):
2723
if (fwd_mps >= -1):
2824
engine_force = -engine_force_value
2925
else:

0 commit comments

Comments
 (0)