Skip to content

Commit

Permalink
GDScript upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
TitanNano committed Dec 5, 2023
1 parent 3ed6cc5 commit 40418f6
Show file tree
Hide file tree
Showing 18 changed files with 104 additions and 86 deletions.
10 changes: 6 additions & 4 deletions project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ config_version=5

config/name="Sim Chopper"
run/main_scene="res://resources/main.tscn"
config/features=PackedStringArray("4.1")
config/features=PackedStringArray("4.2")

[debug]

gdscript/warnings/unassigned_variable=2
gdscript/warnings/shadowed_variable=0
gdscript/warnings/unsafe_property_access=2
gdscript/warnings/unsafe_method_access=2
gdscript/warnings/unsafe_cast=1
gdscript/warnings/unsafe_call_argument=1
gdscript/warnings/unsafe_cast=2
gdscript/warnings/unsafe_call_argument=2
gdscript/warnings/static_called_on_instance=0
shapes/road_navigation/display_network=false
shapes/road_navigation/display_vehicle_target=false
Expand All @@ -36,7 +38,7 @@ window/size/mode.release=3
[editor]

export/convert_text_resources_to_binary=false
required_version="4.1.1"
required_version="4.2.0"

[editor_plugins]

Expand Down
6 changes: 3 additions & 3 deletions resources/Particles/Rotor/rotor_dust_particles_schweizer.tres
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ emission_ring_axis = Vector3(2.08165e-12, 1, 2.08165e-12)
emission_ring_height = 1.0
emission_ring_radius = 6.0
emission_ring_inner_radius = 5.0
angle_min = -90.0
angle_max = 360.0
direction = Vector3(0, 1, 0)
gravity = Vector3(0, 0, 0)
initial_velocity_min = 15.0
initial_velocity_max = 15.0
angular_velocity_min = 30.0
angular_velocity_max = 50.0
gravity = Vector3(0, 0, 0)
radial_accel_min = 15.0
radial_accel_max = 15.0
angle_min = -90.0
angle_max = 360.0
scale_min = 2.0
scale_max = 6.0
color_ramp = SubResource("GradientTexture1D_jtl1a")
5 changes: 3 additions & 2 deletions resources/main.tscn
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[gd_scene load_steps=19 format=3 uid="uid://cvh54xiw8586b"]
[gd_scene load_steps=20 format=3 uid="uid://cvh54xiw8586b"]

[ext_resource type="Script" path="res://src/HUD/HUDController.gd" id="1"]
[ext_resource type="Script" path="res://src/Objects/World/World.gd" id="3"]
Expand Down Expand Up @@ -65,14 +65,15 @@ anchor_right = 1.0
anchor_bottom = 1.0
stretch = true

[node name="SubViewport" type="SubViewport" parent="SubViewportContainer"]
[node name="SubViewport" type="SubViewport" parent="SubViewportContainer" node_paths=PackedStringArray("current_camera_controller")]
handle_input_locally = false
scaling_3d_mode = 1
audio_listener_enable_3d = true
size = Vector2i(1152, 648)
size_2d_override_stretch = true
render_target_update_mode = 4
script = ExtResource("3_2c332")
current_camera_controller = NodePath("Schweizer_300/MainCameraAnchor")

[node name="Schweizer_300" parent="SubViewportContainer/SubViewport" node_paths=PackedStringArray("child_camera", "child_main_camera", "child_debug_camera") groups=["player"] instance=ExtResource("16_e6k8r")]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -45.4393, -9.81614e-07, 17.9155)
Expand Down
13 changes: 5 additions & 8 deletions src/Objects/Helicopters/Helicopter.gd
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
extends RigidBody3D

const DustParticles := preload("res://src/Objects/Particles/DustParticles.gd")
const CameraInterpolation := preload("res://src/Objects/Camera/CameraInterpolation.gd")
const Rotor := preload("res://src/Objects/Helicopters/Rotor.gd")
const Logger := preload("res://src/util/Logger.gd")

Expand Down Expand Up @@ -49,10 +48,8 @@ func _get_top_rotation(delta: float) -> float:


func _update_directional_velocity(direction: Vector3, state: PhysicsDirectBodyState3D) -> Vector3:
var acceleration := DIRECTIONAL_ACCELERATION #* delta
var y_velocity := Vector3.UP * state.linear_velocity

var target_velocity = (direction * CRUISE_SPEED_MS) #+ y_velocity
var acceleration := DIRECTIONAL_ACCELERATION
var target_velocity := (direction * CRUISE_SPEED_MS)

return state.linear_velocity.move_toward(target_velocity, acceleration)

