50 lines
1.4 KiB
GDScript
50 lines
1.4 KiB
GDScript
extends KinematicBody2D
|
|
|
|
var velocity = Vector2.ZERO
|
|
var is_moving = false
|
|
var is_holding_shard = true
|
|
onready var death_particles = $DeathSplatter
|
|
onready var sprite = $Sprite
|
|
onready var anims = $AnimationPlayer
|
|
onready var raycast = $RayCast2D
|
|
onready var shard_position = $Position2D
|
|
const shard = preload("res://objects/collectibles/shard.tscn")
|
|
|
|
func _ready():
|
|
raycast.add_exception($Hitbox)
|
|
|
|
func _physics_process(delta):
|
|
if is_moving: velocity.x = -50
|
|
#Stop when see something
|
|
if raycast.is_colliding(): velocity.x = 0
|
|
velocity.y += 128 * delta
|
|
velocity = move_and_slide_with_snap(velocity, Vector2.DOWN, Vector2.UP, true)
|
|
#Anims
|
|
if velocity.x == 0:
|
|
anims.play("idle")
|
|
else:
|
|
anims.play("walk")
|
|
#Stop at the end of path and give shard
|
|
if position.x <= 80:
|
|
is_moving = false
|
|
velocity.x = 0
|
|
if is_holding_shard:
|
|
anims.play("give")
|
|
var ShardInstance = shard.instance()
|
|
ShardInstance.global_position = Vector2(shard_position.global_position.x,shard_position.global_position.y)
|
|
ShardInstance.number = 1
|
|
get_owner().add_child(ShardInstance)
|
|
is_holding_shard = false
|
|
|
|
|
|
func switch_action():
|
|
is_moving = true
|
|
|
|
func die():
|
|
#Create particles
|
|
remove_child(death_particles)
|
|
get_parent().add_child(death_particles)
|
|
death_particles.global_position = global_position
|
|
death_particles.emitting = true
|
|
Audio.play_sound(Audio.a_die,Audio.ac_die)
|
|
queue_free()
|