From b109b30ae1dc4fcfc4fc41fe8732a09727c8f935 Mon Sep 17 00:00:00 2001
From: liuliang <597380732@qq.com>
Date: Thu, 12 Dec 2024 17:06:08 +0800
Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E4=BA=86=E5=8A=A8=E7=94=BB=E6=8E=A7?=
=?UTF-8?q?=E5=88=B6=E5=99=A8?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
Aniatioon.meta | 8 ++
Aniatioon/SpriteAnimator.cs | 217 +++++++++++++++++++++++++++++++
Aniatioon/SpriteAnimator.cs.meta | 11 ++
Role/Attack.cs | 10 ++
Role/Bullet.cs | 28 ++--
Role/Fun.cs | 28 +---
6 files changed, 266 insertions(+), 36 deletions(-)
create mode 100644 Aniatioon.meta
create mode 100644 Aniatioon/SpriteAnimator.cs
create mode 100644 Aniatioon/SpriteAnimator.cs.meta
diff --git a/Aniatioon.meta b/Aniatioon.meta
new file mode 100644
index 0000000..2085a47
--- /dev/null
+++ b/Aniatioon.meta
@@ -0,0 +1,8 @@
+fileFormatVersion: 2
+guid: 6d134a9b58103a6438abdb8e5c36165b
+folderAsset: yes
+DefaultImporter:
+ externalObjects: {}
+ userData:
+ assetBundleName:
+ assetBundleVariant:
diff --git a/Aniatioon/SpriteAnimator.cs b/Aniatioon/SpriteAnimator.cs
new file mode 100644
index 0000000..37f8ac5
--- /dev/null
+++ b/Aniatioon/SpriteAnimator.cs
@@ -0,0 +1,217 @@
+using System.Collections.Generic;
+using UnityEngine;
+
+///
+/// 表示一个动画序列,包括一系列精灵和每帧的显示时间。
+///
+[System.Serializable]
+public class AnimationSequence
+{
+ [Tooltip("动画序列中的精灵列表")]
+ public List sprites = new List();
+
+ [Tooltip("每一帧的时间,单位秒")]
+ public float frameTime = 0.1f; // 每帧的持续时间(秒)
+}
+
+///
+/// 控制精灵动画播放的类,使用 Dictionary 进行精灵缓存管理。
+///
+public class SpriteAnimator : MonoBehaviour
+{
+ ///
+ /// 定义动画的循环类型。
+ ///
+ public enum LoopType
+ {
+ Loop, // 无限循环
+ Once, // 播放一次
+ PingPong // 来回播放
+ }
+
+ [Header("动画序列列表")]
+ [Tooltip("所有动画序列")]
+ public List animationSequences = new List();
+
+ [Header("SpriteRenderer组件")]
+ [Tooltip("用于显示动画的SpriteRenderer")]
+ public SpriteRenderer spriteRenderer;
+
+ [Header("当前动画编号")]
+ [Tooltip("当前播放的动画编号")]
+ public int currentAnimationIndex = 0;
+
+ [Header("循环类型")]
+ [Tooltip("选择动画的循环类型")]
+ public LoopType loopType = LoopType.Loop;
+
+ // 内部变量
+ private Dictionary animationDictionary = new Dictionary();
+ private AnimationSequence currentAnimation;
+ private int currentFrameIndex = 0;
+ private float frameTimer = 0f;
+ private bool isPlaying = true;
+ private bool pingPongForward = true;
+
+ ///
+ /// 初始化动画缓存。
+ ///
+ private void Awake()
+ {
+ if (animationSequences == null || animationSequences.Count == 0)
+ {
+ Debug.LogError("动画序列列表为空。请在 Inspector 中添加动画序列。");
+ return;
+ }
+
+ for (int i = 0; i < animationSequences.Count; i++)
+ {
+ if (!animationDictionary.ContainsKey(i))
+ {
+ animationDictionary.Add(i, animationSequences[i]);
+ }
+ else
+ {
+ Debug.LogWarning($"发现重复的动画索引 {i}。跳过重复项。");
+ }
+ }
+
+ SetAnimation(currentAnimationIndex);
+ }
+
+ ///
+ /// 在开始时进行初始化。
+ ///
+ private void Start()
+ {
+ if (spriteRenderer == null)
+ {
+ Debug.LogError("SpriteRenderer 'spriteRenderer' 为空。请在 Inspector 中分配 SpriteRenderer。");
+ return;
+ }
+
+ if (currentAnimation != null && currentAnimation.sprites.Count > 0)
+ {
+ frameTimer = currentAnimation.frameTime;
+ spriteRenderer.sprite = currentAnimation.sprites[currentFrameIndex];
+ }
+ else
+ {
+ Debug.LogError("当前动画序列为空或不包含精灵。");
+ }
+ }
+
+ ///
+ /// 每帧更新动画播放。
+ ///
+ private void Update()
+ {
+ if (!isPlaying || currentAnimation == null || currentAnimation.sprites.Count == 0)
+ return;
+
+ frameTimer -= Time.deltaTime;
+
+ if (frameTimer <= 0f)
+ {
+ AdvanceFrame();
+ frameTimer = currentAnimation.frameTime;
+ }
+ }
+
+ ///
+ /// 设置当前播放的动画序列。
+ ///
+ /// 动画编号
+ public void SetAnimation(int index)
+ {
+ if (animationDictionary.TryGetValue(index, out AnimationSequence selectedAnimation))
+ {
+ currentAnimation = selectedAnimation;
+ currentFrameIndex = 0;
+ pingPongForward = true;
+ frameTimer = currentAnimation.frameTime;
+
+ if (currentAnimation.sprites.Count > 0)
+ {
+ spriteRenderer.sprite = currentAnimation.sprites[currentFrameIndex];
+ }
+ else
+ {
+ Debug.LogWarning($"选中的动画索引 {index} 没有包含任何精灵。");
+ }
+ }
+ else
+ {
+ Debug.LogError($"SetAnimation: 动画索引 {index} 在 animationDictionary 中未找到。");
+ }
+ }
+
+ ///
+ /// 控制动画的播放和暂停。
+ ///
+ /// true: 播放动画, false: 暂停动画
+ public void PlayPauseAnimation(bool play)
+ {
+ isPlaying = play;
+ if (isPlaying && currentAnimation != null && currentAnimation.sprites.Count > 0)
+ {
+ // 确保当前帧显示正确
+ spriteRenderer.sprite = currentAnimation.sprites[currentFrameIndex];
+ frameTimer = currentAnimation.frameTime;
+ }
+ }
+
+ ///
+ /// 前进到下一帧或根据循环类型处理帧索引。
+ ///
+ private void AdvanceFrame()
+ {
+ switch (loopType)
+ {
+ case LoopType.Loop:
+ currentFrameIndex = (currentFrameIndex + 1) % currentAnimation.sprites.Count;
+ break;
+
+ case LoopType.Once:
+ currentFrameIndex = Mathf.Min(currentFrameIndex + 1, currentAnimation.sprites.Count - 1);
+ if (currentFrameIndex == currentAnimation.sprites.Count - 1)
+ {
+ isPlaying = false; // 动画播放完毕后停止
+ }
+ break;
+
+ case LoopType.PingPong:
+ if (pingPongForward)
+ {
+ currentFrameIndex++;
+ if (currentFrameIndex >= currentAnimation.sprites.Count - 1)
+ {
+ currentFrameIndex = currentAnimation.sprites.Count - 1;
+ pingPongForward = false;
+ }
+ }
+ else
+ {
+ currentFrameIndex--;
+ if (currentFrameIndex <= 0)
+ {
+ currentFrameIndex = 0;
+ pingPongForward = true;
+ }
+ }
+ break;
+ }
+
+ spriteRenderer.sprite = currentAnimation.sprites[currentFrameIndex];
+ }
+
+ ///
+ /// 在销毁对象时清理缓存。
+ ///
+ private void OnDestroy()
+ {
+ animationDictionary.Clear();
+ animationSequences = null;
+ currentAnimation = null;
+ }
+}
diff --git a/Aniatioon/SpriteAnimator.cs.meta b/Aniatioon/SpriteAnimator.cs.meta
new file mode 100644
index 0000000..95519ff
--- /dev/null
+++ b/Aniatioon/SpriteAnimator.cs.meta
@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 0c9ace101ec275a4e9bdbca5aa9934d8
+MonoImporter:
+ externalObjects: {}
+ serializedVersion: 2
+ defaultReferences: []
+ executionOrder: 0
+ icon: {instanceID: 0}
+ userData:
+ assetBundleName:
+ assetBundleVariant:
diff --git a/Role/Attack.cs b/Role/Attack.cs
index 5d1b3a2..df1f556 100644
--- a/Role/Attack.cs
+++ b/Role/Attack.cs
@@ -62,11 +62,21 @@ public class Attack : MonoBehaviour
}
public void attack(Role targetRole)
{
+
+
if (bulletPrefab==null)
{
Debug.LogError("子弹预制体为空");
return;
}
+
+ /*if(bulletPrefab.GetComponent().myBulletType==BulletType.Spraying)
+ {
+
+ return;
+ }
+ */
+
Vector2 direction = (targetRole.transform.position - transform.position).normalized;
List bulltes= new List();
diff --git a/Role/Bullet.cs b/Role/Bullet.cs
index ba419b6..35b7801 100644
--- a/Role/Bullet.cs
+++ b/Role/Bullet.cs
@@ -9,7 +9,9 @@ using UnityEngine.UIElements;
public enum BulletType
{
Bullet,
- Lightning
+ Lightning,
+ Spraying //原地喷射
+
}
public enum BulletMoveType
@@ -20,7 +22,8 @@ public enum BulletMoveType
///
/// 方向移动
///
- PointToDirection
+ PointToDirection,
+ Scope
}
@@ -62,10 +65,10 @@ public class Bullet : MonoBehaviour
{
[Header("子弹发射角色对象")] public Role role;
[Header("子弹发射范围对象")] public Attack attackObj;
- [Header("子弹类型")]public BulletType myBulletType;
- [Header("攻击类型")]public BulletMoveType bulletMoveType;
- [Header("子弹数据")] public BulletData bulletData =new BulletData();
-
+ [Header("子弹类型")] public BulletType myBulletType;
+ [Header("攻击类型")] public BulletMoveType bulletMoveType;
+ [Header("子弹数据")] public BulletData bulletData = new BulletData();
+ [Header("销毁时间,默认是10f")] public float BulletDeadTimer=10f;
private float timer = 0;
private void Update()
{
@@ -74,16 +77,19 @@ public class Bullet : MonoBehaviour
//更具子弹的移动方式来移动
case BulletMoveType.PeerToPeer:
this.gameObject.transform.Translate(Vector3.up * Time.deltaTime * bulletData.BulletSpeed);
+ timer += Time.deltaTime;
+ if (timer > BulletDeadTimer)
+ {
+ Destroy(this.gameObject);
+ }
break;
case BulletMoveType.PointToDirection:
break;
+ case BulletMoveType.Scope:
+ break;
}
- timer += Time.deltaTime;
- if (timer>10f)
- {
- Destroy(this.gameObject);
- }
+
}
diff --git a/Role/Fun.cs b/Role/Fun.cs
index d682a2c..f7837f8 100644
--- a/Role/Fun.cs
+++ b/Role/Fun.cs
@@ -20,12 +20,6 @@ public enum DamageType
noAttributeDamage,
}
-//public class prefabObjects
-//{
-// public string targetAudience = "targetAudience";
-// public string myPalye = "myPalye";
-
-//}
public class BUff
{
@@ -39,25 +33,7 @@ public class BUff
public class Fun : Base
{
- //// 通过方法名调用对应的方法
- //public void CallMethodByName(string methodName)
- //{
- // // 获取当前类的Type
- // Type type = this.GetType();
-
- // // 获取方法信息
- // MethodInfo methodInfo = type.GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
-
- // if (methodInfo != null)
- // {
- // // 调用该方法
- // methodInfo.Invoke(this,null ); // null代表无参数
- // }
- // else
- // {
- // Debug.LogError("方法未找到: " + methodName);
- // }
- //}
+
///
/// 中毒 //目标 // 自己
///
@@ -69,6 +45,8 @@ public class Fun : Base
Debug.Log("触发中毒");
Debug.Log(myPalye.name);
}
+
+
///
/// 扣血 传入参数 目标,伤害值,攻击类型,使用者
///