Expand All @@ -73,13 +70,13 @@ func _update_rotational_velocity(direction: float, delta: float) -> float:
func _get_tilt(velocity: Vector3) -> Vector3:
var top_speed := self._get_top_speed(1)
var direction := velocity.rotated(Vector3.UP, -self.rotation.y) # revert rotation
var tilt_z = MAX_TILT * (-direction.x / top_speed)
var tilt_x = MAX_TILT * (direction.z / top_speed)
var tilt_z := MAX_TILT * (-direction.x / top_speed)
var tilt_x := MAX_TILT * (direction.z / top_speed)
var tilt := Vector3(deg_to_rad(tilt_x), 0, deg_to_rad(tilt_z))

return tilt

func _process(delta):
func _process(_delta):
if Input.is_action_just_pressed("debug_cam"):
self.switch_debug_camera()

Expand Down
4 changes: 2 additions & 2 deletions src/Objects/Helicopters/Rotor.gd
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var previous_rotation := Vector3.ZERO

func _physics_process(delta: float) -> void:
@warning_ignore("integer_division")
var speed = (self.rpm / 60 * delta) * 360 * self.power
var speed := (self.rpm / 60 * delta) * 360 * self.power

if speed == 0:
return
Expand All @@ -20,7 +20,7 @@ func _physics_process(delta: float) -> void:
func _process(_delta: float) -> void:
#rotor velocity
var rotation_offset: float = abs(self.rotation.y - self.previous_rotation.y)
var previous_transform := Transform3D.IDENTITY.rotated(Vector3.FORWARD, min(max(0.01, rotation_offset), 0.610865) * -1)
var previous_transform := Transform3D.IDENTITY.rotated(Vector3.FORWARD, minf(maxf(0.01, rotation_offset), 0.610865) * -1)

self.previous_rotation = Vector3(self.rotation)
self.material.set_shader_parameter("previous_transform", previous_transform)
8 changes: 4 additions & 4 deletions src/Objects/Networks/RoadNavigation.gd
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func get_global_transform(node: Building, direction := Vector3.ZERO) -> Transfor
@warning_ignore("unused_variable", "shadowed_variable")
var diagonal_offset := self.diagonal_offset(width)
var raw_angle := Vector3.FORWARD.signed_angle_to(direction, Vector3.UP)
var angle := int(snapped(rad_to_deg(raw_angle), 90))
var angle := snappedi(rad_to_deg(raw_angle), 90)
var offset := (width / 4.0) * Vector3.RIGHT.rotated(Vector3.UP, angle)

# set diagonal offset for road courbes
Expand Down Expand Up @@ -144,7 +144,7 @@ func get_nearest_node(global_translation: Vector3) -> Building:
var distance := -1.0
var nearest: Node3D = null

for object in self.node_map.values():
for object in self.node_map.values() as Array[Node3D]:
var new := global_translation.distance_squared_to(object.global_transform.origin)

Logger.debug(["node distance to actor:", new])
Expand Down Expand Up @@ -220,7 +220,7 @@ func update_debug():

for tile_coords in self.network:
var node: Building = self.network[tile_coords]
var node_debug = CSGSphere3D.new()
var node_debug := CSGSphere3D.new()

node_debug.add_to_group("debug")
node_debug.global_transform = self.get_global_transform(node)
Expand All @@ -235,7 +235,7 @@ func update_debug():
var dis := node_pos.distance_to(con_pos)
var dir := node_pos.direction_to(con_pos)
var angle := Vector3.UP.angle_to(dir) * -1
var rotation_axis = dir.cross(Vector3.UP).normalized()
var rotation_axis := dir.cross(Vector3.UP).normalized()

con_debug.radius = 0.3
con_debug.height = dis / 2.0
Expand Down
12 changes: 8 additions & 4 deletions src/Objects/Terrain/Terrain.gd
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@ func _ready() -> void:
func build_async(city: Dictionary):
var rotation := TerrainRotation.new()
var builder_factory := TerrainBuilderFactory.new()
var sea_level = city.simulator_settings["GlobalSeaLevel"]
var simulator_settings: Dictionary = city.get("simulator_settings")
var compass: int = simulator_settings.get("Compass")
var sea_level: int = simulator_settings.get("GlobalSeaLevel")
var tilelist: Dictionary = city.get("tilelist")
var city_size: int = city.get("city_size")

rotation.set_rotation(city.simulator_settings['Compass'])
rotation.set_rotation(compass)

# make function async
await get_tree().process_frame
Expand All @@ -29,9 +33,9 @@ func build_async(city: Dictionary):
"Water": ocean_material
}

