Skip to content

Commit

Permalink
use c# sharp again for lag compensation
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathaneeckhout committed Jan 6, 2024
1 parent ed1f1c8 commit 1db06a0
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 31 deletions.
4 changes: 2 additions & 2 deletions scenes/enemies/BlueMole/BlueMole.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -1082,8 +1082,8 @@ visible = false
position = Vector2(0, -27)
shape = SubResource("CapsuleShape2D_jt1yq")

[node name="LagCompensationComponent" parent="." node_paths=PackedStringArray("HurtBox") instance=ExtResource("20_cfx77")]
HurtBox = NodePath("../HurtArea/HurtBox")
[node name="LagCompensationComponent" parent="." node_paths=PackedStringArray("_hurtBox") instance=ExtResource("20_cfx77")]
_hurtBox = NodePath("../HurtArea/HurtBox")

[node name="AggroArea" type="Area2D" parent="."]
visible = false
Expand Down
4 changes: 2 additions & 2 deletions scenes/enemies/ClamDog/ClamDog.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -1038,8 +1038,8 @@ position = Vector2(-9, -20)
rotation = 1.5708
shape = SubResource("CapsuleShape2D_p27rd")

[node name="LagCompensationComponent" parent="." node_paths=PackedStringArray("HurtBox") instance=ExtResource("4_hk50g")]
HurtBox = NodePath("../HurtArea/HurtBox")
[node name="LagCompensationComponent" parent="." node_paths=PackedStringArray("_hurtBox") instance=ExtResource("4_hk50g")]
_hurtBox = NodePath("../HurtArea/HurtBox")

[node name="AggroRadius" type="Area2D" parent="."]
position = Vector2(0, -45)
Expand Down
4 changes: 2 additions & 2 deletions scenes/enemies/Ladybug/Ladybug.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -747,5 +747,5 @@ visible = false
position = Vector2(0, -10)
shape = SubResource("CapsuleShape2D_qhkmw")

[node name="LagCompensationComponent" parent="." node_paths=PackedStringArray("HurtBox") instance=ExtResource("19_bayn0")]
HurtBox = NodePath("../HurtArea/HurtBox")
[node name="LagCompensationComponent" parent="." node_paths=PackedStringArray("_hurtBox") instance=ExtResource("19_bayn0")]
_hurtBox = NodePath("../HurtArea/HurtBox")
4 changes: 2 additions & 2 deletions scenes/enemies/MoldedDruvar/MoldedDruvar.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -762,8 +762,8 @@ shape = SubResource("CircleShape2D_hetrj")
position = Vector2(-5, -50)
shape = SubResource("CapsuleShape2D_ov0s8")

[node name="LagCompensationComponent" parent="." node_paths=PackedStringArray("HurtBox") instance=ExtResource("4_1c17k")]
HurtBox = NodePath("../HurtArea/HurtBox")
[node name="LagCompensationComponent" parent="." node_paths=PackedStringArray("_hurtBox") instance=ExtResource("4_1c17k")]
_hurtBox = NodePath("../HurtArea/HurtBox")

[node name="AggroRadius" type="Area2D" parent="."]
position = Vector2(0, -45)
Expand Down
4 changes: 2 additions & 2 deletions scenes/enemies/Sheep/Sheep.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -249,5 +249,5 @@ shape = SubResource("CircleShape2D_lcaik")
rotation = 1.5708
shape = SubResource("CapsuleShape2D_uo2bt")

[node name="LagCompensationComponent" parent="." node_paths=PackedStringArray("HurtBox") instance=ExtResource("14_ikli0")]
HurtBox = NodePath("../HurtArea/HurtBox")
[node name="LagCompensationComponent" parent="." node_paths=PackedStringArray("_hurtBox") instance=ExtResource("14_ikli0")]
_hurtBox = NodePath("../HurtArea/HurtBox")
4 changes: 2 additions & 2 deletions scenes/enemies/TreeTrunkGuy/TreeTrunkGuy.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -913,8 +913,8 @@ metadata/_edit_lock_ = true
position = Vector2(0, -38)
shape = SubResource("CapsuleShape2D_7ctfm")

[node name="LagCompensationComponent" parent="." node_paths=PackedStringArray("HurtBox") instance=ExtResource("4_7kflj")]
HurtBox = NodePath("../HurtArea/HurtBox")
[node name="LagCompensationComponent" parent="." node_paths=PackedStringArray("_hurtBox") instance=ExtResource("4_7kflj")]
_hurtBox = NodePath("../HurtArea/HurtBox")

[node name="AggroArea" type="Area2D" parent="."]
collision_layer = 0
Expand Down
4 changes: 2 additions & 2 deletions scenes/enemies/WildBoar/WildBoar.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -1220,8 +1220,8 @@ position = Vector2(0, -27)
rotation = 1.5708
shape = SubResource("CapsuleShape2D_28ggr")

[node name="LagCompensationComponent" parent="." node_paths=PackedStringArray("HurtBox") instance=ExtResource("22_5wt1x")]
HurtBox = NodePath("../HurtArea/HurtBox")
[node name="LagCompensationComponent" parent="." node_paths=PackedStringArray("_hurtBox") instance=ExtResource("22_5wt1x")]
_hurtBox = NodePath("../HurtArea/HurtBox")

