Browse Source

把斗鸡组件拆分为3个

zhengchen 1 year ago
parent
commit
ee0145efc3

+ 44 - 18
Assets/Resources/Cock/prefab_cock_1.prefab

@@ -1538,10 +1538,11 @@ GameObject:
   - component: {fileID: 5221786613589611655}
   - component: {fileID: 2204423604733202173}
   - component: {fileID: -1070774890500468579}
+  - component: {fileID: 1605545425760258602}
+  - component: {fileID: -1411749007126121323}
   - component: {fileID: -8531684669894697885}
   - component: {fileID: 3261479113818730106}
   - component: {fileID: 301670950033994504}
-  - component: {fileID: 2183380708503071168}
   m_Layer: 3
   m_Name: prefab_cock_1
   m_TagString: cock
@@ -1585,7 +1586,7 @@ Animator:
   m_Controller: {fileID: 9100000, guid: c08e2cd20ca874ee487ef60d32bd5ad1, type: 2}
   m_CullingMode: 1
   m_UpdateMode: 0
-  m_ApplyRootMotion: 1
+  m_ApplyRootMotion: 0
   m_LinearVelocityBlending: 0
   m_StabilizeFeet: 0
   m_WarningMessage: 
@@ -1604,11 +1605,35 @@ MonoBehaviour:
   m_Script: {fileID: 11500000, guid: 53ba5bbda83e41628281d34f7cdb84b3, type: 3}
   m_Name: 
   m_EditorClassIdentifier: 
-  reduceHp: 0
   playerId: 0
   cockId: 0
   rightForward: 0
   animator: {fileID: 2204423604733202173}
+--- !u!114 &1605545425760258602
+MonoBehaviour:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  m_GameObject: {fileID: 4844839584457444925}
+  m_Enabled: 1
+  m_EditorHideFlags: 0
+  m_Script: {fileID: 11500000, guid: 517203e70e90457296ca2c6cc9215ea6, type: 3}
+  m_Name: 
+  m_EditorClassIdentifier: 
+--- !u!114 &-1411749007126121323
+MonoBehaviour:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  m_GameObject: {fileID: 4844839584457444925}
+  m_Enabled: 1
+  m_EditorHideFlags: 0
+  m_Script: {fileID: 11500000, guid: 611446e125724d88a45dd79aae10db8a, type: 3}
+  m_Name: 
+  m_EditorClassIdentifier: 
+  reduceHp: 0
 --- !u!54 &-8531684669894697885
 Rigidbody:
   m_ObjectHideFlags: 0
@@ -1620,7 +1645,7 @@ Rigidbody:
   m_Mass: 1
   m_Drag: 0
   m_AngularDrag: 0.05
-  m_UseGravity: 0
+  m_UseGravity: 1
   m_IsKinematic: 0
   m_Interpolate: 0
   m_Constraints: 120
@@ -1734,20 +1759,6 @@ MonoBehaviour:
   m_Name: 
   m_EditorClassIdentifier: 
   Tag: "\u52A8\u6001\u7ED1\u5B9A"
---- !u!64 &2183380708503071168
-MeshCollider:
-  m_ObjectHideFlags: 0
-  m_CorrespondingSourceObject: {fileID: 0}
-  m_PrefabInstance: {fileID: 0}
-  m_PrefabAsset: {fileID: 0}
-  m_GameObject: {fileID: 4844839584457444925}
-  m_Material: {fileID: 0}
-  m_IsTrigger: 0
-  m_Enabled: 1
-  serializedVersion: 4
-  m_Convex: 0
-  m_CookingOptions: 30
-  m_Mesh: {fileID: 2954858793080512207, guid: c8e1df4a0fedb9540896f08670fdd82a, type: 3}
 --- !u!1 &4965920312758957799
 GameObject:
   m_ObjectHideFlags: 0
@@ -4534,6 +4545,7 @@ GameObject:
   m_Component:
   - component: {fileID: 5422928908509764028}
   - component: {fileID: 4237774417912555930}
+  - component: {fileID: 3887819962382202143}
   m_Layer: 3
   m_Name: Box024
   m_TagString: Untagged
@@ -4633,3 +4645,17 @@ SkinnedMeshRenderer:
     m_Center: {x: -0.08384214, y: 0.052456707, z: 0.00000029802322}
     m_Extent: {x: 0.47378123, y: 0.61368656, z: 0.47475332}
   m_DirtyAABB: 0
