Added SelfMovingPlatform

This commit is contained in:
2026-06-12 18:38:22 +03:00
parent 59d71a4c57
commit 22475d78c2
6 changed files with 208 additions and 16 deletions
+7 -9
View File
@@ -1,6 +1,6 @@
@tool
class_name AbstractPlatform
extends Node2D
extends AnimatableBody2D
const TILE_SIZE = 8
@@ -23,8 +23,9 @@ const MAX_WIDTH = TILE_SIZE * 32
func _ready() -> void:
if not Engine.is_editor_hint():
platform_rect.material.set_shader_parameter("seed", randf())
platform_rect.material.set_shader_parameter("tile_size", TILE_SIZE)
platform_rect.material.set_shader_parameter("seed", randf())
rebuild_platform()
@@ -32,14 +33,11 @@ func rebuild_platform() -> void:
var shape := collision.shape as RectangleShape2D
shape.size.x = platform_width
sub_viewport.size = Vector2(platform_width, TILE_SIZE)
var platfor_size := Vector2(platform_width, TILE_SIZE)
sub_viewport.size = platfor_size
icon_sprite.position = platfor_size/2
platform_rect.size.x = platform_width
platform_rect.position = Vector2.ZERO
icon_sprite.position = Vector2.ZERO
platform_rect.material.set_shader_parameter("platform_width", platform_width)
func _physics_process(_delta: float) -> void:
if Engine.is_editor_hint():
return
+3
View File
@@ -0,0 +1,3 @@
[gd_scene format=3 uid="uid://egknefyib2ui"]
[node name="PlatformPath" type="Line2D" unique_id=2089103569]
+50
View File
@@ -0,0 +1,50 @@
@tool
class_name SelfMovingPlatform
extends AbstractPlatform
const SPEED_FACTOR = 0.025
const ANIMATION_ACTIVE = "active"
@export var is_loop := false
@export var path_follow : PathFollow2D
@export_range(0, 25, 0.1) var speed : float = 10.0
var is_reverse := false
var tween: Tween
func _ready() -> void:
super._ready()
icon_sprite.play(ANIMATION_ACTIVE)
if not Engine.is_editor_hint():
_start_movement()
func _start_movement() -> void:
if tween and tween.is_running():
tween.kill()
var target := 0.0 if is_reverse else 1.0
var distance := absf(target - path_follow.progress_ratio)
var duration := distance / (speed * SPEED_FACTOR)
tween = create_tween()
if not is_loop:
tween.set_trans(Tween.TRANS_SINE)
tween.set_ease(Tween.EASE_IN_OUT)
tween.finished.connect(_on_tween_finished)
tween.tween_property(path_follow, "progress_ratio", target, duration)
func _on_tween_finished() -> void:
if is_loop:
path_follow.progress_ratio = 0
else:
is_reverse = not is_reverse
_start_movement()
@@ -0,0 +1 @@
uid://cwji3vqis6w6b
+119
View File
@@ -0,0 +1,119 @@
[gd_scene format=3 uid="uid://jab32eoyc523"]
[ext_resource type="PackedScene" uid="uid://guvijso3k6eq" path="res://game/platform/abstract_platform.tscn" id="1_jmy7p"]
[ext_resource type="Script" uid="uid://cwji3vqis6w6b" path="res://game/platform/self_moving_platform.gd" id="2_ayqtn"]
[ext_resource type="Shader" uid="uid://d4hm3vjx1rhsf" path="res://game/shaders/platform.gdshader" id="2_ex4q8"]
[ext_resource type="Texture2D" uid="uid://cpcuokqv8u2xa" path="res://images/level/platform.png" id="3_ayqtn"]
[ext_resource type="Texture2D" uid="uid://bi2h4g71bro7v" path="res://images/level/platform-icons.png" id="5_ayqtn"]
[sub_resource type="ViewportTexture" id="ViewportTexture_ayqtn"]
viewport_path = NodePath("SubViewport")
[sub_resource type="ShaderMaterial" id="ShaderMaterial_v7ftt"]
resource_local_to_scene = true
shader = ExtResource("2_ex4q8")
shader_parameter/tile_size = 8.0
shader_parameter/platform_width = 24.0
shader_parameter/atlas = ExtResource("3_ayqtn")
shader_parameter/seed = 0.94376683
[sub_resource type="AtlasTexture" id="AtlasTexture_v7ftt"]
atlas = ExtResource("5_ayqtn")
region = Rect2(0, 48, 8, 8)
[sub_resource type="AtlasTexture" id="AtlasTexture_xuyth"]
atlas = ExtResource("5_ayqtn")
region = Rect2(8, 48, 8, 8)
[sub_resource type="AtlasTexture" id="AtlasTexture_4wvh7"]
atlas = ExtResource("5_ayqtn")
region = Rect2(16, 48, 8, 8)
[sub_resource type="AtlasTexture" id="AtlasTexture_q6fss"]
atlas = ExtResource("5_ayqtn")
region = Rect2(24, 48, 8, 8)
[sub_resource type="AtlasTexture" id="AtlasTexture_myyrw"]
atlas = ExtResource("5_ayqtn")
region = Rect2(32, 48, 8, 8)
[sub_resource type="AtlasTexture" id="AtlasTexture_q3dcw"]
atlas = ExtResource("5_ayqtn")
region = Rect2(40, 48, 8, 8)
[sub_resource type="AtlasTexture" id="AtlasTexture_gb0pd"]
atlas = ExtResource("5_ayqtn")
region = Rect2(32, 48, 8, 8)
[sub_resource type="AtlasTexture" id="AtlasTexture_sqmiv"]
atlas = ExtResource("5_ayqtn")
region = Rect2(24, 48, 8, 8)
[sub_resource type="AtlasTexture" id="AtlasTexture_mvt4t"]
atlas = ExtResource("5_ayqtn")
region = Rect2(16, 48, 8, 8)
[sub_resource type="AtlasTexture" id="AtlasTexture_ppe0t"]
atlas = ExtResource("5_ayqtn")
region = Rect2(8, 48, 8, 8)
[sub_resource type="SpriteFrames" id="SpriteFrames_22kvf"]
animations = [{
"frames": [{
"duration": 1.0,
"texture": SubResource("AtlasTexture_v7ftt")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_xuyth")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_4wvh7")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_q6fss")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_myyrw")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_q3dcw")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_gb0pd")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_sqmiv")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_mvt4t")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_ppe0t")
}],
"loop": true,
"name": &"active",
"speed": 10.0
}]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_xuyth"]
resource_local_to_scene = true
size = Vector2(16, 7)
[node name="SelfMovingPlatform" unique_id=137546339 instance=ExtResource("1_jmy7p")]
script = ExtResource("2_ayqtn")
speed = 10.0
[node name="Sprite2D" parent="." index="0" unique_id=264863796]
texture = SubResource("ViewportTexture_ayqtn")
[node name="PlatformRect" parent="SubViewport" parent_id_path=PackedInt32Array(226200089) index="0" unique_id=794315918]
material = SubResource("ShaderMaterial_v7ftt")
[node name="IconSprite" parent="SubViewport" parent_id_path=PackedInt32Array(226200089) index="1" unique_id=1917841765]
position = Vector2(8, 4)
sprite_frames = SubResource("SpriteFrames_22kvf")
animation = &"active"
frame_progress = 0.974508
[node name="CollisionShape2D" parent="." index="2" unique_id=175449124]
shape = SubResource("RectangleShape2D_xuyth")