[node name="AggroArea" type="Area2D" parent="."]
visible = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public partial class LagCompensationComponent : Node2D
{
// The Collision shape used for collision
[Export]
public CollisionShape2D HurtBox { get; set; }
private CollisionShape2D _hurtBox;

// The window size of how long an element should stay in the positionBuffer
public const double PositionBufferTimeWindow = 1.0;
Expand Down Expand Up @@ -67,17 +67,17 @@ public override void _Ready()
positionBuffer = new List<PositionElement> { };

// Get the radius if the shape is a circle
if (HurtBox.Shape is CircleShape2D)
if (_hurtBox.Shape is CircleShape2D)
{
hurtBoxShape = HurtBoxShape.Circle;
hurtBoxRadius = (HurtBox.Shape as CircleShape2D).Radius;
hurtBoxRadius = (_hurtBox.Shape as CircleShape2D).Radius;
}
// Get the radius and the height if it's a capsule
else if (HurtBox.Shape is CapsuleShape2D)
else if (_hurtBox.Shape is CapsuleShape2D)
{
hurtBoxShape = HurtBoxShape.Capsule;
hurtBoxRadius = (HurtBox.Shape as CapsuleShape2D).Radius;
hurtBoxHeight = (HurtBox.Shape as CapsuleShape2D).Height;
hurtBoxRadius = (_hurtBox.Shape as CapsuleShape2D).Radius;
hurtBoxHeight = (_hurtBox.Shape as CapsuleShape2D).Height;
}
else
{
Expand Down Expand Up @@ -126,7 +126,7 @@ public bool IsCircleCollidingWithTargetAtTimestamp(double timestamp, Vector2 cir
bool colliding = false;

// Calculate the actual position of the collisionshape as it can have an offset
Vector2 collisionShapePostion = elementAtTimestamp.Position + HurtBox.Position;
Vector2 collisionShapePostion = elementAtTimestamp.Position + _hurtBox.Position;

// Check the collision according to the shape of the HurtBox
switch (hurtBoxShape)
Expand Down Expand Up @@ -189,15 +189,15 @@ private bool checkCircleCollision(Vector2 targetNodePosition, Vector2 circlePosi
private bool checkCapsuleCollision(Vector2 targetNodePosition, Vector2 circlePosition, float circleRadius)
{

float distanceToLine = 0.0f;
float distanceToLine;
// Calculate the distance between the circle's center and the capsule's central line according to the rotation
if (HurtBox.RotationDegrees == 0.0f)
if (_hurtBox.RotationDegrees == 0.0f)
{
distanceToLine = Math.Abs(targetNodePosition.Y - circlePosition.Y);
}
else if (Math.Abs(HurtBox.RotationDegrees) - 90f < 0.0001f)
else if (Math.Abs(_hurtBox.RotationDegrees) - 90f < 0.1f)
{
distanceToLine = Math.Abs(targetNodePosition.X - circlePosition.Y);
distanceToLine = Math.Abs(targetNodePosition.X - circlePosition.X);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,11 @@ func _ready():
if _target_node.get("component_list") != null:
_target_node.component_list["lag_compensation"] = self

# TODO: check why capsule collision is not working
if HurtBox.shape is CircleShape2D:
# _hurtbox_shape = HURTBOXSHAPE.Circle
_hurtbox_shape = HURTBOXSHAPE.Circle
_hurtbox_radius = HurtBox.shape.radius
elif HurtBox.shape is CapsuleShape2D:
# _hurtbox_shape = HURTBOXSHAPE.Capsule
_hurtbox_shape = HURTBOXSHAPE.Capsule
_hurtbox_radius = HurtBox.shape.radius
_hurtbox_height = HurtBox.shape.height
else:
Expand Down Expand Up @@ -109,7 +108,7 @@ func check_capsule_collision(
# Calculate the distance between the circle's center and the capsule's central line according to the rotation
if HurtBox.rotation_degrees == 0.0:
distance_to_line = abs(targetNodePosition.y - circle_position.y)
elif abs(HurtBox.rotation_degrees) - 90.0 < 0.0001:
elif abs(HurtBox.rotation_degrees) - 90.0 < 0.1:
distance_to_line = abs(targetNodePosition.x - circle_position.y)
else:
GodotLogger.warn("HurtBox has an invalid rotation")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[gd_scene load_steps=2 format=3 uid="uid://cpybc5oxieo6i"]

[ext_resource type="Script" path="res://scripts/components/networking/lagcompensationcomponent/LagCompensationComponent.gd" id="1_c1itp"]
[ext_resource type="Script" path="res://scripts/components/networking/lagcompensationcomponent/LagCompensationComponent.cs" id="1_agt3b"]

[node name="LagCompensationComponent" type="Node2D"]
script = ExtResource("1_c1itp")
script = ExtResource("1_agt3b")

0 comments on commit 1db06a0

Please sign in to comment.