Fix this damn navigation

This commit is contained in:
Hamcha 2020-07-16 14:37:21 +02:00
parent e0797412c6
commit e02d1e4cc3
Signed by: Hamcha
GPG Key ID: 41467804B19A3315
7 changed files with 108 additions and 45 deletions

View File

@ -7,7 +7,7 @@ func _physics_process(_delta):
$Margin/Container/VelocityBox/HBoxContainer3/CurrentSpeed.text = speed_str
var dir_str = str(round(rad2deg(scene.world.map.current_ship_direction))) + " deg"
$Margin/Container/VelocityBox/HBoxContainer2/CurrentDirection.text = dir_str
var current_position_str = Coordinates.as_string(scene.world.map.current_ship_position, true)
var current_position_str = Coordinates.as_string(scene.world.map.current_ship_position + scene.world.map.current_ship_subpos, true)
$Margin/Container/VelocityBox/HBoxContainer4/CurrentPosition.text = current_position_str
var current_target_str = scene.world.map.current_ship_target
if current_target_str == null:

View File

@ -7,7 +7,7 @@ class_name GameObjectEngine
const LIGHT_STRENGTH_MULT = 0.002
const MAX_ENERGY = 2
const MAX_USAGE = 1
const MAX_USAGE = 0.5
enum Direction { LEFT, RIGHT, UP, DOWN }
@ -17,10 +17,14 @@ onready var activationRange = $ActivationRange as ActivationRange
onready var manager = $PowerManager as PowerManager
export var strength = 1.0 setget set_strength
export var max_force = 0.3
var force = 0
func _ready() -> void:
if not Engine.editor_hint:
activationRange.visible = true
$Light2D.visible = true
func set_direction(dir) -> void:
direction = dir
@ -49,7 +53,9 @@ func refresh_sprite() -> void:
$ActivationRange.rotation = rot
func _physics_process(delta: float) -> void:
manager.power_usage = MAX_USAGE * strength
if Engine.editor_hint:
return
manager.power_usage = max(1e-3, MAX_USAGE * strength)
func _input_event(_viewport, event, _shape_idx):
if Engine.editor_hint:
@ -68,3 +74,11 @@ func serialize():
func deserialize(data):
set_direction(data["direction"])
set_strength(data["strength"])
func _power_status_changed(powered):
if powered:
force = max_force
$Light2D.enabled = true
else:
force = 0
$Light2D.enabled = false

View File

@ -26,6 +26,7 @@ region_enabled = true
region_rect = Rect2( 0, 0, 96, 96 )
[node name="Light2D" type="Light2D" parent="."]
visible = false
position = Vector2( 48, 48 )
texture = ExtResource( 4 )
offset = Vector2( 0, 50 )
@ -42,3 +43,6 @@ shape = SubResource( 2 )
[node name="PowerManager" type="Node" parent="."]
script = ExtResource( 5 )
power_usage = 1.0
[connection signal="power_connected" from="PowerManager" to="." method="_power_status_changed" binds= [ true ]]
[connection signal="power_disconnected" from="PowerManager" to="." method="_power_status_changed" binds= [ false ]]

View File

@ -1,8 +1,7 @@
class_name UICommand
enum CommandType {
SetShipSpeed,
SetShipDirection
SetShipTarget
}
var cmd_type = null
var cmd_args = []

View File

@ -15,7 +15,7 @@ func _ready() -> void:
if netgame.hosting:
world.load_map(netgame.get_current_map())
world.map.current_ship_position = Vector2(randf() * 1e4, randf() * 1e4)
world.map.current_ship_target = world.map.current_ship_position + Vector2(-3, 3)
world.map.current_ship_target = null
rpc("spawn_player", 1)
else:
world.load_map(GameWorld.Map.EMPTY)
@ -55,10 +55,6 @@ remotesync func spawn_player(id):
world.spawn_player(id, multiplayer.get_network_unique_id() == id)
remotesync func process_command(cmd: UICommand):
pass
# match cmd.cmd_type:
# UICommand.CommandType.SetShipSpeed:
# world.map.ship_speed = cmd.cmd_args[0]
# UICommand.CommandType.SetShipDirection:
# world.map.ship_direction = cmd.cmd_args[0]
match cmd.cmd_type:
UICommand.CommandType.SetShipTarget:
world.map.current_ship_target = cmd.cmd_args[0]

