rolling fiend automatically detects floor surface and can take inside corners again

This commit is contained in:
Haze Weathers 2023-01-17 16:56:12 -05:00
parent 47a369f9fb
commit 0460f6ec8e
3 changed files with 34 additions and 19 deletions

View file

@ -4,19 +4,29 @@ 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.DOWN
var floor_direction = Vector2.ZERO
func _ready():
if clockwise:
$AnimatedSprite.flip_h = true
floor_test.set_shape($FloorTestShape.shape)
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
var space_state = get_world_2d().direct_space_state
floor_test.transform = global_transform.translated(floor_direction)
if space_state.collide_shape(floor_test, 1).empty():
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

View file

@ -4,9 +4,6 @@
[ext_resource path="res://objects/enemy/rolling_fiend.gd" type="Script" id=3]
[ext_resource path="res://scripts/1px_border.gdshader" type="Shader" id=4]
[sub_resource type="RectangleShape2D" id=8]
extents = Vector2( 4, 4 )
[sub_resource type="ShaderMaterial" id=5]
shader = ExtResource( 4 )
shader_param/border_color = Color( 0, 0, 0, 1 )
@ -28,6 +25,9 @@ animations = [ {
"speed": 10.0
} ]
[sub_resource type="RectangleShape2D" id=8]
extents = Vector2( 3.5, 3.5 )
[sub_resource type="RectangleShape2D" id=4]
extents = Vector2( 4, 4 )
@ -36,16 +36,19 @@ script = ExtResource( 3 )
score_for_killing = 40
move_speed = 50.0
[node name="FloorTestShape" type="CollisionShape2D" parent="."]
shape = SubResource( 8 )
[node name="AnimatedSprite" type="AnimatedSprite" parent="."]
material = SubResource( 5 )
position = Vector2( 4, 4 )
frames = SubResource( 3 )
frame = 1
playing = true
[node name="FloorTestShape" type="CollisionShape2D" parent="."]
position = Vector2( 4, 4 )
shape = SubResource( 8 )
[node name="Hitbox" type="Area2D" parent="."]
position = Vector2( 4, 4 )
[node name="CollisionShape2D" type="CollisionShape2D" parent="Hitbox"]
shape = SubResource( 4 )