+--- !u!64 &3887819962382202143
+MeshCollider:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  m_GameObject: {fileID: 9167025688788380451}
+  m_Material: {fileID: 0}
+  m_IsTrigger: 0
+  m_Enabled: 1
+  serializedVersion: 4
+  m_Convex: 1
+  m_CookingOptions: 30
+  m_Mesh: {fileID: 106541212177282163, guid: 7c886ecf6863ebe48b327cdd75722b6a, type: 3}

+ 184 - 2
Assets/Scenes/Battle.unity

@@ -299,7 +299,7 @@ RectTransform:
   - {fileID: 245293552}
   - {fileID: 768214186}
   m_Father: {fileID: 1661859425}
-  m_RootOrder: 4
+  m_RootOrder: 5
   m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
   m_AnchorMin: {x: 0, y: 0}
   m_AnchorMax: {x: 1, y: 1}
@@ -541,6 +541,41 @@ CanvasRenderer:
   m_PrefabAsset: {fileID: 0}
   m_GameObject: {fileID: 204550757}
   m_CullTransparentMesh: 1
+--- !u!1 &207039175 stripped
+GameObject:
+  m_CorrespondingSourceObject: {fileID: 4861059266450437755, guid: 22ec67147dd0c974e927ea82a0a54334,
+    type: 3}
+  m_PrefabInstance: {fileID: 1591141603}
+  m_PrefabAsset: {fileID: 0}
+--- !u!54 &207039179
+Rigidbody:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  m_GameObject: {fileID: 207039175}
+  serializedVersion: 2
+  m_Mass: 1
+  m_Drag: 0
+  m_AngularDrag: 0.05
+  m_UseGravity: 0
+  m_IsKinematic: 1
+  m_Interpolate: 0
+  m_Constraints: 14
+  m_CollisionDetection: 0
+--- !u!65 &207039180
+BoxCollider:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  m_GameObject: {fileID: 207039175}
+  m_Material: {fileID: 0}
+  m_IsTrigger: 0
+  m_Enabled: 1
+  serializedVersion: 2
+  m_Size: {x: 138.47495, y: 136, z: 0.2}
+  m_Center: {x: 0.04725647, y: -1, z: -0.25}
 --- !u!1001 &216095376
 PrefabInstance:
   m_ObjectHideFlags: 0
@@ -1065,6 +1100,152 @@ CanvasRenderer:
   m_PrefabAsset: {fileID: 0}
   m_GameObject: {fileID: 549166198}
   m_CullTransparentMesh: 1