var builder = builder_factory.create(city.tilelist, rotation, materials)
var builder = builder_factory.create(tilelist, rotation, materials)

builder.set_city_size(city.city_size)
builder.set_city_size(city_size)
builder.set_tile_size(self.world_constants.tile_size)
builder.set_tile_height(self.world_constants.tile_height)
builder.set_sea_level(sea_level)
Expand Down
10 changes: 5 additions & 5 deletions src/Objects/Vehicles/Car.gd
Original file line number Diff line number Diff line change
Expand Up @@ -80,25 +80,25 @@ func _physics_process(_delta: float) -> void:
var current_velocity := direction * self.velocity

# rotation
var angle_dir = (self.global_transform.origin * V3Util.XZ_PLANE).direction_to(target * V3Util.XZ_PLANE)
var angle_dir := (self.global_transform.origin * V3Util.XZ_PLANE).direction_to(target * V3Util.XZ_PLANE)
@warning_ignore("shadowed_variable")
var target_angle = Vector3.FORWARD.signed_angle_to(angle_dir, Vector3.UP)
var target_angle := Vector3.FORWARD.signed_angle_to(angle_dir, Vector3.UP)
var angle_offset := self.angular_offset(self.rotation.y, target_angle)

# ban 180 deg turns
if not self.new and abs(angle_offset) > deg_to_rad(100):
if not self.new and absf(angle_offset) > deg_to_rad(100):
Logger.debug(["attempted", rad_to_deg(angle_offset), "deg turn, blocked"])
self.safe_velocity = Vector3.ZERO
self._on_choose_target()
return

if abs(angle_offset) > deg_to_rad(5):
if absf(angle_offset) > deg_to_rad(5):
current_velocity *= 1

self.target_angle = target_angle
self.set_velocity(current_velocity)

if ProjectSettings.get_setting(CustomProjectSettings.DEBUG_SHAPES_ROAD_NAVIGATION_DISPLAY_VEHICLE_TARGET) \
if ProjectSettings.get_setting(CustomProjectSettings.DEBUG_SHAPES_ROAD_NAVIGATION_DISPLAY_VEHICLE_TARGET) as bool \
and debug_target.is_inside_tree():
debug_target.global_transform.origin = target

Expand Down
14 changes: 8 additions & 6 deletions src/Objects/World/Buildings.gd
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const CityCoordsFeature := preload("res://src/features/CityCoordsFeature.gd")
signal loading_progress(value)
signal spawn_point_encountered(tile_coords, size, altitude)

@export var world_constants: Resource
@export var world_constants: WorldConstants

var city_coords_feature: CityCoordsFeature

Expand Down Expand Up @@ -44,8 +44,8 @@ func build_async(city) -> void:
await self.get_tree().process_frame

func _is_spawn_point(building: Dictionary, tiles: Dictionary) -> bool:
var x = building.tile_coords[0]
var y = building.tile_coords[1]
var x: int = building.tile_coords[0]
var y: int = building.tile_coords[1]

for index in range(x - 1, x + 3):
var tile: Dictionary = tiles[[index, y]]
Expand Down Expand Up @@ -74,11 +74,11 @@ func _is_spawn_point(building: Dictionary, tiles: Dictionary) -> bool:

func _insert_building(building: Dictionary, tiles: Dictionary) -> void:
var budget := TimeBudget.new(0)
var tile: Dictionary = tiles[Array(building.tile_coords)]
var tile: Dictionary = tiles[building.get("tile_cords") as Array]
var building_size: int = building.size
@warning_ignore("shadowed_variable_base_class")
var name: String = building.name
var object := SceneObjectRegistry.load_building(building.building_id)
var object := SceneObjectRegistry.load_building(building.get("building_id") as int)

if not object:
print("unknown building \"%s\"" % name)
Expand All @@ -91,8 +91,10 @@ func _insert_building(building: Dictionary, tiles: Dictionary) -> void:
budget.restart()
var instance: Node3D = object.instantiate()
var instance_time := budget.elapsed()
var tile_coords: Array[int] = building.get("tile_coords")
var altitude: int = tile.get("altitude")

var location := self.city_coords_feature.get_building_coords(building.tile_coords[0], building.tile_coords[1], tile.altitude, building_size)
var location := self.city_coords_feature.get_building_coords(tile_coords[0], tile_coords[1], altitude, building_size)

location.y += 0.1

Expand Down
6 changes: 3 additions & 3 deletions src/Objects/World/Networks.gd
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ extends Node

