2025-08-26 09:24:09 +08:00
|
|
|
extends CharacterBody2D
|
|
|
|
|
class_name EntityBase # 这是个抽象类
|
|
|
|
|
|
2025-08-26 13:56:12 +08:00
|
|
|
var fields = {
|
2025-08-26 12:21:09 +08:00
|
|
|
FieldStore.Entity.MAX_HEALTH: 100,
|
|
|
|
|
FieldStore.Entity.DAMAGE_MULTIPILER: 1,
|
|
|
|
|
FieldStore.Entity.MOVEMENT_SPEED: 1,
|
|
|
|
|
FieldStore.Entity.ATTACK_SPEED: 1,
|
2025-08-26 13:56:12 +08:00
|
|
|
FieldStore.Entity.CRIT_RATE: 0.05,
|
|
|
|
|
FieldStore.Entity.CRIT_DAMAGE: 1,
|
|
|
|
|
FieldStore.Entity.PENERATE: 0,
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-26 15:52:54 +08:00
|
|
|
@export var cooldownUnit: float = 100 # 100毫秒每次攻击
|
2025-08-26 11:39:47 +08:00
|
|
|
@export var isBoss: bool = false
|
2025-08-26 14:26:45 +08:00
|
|
|
@export var displayName: String = "未知实体"
|
2025-08-26 15:52:54 +08:00
|
|
|
@export var sprintMultiplier: float = 7
|
2025-08-26 09:24:09 +08:00
|
|
|
|
2025-08-26 10:55:39 +08:00
|
|
|
@onready var animatree: AnimationTree = $"%animatree"
|
|
|
|
|
@onready var texture: AnimatedSprite2D = $"%texture"
|
|
|
|
|
@onready var hurtbox: Area2D = $"%hurtbox"
|
2025-08-26 13:56:12 +08:00
|
|
|
@onready var statebar: EntityStateBar = $"%statebar"
|
2025-08-26 15:52:54 +08:00
|
|
|
@onready var sounds: Node2D = $"%sounds"
|
2025-08-26 17:28:20 +08:00
|
|
|
@onready var hurtAnimator: AnimationPlayer = $"%hurtAnimator"
|
2025-08-26 10:17:38 +08:00
|
|
|
|
2025-08-26 09:24:09 +08:00
|
|
|
var health: float = 0
|
|
|
|
|
|
2025-08-26 10:17:38 +08:00
|
|
|
var lastDirection: int = 1
|
2025-08-26 12:21:09 +08:00
|
|
|
var lastAttack: int = 0
|
2025-08-26 13:56:12 +08:00
|
|
|
var currentFocusedBoss: EntityBase = null
|
|
|
|
|
var sprinting: bool = false
|
2025-08-26 10:17:38 +08:00
|
|
|
|
2025-08-26 09:24:09 +08:00
|
|
|
func _ready():
|
2025-08-26 12:21:09 +08:00
|
|
|
health = fields.get(FieldStore.Entity.MAX_HEALTH)
|
2025-08-26 13:56:12 +08:00
|
|
|
statebar.visible = !isBoss
|
2025-08-26 14:47:12 +08:00
|
|
|
if !isPlayer():
|
|
|
|
|
currentFocusedBoss = get_tree().get_nodes_in_group("players")[0]
|
2025-08-26 09:24:09 +08:00
|
|
|
func _process(_delta):
|
2025-08-26 12:21:09 +08:00
|
|
|
health = clamp(health, 0, fields.get(FieldStore.Entity.MAX_HEALTH))
|
2025-08-26 10:17:38 +08:00
|
|
|
animatree.set("parameters/blend_position", lerpf(animatree.get("parameters/blend_position"), lastDirection, 0.1))
|
2025-08-26 09:24:09 +08:00
|
|
|
func _physics_process(_delta: float) -> void:
|
2025-08-26 14:26:45 +08:00
|
|
|
if sprinting:
|
|
|
|
|
velocity *= 0.9
|
|
|
|
|
if velocity.length() <= 100:
|
|
|
|
|
sprinting = false
|
|
|
|
|
else:
|
|
|
|
|
velocity = Vector2.ZERO
|
2025-08-26 14:47:12 +08:00
|
|
|
if isPlayer() or is_instance_valid(currentFocusedBoss):
|
|
|
|
|
ai()
|
2025-08-26 09:24:09 +08:00
|
|
|
move_and_slide()
|
|
|
|
|
|
|
|
|
|
# 通用方法
|
2025-08-26 15:52:54 +08:00
|
|
|
func displace(direction: Vector2, isSprinting: bool = false):
|
|
|
|
|
return (direction if isSprinting else direction.normalized()) * fields.get(FieldStore.Entity.MOVEMENT_SPEED) * 400 * abs(animatree.get("parameters/blend_position"))
|
2025-08-26 14:26:45 +08:00
|
|
|
func move(direction: Vector2, isSprinting: bool = false):
|
2025-08-26 15:52:54 +08:00
|
|
|
velocity = displace(direction, isSprinting)
|
2025-08-26 10:17:38 +08:00
|
|
|
var currentDirection = sign(direction.x)
|
|
|
|
|
if currentDirection != 0:
|
|
|
|
|
lastDirection = currentDirection
|
2025-08-26 13:56:12 +08:00
|
|
|
func takeDamage(bullet: BulletBase, crit: bool):
|
2025-08-26 17:28:20 +08:00
|
|
|
hurtAnimator.play("hurt")
|
2025-08-26 13:56:12 +08:00
|
|
|
var baseDamage: float = bullet.fields.get(FieldStore.Bullet.DAMAGE) * randf_range(1 - GameRule.damageOffset, 1 + GameRule.damageOffset)
|
|
|
|
|
var damage = baseDamage + baseDamage * int(crit) * fields.get(FieldStore.Entity.CRIT_DAMAGE)
|
2025-08-26 14:26:45 +08:00
|
|
|
if sprinting:
|
2025-08-26 15:52:54 +08:00
|
|
|
playSound("miss")
|
2025-08-26 14:26:45 +08:00
|
|
|
damage = 0
|
2025-08-26 15:52:54 +08:00
|
|
|
else:
|
|
|
|
|
playSound("hurt")
|
2025-08-26 13:56:12 +08:00
|
|
|
health -= damage
|
|
|
|
|
DamageLabel.create(damage, crit, $"%damageAnchor".global_position + MathTool.randv2_range(GameRule.damageLabelSpawnOffset))
|
|
|
|
|
if isBoss:
|
|
|
|
|
bullet.launcher.setBoss(self)
|
2025-08-26 11:39:47 +08:00
|
|
|
if health <= 0:
|
2025-08-26 13:56:12 +08:00
|
|
|
if isBoss:
|
|
|
|
|
bullet.launcher.setBoss(null)
|
2025-08-26 11:39:47 +08:00
|
|
|
die()
|
2025-08-26 12:21:09 +08:00
|
|
|
func isCooldowned():
|
2025-08-26 13:56:12 +08:00
|
|
|
return Time.get_ticks_msec() - lastAttack >= cooldownUnit / fields.get(FieldStore.Entity.ATTACK_SPEED)
|
2025-08-26 12:21:09 +08:00
|
|
|
func startCooldown():
|
|
|
|
|
var state = isCooldowned()
|
|
|
|
|
if state:
|
|
|
|
|
lastAttack = Time.get_ticks_msec()
|
|
|
|
|
return state
|
|
|
|
|
func tryAttack(type: int):
|
2025-08-26 14:47:12 +08:00
|
|
|
var state = startCooldown()
|
|
|
|
|
if state:
|
2025-08-26 17:28:20 +08:00
|
|
|
playSound("attack" + str(type))
|
2025-08-26 12:21:09 +08:00
|
|
|
attack(type)
|
2025-08-26 14:47:12 +08:00
|
|
|
return state
|
2025-08-26 14:26:45 +08:00
|
|
|
func trySprint():
|
2025-08-26 15:52:54 +08:00
|
|
|
playSound("sprint")
|
2025-08-26 14:26:45 +08:00
|
|
|
sprint()
|
|
|
|
|
sprinting = true
|
2025-08-26 12:21:09 +08:00
|
|
|
func findWeaponAnchor(weaponName: String):
|
|
|
|
|
var anchor = $"%weapons".get_node(weaponName)
|
|
|
|
|
if anchor is Node2D:
|
2025-08-26 13:56:12 +08:00
|
|
|
return anchor.global_position
|
2025-08-26 12:21:09 +08:00
|
|
|
else:
|
|
|
|
|
return Vector2.ZERO
|
2025-08-26 13:56:12 +08:00
|
|
|
func setBoss(boss: EntityBase):
|
|
|
|
|
currentFocusedBoss = boss
|
|
|
|
|
if isPlayer():
|
|
|
|
|
UIState.bossbar.entity = boss
|
2025-08-26 15:52:54 +08:00
|
|
|
func playSound(type: String):
|
|
|
|
|
var body = sounds.get_node_or_null(type)
|
|
|
|
|
if body is AudioStreamPlayer2D:
|
|
|
|
|
var cloned = body.duplicate() as AudioStreamPlayer2D
|
|
|
|
|
add_child(cloned)
|
|
|
|
|
cloned.play()
|
|
|
|
|
await cloned.finished
|
|
|
|
|
cloned.queue_free()
|
2025-08-26 11:39:47 +08:00
|
|
|
|
|
|
|
|
# 关于分组
|
|
|
|
|
func isPlayer():
|
|
|
|
|
return is_in_group("players")
|
2025-08-26 09:24:09 +08:00
|
|
|
|
|
|
|
|
# 抽象方法
|
|
|
|
|
func ai():
|
|
|
|
|
pass
|
|
|
|
|
func attack(_type: int):
|
|
|
|
|
pass
|
2025-08-26 11:39:47 +08:00
|
|
|
func die():
|
|
|
|
|
queue_free()
|
2025-08-26 14:26:45 +08:00
|
|
|
func sprint():
|
|
|
|
|
pass
|
2025-08-26 11:39:47 +08:00
|
|
|
|
|
|
|
|
static func generate(
|
|
|
|
|
entity: PackedScene,
|
|
|
|
|
spawnPosition: Vector2,
|
2025-08-26 12:21:09 +08:00
|
|
|
isMob: bool = true,
|
2025-08-26 13:56:12 +08:00
|
|
|
spawnAsBoss: bool = false,
|
2025-08-26 17:28:20 +08:00
|
|
|
addToWorld: bool = true
|
2025-08-26 11:39:47 +08:00
|
|
|
):
|
2025-08-26 17:28:20 +08:00
|
|
|
var instance: EntityBase = entity.instantiate()
|
2025-08-26 11:39:47 +08:00
|
|
|
instance.position = spawnPosition
|
2025-08-26 13:56:12 +08:00
|
|
|
instance.isBoss = spawnAsBoss
|
2025-08-26 12:21:09 +08:00
|
|
|
if isMob:
|
|
|
|
|
instance.add_to_group("mobs")
|
2025-08-26 17:28:20 +08:00
|
|
|
if addToWorld:
|
|
|
|
|
WorldManager.rootNode.add_child(instance)
|
2025-08-26 11:39:47 +08:00
|
|
|
return instance
|