+--- !u!1 &659952594
+GameObject:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  serializedVersion: 6
+  m_Component:
+  - component: {fileID: 659952595}
+  - component: {fileID: 659952599}
+  - component: {fileID: 659952598}
+  - component: {fileID: 659952597}
+  - component: {fileID: 659952596}
+  m_Layer: 5
+  m_Name: Back
+  m_TagString: Untagged
+  m_Icon: {fileID: 0}
+  m_NavMeshLayer: 0
+  m_StaticEditorFlags: 0
+  m_IsActive: 1
+--- !u!224 &659952595
+RectTransform:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  m_GameObject: {fileID: 659952594}
+  m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
+  m_LocalPosition: {x: 0, y: 0, z: 0}
+  m_LocalScale: {x: 0.40000004, y: 0.40000004, z: 0.40000004}
+  m_ConstrainProportionsScale: 1
+  m_Children: []
+  m_Father: {fileID: 1661859425}
+  m_RootOrder: 3
+  m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
+  m_AnchorMin: {x: 1, y: 1}
+  m_AnchorMax: {x: 1, y: 1}
+  m_AnchoredPosition: {x: -100, y: -120}
+  m_SizeDelta: {x: 161, y: 170}
+  m_Pivot: {x: 0.5, y: 0.5}
+--- !u!114 &659952596
+MonoBehaviour:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  m_GameObject: {fileID: 659952594}
+  m_Enabled: 1
+  m_EditorHideFlags: 0
+  m_Script: {fileID: 11500000, guid: 9d8a209b213548cb8e244ec3af3f2596, type: 3}
+  m_Name: 
+  m_EditorClassIdentifier: 
+--- !u!114 &659952597
+MonoBehaviour:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  m_GameObject: {fileID: 659952594}
+  m_Enabled: 1
+  m_EditorHideFlags: 0
+  m_Script: {fileID: 11500000, guid: 4e29b1a8efbd4b44bb3f3716e73f07ff, type: 3}
+  m_Name: 
+  m_EditorClassIdentifier: 
+  m_Navigation:
+    m_Mode: 3
+    m_WrapAround: 0
+    m_SelectOnUp: {fileID: 0}
+    m_SelectOnDown: {fileID: 0}
+    m_SelectOnLeft: {fileID: 0}
+    m_SelectOnRight: {fileID: 0}
+  m_Transition: 1
+  m_Colors:
+    m_NormalColor: {r: 1, g: 1, b: 1, a: 1}
+    m_HighlightedColor: {r: 0.9607843, g: 0.9607843, b: 0.9607843, a: 1}
+    m_PressedColor: {r: 0.78431374, g: 0.78431374, b: 0.78431374, a: 1}
+    m_SelectedColor: {r: 0.9607843, g: 0.9607843, b: 0.9607843, a: 1}
+    m_DisabledColor: {r: 0.78431374, g: 0.78431374, b: 0.78431374, a: 0.5019608}
+    m_ColorMultiplier: 1
+    m_FadeDuration: 0.1
+  m_SpriteState:
+    m_HighlightedSprite: {fileID: 0}
+    m_PressedSprite: {fileID: 0}
+    m_SelectedSprite: {fileID: 0}
+    m_DisabledSprite: {fileID: 0}
+  m_AnimationTriggers:
+    m_NormalTrigger: Normal
+    m_HighlightedTrigger: Highlighted
+    m_PressedTrigger: Pressed
+    m_SelectedTrigger: Selected
+    m_DisabledTrigger: Disabled
+  m_Interactable: 1
+  m_TargetGraphic: {fileID: 659952598}
+  m_OnClick:
+    m_PersistentCalls:
+      m_Calls:
+      - m_Target: {fileID: 1935069881}
+        m_TargetAssemblyTypeName: Comp.BattleMainComp, Assembly-CSharp
+        m_MethodName: Lift
+        m_Mode: 1
+        m_Arguments:
+          m_ObjectArgument: {fileID: 0}
+          m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
+          m_IntArgument: 0
+          m_FloatArgument: 0
+          m_StringArgument: 
+          m_BoolArgument: 0
+        m_CallState: 1
+--- !u!114 &659952598
+MonoBehaviour:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  m_GameObject: {fileID: 659952594}
+  m_Enabled: 1
+  m_EditorHideFlags: 0
+  m_Script: {fileID: 11500000, guid: fe87c0e1cc204ed48ad3b37840f39efc, type: 3}
+  m_Name: 
+  m_EditorClassIdentifier: 
+  m_Material: {fileID: 0}
+  m_Color: {r: 1, g: 1, b: 1, a: 1}
+  m_RaycastTarget: 1
+  m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0}
+  m_Maskable: 1
+  m_OnCullStateChanged:
+    m_PersistentCalls:
+      m_Calls: []
+  m_Sprite: {fileID: 21300000, guid: 14d4022ae3cfe4c71a2616e546ac1fee, type: 3}
+  m_Type: 0
+  m_PreserveAspect: 0
+  m_FillCenter: 1
+  m_FillMethod: 4
+  m_FillAmount: 1
+  m_FillClockwise: 1
+  m_FillOrigin: 0
+  m_UseSpriteMesh: 0
+  m_PixelsPerUnitMultiplier: 1
+--- !u!222 &659952599
+CanvasRenderer:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  m_GameObject: {fileID: 659952594}
+  m_CullTransparentMesh: 1
 --- !u!1001 &673225776
 PrefabInstance:
   m_ObjectHideFlags: 0
@@ -1937,7 +2118,7 @@ RectTransform:
   m_ConstrainProportionsScale: 1
   m_Children: []
   m_Father: {fileID: 1661859425}
-  m_RootOrder: 3
+  m_RootOrder: 4
   m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
   m_AnchorMin: {x: 0.5, y: 0}
   m_AnchorMax: {x: 0.5, y: 0}
@@ -2446,6 +2627,7 @@ RectTransform:
   - {fileID: 842053958}
   - {fileID: 549166199}
   - {fileID: 1017659099}
+  - {fileID: 659952595}
   - {fileID: 1349947342}
   - {fileID: 63690197}
   m_Father: {fileID: 0}

+ 51 - 200
Assets/Scripts/Comp/CockActionComp.cs

@@ -9,65 +9,20 @@ namespace Comp
 {
     public class CockActionComp : MonoBehaviour
     {
-        private static readonly Color HpGreen = new Color(0.06f, 0.9f, 0.18f);
-        private static readonly Color HpYellow = new Color(0.9f, 0.9f, 0.18f);
-        private static readonly Color HpRed = new Color(0.9f, 0.18f, 0.18f);
-
         private const string CockTag = "cock";
 
+        // 动画控制器
         private ICockController _cockController;
 
-        // 血量条组件
-        private Image _backgroundImage, _foregroundImage;
-
-        private int _maxHp, _curHp;
-
-        public int reduceHp; // 需要扣除的Hp
-
-        public int MaxHp
-        {
-            set
-            {
-                _maxHp = value;
-                _curHp = _maxHp;
-            }
-        }
-
-        private void ReduceCurHp(int hp)
-        {
-            _curHp -= hp;
-            var hpPercent = (float)_curHp / _maxHp;
-            if (hpPercent > 0.5f)
-            {
-                _foregroundImage.color = HpGreen;
-            }
-            else if (hpPercent is >= 0.25f and <= 0.5f)
-            {
-                _foregroundImage.color = HpYellow;
-            }
-            else
-            {
-                _foregroundImage.color = HpRed;
-            }
+        // 移动组件
+        private CockMoveComp _cockMoveComp;
 
-            var backgroundSize = _backgroundImage.rectTransform.sizeDelta;
-            var foregroundSize = _foregroundImage.rectTransform.sizeDelta;
-            var newForegroundWidth = backgroundSize.x * hpPercent;
-            _foregroundImage.rectTransform.sizeDelta = new Vector2(newForegroundWidth, foregroundSize.y);
-        }
+        private bool _highJump; // 是否高跳
 
         // 跑步跟切换待机状态相关参数
 
         private bool _firstTrigger = true; // 第一次碰撞切换为待机
 
-        private Vector3 _thrust;
-
-        private const float MoveSpeed = 1f;
-
-        // 锁定物体相关参数
-
-        private float _miniY; // 最低Y坐标
-
         // 执行动作相关
 
         public Action curAction;
@@ -76,8 +31,6 @@ namespace Comp
 
         // 水平与垂直方向运动相关
 
-        private CockJumpData _lowData, _highData, _curData;
-
         public int playerId;
 
         public int cockId;
@@ -94,16 +47,12 @@ namespace Comp
         {
             get
             {
-                if (_cockController == null)
-                {
-                    _cockController = new AnimatorCockController(animator);
-                    _cockController.OnRun += OnRun;
-                    _cockController.OnIdle += OnIdle;
-                    _cockController.OnAttack += OnAttack;
-                    _cockController.OnJumpAttack += OnJumpAttack;
-                    return _cockController;
-                }
-
+                if (_cockController != null) return _cockController;
+                _cockController = new AnimatorCockController(animator);
+                _cockController.OnRun += OnRun;
+                _cockController.OnIdle += OnIdle;
+                _cockController.OnAttack += OnAttack;
+                _cockController.OnJumpAttack += OnJumpAttack;
                 return _cockController;
             }
         }
@@ -112,37 +61,20 @@ namespace Comp
 
         private void OnCollisionEnter(Collision collision)
         {
-            if (collision.gameObject.CompareTag(CockTag) && _firstTrigger)
-            {
-                _firstTrigger = false;
-                _cockController.IdleInBattle();
-                GameCore.Instance.GetCurState().BattleStart();
-            }
+            if (!collision.gameObject.CompareTag(CockTag) || !_firstTrigger) return;
+            _firstTrigger = false;
+            _cockController.IdleInBattle();
+            GameCore.Instance.GetCurState().BattleStart();
+        }
+
+        private void Awake()
+        {
+            _cockMoveComp = GetComponent<CockMoveComp>();
         }
 
         private void Start()
         {
             _missPrefab = Resources.Load<GameObject>("Prefab/prefab_miss");
-            // 获取血条的填充图片
-            var images = GetComponentsInChildren<Image>();
-            foreach (var image in images)
-            {
-                if (image.name == "Background")
-                {
-                    _backgroundImage = image;
-                }
-                if (image.name == "Foreground")
-                {
-                    _foregroundImage = image;
-                }
-            }
-
-            // 获取最低Y坐标
-            _miniY = transform.position.y;
-            // 跳跃相关
-            _lowData = new CockJumpData(0.5f, 0.45f, false);
-            _highData = new CockJumpData(1.0f, 0.3f, true);
-            _curData = _highData;
             StartCoroutine(CallEverySeconds());
         }
 
@@ -158,165 +90,84 @@ namespace Comp
 
         private void Update()
         {
-            if (_createMiss)
-            {
-                _createMiss = false;
-                CreateMissText();
-            }
-
-            if (reduceHp != 0)
-            {
-                ReduceCurHp(reduceHp);
-                reduceHp = 0;
-            }
+            if (!_createMiss) return;
+            _createMiss = false;
+            CreateMissText();
         }
 
         private void FixedUpdate()
         {
-            transform.Translate(_thrust * Time.deltaTime);
+            // transform.Translate(_thrust * Time.deltaTime);
 
             // 移动游戏对象
             // if (GameCore.Instance.inBattleState)
-            transform.position += _curData.GetJumpVector();
-            if (GameCore.Instance.inBattleState)
-            {
-                if (rightForward)
-                {
-                    transform.position += _curData.GetMoveVector();
-                }
-                else
-                {
-                    transform.position -= _curData.GetMoveVector();
-                }
-            }
-
-            if (transform.position.y < _miniY)
-            {
-                var position = transform.position;
-                position = new Vector3(position.x, _miniY, position.z);
-                transform.position = position;
-            }
+            // transform.position += _curData.GetJumpVector();
+            // if (GameCore.Instance.inBattleState)
+            // {
+            //     if (rightForward)
+            //     {
+            //         transform.position += _curData.GetMoveVector();
+            //     }
+            //     else
+            //     {
+            //         transform.position -= _curData.GetMoveVector();
+            //     }
+            // }
+
+            // if (transform.position.y < _miniY)
+            // {
+            //     var position = transform.position;
+            //     position = new Vector3(position.x, _miniY, position.z);
+            //     transform.position = position;
+            // }
         }
 
         private void OnRun()
         {
-            _thrust = new Vector3(0, 0, MoveSpeed);
+            _cockMoveComp.MoveAndNeverStop();
         }
 
         private void OnIdle()
         {
-            _thrust = new Vector3(0, 0, 0);
+            _cockMoveComp.Stop();
         }
 
         private void OnAttack()
         {
-            _curData.StartMove();
+            SoundCore.Instance.PlaySound(SoundType.CockJumpAttack, "player-" + playerId);
+            _cockMoveComp.Move();
         }
 
         private void OnJumpAttack()
         {
             SoundCore.Instance.PlaySound(SoundType.CockJumpAttack, "player-" + playerId);
-            _curData.StartJump();
+            _cockMoveComp.Move();
+            _cockMoveComp.Jump(_highJump);
         }
 
         public void OnAttackEnd()
         {
-            _curData.StopMove();
         }
 
         public void OnJumpEnd()
         {
-            _curData.StopJump();
         }
 
         public void SetHighJump(bool high)
         {
-            _curData = high ? _highData : _lowData;
+            _highJump = high;
         }
 
         public void CreateMiss()
         {
-            this._createMiss = true;
+            _createMiss = true;
         }
 
+        // ReSharper disable Unity.PerformanceAnalysis
         private void CreateMissText()
         {
             var obj = Instantiate(_missPrefab);
             obj.GetComponent<MissText>().SetTarget(transform);
         }
     }
-
-    public class CockJumpData
-    {
-        private readonly float _gravity; // 重力
-
-        private readonly float _jumpVelocity; // 跳跃速度
-
-        private readonly bool _forward; // 是否前进
-
-        private Vector3 _jumpDirection = Vector3.zero;
-
-        private Vector3 _moveDirection = Vector3.zero;
-
-        private bool _move = false; // 是否移动状态
-
-        private float _timeToJumpApex;
-
-        private float _curJumpTime;
-
-        // 跳跃高度 到达跳跃高度的时间
-        public CockJumpData(float jumpHeight, float timeToJumpApex, bool forward)
-        {
-            _forward = forward;
-            _timeToJumpApex = timeToJumpApex;
-            // 计算跳跃速度和重力
-            _gravity = -(2 * jumpHeight) / Mathf.Pow(timeToJumpApex, 2);
-            _jumpVelocity = Mathf.Abs(_gravity) * timeToJumpApex;
-        }
-
-        public Vector3 GetJumpVector()
-        {
-            if (_curJumpTime < 0)
-            {
-                return Vector3.zero;
-            }
-
-            // 计算重力
-            _jumpDirection.y += _gravity * Time.deltaTime;
-            _curJumpTime -= Time.deltaTime;
-            // 移动游戏对象
-            return _jumpDirection * Time.deltaTime;
-        }
-
-        public Vector3 GetMoveVector()
-        {
-            if (!_move)
-                return Vector3.zero;
-            // 计算位移
-            _moveDirection.x = (_forward ? 30f : -30f) * Time.deltaTime;
-            return _moveDirection * Time.deltaTime;
-        }
-
-        public void StartJump()
-        {
-            _jumpDirection.y = _jumpVelocity;
-            _curJumpTime = _timeToJumpApex * 2; // 双倍时间
-            _move = true;
-        }
-
-        public void StartMove()
-        {
-            _move = true;
-        }
-
-        public void StopJump()
-        {
-            _move = false;
-        }
-
-        public void StopMove()
-        {
-            _move = false;
-        }
-    }
 }

