47 lines
1.2 KiB
GDScript
47 lines
1.2 KiB
GDScript
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
|
|
#Better offset for vertical platforms
|
|
if move_direction == 1:
|
|
right_down_boundry -= 5
|
|
left_up_boundry -= 1
|
|
|
|
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
|