enemy1 script

mail@pastecode.io avatar
unknown
plain_text
7 days ago
3.5 kB
0
Indexable
Never
extends CharacterBody2D

class_name Enemy

var hit_particles = preload("res://Scenes/hit_particles.tscn")

@onready var player = get_tree().current_scene.get_node("Player")
@onready var sprite_animator = $SpriteContainer/Sprite2D/SpriteAnimator

enum States {IDLE, WALKING, CHASING, HIT, ATTACKING, DEAD}
var state : States

var can_chase := false

var can_attack := false
var attacking := false

var direction : Vector2
var distance : float

var speed : float
@export var health : int

var knockback_str := 500.0
var knockback_dir : Vector2
@export var knockback_enabled := false
var knockback_tween 

func _ready():
	speed = randf_range(100, 150)

func _process(_delta):
	print(sprite_animator.current_animation)
	_determine_movement()
	_update_sprite()
	
func _determine_movement():
	if attacking:
		pass
	elif can_attack:
		attack_player(player.global_position)
	elif can_chase:
		direction = global_position.direction_to(player.global_position)
		distance = global_position.distance_to(player.global_position)
		var movement = direction * speed
		velocity = movement + knockback_dir
	else:
		velocity = knockback_dir
	move_and_slide()

func _update_sprite():
	if can_chase:
		state = States.CHASING
	else:
		state = States.IDLE
	match state:
		States.IDLE:
			sprite_animator.play("Idle")
		States.CHASING or States.WALKING:
			sprite_animator.play("Walk")
		States.HIT:
			print("test")
			sprite_animator.play("Hit")

func hit(movement : Vector2, rot : float, damage : int):
	# Update state
	state = States.HIT
	
	# Play animation
	$HitFlashAnimationPlayer.play("hit_flash")
	# Light screen shake
	player.get_node("PlayerCamera").apply_shake(5, 10)
	
	# Decrease health + Check if dead
	health -= damage
	if health > 0:
		# Knockback
		knockback(movement)
		# Particle effect
		spawn_hit_particles(rot)

	else:
		sprite_animator.play("Dead")
		state = States.DEAD 
		Engine.time_scale = 0.01
		await get_tree().create_timer(0.015).timeout
		Engine.time_scale = 1
		queue_free()

func knockback(move_dir : Vector2, strength = knockback_str):
	state = States.HIT
	if !knockback_enabled:
		knockback_dir = move_dir.normalized() * strength
		knockback_tween = get_tree().create_tween()
		knockback_tween.tween_property(self, "knockback_dir", Vector2.ZERO, 0.2)
		await knockback_tween.finished
		state = States.CHASING

func spawn_hit_particles(rot : float):
	var instance = hit_particles.instantiate()
	instance.rotation = rot
	instance.emitting = true
	add_child(instance)

func attack_player(curr_player_pos : Vector2):
	print("test")
	# Set a boolean to indicate that the player is attacking
	attacking = true
	# Play animation where enemy squashes
	sprite_animator.play("SquashAttack")
	# Await the squash animation finished, then play the stretch anim
	await sprite_animator.animation_finished
	# Ease the position to the target position
	# Still keep into account that the enemy can be knocked back
	# Move and slide
	sprite_animator.play("StretchAttack")
	var tween = get_tree().create_tween()
	tween.tween_property(self, "global_position", curr_player_pos, 0.2)
	# Await the position tween to finish, then check for a slide collision
	await tween.finished
	if get_last_slide_collision():
		print("damaged player")
	# Damage the player if there is a slide collision (Implement later)
	
	can_attack = false
	attacking = false
	state = States.IDLE
Leave a Comment