mirror of
https://github.com/Rundll86/Dog-Lynx-And-HCN.git
synced 2026-05-30 07:51:54 +08:00
feat: Add weapon system and attack functionality to Rooster character
- Introduced a weapons node in EntityBase for weapon management. - Updated Rooster to include cooldown for attacks and weapon anchoring. - Implemented attack method in Rooster to generate bullets. - Enhanced BulletBase with life distance and lifetime properties. - Created Chick character with animations and integrated into the world. - Updated PurpleCrystal bullet behavior to move forward based on rotation. - Refactored EntityBase to use a fields dictionary for attributes. - Added FieldStore for managing entity-related constants. - Adjusted GameRule to include bullet speed multiplier.
This commit is contained in:
@@ -3,11 +3,26 @@ class_name BulletBase
|
||||
|
||||
@export var speed: float = 1
|
||||
@export var damage: float = 10
|
||||
@export var lifeDistance: float = -1 # -1表示无限距离
|
||||
@export var lifeTime: float = -1 # -1表示无限时间
|
||||
|
||||
var launcher: EntityBase = null
|
||||
var spawnInWhen: float = 0
|
||||
var spawnInWhere: Vector2 = Vector2.ZERO
|
||||
|
||||
func _ready():
|
||||
area_entered.connect(hit)
|
||||
spawnInWhen = Time.get_ticks_msec()
|
||||
spawnInWhere = position
|
||||
func _process(_delta: float) -> void:
|
||||
if lifeTime > 0:
|
||||
if Time.get_ticks_msec() - spawnInWhen >= lifeTime:
|
||||
destroy()
|
||||
if lifeDistance > 0:
|
||||
if position.distance_to(spawnInWhere) >= lifeDistance:
|
||||
destroy()
|
||||
func _physics_process(_delta: float) -> void:
|
||||
ai()
|
||||
|
||||
func hit(target: Node):
|
||||
var entity: EntityBase = EntityTool.fromHurtbox(target)
|
||||
@@ -16,6 +31,13 @@ func hit(target: Node):
|
||||
if GameRule.allowFriendlyFire:
|
||||
if entity.isPlayer() == launcher.isPlayer(): return
|
||||
entity.takeDamage(self)
|
||||
func forward(direction: Vector2):
|
||||
position += direction.normalized() * speed * GameRule.bulletSpeedMultiplier
|
||||
|
||||
func ai():
|
||||
pass
|
||||
func destroy():
|
||||
queue_free()
|
||||
|
||||
static func generate(
|
||||
bullet: PackedScene,
|
||||
|
||||
@@ -1,10 +1,16 @@
|
||||
extends CharacterBody2D
|
||||
class_name EntityBase # 这是个抽象类
|
||||
|
||||
@export var maxHealth: float = 100
|
||||
@export var movementSpeed: float = 1
|
||||
@export var fields: Dictionary = {
|
||||
FieldStore.Entity.MAX_HEALTH: 100,
|
||||
FieldStore.Entity.DAMAGE_MULTIPILER: 1,
|
||||
FieldStore.Entity.MOVEMENT_SPEED: 1,
|
||||
FieldStore.Entity.ATTACK_SPEED: 1,
|
||||
FieldStore.Entity.CRIT_RATE: 0.05, # 0.05 = 5%
|
||||
FieldStore.Entity.CRIT_DAMAGE: 2 # 2 = 200%
|
||||
} # 存一下词条
|
||||
@export var isBoss: bool = false
|
||||
@export var weapons: Array[Node2D] = []
|
||||
@export var cooldownUnit: float = 100 # 100毫秒每次攻击
|
||||
|
||||
@onready var animatree: AnimationTree = $"%animatree"
|
||||
@onready var texture: AnimatedSprite2D = $"%texture"
|
||||
@@ -13,11 +19,12 @@ class_name EntityBase # 这是个抽象类
|
||||
var health: float = 0
|
||||
|
||||
var lastDirection: int = 1
|
||||
var lastAttack: int = 0
|
||||
|
||||
func _ready():
|
||||
health = maxHealth
|
||||
health = fields.get(FieldStore.Entity.MAX_HEALTH)
|
||||
func _process(_delta):
|
||||
health = clamp(health, 0, maxHealth)
|
||||
health = clamp(health, 0, fields.get(FieldStore.Entity.MAX_HEALTH))
|
||||
animatree.set("parameters/blend_position", lerpf(animatree.get("parameters/blend_position"), lastDirection, 0.1))
|
||||
func _physics_process(_delta: float) -> void:
|
||||
velocity = Vector2.ZERO
|
||||
@@ -26,7 +33,7 @@ func _physics_process(_delta: float) -> void:
|
||||
|
||||
# 通用方法
|
||||
func move(direction: Vector2):
|
||||
velocity = direction.normalized() * movementSpeed * 150 * abs(animatree.get("parameters/blend_position"))
|
||||
velocity = direction.normalized() * fields.get(FieldStore.Entity.MOVEMENT_SPEED) * 200 * abs(animatree.get("parameters/blend_position"))
|
||||
var currentDirection = sign(direction.x)
|
||||
if currentDirection != 0:
|
||||
lastDirection = currentDirection
|
||||
@@ -34,6 +41,22 @@ func takeDamage(bullet: BulletBase):
|
||||
health -= bullet.damage
|
||||
if health <= 0:
|
||||
die()
|
||||
func isCooldowned():
|
||||
return Time.get_ticks_msec() - lastAttack >= cooldownUnit
|
||||
func startCooldown():
|
||||
var state = isCooldowned()
|
||||
if state:
|
||||
lastAttack = Time.get_ticks_msec()
|
||||
return state
|
||||
func tryAttack(type: int):
|
||||
if startCooldown():
|
||||
attack(type)
|
||||
func findWeaponAnchor(weaponName: String):
|
||||
var anchor = $"%weapons".get_node(weaponName)
|
||||
if anchor is Node2D:
|
||||
return (anchor.position + texture.position) * Vector2(animatree.get("parameters/blend_position"), 1) + position
|
||||
else:
|
||||
return Vector2.ZERO
|
||||
|
||||
# 关于分组
|
||||
func isPlayer():
|
||||
@@ -51,11 +74,14 @@ static func generate(
|
||||
entity: PackedScene,
|
||||
spawnPosition: Vector2,
|
||||
spawnRotation: float,
|
||||
isMob: bool = true,
|
||||
addtoWorld: bool = true
|
||||
):
|
||||
var instance = entity.instance()
|
||||
var instance: EntityBase = entity.instance()
|
||||
instance.position = spawnPosition
|
||||
instance.rotation = spawnRotation
|
||||
if isMob:
|
||||
instance.add_to_group("mobs")
|
||||
if addtoWorld:
|
||||
WorldTool.rootNode.add_child(instance)
|
||||
return instance
|
||||
|
||||
Reference in New Issue
Block a user