Skip to content

Commit

Permalink
feat(game): Moving platforms (#313)
Browse files Browse the repository at this point in the history
  • Loading branch information
elementbound authored Oct 21, 2024
1 parent 99f6e92 commit a557f98
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 72 deletions.
16 changes: 14 additions & 2 deletions examples/forest-brawl/maps/three-peaks.tscn

Large diffs are not rendered by default.

71 changes: 2 additions & 69 deletions examples/forest-brawl/scenes/brawler.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ properties/1/sync = false
properties/1/watch = true

[node name="Brawler" type="CharacterBody3D" groups=["Brawlers"]]
platform_on_leave = 2
platform_floor_layers = 0
script = ExtResource("1_np8na")
speed = 4.0
death_depth = 16.0
Expand All @@ -103,73 +105,6 @@ respawn_time = 8.0
[node name="bomber-guy" parent="." instance=ExtResource("7_ij3cr")]
transform = Transform3D(0.5, 0, 0, 0, 0.5, 0, 0, 0, 0.5, 0, -0.5, 0)

[node name="Skeleton3D" parent="bomber-guy/rig" index="0"]
bones/0/position = Vector3(3.20746e-15, 0.583857, -0.0893679)
bones/0/rotation = Quaternion(0.255292, -7.97688e-15, 1.00226e-14, 0.966864)
bones/1/rotation = Quaternion(0.0205548, 1.19184e-07, 2.45031e-09, 0.999789)
bones/1/scale = Vector3(1.01931, 0.96247, 1.01931)
bones/2/rotation = Quaternion(0.096395, -4.59033e-12, 2.30305e-08, 0.995343)
bones/2/scale = Vector3(1.04897, 0.907002, 1.05131)
bones/3/rotation = Quaternion(-0.181044, -1.17239e-07, -2.15821e-08, 0.983475)
bones/3/scale = Vector3(0.935252, 1.1196, 0.963441)
bones/4/rotation = Quaternion(0.376178, 1.10453e-07, 4.48439e-08, 0.926548)
bones/4/scale = Vector3(0.922825, 1.17435, 0.922825)
bones/5/rotation = Quaternion(-0.0765415, -3.93158e-08, -1.58714e-08, 0.997066)
bones/5/scale = Vector3(1.00009, 1.00661, 0.995893)
bones/6/rotation = Quaternion(-0.43687, -4.72473e-08, -2.26897e-08, 0.899525)
bones/6/scale = Vector3(1.08356, 1.00264, 0.945663)
bones/7/position = Vector3(-3.6176e-08, 0.583857, -0.0893679)
bones/7/rotation = Quaternion(-0.135651, -0.571304, -0.59224, 0.551781)
bones/8/position = Vector3(3.6176e-08, 0.583857, -0.0893679)
bones/8/rotation = Quaternion(-0.135651, 0.571304, 0.59224, 0.551781)
bones/9/position = Vector3(0.185305, 0.611893, -0.0339381)
bones/9/rotation = Quaternion(0.905251, -2.21189e-05, -0.000117811, 0.424877)
bones/9/scale = Vector3(1.00629, 0.987535, 1.00629)
bones/10/rotation = Quaternion(1.23085e-07, -5.52346e-06, -2.98159e-11, 1)
bones/11/rotation = Quaternion(0.385459, 6.31284e-06, -3.32111e-05, 0.922725)
bones/11/scale = Vector3(1.0012, 0.988229, 1.01087)
bones/12/rotation = Quaternion(-9.67563e-08, 8.29257e-06, -2.16274e-09, 1)
bones/13/rotation = Quaternion(-0.505198, 0.000109451, 2.25149e-05, 0.863003)
bones/13/scale = Vector3(0.992628, 0.997842, 1.00979)
bones/14/rotation = Quaternion(5.02433e-08, 0.977548, -0.210714, 2.33145e-07)
bones/14/scale = Vector3(0.99948, 1.00078, 0.999746)
bones/15/position = Vector3(-0.185305, 0.611893, -0.0339381)
bones/15/rotation = Quaternion(0.99633, 4.19078e-05, -0.000130651, -0.08559)
bones/15/scale = Vector3(1.00638, 0.987357, 1.00638)
bones/17/rotation = Quaternion(0.389683, 1.45563e-05, -3.68506e-05, 0.920949)
bones/17/scale = Vector3(1.00122, 0.987896, 1.0112)
bones/18/rotation = Quaternion(-4.84353e-08, 4.04099e-06, 1.10813e-08, 1)
bones/19/rotation = Quaternion(-0.871762, 8.53352e-05, 7.82363e-05, 0.48993)
bones/19/scale = Vector3(0.992445, 0.99873, 1.0091)
bones/20/rotation = Quaternion(1.02643e-08, 0.977516, -0.210861, -2.46051e-07)
bones/20/scale = Vector3(1.00003, 0.999961, 1.00001)
bones/21/position = Vector3(0.0507796, 1.02886, 0.287226)
bones/21/rotation = Quaternion(-0.472842, -0.270157, -0.416495, 0.727989)
bones/22/position = Vector3(0.242455, 1.04749, 0.191412)
bones/22/rotation = Quaternion(-0.280276, 0.143785, -0.88237, 0.349562)
bones/22/scale = Vector3(1.07211, 0.87008, 1.07211)
bones/23/rotation = Quaternion(3.18531e-08, 0.0853649, 8.32473e-08, 0.99635)
bones/24/rotation = Quaternion(0.421944, 0.147999, -0.308633, 0.839527)
bones/24/scale = Vector3(0.960023, 0.997541, 1.06918)
bones/25/rotation = Quaternion(2.97979e-08, -0.026165, -1.54692e-07, 0.999658)
bones/26/rotation = Quaternion(0.143976, -0.0275267, 0.0637975, 0.987139)
bones/26/scale = Vector3(1.00575, 0.989855, 1.00455)
bones/27/position = Vector3(-0.0507796, 1.02886, 0.287226)
bones/27/rotation = Quaternion(-0.472842, 0.270157, 0.416495, 0.727989)
bones/28/position = Vector3(-0.242455, 1.04749, 0.191412)
bones/28/rotation = Quaternion(-0.425562, -0.363618, 0.693798, 0.453127)
bones/28/scale = Vector3(0.983063, 1.03476, 0.983063)
bones/29/rotation = Quaternion(3.98409e-08, -0.0370571, -2.15209e-07, 0.999313)
bones/30/rotation = Quaternion(0.232389, -0.0893491, 0.253375, 0.93478)
bones/30/scale = Vector3(1.06864, 0.878868, 1.06637)
bones/31/rotation = Quaternion(-3.48398e-08, -0.117463, 1.57868e-07, 0.993077)
bones/32/rotation = Quaternion(0.41303, -0.11386, 0.189505, 0.883476)
bones/32/scale = Vector3(0.954337, 1.01045, 1.05417)
bones/33/position = Vector3(0.1184, 1.00881, 0.15031)
bones/33/rotation = Quaternion(-1.44276e-14, 0.544482, 0.838773, -2.01673e-14)
bones/34/position = Vector3(-0.1184, 1.00881, 0.15031)
bones/34/rotation = Quaternion(-1.44276e-14, 0.544482, 0.838773, -2.01673e-14)

[node name="AnimationTree" type="AnimationTree" parent="."]
tree_root = SubResource("AnimationNodeBlendTree_8to0y")
anim_player = NodePath("../bomber-guy/AnimationPlayer")
Expand Down Expand Up @@ -227,5 +162,3 @@ replication_config = SubResource("SceneReplicationConfig_4cjl8")
script = ExtResource("8_pj7o0")
root = NodePath("..")
properties = Array[String]([":transform"])

[editable path="bomber-guy"]
27 changes: 27 additions & 0 deletions examples/forest-brawl/scenes/moving-platform.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[gd_scene load_steps=5 format=3 uid="uid://cjavt41cokqv5"]

[ext_resource type="Script" path="res://examples/forest-brawl/scripts/moving-platform.gd" id="1_q5d25"]
[ext_resource type="PackedScene" uid="uid://bnyoffewc4b" path="res://examples/forest-brawl/models/kenney-platformer-kit/blockHexagon.glb" id="2_h7k7v"]
[ext_resource type="Script" path="res://addons/netfox/tick-interpolator.gd" id="3_b65sx"]

[sub_resource type="ConvexPolygonShape3D" id="ConvexPolygonShape3D_y7a63"]
points = PackedVector3Array(-0.557, 0.890101, -0.299866, 0.648933, 0.781605, 0.229175, 0.62806, 0.823296, 0.270961, -0.0369031, 0.0136385, 0.556084, 0.0421915, 0.0163715, -0.552961, -0.163975, 0.823296, 0.646129, -0.5, 0.00740408, 0.281541, 0.0426543, 0.989841, -0.559451, 0.48164, 0.0113339, -0.2993, -0.495398, 0.988618, 0.298944, 0.523822, 0.76081, -0.45848, -0.5, 0.00740408, -0.281541, 0.48164, 0.0113339, 0.2993, 0.499254, 0.986877, 0.297878, -0.143102, 0.76081, -0.667022, 0.148614, 0.76081, 0.667022, -0.643422, 0.781605, 0.249937, 0.499253, 0.986876, -0.297878, -0.0372869, 0.991605, 0.561466, -0.518184, 0.76081, 0.45848, 0.148614, 0.802501, -0.667022, 0.648933, 0.781605, -0.229175, -0.643422, 0.781605, -0.249937, 0.523822, 0.76081, 0.45848, -0.518184, 0.76081, -0.45848, -0.495399, 0.988619, -0.298944, -0.0368981, 0.0136993, -0.556015, 0.0421962, 0.016314, 0.553027, -0.143102, 0.76081, 0.667022, -0.163975, 0.823296, -0.646129, 0.148614, 0.802501, 0.667022, -0.037283, 0.991557, -0.561412)

[node name="Moving Platform" type="AnimatableBody3D"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -2, 0, -6)
sync_to_physics = false
script = ExtResource("1_q5d25")
speed = 1.0

[node name="blockHexagon" parent="." instance=ExtResource("2_h7k7v")]

[node name="CollisionShape3D" type="CollisionShape3D" parent="."]
shape = SubResource("ConvexPolygonShape3D_y7a63")

[node name="Target" type="Node3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, -4)

[node name="TickInterpolator" type="Node" parent="." node_paths=PackedStringArray("root")]
script = ExtResource("3_b65sx")
root = NodePath("..")
properties = Array[String]([":transform"])
13 changes: 12 additions & 1 deletion examples/forest-brawl/scripts/brawler-controller.gd
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,19 @@ func _rollback_tick(delta, tick, is_fresh):
if is_fresh:
GameEvents.on_brawler_respawn.emit(self)

# Add the gravity.
# Apply gravity
_force_update_is_on_floor()
if not is_on_floor():
velocity.y -= gravity * delta

# Stick to moving platforms
var platform_velocity := Vector3.ZERO
var collision_result := KinematicCollision3D.new()
if test_move(global_transform, Vector3.DOWN * delta, collision_result):
var collider := collision_result.get_collider()
if collider is MovingPlatform:
var platform := collider as MovingPlatform
platform_velocity = platform.get_velocity()

# Jump
if input.movement.y > 0 and is_on_floor():
Expand All @@ -124,9 +133,11 @@ func _rollback_tick(delta, tick, is_fresh):
transform = transform.looking_at(position + Vector3(input.aim.x, 0, input.aim.z), Vector3.UP, true).scaled_local(scale)

# Apply movement
velocity += platform_velocity
velocity *= NetworkTime.physics_factor
move_and_slide()
velocity /= NetworkTime.physics_factor
velocity -= platform_velocity

# Death
if position.y < -death_depth and tick > respawn_tick and is_fresh:
Expand Down
27 changes: 27 additions & 0 deletions examples/forest-brawl/scripts/moving-platform.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
extends AnimatableBody3D
class_name MovingPlatform

@export var speed: float = 2.
@onready var _origin: Vector3 = global_position
@onready var _target: Vector3 = $Target.global_position
@onready var _distance: float = _origin.distance_to(_target)
var _velocity: Vector3 = Vector3.ZERO

func get_velocity() -> Vector3:
return _velocity

func _ready():
NetworkRollback.on_prepare_tick.connect(_apply_tick)

func _apply_tick(tick: int):
var previous_position = _get_position_for_tick(tick - 1)
global_position = _get_position_for_tick(tick)

_velocity = (global_position - previous_position) / NetworkTime.ticktime

func _get_position_for_tick(tick: int):
var distance_moved = NetworkTime.ticks_to_seconds(tick) * speed
var progress = distance_moved / _distance
progress = pingpong(progress, 1)

return _origin.lerp(_target, progress)

0 comments on commit a557f98

Please sign in to comment.