From 19a270cb1eb949eefadd142f6b476a9fbadf390a Mon Sep 17 00:00:00 2001 From: TsubakiLoL <2789646812@qq.com> Date: Fri, 25 Oct 2024 17:45:08 +0800 Subject: [PATCH] 10.25 --- autoload/database/database.gd | 12 ++--- autoload/global/global.gd | 4 ++ scene/behavour_tree/condition_accuse.gd | 1 + .../character/dead_scene_sprite/dead_scene.gd | 3 +- scene/test/action_attack.gd | 3 +- scene/test/action_move_to_unit.gd | 5 +- scene/test/atk.gd | 20 ++++++++ scene/test/character.gd | 48 ++++++++++++++++--- scene/test/character.tscn | 13 +++-- scene/test/condition_attack.gd | 2 +- scene/test/idle.gd | 5 +- scene/test/other_character.tscn | 2 +- scene/test/run.gd | 32 ++++++++++--- scene/test/test_main.gd | 26 ++++++++++ scene/test/test_main.tscn | 23 +++------ 15 files changed, 149 insertions(+), 50 deletions(-) create mode 100644 scene/test/atk.gd diff --git a/autoload/database/database.gd b/autoload/database/database.gd index 9d48022..d8ff216 100644 --- a/autoload/database/database.gd +++ b/autoload/database/database.gd @@ -7,15 +7,15 @@ var init_favour:Dictionary={ #测试族群1 "test_1":{ #对测试族群2的初始好感度为-10 - "test_1":20, - "test_2":-50, - "player":-20, + "test_1":-100, + "test_2":-100, + "player":-100, }, "test_2":{ - "test_2":100, - "test_1":50, - "player":-20, + "test_2":-100, + "test_1":-100, + "player":-100, } } diff --git a/autoload/global/global.gd b/autoload/global/global.gd index a7988e3..bcb8061 100644 --- a/autoload/global/global.gd +++ b/autoload/global/global.gd @@ -20,6 +20,10 @@ func get_unit_instance(id:String): return unit_instance_dic[id] else: return null +func delete_unit_instance(id:String): + unit_instance_dic.erase(id) + + pass #单位好感度字典 var unit_favour_dic:Dictionary={ diff --git a/scene/behavour_tree/condition_accuse.gd b/scene/behavour_tree/condition_accuse.gd index 19b1bc8..8acafb3 100644 --- a/scene/behavour_tree/condition_accuse.gd +++ b/scene/behavour_tree/condition_accuse.gd @@ -11,6 +11,7 @@ func tick(actor:Node,black_board:Blackboard): if res: black_board.set_value("target_unit_id",i.unit_id) return SUCCESS + black_board.erase_value("target_unit_id") return FAILURE pass diff --git a/scene/character/dead_scene_sprite/dead_scene.gd b/scene/character/dead_scene_sprite/dead_scene.gd index 53ac13d..0629325 100644 --- a/scene/character/dead_scene_sprite/dead_scene.gd +++ b/scene/character/dead_scene_sprite/dead_scene.gd @@ -9,10 +9,11 @@ func _ready() -> void: func _on_animation_finished() -> void: - animation_player.play("mddulutehide") + animation_player.play("modulutehide") pass # Replace with function body. func _on_animation_player_animation_finished(anim_name: StringName) -> void: + print("dada") queue_free() pass # Replace with function body. diff --git a/scene/test/action_attack.gd b/scene/test/action_attack.gd index 8f15b95..a8343c8 100644 --- a/scene/test/action_attack.gd +++ b/scene/test/action_attack.gd @@ -5,7 +5,8 @@ func tick(actor:Node,black_board:Blackboard): var unit:Unit=actor if unit.is_attacking(): var target_unit:Unit=Global.get_unit_instance(black_board.get_value("target_unit_id")) - unit.set_target(target_unit.global_position) + if is_instance_valid(target_unit): + unit.set_target(target_unit.global_position) return RUNNING if unit.is_attack_finished(): black_board.erase_value("target_unit_id") diff --git a/scene/test/action_move_to_unit.gd b/scene/test/action_move_to_unit.gd index bb33a19..4344e38 100644 --- a/scene/test/action_move_to_unit.gd +++ b/scene/test/action_move_to_unit.gd @@ -13,6 +13,7 @@ func tick(actor:Node,black_board:Blackboard): return FAILURE #获取要移动到的目标实例 var target_instance:Unit=Global.get_unit_instance(target_id) + if not unit.is_unit_instance_in_touch_area(target_instance): unit.set_target_pos(target_instance.global_position) match unit.get_dir(): @@ -52,7 +53,3 @@ func tick(actor:Node,black_board:Blackboard): unit.play_animation("left_right_idle") return FAILURE - pass - - - pass diff --git a/scene/test/atk.gd b/scene/test/atk.gd new file mode 100644 index 0000000..18c3306 --- /dev/null +++ b/scene/test/atk.gd @@ -0,0 +1,20 @@ +extends State +var target +func enter_state(n): + if n is Unit: + target=n + get_player().attack() + pass +func update_state(delta): + pass +func update_state_phy(delta): + if get_player().is_attack_finished(): + change_to_state("idle") +func exit_state(): + target=null + pass + +func process_message(type:String,n): + + + pass diff --git a/scene/test/character.gd b/scene/test/character.gd index 14df247..27b5156 100644 --- a/scene/test/character.gd +++ b/scene/test/character.gd @@ -46,9 +46,10 @@ var action_emoji:Dictionary={ "hp_max":100, "hp":100, + "atk":10, } #允许的状态队列 -var state_value_array:Array=["hungry","fatigue","rage","tensity","panic","pressure","hp_max","hp"] +var state_value_array:Array=["hungry","fatigue","rage","tensity","panic","pressure","hp_max","hp","atk"] #设置状态值 func get_state_value(state_value_name:String): if state_value_name in state_value_array: @@ -59,7 +60,13 @@ func get_state_value(state_value_name:String): signal state_value_changed(state_value_name:String,value) #设置状态值 func set_state_value(state_value_name:String,value)->bool: + if state_value_name in state_value_array: + #对每种属性更改进行特殊设置 + match state_value_name: + "hp": + if value<=0: + dead() unit_data[state_value_name]=value state_value_changed.emit(state_value_name,value) return true @@ -79,6 +86,10 @@ func set_hungry(value): return set_state_value("hungry",value) +func get_atk(): + return get_state_value("atk") +func set_atk(value): + return set_state_value("atk",value) @@ -133,7 +144,7 @@ func _ready() -> void: new_sprite_animation.position=unit_data["sprite_offset"] new_sprite_animation.scale=unit_data["sprite_scale"] animation=new_sprite_animation - + animation.frame_changed.connect(frame_changed) #state_machine.launch() func set_target_pos(target:Vector2): agent.target_position=target @@ -184,6 +195,18 @@ func is_unit_instance_in_touch_area(instance:Node): return instance in touch_area.get_overlapping_bodies() if instance is Area2D: return instance in touch_area.get_overlapping_areas() + +#判断单位是否再攻击范围内 +func is_unit_instance_in_attack_area(instance:Node): + if attack_area==null: + return false + else: + if instance is PhysicsBody2D: + return instance in attack_area.get_overlapping_bodies() + if instance is Area2D: + return instance in attack_area.get_overlapping_areas() + + pass #指责(口角) func accuse(unit_id:String): show_action("指责") @@ -225,16 +248,16 @@ func is_attacking(): return animation.animation in [&"up_attack",&"down_attack",&"left_right_attack"] and animation.is_playing() #在固定帧使用攻击 func use_attack_damage(): - + print("使用攻击") for i in attack_area.get_overlapping_bodies(): - if i is Unit and i!=self: - i.attacked(unit_id) + i.attacked(unit_id,get_atk()) pass -func attacked(by_unit_id:String): +func attacked(by_unit_id:String,damage:float): show_action("受伤") Global.set_unit_favour(unit_id,by_unit_id,Global.get_unit_favour(unit_id,by_unit_id)-20) + set_state_value("hp",get_state_value("hp")-damage) pass func set_target(global_pos:Vector2): @@ -282,7 +305,7 @@ func get_dir()->int: func second_timer_time_out(): - set_hungry(clamp(get_hungry()+10,0,100)) + set_hungry(clamp(get_hungry()+1,0,100)) pass func eat(food:Food): @@ -291,3 +314,14 @@ func eat(food:Food): set_hungry(clamp(get_hungry()-food.hungry,0,100)) food.eated() pass + +const DEAD_SCENE = preload("res://scene/character/dead_scene_sprite/dead_scene.tscn") +func dead(): + var new_dead_scene=DEAD_SCENE.instantiate() + new_dead_scene.sprite_frames=animation.sprite_frames + new_dead_scene.global_position=animation.global_position + new_dead_scene.scale=animation.scale + get_parent().add_child(new_dead_scene) + Global.delete_unit_instance(unit_id) + queue_free() + pass diff --git a/scene/test/character.tscn b/scene/test/character.tscn index 4f194f2..3808bbc 100644 --- a/scene/test/character.tscn +++ b/scene/test/character.tscn @@ -1,15 +1,16 @@ -[gd_scene load_steps=15 format=3 uid="uid://cf2g2urxaukxb"] +[gd_scene load_steps=16 format=3 uid="uid://cf2g2urxaukxb"] [ext_resource type="Script" path="res://scene/test/self_character.gd" id="1_d0trv"] [ext_resource type="Script" path="res://scene/test/state_machine.gd" id="2_v5m4x"] [ext_resource type="Script" path="res://scene/test/idle.gd" id="3_gcdcj"] [ext_resource type="Script" path="res://scene/test/run.gd" id="4_c7f2w"] [ext_resource type="Texture2D" uid="uid://csk8u15wepd1w" path="res://icon.svg" id="5_lkgch"] +[ext_resource type="Script" path="res://scene/test/atk.gd" id="5_ol236"] [sub_resource type="CircleShape2D" id="CircleShape2D_740ny"] [sub_resource type="RectangleShape2D" id="RectangleShape2D_7uxj5"] -size = Vector2(41, 34) +size = Vector2(100, 108) [sub_resource type="Animation" id="Animation_tov45"] length = 0.001 @@ -60,11 +61,12 @@ colors = PackedColorArray(0, 1, 0, 1, 0, 1, 0, 1) [sub_resource type="GradientTexture1D" id="GradientTexture1D_jp0r7"] gradient = SubResource("Gradient_vdy2i") -[node name="CharacterBody2D" type="CharacterBody2D" node_paths=PackedStringArray("agent", "state_machine", "rotate")] +[node name="CharacterBody2D" type="CharacterBody2D" node_paths=PackedStringArray("agent", "state_machine", "rotate", "attack_area")] script = ExtResource("1_d0trv") agent = NodePath("agent") state_machine = NodePath("state_machine") rotate = NodePath("rotate") +attack_area = NodePath("rotate/attack_area") [node name="agent" type="NavigationAgent2D" parent="."] path_postprocessing = 1 @@ -89,14 +91,15 @@ script = ExtResource("3_gcdcj") [node name="run" type="Node" parent="state_machine"] script = ExtResource("4_c7f2w") -[node name="Node3" type="Node" parent="state_machine"] +[node name="atk" type="Node" parent="state_machine"] +script = ExtResource("5_ol236") [node name="rotate" type="Node2D" parent="."] [node name="attack_area" type="Area2D" parent="rotate"] [node name="CollisionShape2D" type="CollisionShape2D" parent="rotate/attack_area"] -position = Vector2(20.5, 0) +position = Vector2(50, 1) shape = SubResource("RectangleShape2D_7uxj5") [node name="Sprite2D" type="Sprite2D" parent="."] diff --git a/scene/test/condition_attack.gd b/scene/test/condition_attack.gd index 945a649..8de9f5b 100644 --- a/scene/test/condition_attack.gd +++ b/scene/test/condition_attack.gd @@ -3,7 +3,7 @@ func tick(actor:Node,black_board:Blackboard): var unit:UnitOther=actor var all_bodys=unit.sense_area.get_overlapping_bodies() for i in all_bodys: - if i is Unit: + if i is Unit and i !=unit: var favour=Global.get_unit_favour(unit.unit_id,i.unit_id) var res=get_res(favour,unit.get_hungry()) if res: diff --git a/scene/test/idle.gd b/scene/test/idle.gd index a478a19..6d87d2e 100644 --- a/scene/test/idle.gd +++ b/scene/test/idle.gd @@ -25,5 +25,8 @@ func process_message(type:String,n): "move": get_player().set_target_pos(n) change_to_state("run") - + "attack": + get_player().set_target_pos(n.global_position) + change_to_state("run",n) + pass pass diff --git a/scene/test/other_character.tscn b/scene/test/other_character.tscn index 7799e01..58d40cb 100644 --- a/scene/test/other_character.tscn +++ b/scene/test/other_character.tscn @@ -108,7 +108,7 @@ shape = SubResource("RectangleShape2D_lphpm") [node name="BeehaveTree" type="Node" parent="." node_paths=PackedStringArray("blackboard", "actor")] script = ExtResource("2_twyt5") -blackboard = NodePath("@Node@37976") +blackboard = NodePath("@Node@113329") actor = NodePath("..") [node name="SelectorComposite" type="Node" parent="BeehaveTree"] diff --git a/scene/test/run.gd b/scene/test/run.gd index 607e1d4..65d4592 100644 --- a/scene/test/run.gd +++ b/scene/test/run.gd @@ -1,15 +1,12 @@ extends State +var target func enter_state(n): + if n is Unit: + target=n pass func update_state(delta): pass func update_state_phy(delta): - if get_player().is_move_finished(): - - change_to_state("idle") - print("切换为idle") - return - match get_player().get_dir(): 0: get_player().play_animation("up") @@ -19,8 +16,25 @@ func update_state_phy(delta): get_player().play_animation("left_right") 3: get_player().play_animation("left_right") + if target!=null and is_instance_valid(target): + if get_player().is_unit_instance_in_attack_area(target): + change_to_state("atk",target) + get_player().stop_move() + return + get_player().set_target_pos(target.global_position) + + return + + if get_player().is_move_finished(): + + change_to_state("idle") + print("切换为idle") + return + + pass func exit_state(): + target=null pass func process_message(type:String,n): @@ -28,5 +42,9 @@ func process_message(type:String,n): "move": get_player().set_target_pos(n) change_to_state("run") - + target=null + "attack": + get_player().set_target_pos(n.global_position) + change_to_state("run",n) + pass pass diff --git a/scene/test/test_main.gd b/scene/test/test_main.gd index 87094c8..5399d7e 100644 --- a/scene/test/test_main.gd +++ b/scene/test/test_main.gd @@ -9,6 +9,32 @@ func _on_control_gui_input(event: InputEvent) -> void: if event.is_action_pressed("mouse_right"): if player: player.sent_message("move",player.get_global_mouse_position()) + + + if event.is_action_pressed("mouse_left"): + var arr=%mouse_finder.get_overlapping_bodies() + print(arr) + if arr.size()==0: + return + var target=get_closest_node(player,arr,Unit) + print(target) + if target!=null: + + player.sent_message("attack",target) pass pass # Replace with function body. + +func get_closest_node(self_node:Node2D,array:Array,target_class): + if array.size()==0: + return null + var length=999999999 + var node=null + for i in range(0,array.size()): + print(is_instance_of(i,target_class)) + if is_instance_of(array[i],target_class): + var l=(array[i].global_position-self_node.global_position).length() + if l