View File

@ -7,25 +7,24 @@ var ship_direction = 0
var ship_speed = 0
var current_ship_position = Vector2.ZERO
var current_ship_subpos = Vector2.ZERO
var current_ship_target = Vector2.ZERO
var current_ship_direction = 0
var current_ship_speed = 0
var current_ship_direction = 0.0
var current_ship_speed = 0.0
puppet var pup_ship_position = Vector2.ZERO
puppet var pup_ship_subpos = Vector2.ZERO
puppet var pup_ship_target = Vector2.ZERO
puppet var pup_ship_direction = 0
puppet var pup_ship_speed = 0
puppet var pup_ship_direction = 0.0
puppet var pup_ship_speed = 0.0
const MAX_ACCELERATION = 0.03
const DIRECTION_EASE = 0.4
const EPSILON = 0.01
const MAX_STEERING = 0.06
const EPSILON = 1e-3
const SCROLL_MULTIPLIER = 1e4
const ENGINE_MULTIPLIER = 5000
const MAX_SPEED = 0.2 # in Sectors/Second
const SLOW_SPEED = 0.02
const SLOW_SPEED_THRESHOLD = 0.2
const NO_SPEED_THRESHOLD = 0.1
const NO_SPEED_THRESHOLD = 0.01
const ProbeElectricity = preload("res://Actors/Systems/Electricity/ElectricProbe.tscn")
@ -42,7 +41,7 @@ func _ready():
if Engine.editor_hint:
return
$editor.queue_free()
set_unlit(false)
set_unlit(true)
if not is_network_master():
return
@ -59,19 +58,33 @@ func _ready():
func _process(delta: float):
if Engine.editor_hint:
return
var current_speed_ratio = current_ship_speed / MAX_SPEED
var current_speed_ratio = 0
var engines = get_engine_count()
var max_speed = get_max_speed()
if engines > 0:
current_speed_ratio = current_ship_speed / max_speed
# Ease ship speed/direction changes
if abs(ship_direction - current_ship_direction) < EPSILON:
if abs(current_ship_direction) > PI*2:
current_ship_direction -= PI*2 * sign(current_ship_direction)
var direction_delta = ship_direction - current_ship_direction
if direction_delta < 0:
direction_delta += PI*2
if direction_delta > PI:
direction_delta -= PI*2
if abs(direction_delta) < 1e-3:
current_ship_direction = ship_direction
else:
current_ship_direction = lerp(current_ship_direction, ship_direction, delta * current_speed_ratio * DIRECTION_EASE)
# Avoid stuttering by not turning until there's enough reason to
if abs(direction_delta) > 0.1:
current_ship_direction += MAX_STEERING * engines * delta * sign(direction_delta)
var speed_delta = ship_speed - current_ship_speed
if abs(speed_delta) < EPSILON:
current_ship_speed = ship_speed
else:
current_ship_speed += MAX_ACCELERATION * max(0.1, 1-ease(current_speed_ratio, 0.6)) * delta * sign(speed_delta)
current_ship_speed += MAX_ACCELERATION * max_speed * delta * sign(speed_delta)
set_engine_strength(current_ship_speed * ENGINE_MULTIPLIER)
$deepspace.rotation = -current_ship_direction-PI/2
@ -81,27 +94,61 @@ func _physics_process(delta):
if Engine.editor_hint:
return
if is_network_master():
var distance = current_ship_position.distance_to(current_ship_target)
if distance > NO_SPEED_THRESHOLD:
ship_direction = current_ship_target.angle_to_point(current_ship_position)
if distance < SLOW_SPEED_THRESHOLD:
ship_speed = SLOW_SPEED
elif current_ship_speed == 0 or distance / current_ship_speed > ease(current_ship_speed, 2.8)*4000:
ship_speed = MAX_SPEED
var adj_position = current_ship_position+current_ship_subpos
if current_ship_target != null:
var distance = adj_position.distance_to(current_ship_target)
if distance > NO_SPEED_THRESHOLD:
ship_direction = current_ship_target.angle_to_point(adj_position)
var max_speed = get_max_speed()
if max_speed > 0:
var deceleration_time = current_ship_speed / (MAX_ACCELERATION * max_speed)
if current_ship_speed * deceleration_time < distance:
ship_speed = max_speed
else:
ship_speed = 0.0
else:
ship_speed = 0.0
rset("pup_ship_direction", ship_direction)
rset("pup_ship_speed", ship_speed)
else:
ship_speed = 0
rset("pup_ship_direction", ship_direction)
rset("pup_ship_speed", ship_speed)
ship_speed = 0.0
rset("pup_ship_speed", ship_speed)
if current_ship_speed > EPSILON:
var pos_delta = (Vector2.RIGHT * current_ship_speed * delta).rotated(current_ship_direction)
current_ship_subpos += pos_delta
if current_ship_subpos.x >= 1.0:
var int_part = floor(current_ship_subpos.x)
current_ship_position.x += int_part
current_ship_subpos.x -= int_part
if current_ship_subpos.y >= 1.0:
var int_part = floor(current_ship_subpos.y)
current_ship_position.y += int_part
current_ship_subpos.y -= int_part
rset("pup_ship_position", current_ship_position)
rset("pup_ship_subpos", current_ship_subpos)
else:
ship_speed = 0
ship_speed = 0.0
rset("pup_ship_speed", ship_speed)
if current_ship_speed > EPSILON:
current_ship_position += (Vector2.RIGHT * current_ship_speed * delta).rotated(current_ship_direction)
rset("pup_ship_position", current_ship_position)
else:
ship_direction = pup_ship_direction
ship_speed = pup_ship_speed
current_ship_position = pup_ship_position
current_ship_subpos = pup_ship_subpos
func get_engine_count():
var working_engines = 0
for child in $engines.get_children():
var engine = child as GameObjectEngine
if engine.force > 0:
working_engines += 1
return working_engines
func get_max_speed():
var max_speed = 0
for child in $engines.get_children():
var engine = child as GameObjectEngine
max_speed += engine.force
return max_speed
# Serialization / Deserialization

