From b3920b897516674611f4b9eab43156a1de1a096d Mon Sep 17 00:00:00 2001 From: Martin Felis Date: Fri, 19 Aug 2022 19:13:39 +0200 Subject: [PATCH] Allow clamping of movable speed --- Components/MovableComponent.gd | 10 ++++++---- Entities/PlayerEntity.gd | 15 ++++++++++----- Entities/PlayerEntity.tscn | 1 + Game.gd | 9 +-------- Utils/SpringDamper.gd | 13 +++++++++++++ 5 files changed, 31 insertions(+), 17 deletions(-) diff --git a/Components/MovableComponent.gd b/Components/MovableComponent.gd index bf5b9ea..3c8cc17 100644 --- a/Components/MovableComponent.gd +++ b/Components/MovableComponent.gd @@ -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) diff --git a/Entities/PlayerEntity.gd b/Entities/PlayerEntity.gd index ba8b09a..c1c90ae 100644 --- a/Entities/PlayerEntity.gd +++ b/Entities/PlayerEntity.gd @@ -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( @@ -30,15 +31,19 @@ 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): - assert(movable_component) - - movable_component.target = position + +func _unhandled_input(event): + if event is InputEventMouseButton and event.pressed: + assert(movable_component) + + movable_component.target = event.position diff --git a/Entities/PlayerEntity.tscn b/Entities/PlayerEntity.tscn index d6a7fa3..b090e86 100644 --- a/Entities/PlayerEntity.tscn +++ b/Entities/PlayerEntity.tscn @@ -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 )] diff --git a/Game.gd b/Game.gd index 92970ae..c783a22 100644 --- a/Game.gd +++ b/Game.gd @@ -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 diff --git a/Utils/SpringDamper.gd b/Utils/SpringDamper.gd index 9362c22..8455d84 100644 --- a/Utils/SpringDamper.gd +++ b/Utils/SpringDamper.gd @@ -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