From 1b7b6acd096e701a51e8a04eab5d0f52f45a4211 Mon Sep 17 00:00:00 2001 From: Jakob Feldmann Date: Mon, 23 May 2022 22:12:09 +0200 Subject: [PATCH] First working spring implementation The behaviour of the spring is erratic still. The main problem is: Blobby being slowed down, disproportionally much when touching the spring surface. A communication between the contraption and Blobbys physics needs to happen. He has to slow down according to the, motion of the spring plattform, and not go to 0 because hes touching some ground. The other thing is that it should be always possible to jump. Currently the platform can move down faster than blobby and cause him to hover, which makes jumping unavailable. --- src/Actors/Blobby/Blobby.gd | 4 +-- src/Actors/Blobby/Blobby.tscn | 18 +++++------ src/Actors/Enemy/Enemy.gd | 1 + src/Contraptions/Platform/Spring.gd | 32 ++++++++++++++++--- src/Contraptions/Platform/Spring.tscn | 46 +++++++++++++++------------ src/Levels/Plattforms Level.tscn | 2 ++ 6 files changed, 66 insertions(+), 37 deletions(-) diff --git a/src/Actors/Blobby/Blobby.gd b/src/Actors/Blobby/Blobby.gd index 601a446..4942095 100644 --- a/src/Actors/Blobby/Blobby.gd +++ b/src/Actors/Blobby/Blobby.gd @@ -20,13 +20,13 @@ onready var camera = $Camera2D # TODO This is the worst thing since... you know # When the Enemy stomp AREA enters the enemy collision area -> stomp -func _on_Skin_area_entered(area: Area2D) -> void: +func _on_BlobbySkin_area_entered(area: Area2D) -> void: if area.name == "StompDetector": velocity = calculate_stomp_velocity(velocity, stomp_feedback) # When the Enemy collision BODY enters the enemy collision area -> die -func _on_Skin_body_entered(body: Node) -> void: +func _on_BlobbySkin_body_entered(body: Node) -> void: if body.name == "EnemyBody": die() diff --git a/src/Actors/Blobby/Blobby.tscn b/src/Actors/Blobby/Blobby.tscn index 5b6873b..05770f9 100644 --- a/src/Actors/Blobby/Blobby.tscn +++ b/src/Actors/Blobby/Blobby.tscn @@ -39,11 +39,11 @@ script = ExtResource( 2 ) [node name="ShiftTween" type="Tween" parent="Camera2D"] -[node name="Skin" type="Area2D" parent="."] +[node name="BlobbySkin" type="Area2D" parent="."] collision_mask = 126 -[node name="CollisionPolygon2D" type="CollisionPolygon2D" parent="Skin"] -scale = Vector2( 1.03, 1.03 ) +[node name="CollisionPolygon2D" type="CollisionPolygon2D" parent="BlobbySkin"] +scale = Vector2( 1.04, 1.04 ) polygon = PoolVector2Array( -6.7, -3.311, -2.676, -7.3, 3.939, -7.3, 8, -1.863, 8, 4.912, 4.944, 8.5, -1.03623, 8.5, -4.213, 8.5, -6.7, 6.089 ) [node name="PlayerStateMachine" type="Node" parent="."] @@ -74,13 +74,13 @@ valign = 1 position = Vector2( 3.86988, 6.34765 ) [node name="Left_Wallcast1" type="RayCast2D" parent="WallRaycasts/LeftWallRaycast"] -position = Vector2( -10.706, -8.03844 ) +position = Vector2( -10.5748, -8.60978 ) enabled = true cast_to = Vector2( -2, 0 ) collision_mask = 9 [node name="Left_Wallcast2" type="RayCast2D" parent="WallRaycasts/LeftWallRaycast"] -position = Vector2( -10.706, -1.9261 ) +position = Vector2( -10.5888, -1.27766 ) enabled = true cast_to = Vector2( -2, 0 ) collision_mask = 9 @@ -88,17 +88,17 @@ collision_mask = 9 [node name="RightWallRaycast" type="Node2D" parent="WallRaycasts"] [node name="Right_Wallcast1" type="RayCast2D" parent="WallRaycasts/RightWallRaycast"] -position = Vector2( 7.97737, -0.838908 ) +position = Vector2( 8.00081, -0.776408 ) enabled = true cast_to = Vector2( 2, 0 ) collision_mask = 9 [node name="Right_Wallcast2" type="RayCast2D" parent="WallRaycasts/RightWallRaycast"] -position = Vector2( 8.0633, 3.84536 ) +position = Vector2( 8.01643, 3.91567 ) enabled = true cast_to = Vector2( 2, 0 ) collision_mask = 9 -[connection signal="area_entered" from="Skin" to="." method="_on_Skin_area_entered"] -[connection signal="body_entered" from="Skin" to="." method="_on_Skin_body_entered"] +[connection signal="area_entered" from="BlobbySkin" to="." method="_on_BlobbySkin_area_entered"] +[connection signal="body_entered" from="BlobbySkin" to="." method="_on_BlobbySkin_body_entered"] [connection signal="timeout" from="PlayerStateMachine/JumpBufferTimer" to="." method="_on_JumpBufferTimer_timeout"] diff --git a/src/Actors/Enemy/Enemy.gd b/src/Actors/Enemy/Enemy.gd index da4fba4..4607ce4 100644 --- a/src/Actors/Enemy/Enemy.gd +++ b/src/Actors/Enemy/Enemy.gd @@ -8,6 +8,7 @@ func _ready() -> void: velocity.x = -30 +# TODO Unify areas of kinematic bodies to skin func _on_StompDetector_body_entered(body: Node) -> void: if body.global_position.y > get_node("StompDetector").global_position.y: return diff --git a/src/Contraptions/Platform/Spring.gd b/src/Contraptions/Platform/Spring.gd index cea14bc..04fda9b 100644 --- a/src/Contraptions/Platform/Spring.gd +++ b/src/Contraptions/Platform/Spring.gd @@ -6,10 +6,11 @@ const PhysicsConst = preload("res://src/Utilities/Physic/PhysicsConst.gd") # var a: int = 2 # var b: Strin = "text" var mass = 1 -var spring_k = -200 +var spring_k = -500 var start_y = 0 var y_velocity = 0 -var friction = 0.9 +var friction = 0.91 +var stored_incoming_velocity = 0 # Called when the node enters the scene tree for the first time. @@ -22,7 +23,6 @@ func _physics_process(delta: float) -> void: var spring_force = spring_k * (self.position.y - self.start_y) var weight_force = PhysicsConst.gravity * mass var result_force = weight_force + spring_force - y_velocity += PhysicsFunc.convert_force_to_velocity( result_force, mass, delta ) @@ -30,14 +30,36 @@ func _physics_process(delta: float) -> void: y_velocity *= friction self.position.y += y_velocity * delta + if !_if_do_bounce(): + _store_incoming_velocity() -func _on_Area2D_area_entered(area: Area2D) -> void: +# TODO this works against the physics process +func _store_incoming_velocity(): + var areas: Array = $EnteringVelocityDetector.get_overlapping_areas() + for i in range(0, areas.size()): + if areas[i].name == "BlobbySkin": + print_debug(stored_incoming_velocity) + if areas[i].get_parent().velocity.y != 0: + stored_incoming_velocity = areas[i].get_parent().velocity.y + + +func _if_do_bounce() -> bool: + var areas: Array = $SpringSkin.get_overlapping_areas() + for i in range(0, areas.size()): + if areas[i].name == "BlobbySkin": + _Kinematic_Body_on_Spring(areas[i]) + return true + return false + + +func _Kinematic_Body_on_Spring(area: Area2D) -> void: var area_parent = area.get_parent() - var a_velocity = area_parent.velocity.y + var a_velocity = stored_incoming_velocity var a_mass = area_parent.mass var b_velocity = y_velocity var b_mass = mass y_velocity += PhysicsFunc.complete_unelastic_shock( a_velocity, b_velocity, a_mass, b_mass ) + stored_incoming_velocity = area_parent.velocity.y diff --git a/src/Contraptions/Platform/Spring.tscn b/src/Contraptions/Platform/Spring.tscn index 2535cec..d811e95 100644 --- a/src/Contraptions/Platform/Spring.tscn +++ b/src/Contraptions/Platform/Spring.tscn @@ -1,38 +1,42 @@ -[gd_scene load_steps=5 format=2] +[gd_scene load_steps=6 format=2] [ext_resource path="res://src/Contraptions/Platform/Spring.gd" type="Script" id=1] [ext_resource path="res://assets/environment/blocks/Basic stone block.png" type="Texture" id=2] -[sub_resource type="RectangleShape2D" id=1] -extents = Vector2( 11.913, 1.58343 ) - [sub_resource type="RectangleShape2D" id=2] -extents = Vector2( 11.8988, 1.56963 ) +extents = Vector2( 11.4526, 1.7975 ) + +[sub_resource type="RectangleShape2D" id=1] +extents = Vector2( 11.9386, 1.57982 ) + +[sub_resource type="RectangleShape2D" id=3] +extents = Vector2( 11.4, 1.42384 ) [node name="Spring" type="Node2D"] script = ExtResource( 1 ) -[node name="StaticBody2D" type="StaticBody2D" parent="."] -collision_layer = 32 -collision_mask = 41 - -[node name="Sprite" type="Sprite" parent="StaticBody2D"] -position = Vector2( -11.9516, -1.58488 ) +[node name="Sprite" type="Sprite" parent="."] scale = Vector2( 1.48986, 0.197785 ) texture = ExtResource( 2 ) -centered = false -[node name="SpringBody" type="CollisionShape2D" parent="StaticBody2D"] -position = Vector2( -0.0323062, -0.0131137 ) -shape = SubResource( 1 ) - -[node name="Spring" type="Area2D" parent="StaticBody2D"] -position = Vector2( -0.0323062, -0.0131137 ) +[node name="SpringSkin" type="Area2D" parent="."] collision_layer = 32 collision_mask = 3 -[node name="CollisionShape2D" type="CollisionShape2D" parent="StaticBody2D/Spring"] -position = Vector2( 0.0323062, 0.0137925 ) +[node name="CollisionShape2D" type="CollisionShape2D" parent="SpringSkin"] shape = SubResource( 2 ) -[connection signal="area_entered" from="StaticBody2D/Spring" to="." method="_on_Area2D_area_entered"] +[node name="SpringBody" type="KinematicBody2D" parent="."] +collision_layer = 32 +collision_mask = 41 + +[node name="CollisionShape2D" type="CollisionShape2D" parent="SpringBody"] +shape = SubResource( 1 ) + +[node name="EnteringVelocityDetector" type="Area2D" parent="."] +position = Vector2( 0, -3.99571 ) +collision_layer = 32 +collision_mask = 41 + +[node name="CollisionShape2D" type="CollisionShape2D" parent="EnteringVelocityDetector"] +shape = SubResource( 3 ) diff --git a/src/Levels/Plattforms Level.tscn b/src/Levels/Plattforms Level.tscn index a862c63..ab3bdb5 100644 --- a/src/Levels/Plattforms Level.tscn +++ b/src/Levels/Plattforms Level.tscn @@ -64,3 +64,5 @@ position = Vector2( 206.918, 601.665 ) [node name="Blobby" parent="." instance=ExtResource( 1 )] position = Vector2( 50.7867, 604.063 ) + +[editable path="Spring"]