From 61a4cabf15b4a1703bc0ac7ec001673b024af98b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=A8=E8=90=BD=E5=9F=BA=E5=9B=B4=E8=99=BE?= <3161880837@qq.com> Date: Tue, 26 Aug 2025 10:17:38 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0EntityBase=E5=92=8CRooster?= =?UTF-8?q?=E7=B1=BB=EF=BC=8C=E6=B7=BB=E5=8A=A0=E5=8A=A8=E7=94=BB=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E5=B9=B6=E4=BF=AE=E5=A4=8D=E5=8F=98=E9=87=8F=E5=91=BD?= =?UTF-8?q?=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/Abstracts/EntityBase.tscn | 92 ++++++++++++++++++++++++-- components/Characters/Rooster.tscn | 15 +++-- scripts/Contents/Characters/Rooster.gd | 4 +- scripts/Statemachine/EntityBase.gd | 10 +++ 4 files changed, 108 insertions(+), 13 deletions(-) diff --git a/components/Abstracts/EntityBase.tscn b/components/Abstracts/EntityBase.tscn index 3577f06..2d57d5d 100644 --- a/components/Abstracts/EntityBase.tscn +++ b/components/Abstracts/EntityBase.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=6 format=3 uid="uid://cvogxi7mktumf"] +[gd_scene load_steps=13 format=3 uid="uid://cvogxi7mktumf"] [ext_resource type="Script" path="res://scripts/Statemachine/EntityBase.gd" id="1_mvol6"] [ext_resource type="PackedScene" uid="uid://dcjqjqere8ets" path="res://components/UI/EntityStateBar.tscn" id="2_uje1g"] @@ -6,7 +6,72 @@ [sub_resource type="SpriteFrames" id="SpriteFrames_wcqpm"] [sub_resource type="CircleShape2D" id="CircleShape2D_buhm1"] -radius = 43.2897 +radius = 40.0 + +[sub_resource type="Animation" id="Animation_3wcjn"] +length = 0.001 +tracks/0/type = "value" +tracks/0/imported = false +tracks/0/enabled = true +tracks/0/path = NodePath(".:scale") +tracks/0/interp = 1 +tracks/0/loop_wrap = true +tracks/0/keys = { +"times": PackedFloat32Array(0), +"transitions": PackedFloat32Array(1), +"update": 0, +"values": [Vector2(1, 1)] +} + +[sub_resource type="Animation" id="Animation_4veur"] +resource_name = "left" +tracks/0/type = "value" +tracks/0/imported = false +tracks/0/enabled = true +tracks/0/path = NodePath(".:scale") +tracks/0/interp = 1 +tracks/0/loop_wrap = true +tracks/0/keys = { +"times": PackedFloat32Array(0), +"transitions": PackedFloat32Array(1), +"update": 0, +"values": [Vector2(-1, 1)] +} + +[sub_resource type="Animation" id="Animation_fgeas"] +resource_name = "left" +tracks/0/type = "value" +tracks/0/imported = false +tracks/0/enabled = true +tracks/0/path = NodePath(".:scale") +tracks/0/interp = 1 +tracks/0/loop_wrap = true +tracks/0/keys = { +"times": PackedFloat32Array(0), +"transitions": PackedFloat32Array(1), +"update": 0, +"values": [Vector2(1, 1)] +} + +[sub_resource type="AnimationLibrary" id="AnimationLibrary_kbmff"] +_data = { +"RESET": SubResource("Animation_3wcjn"), +"left": SubResource("Animation_4veur"), +"right": SubResource("Animation_fgeas") +} + +[sub_resource type="AnimationNodeAnimation" id="AnimationNodeAnimation_ao5k4"] +animation = &"left" + +[sub_resource type="AnimationNodeAnimation" id="AnimationNodeAnimation_611yr"] +animation = &"right" + +[sub_resource type="AnimationNodeBlendSpace1D" id="AnimationNodeBlendSpace1D_51ube"] +blend_point_0/node = SubResource("AnimationNodeAnimation_ao5k4") +blend_point_0/pos = -1.0 +blend_point_1/node = SubResource("AnimationNodeAnimation_611yr") +blend_point_1/pos = 1.0 +snap = 1.0 [sub_resource type="RectangleShape2D" id="RectangleShape2D_ce3to"] size = Vector2(64, 14) @@ -15,17 +80,30 @@ size = Vector2(64, 14) script = ExtResource("1_mvol6") [node name="texture" type="AnimatedSprite2D" parent="."] +unique_name_in_owner = true +position = Vector2(0, -50) sprite_frames = SubResource("SpriteFrames_wcqpm") -[node name="statebar" parent="texture" instance=ExtResource("2_uje1g")] -position = Vector2(0, -51) - [node name="hurtbox" type="Area2D" parent="texture"] +unique_name_in_owner = true [node name="hitbox" type="CollisionShape2D" parent="texture/hurtbox"] -position = Vector2(7, 12) shape = SubResource("CircleShape2D_buhm1") +[node name="animator" type="AnimationPlayer" parent="texture"] +libraries = { +"": SubResource("AnimationLibrary_kbmff") +} + +[node name="animatree" type="AnimationTree" parent="texture/animator"] +unique_name_in_owner = true +root_node = NodePath("%animatree/../..") +tree_root = SubResource("AnimationNodeBlendSpace1D_51ube") +anim_player = NodePath("..") +parameters/blend_position = 1.0 + [node name="movebox" type="CollisionShape2D" parent="."] -position = Vector2(16, 69) shape = SubResource("RectangleShape2D_ce3to") + +[node name="statebar" parent="." instance=ExtResource("2_uje1g")] +position = Vector2(0, -100) diff --git a/components/Characters/Rooster.tscn b/components/Characters/Rooster.tscn index d830988..752c6fa 100644 --- a/components/Characters/Rooster.tscn +++ b/components/Characters/Rooster.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=6 format=3 uid="uid://bm7ymrri6pykb"] +[gd_scene load_steps=7 format=3 uid="uid://bm7ymrri6pykb"] [ext_resource type="PackedScene" uid="uid://cvogxi7mktumf" path="res://components/Abstracts/EntityBase.tscn" id="1_e5pl8"] [ext_resource type="Script" path="res://scripts/Contents/Characters/Rooster.gd" id="2_oqdqd"] @@ -19,12 +19,19 @@ animations = [{ "speed": 5.0 }] +[sub_resource type="CircleShape2D" id="CircleShape2D_h1v0q"] +radius = 41.0122 + [node name="Rooster" instance=ExtResource("1_e5pl8")] script = ExtResource("2_oqdqd") [node name="texture" parent="." index="0"] +position = Vector2(0, -70) sprite_frames = SubResource("SpriteFrames_4v2ol") -[node name="statebar" parent="texture" index="0" node_paths=PackedStringArray("entity")] -position = Vector2(0, -83) -entity = NodePath("../..") +[node name="hitbox" parent="texture/hurtbox" index="0"] +position = Vector2(12, 16) +shape = SubResource("CircleShape2D_h1v0q") + +[node name="statebar" parent="." index="2"] +position = Vector2(0, -151) diff --git a/scripts/Contents/Characters/Rooster.gd b/scripts/Contents/Characters/Rooster.gd index 0f14c5d..e927cdc 100644 --- a/scripts/Contents/Characters/Rooster.gd +++ b/scripts/Contents/Characters/Rooster.gd @@ -2,8 +2,8 @@ extends EntityBase class_name Rooster func ai(): - var vector = Vector2( + var direction = Vector2( Input.get_axis("m_left", "m_right"), Input.get_axis("m_up", "m_down") ) - move(vector) + move(direction) diff --git a/scripts/Statemachine/EntityBase.gd b/scripts/Statemachine/EntityBase.gd index cc631dd..2b03175 100644 --- a/scripts/Statemachine/EntityBase.gd +++ b/scripts/Statemachine/EntityBase.gd @@ -4,12 +4,19 @@ class_name EntityBase # 这是个抽象类 @export var maxHealth: float = 100 @export var movementSpeed: float = 100 +@onready var animatree = $"%animatree" +@onready var texture = $"%texture" +@onready var hurtbox = $"%hurtbox" + var health: float = 0 +var lastDirection: int = 1 + func _ready(): health = maxHealth func _process(_delta): health = clamp(health, 0, maxHealth) + animatree.set("parameters/blend_position", lerpf(animatree.get("parameters/blend_position"), lastDirection, 0.1)) func _physics_process(_delta: float) -> void: velocity = Vector2.ZERO ai() @@ -18,6 +25,9 @@ func _physics_process(_delta: float) -> void: # 通用方法 func move(direction: Vector2): velocity = direction.normalized() * movementSpeed + var currentDirection = sign(direction.x) + if currentDirection != 0: + lastDirection = currentDirection # 抽象方法 func ai():