+ 75 - 0
Assets/Scripts/Comp/CockHpComp.cs

@@ -0,0 +1,75 @@
+using System;
+using UnityEngine;
+using UnityEngine.UI;
+
+namespace Comp
+{
+    public class CockHpComp : MonoBehaviour
+    {
+        // 血量条组件
+        private Image _backgroundImage, _foregroundImage;
+
+        private int _maxHp, _curHp;
+
+        public int reduceHp; // 需要扣除的Hp
+
+        public int MaxHp
+        {
+            set
+            {
+                _maxHp = value;
+                _curHp = _maxHp;
+            }
+        }
+
+        private void ReduceCurHp(int hp)
+        {
+            _curHp -= hp;
+            var hpPercent = (float)_curHp / _maxHp;
+            if (hpPercent > 0.5f)
+            {
+                _foregroundImage.color = Color.green;
+            }
+            else if (hpPercent is >= 0.25f and <= 0.5f)
+            {
+                _foregroundImage.color = Color.yellow;
+            }
+            else
+            {
+                _foregroundImage.color = Color.red;
+            }
+
+            var backgroundSize = _backgroundImage.rectTransform.sizeDelta;
+            var foregroundSize = _foregroundImage.rectTransform.sizeDelta;
+            var newForegroundWidth = backgroundSize.x * hpPercent;
+            _foregroundImage.rectTransform.sizeDelta = new Vector2(newForegroundWidth, foregroundSize.y);
+        }
+
+        private void Start()
+        {
+            // 获取血条的填充图片
+            var images = GetComponentsInChildren<Image>();
+            foreach (var image in images)
+            {
+                if (image.name == "Background")
+                {
+                    _backgroundImage = image;
+                }
+
+                if (image.name == "Foreground")
+                {
+                    _foregroundImage = image;
+                }
+            }
+        }
+
+        private void Update()
+        {
+            if (reduceHp != 0)
+            {
+                ReduceCurHp(reduceHp);
+                reduceHp = 0;
+            }
+        }
+    }
+}

