Allow clamping of movable speed

WorldChunkRefactoring
Martin Felis 2022-08-19 19:13:39 +02:00
parent 3525ef2ecd
commit b3920b8975
5 changed files with 31 additions and 17 deletions

View File

@ -5,9 +5,7 @@ var SpringDamper = preload("res://Utils/SpringDamper.gd")
export var target = Vector2 (0, 0)
export var pos = Vector2 (0, 0)
export var vel = Vector2 (0, 0)
export var max_speed = 50
export var spring_k = 10
export var spring_b = 0.5
export var max_speed = 100000
onready var spring_damper = SpringDamper.new(4, .99, 0.5)
@ -24,9 +22,13 @@ func _ready():
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
if (target - pos).length_squared() > pos_error_eps * pos_error_eps:
var spring_res = spring_damper.calc(pos, vel, target, delta)
var spring_res = spring_damper.calc_clamped_speed(pos, vel, target, delta, max_speed)
pos = spring_res[0]
vel = spring_res[1]
var speed = vel.length()
if speed > max_speed:
vel = vel * max_speed / speed
emit_signal("position_updated", pos)

View File

@ -21,6 +21,7 @@ func _ready():
movable_component.target = self.transform.origin
movable_component.pos = self.transform.origin
func _process(delta):
if is_active and color_component:
var color = Color(
@ -31,14 +32,18 @@ func _process(delta):
color_component.set_color(color)
pass
func _on_entity_clicked():
is_active = not is_active
func _on_position_updated(new_position):
transform.origin = new_position
update()
func on_world_location_clicked(position):
func _unhandled_input(event):
if event is InputEventMouseButton and event.pressed:
assert(movable_component)
movable_component.target = position
movable_component.target = event.position

View File

@ -14,6 +14,7 @@ script = ExtResource( 1 )
[node name="Movable" type="Node2D" parent="Components"]
script = ExtResource( 5 )
max_speed = 1000
[node name="Clickable" parent="Components" instance=ExtResource( 4 )]

View File

@ -3,14 +3,13 @@ extends Node2D
onready var SimpleEntity = preload("res://Entities/SimpleEntity.tscn")
onready var WanderingEntity = preload("res://Entities/WanderingEntity.tscn")
onready var player = $PlayerEntity
onready var entities = $Entities
signal world_location_clicked
# Called when the node enters the scene tree for the first time.
func _ready():
self.connect("world_location_clicked", player, "on_world_location_clicked")
pass
func _on_AddEntityButton_pressed():
var entity_instance = SimpleEntity.instance()
@ -24,9 +23,3 @@ func _on_AddWanderingEntity_pressed():
entity_instance.transform.origin = Vector2(randf() * viewport_rect.size[0], randf() * viewport_rect.size[1])
entities.add_child(entity_instance)
entity_instance.set_target(Vector2(randf() * viewport_rect.size[0], randf() * viewport_rect.size[1]))
func _unhandled_input(event):
if event is InputEventMouseButton and event.pressed:
#get_tree().get_root().set_input_as_handled()
emit_signal("world_location_clicked", event.position)
pass

View File

@ -26,3 +26,16 @@ func calc(x, v, xt, h:float):
var det_x = f * x + h * v + hhoo * xt
var det_v = v + hoo * (xt - x)
return [det_x * det_inv, det_v * det_inv]
func calc_clamped_speed(x, v, xt, h:float, s_max:float):
var res = calc(x, v, xt, h)
var x_new = res[0]
var vel_new = (x_new - x) / h
var speed_new = vel_new.length()
if speed_new > s_max:
vel_new = (vel_new / speed_new) * s_max
x_new = x + vel_new * h
return [x_new, vel_new]
return res