2025-08-26 09:24:09 +08:00
|
|
|
extends CharacterBody2D
|
|
|
|
|
class_name EntityBase # 这是个抽象类
|
|
|
|
|
|
|
|
|
|
@export var maxHealth: float = 100
|
2025-08-26 10:55:39 +08:00
|
|
|
@export var movementSpeed: float = 1
|
2025-08-26 11:39:47 +08:00
|
|
|
@export var isBoss: bool = false
|
|
|
|
|
@export var weapons: Array[Node2D] = []
|
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 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 09:24:09 +08:00
|
|
|
func _ready():
|
|
|
|
|
health = maxHealth
|
|
|
|
|
func _process(_delta):
|
|
|
|
|
health = clamp(health, 0, maxHealth)
|
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:
|
|
|
|
|
velocity = Vector2.ZERO
|
|
|
|
|
ai()
|
|
|
|
|
move_and_slide()
|
|
|
|
|
|
|
|
|
|
# 通用方法
|
|
|
|
|
func move(direction: Vector2):
|
2025-08-26 10:55:39 +08:00
|
|
|
velocity = direction.normalized() * movementSpeed * 150 * abs(animatree.get("parameters/blend_position"))
|
2025-08-26 10:17:38 +08:00
|
|
|
var currentDirection = sign(direction.x)
|
|
|
|
|
if currentDirection != 0:
|
|
|
|
|
lastDirection = currentDirection
|
2025-08-26 11:39:47 +08:00
|
|
|
func takeDamage(bullet: BulletBase):
|
|
|
|
|
health -= bullet.damage
|
|
|
|
|
if health <= 0:
|
|
|
|
|
die()
|
|
|
|
|
|
|
|
|
|
# 关于分组
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
|
|
static func generate(
|
|
|
|
|
entity: PackedScene,
|
|
|
|
|
spawnPosition: Vector2,
|
|
|
|
|
spawnRotation: float,
|
|
|
|
|
addtoWorld: bool = true
|
|
|
|
|
):
|
|
|
|
|
var instance = entity.instance()
|
|
|
|
|
instance.position = spawnPosition
|
|
|
|
|
instance.rotation = spawnRotation
|
|
|
|
|
if addtoWorld:
|
|
|
|
|
WorldTool.rootNode.add_child(instance)
|
|
|
|
|
return instance
|