+ 3 - 0
Assets/Scripts/Comp/CockHpComp.cs.meta

@@ -0,0 +1,3 @@
+fileFormatVersion: 2
+guid: 611446e125724d88a45dd79aae10db8a
+timeCreated: 1681729241

+ 76 - 0
Assets/Scripts/Comp/CockMoveComp.cs

@@ -0,0 +1,76 @@
+using System;
+using UnityEngine;
+
+namespace Comp
+{
+    public class CockMoveComp : MonoBehaviour
+    {
+        // 刚体
+        private Rigidbody _rigidbody;
+
+        // 水平运动相关
+        private bool _isMoving; // 是否进行水平运动
+
+        private float _movingTime; // 水平运动时间
+
+        private const float MovingSpeed = 1f; // 移动速度
+
+        private const float MovingTime = 1f; // 移动时间
+
+        // 跳跃相关
+        private const float HighJumpSpeed = 2f; // 高跳速度
+
+        private const float LowJumpSpeed = 1f; // 低跳速度
+
+        private Vector3 CreateMovingVector(float speed)
+        {
+            var forward = transform.forward;
+            return new Vector3(speed * forward.x, 0, 0);
+        }
+
+        private Vector3 CreateJumpingVector(float y)
+        {
+            return new Vector3(0, y, 0);
+        }
+
+        public void Move()
+        {
+            _isMoving = true;
+            _movingTime = MovingTime;
+        }
+
+        public void MoveAndNeverStop()
+        {
+            _isMoving = true;
+            _movingTime = float.MaxValue;
+        }
+
+        public void Stop()
+        {
+            _isMoving = false;
+            _movingTime = 0;
+        }
+
+        public void Jump(bool highJump)
+        {
+            var jumpSpeed = highJump ? HighJumpSpeed : LowJumpSpeed;
+            var jumpingVector = CreateJumpingVector(jumpSpeed);
+            _rigidbody.AddForce(jumpingVector); // 添加力
+        }
+
+        private void Start()
+        {
+            _rigidbody = GetComponent<Rigidbody>();
+        }
+
+        private void FixedUpdate()
+        {
+            if (!_isMoving) return;
+            var movingVector = CreateMovingVector(MovingSpeed);
+            transform.position += movingVector * Time.deltaTime;
+            _movingTime -= Time.deltaTime;
+            if (_movingTime <= 0)
+                _isMoving = false;
+        }
+    }
+}

