hero-mark-2/objects/enemy/rolling_fiend.gd

32 lines
1 KiB
GDScript

extends "res://objects/enemy/enemy.gd"
export var move_speed: float = 0.0
export var clockwise: bool = false
onready var floor_test = Physics2DShapeQueryParameters.new()
onready var test_shape = $FloorTestShape
var floor_direction = Vector2.ZERO
func _ready():
if clockwise:
$AnimatedSprite.flip_h = true
floor_test.set_shape(test_shape.shape)
floor_test.collision_layer = 1
for dir in [Vector2.DOWN, Vector2.UP, Vector2.LEFT, Vector2.RIGHT]:
if is_on_surface(dir):
floor_direction = dir
break
func _physics_process(delta):
var rot_dir = -1.5708 if clockwise else 1.5708
if !is_on_surface(floor_direction):
floor_direction = floor_direction.rotated(-rot_dir)
elif is_on_surface(floor_direction.rotated(rot_dir)):
floor_direction = floor_direction.rotated(rot_dir)
position += floor_direction.rotated(rot_dir) * move_speed * delta
func is_on_surface(dir):
var space_state = get_world_2d().direct_space_state
floor_test.transform = test_shape.global_transform.translated(dir)
return space_state.collide_shape(floor_test, 1).size() > 0