pneuma-godot/code/bamboo.gd

99 lines
2.1 KiB
GDScript3
Raw Normal View History

extends CharacterBody2D
const SPEED = 150
const DAMAGE = 7
const EXP_AMOUNT = 1
@onready var knockback_timer = $KnockbackTimer
@onready var attack_timer = $AttackTimer
@onready var notice = $Notice
@onready var attack = $Attack
@onready var animation_player = $AnimationPlayer
@export var health = 100
var knockback = Vector2.ZERO
var near_player = false
var is_attacking = false
var is_knocked = false
var is_dead = false
var can_move = true
signal death
func change_hp(dmg):
2024-06-13 16:44:02 +00:00
if not is_dead:
health += dmg
if health <= 0:
health = 0
for body in attack.get_overlapping_bodies():
body.change_hp(DAMAGE)
body.add_exp(EXP_AMOUNT)
is_dead = true
death.emit()
func _on_notice_body_entered(body):
2024-06-13 16:44:02 +00:00
near_player = true
body.ai_controller.reward += 1
2024-06-13 16:44:02 +00:00
func _on_notice_body_exited(body):
2024-06-13 16:44:02 +00:00
near_player = false
func _on_attack_body_entered(body):
2024-06-13 16:44:02 +00:00
is_attacking = false
body.ai_controller.reward += 1
func _on_attack_body_exited(body):
2024-06-13 16:44:02 +00:00
is_attacking = true
func _physics_process(delta):
2024-06-13 16:44:02 +00:00
if near_player and not is_dead:
if not is_knocked:
for body in notice.get_overlapping_bodies():
if self.to_local(body.global_position).x > 30:
velocity.x = SPEED
elif self.to_local(body.global_position).x < -30:
velocity.x = -SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
if self.to_local(body.global_position).y > 30:
velocity.y = SPEED
elif self.to_local(body.global_position).y < -30:
velocity.y = -SPEED
else:
velocity.y = move_toward(velocity.y, 0, SPEED)
if not is_attacking:
for enemy in attack.get_overlapping_bodies():
enemy.change_hp(-DAMAGE)
attack_timer.start()
is_attacking = true
else:
if can_move:
self.velocity = knockback
knockback_timer.start()
can_move = false
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
velocity.y = move_toward(velocity.y, 0, SPEED)
move_and_slide()
func _on_attack_timer_timeout():
2024-06-13 16:44:02 +00:00
is_attacking = false
func _on_knockback_timer_timeout():
2024-06-13 16:44:02 +00:00
knockback = Vector2.ZERO
is_knocked = false
can_move = true
func _on_death():
2024-06-13 16:44:02 +00:00
animation_player.play("death")