+ 3 - 0
Assets/Scripts/Comp/CockMoveComp.cs.meta

@@ -0,0 +1,3 @@
+fileFormatVersion: 2
+guid: 517203e70e90457296ca2c6cc9215ea6
+timeCreated: 1681721318

+ 10 - 9
Assets/Scripts/Game/GameBattleState.cs

@@ -26,7 +26,7 @@ namespace Game
             if (_battleTimer == null)
             {
                 Debug.Log("battle timer start");
-                foreach (var cockActionComp in _gameCore.cockDict)
+                foreach (var cockActionComp in _gameCore.cockActionComps)
                 {
                     if (cockActionComp.Key == _gameCore.localPlayer.playerId)
                     {
@@ -101,25 +101,26 @@ namespace Game
             for (int i = 0; i <= battleDetailObj.logs.Length - 1; i++)
             {
                 var log = battleDetailObj.logs[i];
-                var fromComp = _gameCore.GetCockActionCompByPlayerId(log.from);
-                fromComp.SetHighJump(i == 0);
-                var toComp = _gameCore.GetCockActionCompByPlayerId(log.to);
+                var attackCockActionComp = _gameCore.GetCockActionCompByPlayerId(log.from);
+                attackCockActionComp.SetHighJump(i == 0);
+                var counterattackCockActionComp = _gameCore.GetCockActionCompByPlayerId(log.to);
                 if (i == 0 && log.value == 0) // 攻击落空了
                 {
-                    toComp.CreateMiss();
+                    counterattackCockActionComp.CreateMiss();
                 }
-                else
+                else if (i == 0 && log.value > 0)
                 {
-                    toComp.reduceHp = log.value;
+                    var cockHpComp = _gameCore.GetCockHpCompByPlayerId(log.to);
+                    cockHpComp.reduceHp = log.value;
                 }
 
                 switch (log.type)
                 {
                     case 1:
-                        fromComp.curAction = fromComp.CockController.JumpAndAttack;
+                        attackCockActionComp.curAction = attackCockActionComp.CockController.JumpAndAttack;
                         break;
                     case 2:
-                        fromComp.curAction = fromComp.CockController.Attack;
+                        attackCockActionComp.curAction = attackCockActionComp.CockController.Attack;
                         break;
                 }
             }

+ 13 - 10
Assets/Scripts/Game/GameCore.cs

@@ -25,7 +25,9 @@ namespace Game
 
         [RdReset] public int diamond;
 
-        [RdReset] public Dictionary<int, CockActionComp> cockDict;
+        [RdReset] public Dictionary<int, CockActionComp> cockActionComps;
+
+        [RdReset] public Dictionary<int, CockHpComp> cockHpComps;
 
         [RdReset] public Dictionary<int, MasterActionComp> masterDict;
 
@@ -76,7 +78,8 @@ namespace Game
             this.camera = camera;
             this.resultPanel = resultPanel;
 
-            cockDict = new Dictionary<int, CockActionComp>();
+            cockActionComps = new Dictionary<int, CockActionComp>();
+            cockHpComps = new Dictionary<int, CockHpComp>();
             masterDict = new Dictionary<int, MasterActionComp>();
 
             winPlayerId = jsonLogs.winPlayer;
@@ -108,22 +111,22 @@ namespace Game
             return GetControllerByPlayerId(gamePlayer.playerId);
         }
 
-        public ICockController GetControllerByPlayerId(int playerId)
+        private ICockController GetControllerByPlayerId(int playerId)
         {
-            cockDict.TryGetValue(playerId, out var cockActionComp);
+            cockActionComps.TryGetValue(playerId, out var cockActionComp);
             return cockActionComp != null ? cockActionComp.CockController : null;
         }
 
-        public IMasterController GetMasterControllerByPlayerId(int playerId)
+        public CockActionComp GetCockActionCompByPlayerId(int playerId)
         {
-            masterDict.TryGetValue(playerId, out var masterActionComp);
-            return masterActionComp != null ? masterActionComp.MasterController : null;
+            cockActionComps.TryGetValue(playerId, out var cockActionComp);
+            return cockActionComp != null ? cockActionComp : null;
         }
 
-        public CockActionComp GetCockActionCompByPlayerId(int playerId)
+        public CockHpComp GetCockHpCompByPlayerId(int playerId)
         {
-            cockDict.TryGetValue(playerId, out var cockActionComp);
-            return cockActionComp != null ? cockActionComp : null;
+            cockHpComps.TryGetValue(playerId, out var cockHpComp);
+            return cockHpComp != null ? cockHpComp : null;
         }
 
         public void ReportMasterBack(bool leftSide)

+ 2 - 2
Assets/Scripts/Game/GameLiftState.cs

@@ -37,7 +37,7 @@ namespace Game
         {
             _gameCore.masterBack.Item1 = false;
             _gameCore.masterBack.Item2 = false;
-            foreach (var cockActionComp in _gameCore.cockDict)
+            foreach (var cockActionComp in _gameCore.cockActionComps)
             {
                 cockActionComp.Value.gameObject.GetComponent<SphereCollider>().isTrigger = true;
             }
@@ -49,7 +49,7 @@ namespace Game
 
         public override void ExitState()
         {
-            foreach (var cockActionComp in _gameCore.cockDict)
+            foreach (var cockActionComp in _gameCore.cockActionComps)
             {
                 cockActionComp.Value.gameObject.GetComponent<SphereCollider>().isTrigger = false;
             }

+ 14 - 8
Assets/Scripts/Game/GamePendingState.cs

@@ -47,7 +47,16 @@ namespace Game
             var antiCockPrefab = CockFactory.Instance.GenerateCockPrefab(_gameCore.antiPlayer.cockId);
             var localCock = Object.Instantiate(localCockPrefab);
             var antiCock = Object.Instantiate(antiCockPrefab);
-            // 处理鸡组件
+            // 处理鸡Hp组件
+            var localCockHpComp = localCock.GetComponent<CockHpComp>();
+            var antiCockHpComp = antiCock.GetComponent<CockHpComp>();
+
+            localCockHpComp.MaxHp = _gameCore.localPlayer.hp;
+            antiCockHpComp.MaxHp = _gameCore.antiPlayer.hp;
+
+            _gameCore.cockHpComps.Add(_gameCore.localPlayer.playerId, localCockHpComp);
+            _gameCore.cockHpComps.Add(_gameCore.antiPlayer.playerId, antiCockHpComp);
+            // 处理鸡动作组件
             var localCockActionComp = localCock.GetComponent<CockActionComp>();
             var antiCockActionComp = antiCock.GetComponent<CockActionComp>();
 
@@ -57,15 +66,12 @@ namespace Game
             localCockActionComp.playerId = _gameCore.localPlayer.playerId;
             antiCockActionComp.playerId = _gameCore.antiPlayer.playerId;
 
-            localCockActionComp.MaxHp = _gameCore.localPlayer.hp;
-            antiCockActionComp.MaxHp = _gameCore.antiPlayer.hp;
-
-            _gameCore.cockDict.Add(_gameCore.localPlayer.playerId, localCockActionComp);
-            _gameCore.cockDict.Add(_gameCore.antiPlayer.playerId, antiCockActionComp);
-
+            _gameCore.cockActionComps.Add(_gameCore.localPlayer.playerId, localCockActionComp);
+            _gameCore.cockActionComps.Add(_gameCore.antiPlayer.playerId, antiCockActionComp);
+            // 处理声音组件
             localCock.GetComponent<SoundCtrl>().Tag = "player-" + _gameCore.localPlayer.playerId;
             antiCock.GetComponent<SoundCtrl>().Tag = "player-" + _gameCore.antiPlayer.playerId;
-
+            // 处理方向位置
             localCock.transform.eulerAngles = new Vector3(0, 90, 0);
             antiCock.transform.eulerAngles = new Vector3(0, -90, 0);