mirror of
https://github.com/GDQuest/godot-platformer-2d.git
synced 2026-07-17 16:34:54 +00:00
Simplify the MovingPlatform and Waypoints
This commit is contained in:
parent
7ce97ad1b1
commit
5c540c668b
4 changed files with 76 additions and 89 deletions
|
|
@ -1,43 +1,45 @@
|
|||
# Moving platform, moves to target positions given by the Waypoints node
|
||||
# extends KinematicBody2D
|
||||
tool
|
||||
extends KinematicBody2D
|
||||
|
||||
# # onready var wait_timer: Timer = $Timer
|
||||
# onready var waypoints := $Waypoints
|
||||
onready var timer: Timer = $Timer
|
||||
onready var tween: Tween = $Tween
|
||||
onready var waypoints: = $Waypoints
|
||||
|
||||
# # export var speed := 400.0
|
||||
export var speed: = 400.0
|
||||
export var wait_time: = 1.0 setget set_wait_time
|
||||
|
||||
# # var target_position := Vector2()
|
||||
|
||||
#
|
||||
# # func _ready() -> void:
|
||||
# if not waypoints:
|
||||
# print("Missing Waypoints node for %s: %s" % [name, get_path()])
|
||||
# assert(waypoints)
|
||||
# set_physics_process(false)
|
||||
# return
|
||||
# position = waypoints.get_start_position()
|
||||
# target_position = waypoints.get_next_point_position()
|
||||
func _ready() -> void:
|
||||
if Engine.editor_hint:
|
||||
return
|
||||
if not waypoints:
|
||||
printerr("Missing Waypoints node for %s: %s" % [name, get_path()])
|
||||
return
|
||||
position = waypoints.get_start_position()
|
||||
timer.start()
|
||||
|
||||
#
|
||||
# # func _physics_process(delta: float) -> void:
|
||||
# var direction := (target_position - position).normalized()
|
||||
# var motion := direction * speed * delta
|
||||
# var distance_to_target := position.distance_to(target_position)
|
||||
# if motion.length() > distance_to_target:
|
||||
# position = target_position
|
||||
# target_position = waypoints.get_next_point_position()
|
||||
# set_physics_process(false)
|
||||
# wait_timer.start()
|
||||
# else:
|
||||
# position += motion
|
||||
|
||||
#
|
||||
# # func _draw() -> void:
|
||||
# var shape := $CollisionShape2D
|
||||
# var extents: Vector2 = shape.shape.extents * 2.0
|
||||
# var rect := Rect2(shape.position - extents / 2.0, extents)
|
||||
# draw_rect(rect, Color('fff'))
|
||||
func _draw() -> void:
|
||||
var shape: = $CollisionShape2D
|
||||
var extents: Vector2 = shape.shape.extents * 2.0
|
||||
var rect: = Rect2(shape.position - extents / 2.0, extents)
|
||||
draw_rect(rect, Color('fff'))
|
||||
|
||||
#
|
||||
# # func _on_Timer_timeout() -> void:
|
||||
# set_physics_process(true)
|
||||
|
||||
func _on_Timer_timeout() -> void:
|
||||
var target_position: Vector2 = waypoints.get_next_point_position()
|
||||
var distance_to_target: = position.distance_to(target_position)
|
||||
tween.interpolate_property(self, "position", position, target_position, distance_to_target / speed)
|
||||
tween.start()
|
||||
|
||||
|
||||
func _on_Tween_tween_all_completed() -> void:
|
||||
timer.start()
|
||||
|
||||
|
||||
func set_wait_time(value: float) -> void:
|
||||
wait_time = value
|
||||
if not timer:
|
||||
yield(self, "ready")
|
||||
timer.wait_time = wait_time
|
||||
|
|
|
|||
|
|
@ -2,11 +2,13 @@
|
|||
|
||||
[ext_resource path="res://src/Objects/MovingPlatform/MovingPlatform.gd" type="Script" id=1]
|
||||
|
||||
|
||||
[sub_resource type="RectangleShape2D" id=1]
|
||||
extents = Vector2( 89, 18 )
|
||||
|
||||
[node name="MovingPlatform" type="KinematicBody2D"]
|
||||
collision_layer = 2
|
||||
collision_mask = 0
|
||||
motion/sync_to_physics = true
|
||||
script = ExtResource( 1 )
|
||||
|
||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
|
||||
|
|
@ -15,4 +17,7 @@ shape = SubResource( 1 )
|
|||
[node name="Timer" type="Timer" parent="."]
|
||||
one_shot = true
|
||||
|
||||
[node name="Tween" type="Tween" parent="."]
|
||||
playback_process_mode = 0
|
||||
[connection signal="timeout" from="Timer" to="." method="_on_Timer_timeout"]
|
||||
[connection signal="tween_all_completed" from="Tween" to="." method="_on_Tween_tween_all_completed"]
|
||||
|
|
|
|||
|
|
@ -1,87 +1,68 @@
|
|||
tool
|
||||
extends Node2D
|
||||
extends Line2D
|
||||
|
||||
enum Mode {CYCLE, PING_PONG}
|
||||
|
||||
export(Mode) var mode := Mode.CYCLE setget set_mode
|
||||
export(Mode) var mode: = Mode.CYCLE setget set_mode
|
||||
|
||||
export var editor_process := true setget set_editor_process
|
||||
export var triangle_color: = Color(0.722656, 0.908997, 1)
|
||||
export var triangle_radius: = 6.0
|
||||
|
||||
export var line_color := Color(0.228943, 0.710254, 0.945312)
|
||||
export var line_width := 10.0
|
||||
export var triangle_color := Color(0.722656, 0.908997, 1)
|
||||
|
||||
var _active_point_index := 0
|
||||
var _direction := 1
|
||||
var _active_point_index: = 0
|
||||
var _direction: = 1
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
set_as_toplevel(true)
|
||||
if not Engine.editor_hint:
|
||||
set_process(false)
|
||||
|
||||
|
||||
func _process(delta: float) -> void:
|
||||
update()
|
||||
# set_as_toplevel(true)
|
||||
visible = Engine.editor_hint
|
||||
|
||||
|
||||
func _draw() -> void:
|
||||
if not Engine.editor_hint:
|
||||
return
|
||||
if not get_child_count() > 1:
|
||||
if not points.size() > 1:
|
||||
return
|
||||
var points := PoolVector2Array()
|
||||
var triangles := []
|
||||
var last_point := Vector2.ZERO
|
||||
for child in get_children():
|
||||
assert(child is Node2D)
|
||||
points.append(child.position)
|
||||
if points.size() > 1:
|
||||
var center: Vector2 = (child.position + last_point) / 2
|
||||
var angle := last_point.angle_to_point(child.position)
|
||||
triangles.append({center=center, angle=angle})
|
||||
last_point = child.position
|
||||
|
||||
# Add last segment and arrow if in cycling mode
|
||||
if mode == Mode.CYCLE:
|
||||
var first_child_position: Vector2 = get_child(0).position
|
||||
points.append(first_child_position)
|
||||
var center := (first_child_position + last_point) / 2
|
||||
var angle := last_point.angle_to_point(first_child_position)
|
||||
triangles.append({center=center, angle=angle})
|
||||
var triangles: = []
|
||||
var previous_point: = points[0]
|
||||
for point in points:
|
||||
if point == points[0] or (point == points[-1] and mode == Mode.CYCLE):
|
||||
continue
|
||||
triangles.append({
|
||||
center=(point + previous_point) / 2,
|
||||
angle=previous_point.angle_to_point(point)}
|
||||
)
|
||||
previous_point = point
|
||||
|
||||
draw_polyline(points, line_color, line_width, true)
|
||||
if mode == Mode.CYCLE:
|
||||
triangles.append({
|
||||
center=(points[-1] - points[0]) / 2,
|
||||
angle=points[-1].angle_to_point(points[0])}
|
||||
)
|
||||
draw_line(points[-1], points[0], default_color, width)
|
||||
for triangle in triangles:
|
||||
DrawingUtils.draw_triangle(self, triangle['center'], triangle['angle'], line_width * 2.0, triangle_color)
|
||||
DrawingUtils.draw_triangle(self, triangle['center'], triangle['angle'], triangle_radius, triangle_color)
|
||||
|
||||
|
||||
func get_start_position() -> Vector2:
|
||||
return get_child(0).global_position
|
||||
return points[0]
|
||||
|
||||
|
||||
func get_current_point_position() -> Vector2:
|
||||
return get_child(_active_point_index).global_position
|
||||
return points[_active_point_index]
|
||||
|
||||
|
||||
func get_next_point_position():
|
||||
match mode:
|
||||
Mode.CYCLE:
|
||||
_active_point_index = (_active_point_index + 1) % get_child_count()
|
||||
_active_point_index = (_active_point_index + 1) % points.size()
|
||||
Mode.PING_PONG:
|
||||
var index := _active_point_index + _direction
|
||||
if index < 0 or index > get_child_count() - 1:
|
||||
var index: = _active_point_index + _direction
|
||||
if index < 0 or index > points.size() - 1:
|
||||
_direction *= -1
|
||||
_active_point_index += _direction
|
||||
return get_current_point_position()
|
||||
|
||||
|
||||
func set_editor_process(value: bool) -> void:
|
||||
editor_process = value
|
||||
if not Engine.editor_hint:
|
||||
return
|
||||
set_process(value)
|
||||
|
||||
|
||||
func set_mode(value: int) -> void:
|
||||
# assert(value in Mode)
|
||||
mode = value
|
||||
update()
|
||||
|
|
|
|||
|
|
@ -2,6 +2,5 @@
|
|||
|
||||
[ext_resource path="res://src/Objects/MovingPlatform/Waypoints.gd" type="Script" id=1]
|
||||
|
||||
|
||||
[node name="Waypoints" type="Node2D"]
|
||||
[node name="Waypoints" type="Line2D"]
|
||||
script = ExtResource( 1 )
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue