added moving platform
This commit is contained in:
parent
d5999d41c7
commit
c9ce22b112
8 changed files with 175 additions and 18 deletions
43
objects/environment/moving_platform/moving_platform.gd
Normal file
43
objects/environment/moving_platform/moving_platform.gd
Normal file
|
@ -0,0 +1,43 @@
|
|||
extends KinematicBody2D
|
||||
|
||||
export var direction = 1
|
||||
export var speed = 20
|
||||
#How far to move
|
||||
export var left_up_boundry = 0.0
|
||||
export var right_down_boundry = 0.0
|
||||
#Move horizontal or vertical
|
||||
export(int, "Horizontal", "Vertical") var move_direction
|
||||
#Onreadys
|
||||
onready var startpos = position
|
||||
onready var sprite = $Sprite
|
||||
onready var hitbox = $CollisionShape2D
|
||||
|
||||
func _ready():
|
||||
sprite.set_region_rect(Rect2(0,0,8 * scale.x,4))
|
||||
hitbox.scale.x = scale.x
|
||||
scale.x = 1
|
||||
left_up_boundry *= 8
|
||||
right_down_boundry *= 8
|
||||
|
||||
func _physics_process(delta):
|
||||
if move_direction == 0:
|
||||
move_side_to_side(delta)
|
||||
else: move_up_and_down(delta)
|
||||
|
||||
func move_side_to_side(delta):
|
||||
#Move
|
||||
position.x += direction * (speed * delta)
|
||||
#Switch dir
|
||||
if position.x >= startpos.x + (right_down_boundry):
|
||||
direction = -1
|
||||
if position.x <= startpos.x + (-left_up_boundry):
|
||||
direction = 1
|
||||
|
||||
func move_up_and_down(delta):
|
||||
#Move
|
||||
position.y += direction * (speed * delta)
|
||||
#Switch dir
|
||||
if position.y >= startpos.y + (right_down_boundry):
|
||||
direction = -1
|
||||
if position.y <= startpos.y + (-left_up_boundry):
|
||||
direction = 1
|
23
objects/environment/moving_platform/moving_platform.tscn
Normal file
23
objects/environment/moving_platform/moving_platform.tscn
Normal file
|
@ -0,0 +1,23 @@
|
|||
[gd_scene load_steps=4 format=2]
|
||||
|
||||
[ext_resource path="res://graphics/moving_platform/moving_platform.png" type="Texture" id=1]
|
||||
[ext_resource path="res://objects/environment/moving_platform/moving_platform.gd" type="Script" id=2]
|
||||
|
||||
[sub_resource type="RectangleShape2D" id=1]
|
||||
extents = Vector2( 4, 2 )
|
||||
|
||||
[node name="MovingPlatform" type="KinematicBody2D"]
|
||||
motion/sync_to_physics = true
|
||||
script = ExtResource( 2 )
|
||||
|
||||
[node name="Sprite" type="Sprite" parent="."]
|
||||
position = Vector2( 4, 2 )
|
||||
texture = ExtResource( 1 )
|
||||
region_enabled = true
|
||||
region_rect = Rect2( 0, 0, 8, 4 )
|
||||
region_filter_clip = true
|
||||
|
||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
|
||||
position = Vector2( 4, 2 )
|
||||
shape = SubResource( 1 )
|
||||
one_way_collision = true
|
Loading…
Add table
Add a link
Reference in a new issue