const TimeBudget := preload("res://src/util/TimeBudget.gd")
const CityCoordsFeature := preload("res://src/features/CityCoordsFeature.gd")
const WorldConstants := preload("res://src/Objects/Data/WorldConstants.gd")
const SceneObjectRegistry := preload("res://src/SceneObjectRegistry.gd")
const CarSpawner := preload("res://src/Objects/Spawner/CarSpawner.gd")
const Building := preload("res://src/Objects/Map/Building.gd")
Expand Down Expand Up @@ -31,7 +30,7 @@ func build_async(city: Dictionary):
self.city_coords_feature = CityCoordsFeature.new(world_constants, sea_level)

for key in networks:
var network_section: Building = Building.new(networks[key])
var network_section: Building = Building.new(networks.get(key) as Dictionary)
var object := SceneObjectRegistry.load_network(network_section.building_id())
@warning_ignore("shadowed_variable_base_class")
var name: String = network_section.name()
Expand All @@ -43,7 +42,8 @@ func build_async(city: Dictionary):

var instance: Node3D = object.instantiate()
var tile: Dictionary = tiles[key]
var location := self.city_coords_feature.get_building_coords(network_section.tile_coords()[0], network_section.tile_coords()[1], tile.altitude, 1)
var altitude: int = tile.get("altitude")
var location := self.city_coords_feature.get_building_coords(network_section.tile_coords()[0], network_section.tile_coords()[1], altitude, 1)

# is a suspension / pylon bridge part or raised powerline
if network_section.building_id() in range(0x51, 0x5E):
Expand Down
12 changes: 7 additions & 5 deletions src/Objects/World/World.gd
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,12 @@ func _load_map_async(city: Dictionary):
await self.terrain.build_async(city)
await self.networks.build_async(city)
await self.buildings.build_async(city)

var city_size: int = city.get("city_size")

self._setup_probing(city.city_size)
self._setup_probing(city_size)
self.backdrop.build(
city.city_size,
city_size,
self.world_constants.tile_size,
self.sea_level * self.world_constants.tile_height
)
Expand All @@ -78,8 +80,8 @@ func _load_map_async(city: Dictionary):


func _create_snapshot() -> void:
var packed_scene = PackedScene.new()
var file_name = "{year}-{month}-{day}-{hour}-{minute}-{second}.tscn".format(Time.get_datetime_dict_from_system())
var packed_scene := PackedScene.new()
var file_name := "{year}-{month}-{day}-{hour}-{minute}-{second}.tscn".format(Time.get_datetime_dict_from_system())
packed_scene.pack(get_tree().get_current_scene())
var result := ResourceSaver.save(packed_scene, "res://snapshots/%s" % file_name)

Expand All @@ -97,7 +99,7 @@ func _spawn_player() -> void:
player.snap_camera()


func _insert_spawn_point(building_coords: Array, building_size: int, altitude: int) -> void:
func _insert_spawn_point(building_coords: Array[int], building_size: int, altitude: int) -> void:
print("SPAWN POINT AT {point}".format({ "point": building_coords }))
var spawn_host_scene := preload("res://resources/Objects/spawn_host.tscn")
var spawn_host: Node3D = spawn_host_scene.instantiate()
Expand Down
8 changes: 4 additions & 4 deletions src/SceneObjectRegistry.gd
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,13 @@ const buildings = {

static func _load(index: Dictionary, object_id: int) -> PackedScene:
assert(object_id in index, "0x%x is not a vaild object id" % object_id)
assert(ResourceLoader.exists(index[object_id], "PackedScene"), "\"%s\" is not a PackedScene" % index[object_id])
assert(ResourceLoader.exists(index.get(object_id) as String, "PackedScene"), "\"%s\" is not a PackedScene" % index[object_id])

return ResourceLoader.load(index[object_id]) as PackedScene
return ResourceLoader.load(index.get(object_id) as String) as PackedScene

static func load_network(object_id) -> PackedScene:
static func load_network(object_id: int) -> PackedScene:
return _load(networks, object_id)


static func load_building(object_id) -> PackedScene:
static func load_building(object_id: int) -> PackedScene:
return _load(buildings, object_id)
2 changes: 1 addition & 1 deletion src/features/CityCoordsFeature.gd
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func _init(world_constants: WorldConstants, sea_level: int):
func get_world_coords(x: int, y: int, z: int) -> Vector3:
return Vector3(
(x * self.world_constants.tile_size),
max(z, self.sea_level - 1) * self.world_constants.tile_height,
maxf(z, self.sea_level - 1) * self.world_constants.tile_height,
(y * self.world_constants.tile_size)
)

Expand Down
Loading

0 comments on commit 40418f6

Please sign in to comment.