Files
retromariogodot/scripts/enemies/coopa.gd
T
2024-08-03 10:52:53 +02:00

108 lines
2.3 KiB
GDScript

extends RigidBody2D
@export var speed = -70
@onready var ray_cast_left = $RayCastLeft
@onready var ray_cast_right = $RayCastRight
@onready var animated_sprite_2d = $AnimatedSprite2D
@onready var timer = $Timer
@onready var die = $die
@onready var ray_cast_left_2 = $RayCastLeft2
@onready var ray_cast_right_2 = $RayCastRight2
var innocent = false
@onready var timer_2 = $Timer2
var is_down = false
@onready var collision_shape_2d = $CollisionShape2D
@onready var ray_cast_left_3 = $RayCastLeft3
@onready var ray_cast_right_3 = $RayCastRight3
func _ready():
var coll = CapsuleShape2D.new()
coll.height = 24
coll.radius = 7
collision_shape_2d.shape = coll
func _physics_process(delta):
linear_velocity.x = speed
if ray_cast_left_2 == null:
return
if ray_cast_left.is_colliding() or ray_cast_left_2.is_colliding():
speed = abs(speed)
animated_sprite_2d.flip_h = true
elif ray_cast_right.is_colliding() or ray_cast_right_2.is_colliding():
speed = -abs(speed)
animated_sprite_2d.flip_h = false
var other = null
if ray_cast_left_3.is_colliding():
other = ray_cast_left_3.get_collider()
elif ray_cast_right_3.is_colliding():
other = ray_cast_right_3.get_collider()
if is_down and speed != 0 and other != null:
other.f_die()
if innocent:
return
var body = null
if ray_cast_left_2.is_colliding():
if speed != 0:
body = ray_cast_left_2.get_collider()
else:
speed = 160
innocent = true
die_sound.play()
timer_2.start()
elif ray_cast_right_2.is_colliding():
if speed != 0:
body = ray_cast_right_2.get_collider()
else:
speed = -160
innocent = true
die_sound.play()
timer_2.start()
if body != null:
body.shrink()
innocent = true
timer_2.start()
@onready var die_sound = $dieSound
func f_die():
die_sound.play()
speed = 0
innocent = true
collision_layer = 32
timer.start()
func f_shrink(body):
die_sound.play()
if not is_down:
speed = 0
collision_shape_2d.shape.height -= 9
die.position.y += 2
animated_sprite_2d.play("shrink")
is_down = true
ray_cast_left_2.scale.x = 2
ray_cast_right_2.scale.x = 2
elif speed == 0:
speed = 160 if body.position.x < position.x else -160
else:
speed = 0
func _on_timer_2_timeout():
innocent = false
func _on_die_body_entered(body):
f_shrink(body)
body.velocity.y = body.JUMPt_velocity/1.5
func _on_timer_timeout():
queue_free()