命中检测

master
chendian 2 years ago
parent d90f26d1e6
commit 729cfc29e6

File diff suppressed because one or more lines are too long

@ -24,6 +24,40 @@ func raycast_wall(from:Vector3,to:Vector3) -> bool:
var result = space_state.intersect_ray(query)
return result.size()>0
func attack_detection(pos:Vector3,target_pos:Vector3,target_radius:float,target_height:float,attack_dir:Vector2,attack_height:float,attack_size:Vector2,attack_radius:float) -> bool:
# 检查Y
var attack_y_min = pos.y
var attack_y_max = pos.y + attack_height
var target_y_min = target_pos.y
var target_y_max = target_pos.y + target_height
if attack_y_min > target_y_max || attack_y_max < target_y_min:
return false
# 保底距离
if pos.distance_to(target_pos) < 0.01:
return true
# 2d距离
var dist_2d = Vector2(pos.x,pos.z).distance_to(Vector2(target_pos.x,target_pos.z))
# 如果是圆形
if attack_radius:
return dist_2d <= attack_radius + target_radius
# 旋转target
var angle = attack_dir.angle_to(Vector2.RIGHT)
target_pos = target_pos - pos
target_pos = target_pos.rotated(Vector3.UP,angle)
var dist_x = abs(target_pos.x) - attack_size.x
var dist_y = abs(target_pos.z) - attack_size.y
if dist_x <= target_radius && dist_y <= target_radius:
if (dist_x <= 0 || dist_y <= 0):
return true
var dist_squared = pow(dist_x, 2) + pow(dist_y, 2)
return dist_squared <= pow(target_radius, 2)
return false
func refresh_animation_lib():
var animation_library_path = "res://resource/skill_animation_library/animation_library.tres"
var animation_library = load(animation_library_path)

@ -22,17 +22,24 @@ func attack():
var attack_index = status.skill_attack_index
if attack_index >= len(attack_list):
attack_index = len(attack_list) - 1
var attack = attack_list[attack_index]
var attack = attack_list[attack_index] as AttackCfg
var enemy_list = Global.character_mgr.get_enemy_list(character.id())
for enemy in enemy_list:
var pos_dir = enemy.pos2D()-character.pos2D()
var distance = pos_dir.length()
#test
if (distance < 2):
var pos = character.pos()
var target_pos = enemy.pos()
var target_radius = enemy.radius()
var target_height = enemy.height()
var attack_dir = status.skill_dir.normalized()
var attack_height = attack.height
var attack_size = attack.size
var attack_radius = attack.radius
var offset_xz = attack_dir * attack.offset.x
pos += Vector3(offset_xz.x,attack.offset.y,offset_xz.y)
if Util.attack_detection(pos,target_pos,target_radius,target_height,attack_dir,attack_height,attack_size,attack_radius):
var hit_info = HitInfo.new()
hit_info.from = character.id()
hit_info.to = enemy.id()
hit_info.dir = status.skill_dir
hit_info.dir = attack_dir
hit_info.attack = attack
hit_info_list.append(hit_info)

@ -14,12 +14,15 @@ func init(id:int,cfg:CharacterCfg,team:Enum.ETeam):
status.team = team
status.cfg = cfg
var half_height = Setting.pixel_size * cfg.sprite_harf_height
var height = half_height * 2
var width = Setting.pixel_size * cfg.sprite_width
var body_scale = Vector3(width,half_height*2,width)
var body_scale = Vector3(width,height,width)
collision.position = Vector3(0,half_height,0)
collision.scale = body_scale
status.basic_offset = Vector3(0,half_height,0)
status.ui_offset = Vector3(0,half_height*2.2,0)
status.ui_offset = Vector3(0,height*1.1,0)
status.radius = width
status.height = height
view.init(cfg.sprite_frames)
skill.init()
effect.init(cfg.type,body_scale)
@ -64,6 +67,8 @@ func cfg()->CharacterCfg:return status.cfg
func team()->Enum.ETeam:return status.team
func pos2D()->Vector2:return Vector2(position.x,position.z)
func pos()->Vector3:return position
func radius()->float:return status.radius
func height()->float:return status.height
func view_pos()->Vector3:return position + view.position
func ui_pos()->Vector3:return position + status.ui_offset

@ -8,6 +8,8 @@ class_name Status
@export_category("静态属性")
@export var cfg : CharacterCfg #配置表
@export var height : float # 受击框高度
@export var radius : float # 受击框半径
@export_category("战斗状态")
@export var hp : float #当前血量

@ -2,9 +2,6 @@ extends Resource
class_name AttackCfg
#命中框信息
#数值信息
@export var damage_rate : float = 1 #伤害系数
@export var break_level : int = 1 #硬直等级
@export var stun_attack : float = 10 #眩晕值伤害
@ -16,4 +13,8 @@ class_name AttackCfg
@export var is_floating : bool #是否浮空技
@export var is_hit_down : bool #是否击落技
@export var is_rebound : bool #是否地面反弹技能
@export var height : float = 2 #判定框高度
@export var radius : float #圆形判定框 半径
@export var size : Vector2 = Vector2(2,1) #矩形判定框 大小
@export var offset : Vector2 = Vector2(1,0) #偏移

Loading…
Cancel
Save