2025-08-26 09:24:09 +08:00
|
|
|
extends CharacterBody2D
|
|
|
|
|
class_name EntityBase # 这是个抽象类
|
|
|
|
|
|
2025-08-27 20:47:04 +08:00
|
|
|
signal hit(damage: float, bullet: BulletBase, crit: bool)
|
|
|
|
|
signal healed(amount: float)
|
|
|
|
|
signal healthChanged(health: float)
|
|
|
|
|
|
2025-08-27 21:14:06 +08:00
|
|
|
signal energyChanged(energy: float)
|
|
|
|
|
|
2025-08-26 13:56:12 +08:00
|
|
|
var fields = {
|
2025-08-29 07:58:52 +08:00
|
|
|
# 数值上限
|
2025-08-26 12:21:09 +08:00
|
|
|
FieldStore.Entity.MAX_HEALTH: 100,
|
2025-08-29 07:58:52 +08:00
|
|
|
FieldStore.Entity.MAX_ENERGY: 200,
|
|
|
|
|
FieldStore.Entity.EXTRA_APPLE_MAX: 0,
|
|
|
|
|
FieldStore.Entity.EXTRA_BULLET_COUNT: 0,
|
|
|
|
|
# 速度
|
2025-08-26 12:21:09 +08:00
|
|
|
FieldStore.Entity.MOVEMENT_SPEED: 1,
|
|
|
|
|
FieldStore.Entity.ATTACK_SPEED: 1,
|
2025-08-29 07:58:52 +08:00
|
|
|
# 伤害
|
|
|
|
|
FieldStore.Entity.DAMAGE_MULTIPILER: 1,
|
2025-08-26 13:56:12 +08:00
|
|
|
FieldStore.Entity.CRIT_DAMAGE: 1,
|
2025-08-29 07:58:52 +08:00
|
|
|
# 概率相关
|
|
|
|
|
FieldStore.Entity.CRIT_RATE: 0.05,
|
2025-08-26 13:56:12 +08:00
|
|
|
FieldStore.Entity.PENERATE: 0,
|
2025-08-27 10:23:57 +08:00
|
|
|
FieldStore.Entity.OFFSET_SHOOT: 3,
|
2025-08-29 07:58:52 +08:00
|
|
|
FieldStore.Entity.DROP_APPLE_RATE: 0,
|
|
|
|
|
FieldStore.Entity.PENARATION_RESISTANCE: 0,
|
|
|
|
|
FieldStore.Entity.LUCK_VALUE: 1,
|
2025-08-29 10:26:41 +08:00
|
|
|
# 治疗
|
2025-08-27 10:23:57 +08:00
|
|
|
FieldStore.Entity.HEAL_ABILITY: 1,
|
2025-08-29 10:26:41 +08:00
|
|
|
# 价格减免
|
2025-08-27 10:23:57 +08:00
|
|
|
FieldStore.Entity.PRICE_REDUCTION: 0,
|
2025-08-29 07:58:52 +08:00
|
|
|
# 饲料
|
2025-08-27 16:33:21 +08:00
|
|
|
FieldStore.Entity.FEED_COUNT_SHOW: 3,
|
2025-08-27 20:27:01 +08:00
|
|
|
FieldStore.Entity.FEED_COUNT_CAN_MADE: 1,
|
2025-08-29 10:26:41 +08:00
|
|
|
# 储能
|
|
|
|
|
FieldStore.Entity.ENERGY_MULTIPILER: 1,
|
|
|
|
|
FieldStore.Entity.SAVE_ENERGY: 1,
|
|
|
|
|
FieldStore.Entity.ENERGY_REGENERATION: 1,
|
2025-08-27 10:23:57 +08:00
|
|
|
}
|
2025-08-29 10:50:22 +08:00
|
|
|
var attackCooldownMap = {
|
|
|
|
|
0: 100
|
|
|
|
|
}
|
2025-08-27 10:23:57 +08:00
|
|
|
var inventory = {
|
|
|
|
|
ItemStore.ItemType.BASEBALL: 100,
|
|
|
|
|
ItemStore.ItemType.BASKETBALL: 100,
|
2025-08-28 07:45:50 +08:00
|
|
|
ItemStore.ItemType.APPLE: 5, # 初始苹果数量
|
2025-08-27 10:23:57 +08:00
|
|
|
}
|
|
|
|
|
var inventoryMax = {
|
|
|
|
|
ItemStore.ItemType.BASEBALL: INF, # 无限
|
|
|
|
|
ItemStore.ItemType.BASKETBALL: INF,
|
2025-08-29 10:26:41 +08:00
|
|
|
ItemStore.ItemType.APPLE: 5, # 最多5个苹果
|
2025-08-26 13:56:12 +08:00
|
|
|
}
|
|
|
|
|
|
2025-08-29 10:50:22 +08:00
|
|
|
@export var defaultCooldownUnit: float = 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-27 21:14:06 +08:00
|
|
|
@export var sprintMultiplier: float = 4
|
2025-08-26 20:46:02 +08:00
|
|
|
@export var drops: Array[ItemStore.ItemType] = []
|
|
|
|
|
@export var dropCounts: Array[Vector2] = []
|
2025-08-29 08:24:18 +08:00
|
|
|
@export var appleCount: Vector2i = Vector2(0, 2) # 死亡后掉落的苹果数量
|
2025-08-29 12:48:04 +08:00
|
|
|
@export var level: int = 1
|
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 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-27 10:23:57 +08:00
|
|
|
@onready var damageAnchor: Node2D = $"%damageAnchor"
|
2025-08-28 07:28:11 +08:00
|
|
|
var statebar: EntityStateBar
|
2025-08-26 10:17:38 +08:00
|
|
|
|
2025-08-26 09:24:09 +08:00
|
|
|
var health: float = 0
|
2025-08-28 07:45:50 +08:00
|
|
|
var energy: float = 0
|
2025-08-27 11:08:11 +08:00
|
|
|
var sprinting: bool = false
|
2025-08-26 09:24:09 +08:00
|
|
|
|
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
|
2025-08-26 10:17:38 +08:00
|
|
|
|
2025-08-26 09:24:09 +08:00
|
|
|
func _ready():
|
2025-08-29 10:50:22 +08:00
|
|
|
register()
|
2025-08-28 07:28:11 +08:00
|
|
|
var selfStatebar: EntityStateBar = $"%statebar"
|
|
|
|
|
if isBoss:
|
|
|
|
|
selfStatebar.hide()
|
|
|
|
|
else:
|
|
|
|
|
statebar = selfStatebar
|
|
|
|
|
statebar.entity = self
|
2025-08-26 20:46:02 +08:00
|
|
|
if isPlayer():
|
2025-08-28 12:58:00 +08:00
|
|
|
statebar.levelLabels.hide()
|
2025-08-26 20:46:02 +08:00
|
|
|
UIState.player = self
|
2025-08-27 21:14:06 +08:00
|
|
|
energyChanged.connect(
|
|
|
|
|
func(newEnergy):
|
|
|
|
|
UIState.energyPercent.maxValue = fields.get(FieldStore.Entity.MAX_ENERGY)
|
|
|
|
|
UIState.energyPercent.setCurrent(newEnergy)
|
|
|
|
|
)
|
2025-08-26 20:46:02 +08:00
|
|
|
else:
|
2025-08-26 14:47:12 +08:00
|
|
|
currentFocusedBoss = get_tree().get_nodes_in_group("players")[0]
|
2025-08-28 13:58:31 +08:00
|
|
|
applyLevel()
|
|
|
|
|
health = fields.get(FieldStore.Entity.MAX_HEALTH)
|
2025-08-28 21:57:04 +08:00
|
|
|
energy = fields.get(FieldStore.Entity.MAX_ENERGY)
|
2025-08-28 07:28:11 +08:00
|
|
|
healthChanged.connect(
|
|
|
|
|
func(newHealth):
|
2025-08-28 11:00:19 +08:00
|
|
|
if is_instance_valid(statebar):
|
2025-08-28 07:28:11 +08:00
|
|
|
statebar.healthBar.maxValue = fields.get(FieldStore.Entity.MAX_HEALTH)
|
|
|
|
|
statebar.healthBar.setCurrent(newHealth)
|
|
|
|
|
)
|
2025-08-27 20:47:04 +08:00
|
|
|
healthChanged.emit(health)
|
2025-08-27 21:14:06 +08:00
|
|
|
energyChanged.emit(energy)
|
2025-08-29 12:51:43 +08:00
|
|
|
spawn()
|
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-27 20:27:01 +08:00
|
|
|
energy = clamp(energy, 0, fields.get(FieldStore.Entity.MAX_ENERGY))
|
2025-08-27 10:23:57 +08:00
|
|
|
for i in inventory:
|
|
|
|
|
inventory[i] = clamp(inventory[i], 0, inventoryMax[i])
|
2025-08-28 11:00:19 +08:00
|
|
|
if isBoss:
|
|
|
|
|
if UIState.player.currentFocusedBoss == self:
|
|
|
|
|
statebar = UIState.bossbar
|
|
|
|
|
else:
|
|
|
|
|
statebar = null
|
|
|
|
|
if is_instance_valid(statebar):
|
|
|
|
|
statebar.levelLabel.text = str(level)
|
2025-08-26 09:24:09 +08:00
|
|
|
func _physics_process(_delta: float) -> void:
|
2025-08-27 15:38:30 +08:00
|
|
|
animatree.set("parameters/blend_position", lerpf(animatree.get("parameters/blend_position"), lastDirection, 0.2))
|
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-29 10:26:41 +08:00
|
|
|
storeEnergy(0.01 * fields.get(FieldStore.Entity.ENERGY_REGENERATION))
|
2025-08-26 09:24:09 +08:00
|
|
|
|
|
|
|
|
# 通用方法
|
2025-08-28 11:00:19 +08:00
|
|
|
func applyLevel():
|
|
|
|
|
fields[FieldStore.Entity.MAX_HEALTH] *= (1 + GameRule.entityHealthIncreasePerWave * (GameRule.difficulty + 1)) ** level
|
|
|
|
|
fields[FieldStore.Entity.DAMAGE_MULTIPILER] *= (1 + GameRule.entityDamageIncreasePerWave * (GameRule.difficulty + 1)) ** level
|
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-29 10:50:22 +08:00
|
|
|
var baseDamage: float = bullet.damage * bullet.launcher.fields.get(FieldStore.Entity.DAMAGE_MULTIPILER) * randf_range(1 - GameRule.damageOffset, 1 + GameRule.damageOffset)
|
2025-08-26 13:56:12 +08:00
|
|
|
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-28 15:45:38 +08:00
|
|
|
storeEnergy(damage * 0.35)
|
2025-08-26 14:26:45 +08:00
|
|
|
damage = 0
|
2025-08-26 15:52:54 +08:00
|
|
|
else:
|
|
|
|
|
playSound("hurt")
|
2025-08-28 13:01:16 +08:00
|
|
|
storeEnergy(damage * -0.5)
|
2025-08-28 12:34:41 +08:00
|
|
|
position += Vector2.from_angle(bullet.position.angle_to_point(position)) * bullet.knockback
|
2025-08-27 20:47:04 +08:00
|
|
|
hit.emit(damage, bullet, crit)
|
|
|
|
|
healthChanged.emit(health)
|
2025-08-26 13:56:12 +08:00
|
|
|
health -= damage
|
2025-08-27 10:23:57 +08:00
|
|
|
DamageLabel.create(damage, crit, damageAnchor.global_position + MathTool.randv2_range(GameRule.damageLabelSpawnOffset))
|
2025-08-27 08:58:14 +08:00
|
|
|
if isBoss and bullet.launcher.isPlayer():
|
2025-08-26 13:56:12 +08:00
|
|
|
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:
|
2025-08-28 13:01:16 +08:00
|
|
|
bullet.launcher.storeEnergy(energy * 0.35)
|
2025-08-26 13:56:12 +08:00
|
|
|
bullet.launcher.setBoss(null)
|
2025-08-27 10:23:57 +08:00
|
|
|
tryDie(bullet)
|
2025-08-28 15:53:39 +08:00
|
|
|
return damage
|
2025-08-28 14:15:01 +08:00
|
|
|
func collectItem(itemType: ItemStore.ItemType, amount: int):
|
|
|
|
|
inventory[itemType] += amount
|
|
|
|
|
playSound("collect")
|
2025-08-27 11:08:11 +08:00
|
|
|
func storeEnergy(value: float):
|
|
|
|
|
energy += value * fields.get(FieldStore.Entity.ENERGY_MULTIPILER)
|
2025-08-27 21:14:06 +08:00
|
|
|
energyChanged.emit(energy)
|
2025-08-27 20:27:01 +08:00
|
|
|
func useEnergy(value: float):
|
2025-08-29 10:26:41 +08:00
|
|
|
value /= fields.get(FieldStore.Entity.SAVE_ENERGY)
|
2025-08-27 20:27:01 +08:00
|
|
|
var state = energy >= value
|
|
|
|
|
if state:
|
|
|
|
|
energy -= value
|
2025-08-27 21:14:06 +08:00
|
|
|
energyChanged.emit(energy)
|
2025-08-27 20:27:01 +08:00
|
|
|
return state
|
2025-08-29 10:50:22 +08:00
|
|
|
func isCooldowned(type: int):
|
|
|
|
|
return WorldManager.getTime() - lastAttack >= attackCooldownMap.get(type, defaultCooldownUnit) / fields.get(FieldStore.Entity.ATTACK_SPEED)
|
|
|
|
|
func startCooldown(type: int):
|
|
|
|
|
var state = isCooldowned(type)
|
2025-08-26 12:21:09 +08:00
|
|
|
if state:
|
2025-08-29 10:27:32 +08:00
|
|
|
lastAttack = WorldManager.getTime()
|
2025-08-26 12:21:09 +08:00
|
|
|
return state
|
|
|
|
|
func tryAttack(type: int):
|
2025-08-29 10:50:22 +08:00
|
|
|
var state = startCooldown(type)
|
2025-08-26 14:47:12 +08:00
|
|
|
if state:
|
2025-08-27 12:46:20 +08:00
|
|
|
if attack(type):
|
|
|
|
|
playSound("attack" + str(type))
|
2025-08-26 14:47:12 +08:00
|
|
|
return state
|
2025-08-26 14:26:45 +08:00
|
|
|
func trySprint():
|
2025-08-29 12:48:04 +08:00
|
|
|
playSound("sprint")
|
|
|
|
|
sprint()
|
|
|
|
|
sprinting = true
|
2025-08-27 10:23:57 +08:00
|
|
|
func tryDie(by: BulletBase):
|
2025-08-26 20:46:02 +08:00
|
|
|
for drop in range(min(len(drops), len(dropCounts))):
|
|
|
|
|
var item = drops[drop]
|
|
|
|
|
var count = ceil(randf_range(dropCounts[drop].x, dropCounts[drop].y))
|
|
|
|
|
for i in range(count):
|
|
|
|
|
ItemDropped.generate(item, count, position + MathTool.randv2_range(GameRule.itemDroppedSpawnOffset))
|
2025-08-28 13:19:42 +08:00
|
|
|
if MathTool.rate(
|
|
|
|
|
GameRule.appleDropRate +
|
|
|
|
|
by.launcher.fields.get(FieldStore.Entity.DROP_APPLE_RATE) +
|
|
|
|
|
GameRule.appleDropRateInfluenceByLuckValue * by.launcher.fields[FieldStore.Entity.LUCK_VALUE]
|
|
|
|
|
) or isBoss:
|
2025-08-27 10:23:57 +08:00
|
|
|
for i in randi_range(appleCount.x, appleCount.y):
|
|
|
|
|
ItemDropped.generate(ItemStore.ItemType.APPLE, 1, position + MathTool.randv2_range(GameRule.itemDroppedSpawnOffset))
|
2025-08-28 08:15:18 +08:00
|
|
|
await die()
|
|
|
|
|
if isPlayer() and UIState.player == self:
|
|
|
|
|
UIState.setPanel("GameOver")
|
2025-08-27 10:23:57 +08:00
|
|
|
func tryHeal(count: float):
|
|
|
|
|
if inventory[ItemStore.ItemType.APPLE] > 0 and health < fields.get(FieldStore.Entity.MAX_HEALTH):
|
|
|
|
|
inventory[ItemStore.ItemType.APPLE] -= 1
|
|
|
|
|
playSound("heal")
|
2025-08-27 20:47:04 +08:00
|
|
|
healed.emit(heal(count * fields.get(FieldStore.Entity.HEAL_ABILITY)))
|
|
|
|
|
healthChanged.emit(health)
|
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
|
2025-08-28 07:35:34 +08:00
|
|
|
if isPlayer() and boss and UIState.bossbar.entity != boss:
|
2025-08-26 13:56:12 +08:00
|
|
|
UIState.bossbar.entity = boss
|
2025-08-28 07:35:34 +08:00
|
|
|
boss.healthChanged.emit(boss.health)
|
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-27 20:47:04 +08:00
|
|
|
func heal(count: float):
|
|
|
|
|
health += count
|
|
|
|
|
return count
|
2025-08-29 10:50:22 +08:00
|
|
|
func register():
|
|
|
|
|
pass
|
2025-08-29 12:51:43 +08:00
|
|
|
func spawn():
|
|
|
|
|
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-28 11:00:19 +08:00
|
|
|
instance.level = clamp((round(Wave.current * (1 + GameRule.entityLevelOffsetByWave * randf_range(-1, 1)))), 1, INF)
|
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
|
2025-08-26 17:38:45 +08:00
|
|
|
static func mobCount():
|
|
|
|
|
return len(WorldManager.tree.get_nodes_in_group("mobs"))
|