Blud_Mage/Scripts/Enemy.gd

128 lines
2.8 KiB
GDScript3
Raw Normal View History

2023-10-10 12:56:34 -04:00
extends CharacterBody2D
2023-10-10 10:22:04 -04:00
2023-10-10 12:56:34 -04:00
@onready var levelup = [300, 900, 2700, 6500, 14000, 23000, 34000, 48000, 64000]
@onready var is_facing_left: bool = false
@onready var is_attacking: bool = false
@onready var is_dying: bool = false
@export var level: int = 1
@export var hp: int = 100
@export var maxhp: int = 100
2023-10-10 13:34:26 -04:00
@export var maxvel: float = 2
2023-10-10 12:56:34 -04:00
@export var experience: int = 0
@export var state: String = "Idle"
2023-10-10 14:54:21 -04:00
@export var damage: int = 5
2023-10-10 12:56:34 -04:00
@onready var anim: Node = get_node("Sprite")
@onready var cooldown: Node = get_node("Atk_cooldown")
@onready var lvlanim: Node = get_node("LvlUp")
2023-10-10 13:34:26 -04:00
@onready var lvlsnd: Node = get_node("LvlUpSnd")
2023-10-10 14:24:19 -04:00
@onready var hitanim: Node = get_node("Hit")
2023-10-10 14:35:20 -04:00
@onready var atk1l: Node = get_node("AttackArea/Attack1CollisionL")
@onready var atk1r: Node = get_node("AttackArea/Attack1CollisionR")
@onready var atk2l: Node = get_node("AttackArea/Attack2CollisionL")
@onready var atk2r: Node = get_node("AttackArea/Attack2CollisionR")
2023-10-10 10:22:04 -04:00
# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
2023-10-10 12:56:34 -04:00
2023-10-10 10:22:04 -04:00
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
2023-10-10 12:56:34 -04:00
if abs(velocity.x) < 0.1 && abs(velocity.y) < 0.1 && !is_attacking:
setAnimState("Idle")
elif !is_attacking:
setAnimState("Run")
is_facing_left = velocity.x >= 0
# AI STUFF
2023-10-10 13:34:26 -04:00
#velocity = processAI(objects,velocity,delta)
2023-10-10 12:56:34 -04:00
2023-10-10 14:54:21 -04:00
move_and_slide()
2023-10-10 12:56:34 -04:00
2023-10-10 13:34:26 -04:00
func processAI(objs, delta):
var vec: Vector2 = Vector2(0,0)
for obj in objs:
vec += obj.vecpos * obj.weight * (1 / (position - obj.vecpos)) * delta
return vec.normalized()
2023-10-10 12:56:34 -04:00
func attack():
is_attacking = true
if (level > 1):
setAnimState("Attack2")
if is_facing_left:
2023-10-10 14:35:20 -04:00
atk2r.set_disabled(false)
2023-10-10 15:02:43 -04:00
else:
atk2l.set_disabled(false)
2023-10-10 12:56:34 -04:00
else:
setAnimState("Attack")
if is_facing_left:
2023-10-10 14:35:20 -04:00
atk1r.set_disabled(false)
2023-10-10 15:02:43 -04:00
else:
atk1l.set_disabled(false)
2023-10-10 12:56:34 -04:00
2023-10-10 10:22:04 -04:00
2023-10-10 12:56:34 -04:00
func stop_attack():
is_attacking = false
2023-10-10 14:54:19 -04:00
atk1l.set_disabled(true)
atk1r.set_disabled(true)
atk2l.set_disabled(true)
atk2r.set_disabled(true)
2023-10-10 12:56:34 -04:00
func setAnimState(newstate):
anim.animation = newstate
anim.flip_h = !is_facing_left
state = newstate
func receive_damage(dmg):
2023-10-10 14:24:19 -04:00
if hitanim.is_emitting():
pass
2023-10-10 12:56:34 -04:00
hp -= dmg
2023-10-10 14:24:19 -04:00
hitanim.restart()
2023-10-10 12:56:34 -04:00
if hp < 0:
velocity = Vector2(0,0)
setAnimState("Die")
state = "Die"
is_dying = true
func receive_exp(x):
experience += x
if experience > levelup[level - 1] && level <= 8:
setLevel(level + 1)
2023-10-10 14:24:19 -04:00
2023-10-10 12:56:34 -04:00
func setLevel(lvl):
level = lvl
lvlanim.restart()
2023-10-10 13:34:26 -04:00
lvlsnd.play()
2023-10-10 12:56:34 -04:00
if level > 2:
cooldown.wait_time = 0.5
2023-10-10 10:22:04 -04:00
func _on_atk_cooldown_timeout():
2023-10-10 12:56:34 -04:00
attack()
func _on_sprite_animation_looped():
if is_attacking:
stop_attack()
is_dying = false
2023-10-10 13:34:26 -04:00
2023-10-10 14:54:21 -04:00
func _on_attack_area_body_entered(body):
if typeof(body) == typeof(TemplateSpawnable):
body.receive_damage(damage)