View File

@ -26,7 +26,7 @@ var last_pos = []
func _physics_process(delta):
radar_next_remaining -= delta
if radar_next_remaining < 0:
var current_position = scene.world.map.current_ship_position * 100
var current_position = (scene.world.map.current_ship_position + scene.world.map.current_ship_subpos) * 100
last_pos.append(current_position)
if last_pos.size() > 20:
last_pos.pop_front()
@ -39,7 +39,7 @@ func _physics_process(delta):
func _draw():
var win_size = get_global_rect().size
var current_position = scene.world.map.current_ship_position * 100
var current_position = (scene.world.map.current_ship_position + scene.world.map.current_ship_subpos) * 100
if set_position:
origin = current_position - win_size/2.0
last_origin = origin
@ -68,6 +68,9 @@ func _draw():
draw_circle(last_pos[pos_index] - origin, 2, Color(1, 0, 0, pos_index*1.0/point_count))
draw_target(viewport, current_position, Color.red)
# Debug directions:
#draw_line(current_position - viewport.position, current_position - viewport.position + Vector2.RIGHT.rotated(scene.world.map.ship_direction) * 100, Color.green)
#draw_line(current_position - viewport.position, current_position - viewport.position + Vector2.RIGHT.rotated(scene.world.map.current_ship_direction) * 100, Color.yellow)
var current_target = scene.world.map.current_ship_target
if current_target != null: