1
This commit is contained in:
155
unity/Assets/Script/GameObject/Player/CharacterCombat.cs
Normal file
155
unity/Assets/Script/GameObject/Player/CharacterCombat.cs
Normal file
@@ -0,0 +1,155 @@
|
||||
using Manager;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class CharacterCombat : CombaSystem
|
||||
{
|
||||
|
||||
[Header("音效")]
|
||||
public AudioSource audioSource;
|
||||
public PlayerControler controler;
|
||||
|
||||
[Header("特效")]
|
||||
public GameObject effect;
|
||||
|
||||
CameraVR cameraVR;
|
||||
|
||||
protected override void Awake()
|
||||
{
|
||||
maxHp = PlayerInfo.Instance.info.maxHp;
|
||||
currentHp = PlayerInfo.Instance.info.currentHp;
|
||||
attack = PlayerInfo.Instance.info.testAttack;
|
||||
|
||||
audioSource = transform.root.GetComponentInChildren<AudioSource>();
|
||||
|
||||
_animator = GetComponent<Animator>();
|
||||
|
||||
PlayerInfo.Instance.OnUpdateAttrbute += OnChangeAttrbute;
|
||||
|
||||
}
|
||||
private void OnDisable()
|
||||
{
|
||||
PlayerInfo.Instance.OnUpdateAttrbute -= OnChangeAttrbute;
|
||||
}
|
||||
|
||||
public override void MelleeAttackAnimEvent()
|
||||
{
|
||||
base.MelleeAttackAnimEvent();
|
||||
|
||||
Collider2D[] hitColliders = Physics2D.OverlapBoxAll(AttackAreaPos,attackSize,0,layerMasks[0]);
|
||||
Collider2D[] destructiveColliders = Physics2D.OverlapBoxAll(AttackAreaPos, attackSize, 0,layerMasks[1]);
|
||||
Collider2D[] treeColliders = Physics2D.OverlapBoxAll(AttackAreaPos, attackSize, 0, layerMasks[2]);
|
||||
|
||||
|
||||
foreach (Collider2D hitInfo in hitColliders)
|
||||
{
|
||||
|
||||
if(hitInfo.tag=="Enemy")
|
||||
{//小怪
|
||||
}
|
||||
if (hitInfo.tag == "Boss")
|
||||
{
|
||||
|
||||
hitInfo.GetComponentInChildren<BossAI>().TakeDamage("hurt-front",GetAttack());
|
||||
if (_summerClip != null)
|
||||
SoundManager.Instance.PlayEffect(_audio, _summerClip);
|
||||
//加入卡肉,特效
|
||||
if(effect!=null)
|
||||
{
|
||||
GameObject go = Instantiate(effect);
|
||||
|
||||
go.transform.position = hitInfo.transform.position + new Vector3(0,4,0);
|
||||
|
||||
//开始计时
|
||||
string timerPath = "Perfab/Timer";
|
||||
GameObject timer = Resloader.Load<GameObject>(timerPath);//寻找预制体
|
||||
|
||||
GameObject time = Instantiate(timer);
|
||||
time.GetComponent<Timer>().CreateTime(0.2f, () =>
|
||||
{
|
||||
ManagersMode.Poll.UnSpwan("EffectPoll", effect.name, go);
|
||||
}, false);
|
||||
|
||||
}
|
||||
|
||||
cameraVR = UnityExpandFunction.GetCamera();
|
||||
if (cameraVR != null)
|
||||
{
|
||||
cameraVR.ShakeCamera(0.5f, 2);
|
||||
}
|
||||
}
|
||||
}
|
||||
foreach (Collider2D hitInfo in destructiveColliders)
|
||||
{
|
||||
|
||||
}
|
||||
foreach (Collider2D hitInfo in treeColliders)//数目
|
||||
{
|
||||
Debug.Log("攻击树木");
|
||||
hitInfo.GetComponentInChildren<Tree>().TakeDamage();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//受伤
|
||||
public override void TakeDamage(float atk)
|
||||
{
|
||||
currentHp -= atk;
|
||||
|
||||
OnHit?.Invoke(currentHp);//UI显示
|
||||
|
||||
if(currentHp<=0)
|
||||
{
|
||||
//死亡
|
||||
return;
|
||||
}
|
||||
_animator.Play("hurt");
|
||||
}
|
||||
private void OnChangeAttrbute(LotteryType type, float value)
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case LotteryType.Hp:
|
||||
currentHp += value;
|
||||
if(currentHp>=maxHp)
|
||||
{
|
||||
currentHp = maxHp;
|
||||
}
|
||||
OnHit?.Invoke(currentHp);
|
||||
break;
|
||||
case LotteryType.Mp:
|
||||
maxHp += value;
|
||||
OnHit?.Invoke(currentHp);
|
||||
break;
|
||||
case LotteryType.Attack:
|
||||
attack += value;
|
||||
OnHit?.Invoke(currentHp);
|
||||
break;
|
||||
case LotteryType.UpMaxHp:
|
||||
PlayerInfo.Instance.info.maxHp += value;
|
||||
maxHp += value;
|
||||
OnHit?.Invoke(currentHp);
|
||||
break;
|
||||
case LotteryType.UpHp:
|
||||
Date.Instance.ItemDate[1].value += (int)value;
|
||||
OnHit?.Invoke(currentHp);
|
||||
break;
|
||||
case LotteryType.UpAttack:
|
||||
PlayerInfo.Instance.info.attack += value;
|
||||
attack += value;
|
||||
OnHit?.Invoke(currentHp);
|
||||
break;
|
||||
default:break;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 攻击力量
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public float GetAttack()
|
||||
{
|
||||
return attack + (PlayerInfo.Instance.info.Level * 10);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0c801442456af8848aeddf812bf4ca75
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
210
unity/Assets/Script/GameObject/Player/PlayerControler.cs
Normal file
210
unity/Assets/Script/GameObject/Player/PlayerControler.cs
Normal file
@@ -0,0 +1,210 @@
|
||||
using Manager;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
public class PlayerControler : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private InputActions inputActions;
|
||||
[SerializeField] public Animator animator;
|
||||
[SerializeField] private Rigidbody2D rb;
|
||||
[SerializeField] private BoxCollider2D bc;
|
||||
|
||||
private CameraVR cameraVR;
|
||||
|
||||
[Header("正常速度")]
|
||||
[SerializeField] private float normalSpeed;
|
||||
Vector2 InputDir;//移动方向
|
||||
|
||||
|
||||
public float dodgeCoolDown;//闪避Cd时间
|
||||
public float dodgeSpeed;
|
||||
private float dodge;
|
||||
private bool isCanDodge;
|
||||
|
||||
|
||||
|
||||
private bool isWuDi;
|
||||
public float wuDiTimer;
|
||||
|
||||
public CharacterCombat ch;
|
||||
public CharacterHp info;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
inputActions = new InputActions();
|
||||
cameraVR = GameObject.FindWithTag("Camera").GetComponentInChildren<CameraVR>();
|
||||
}
|
||||
private void Start()
|
||||
{
|
||||
|
||||
inputActions.GamePlay.ComBa_01.started += this.Attack_01;
|
||||
inputActions.GamePlay.ComBa_02.started += this.Attack_02;
|
||||
inputActions.GamePlay.Dodge.started += this.Dodge;
|
||||
inputActions.UI.Stop.started += this.OnStop;
|
||||
inputActions.UI.Bag.started += this.OpenBag;
|
||||
|
||||
dodge = dodgeCoolDown;
|
||||
isCanDodge = true;
|
||||
|
||||
ManagersMode.Poll.CreateGameObjectPoll("Timer", 10);
|
||||
ManagersMode.Poll.CreateGameObjectPoll("BulletPoll", 3);
|
||||
ManagersMode.Poll.CreateGameObjectPoll("EffectPoll", 2);
|
||||
|
||||
info.Init(ch);
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
|
||||
if(ch.currentHp<=0)
|
||||
{
|
||||
UIManager.Instance.Show<UIGameOver>();
|
||||
ch._animator.Play("die");
|
||||
}
|
||||
if(!animator.CheckAnimationTag("Roll"))
|
||||
{
|
||||
animator.speed = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
this.transform.Translate(InputDir*dodgeSpeed*0.01f);//闪避移动
|
||||
}
|
||||
CanDodge();
|
||||
|
||||
|
||||
UseItem();
|
||||
}
|
||||
private void OnEnable()
|
||||
{
|
||||
inputActions.Enable();
|
||||
}
|
||||
private void OnDisable()
|
||||
{
|
||||
inputActions.Disable();
|
||||
}
|
||||
private void FixedUpdate()
|
||||
{
|
||||
if(!animator.CheckAnimationTag("Die"))
|
||||
{
|
||||
this.MoveController();
|
||||
}
|
||||
else
|
||||
{
|
||||
this.rb.velocity =Vector2.zero;
|
||||
}
|
||||
}
|
||||
|
||||
#region 移动 新版输入系统
|
||||
private void MoveController()
|
||||
{
|
||||
InputDir = inputActions.GamePlay.Move.ReadValue<Vector2>().normalized;
|
||||
|
||||
animator.SetFloat("Horizontal", InputDir.x);
|
||||
animator.SetFloat("Vertical", InputDir.y);
|
||||
animator.SetFloat("Speed", InputDir.sqrMagnitude);
|
||||
|
||||
this.rb.velocity = InputDir * normalSpeed;
|
||||
|
||||
if (InputDir.x < 0)//向左
|
||||
{
|
||||
transform.localScale = new Vector3(1, 1, 1);
|
||||
}
|
||||
if (InputDir.x > 0)//向左
|
||||
{
|
||||
transform.localScale = new Vector3(-1, 1, 1);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnStop(InputAction.CallbackContext obj)
|
||||
{
|
||||
UIManager.Instance.Show<UIGameOver>();
|
||||
}
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// 角色攻击
|
||||
/// </summary>
|
||||
private void Attack_01(InputAction.CallbackContext obj)
|
||||
{
|
||||
animator.SetTrigger("Attack_01");
|
||||
}
|
||||
private void Attack_02(InputAction.CallbackContext obj)
|
||||
{
|
||||
animator.SetTrigger("Attack_02");
|
||||
}
|
||||
|
||||
private bool CanDodge()
|
||||
{
|
||||
if(isCanDodge)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
dodgeCoolDown -= Time.deltaTime;
|
||||
if(dodgeCoolDown<=0)
|
||||
{
|
||||
isCanDodge = true;
|
||||
dodgeCoolDown = dodge;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
/// <summary>
|
||||
/// 角色闪避
|
||||
/// </summary>
|
||||
private void Dodge(InputAction.CallbackContext obj)
|
||||
{
|
||||
//||animator.CheckAnimationTag("Hit")
|
||||
if (!CanDodge())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
StartCoroutine(nameof(SetWuDi));//无敌状态
|
||||
|
||||
isCanDodge = false;
|
||||
animator.speed = 1f;
|
||||
|
||||
if (InputDir.x!=0)
|
||||
{
|
||||
animator.Play("roll");
|
||||
}
|
||||
if (InputDir.y > 0)
|
||||
{
|
||||
animator.Play("roll-up");
|
||||
}
|
||||
if (InputDir.y < 0)
|
||||
{
|
||||
animator.Play("roll-down");
|
||||
}
|
||||
}
|
||||
|
||||
IEnumerator SetWuDi()
|
||||
{
|
||||
isWuDi = true;
|
||||
yield return new WaitForSeconds(wuDiTimer);
|
||||
isWuDi = false;
|
||||
}
|
||||
|
||||
public bool GetIsDodge()
|
||||
{
|
||||
return isWuDi;
|
||||
}
|
||||
|
||||
|
||||
|
||||
private void OpenBag(InputAction.CallbackContext obj)
|
||||
{
|
||||
UIManager.Instance.Show<UIBagInfo>();
|
||||
}
|
||||
public void UseItem()
|
||||
{
|
||||
if(Input.GetKeyDown(KeyCode.Alpha1))
|
||||
{
|
||||
PlayerInfo.Instance.BagMgr.UserItem(1, 1);//回血道具
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 09455605d05154d4d9194d9b1781ba9b
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
154
unity/Assets/Script/GameObject/Player/PlayerInfo.cs
Normal file
154
unity/Assets/Script/GameObject/Player/PlayerInfo.cs
Normal file
@@ -0,0 +1,154 @@
|
||||
using Managers;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
|
||||
public class PlayerInfo : Singleton<PlayerInfo>
|
||||
{
|
||||
public CharInfo info;
|
||||
|
||||
public Action OnUpdateBagInfo;
|
||||
public Action OnUpdateMainUI;
|
||||
public Action OnUpdateLevel;
|
||||
|
||||
public Action<LotteryType, float> OnUpdateAttrbute;
|
||||
|
||||
public Managers.BagManager BagMgr;
|
||||
|
||||
public void Init(CharInfo date)
|
||||
{
|
||||
|
||||
info = date;
|
||||
info.playerName = date.playerName;
|
||||
info.Level = date.Level;
|
||||
info.exp = date.exp;
|
||||
|
||||
info.maxHp = date.maxHp;
|
||||
info.attack = date.attack;
|
||||
|
||||
info.testAttack = date.testAttack;
|
||||
info.currentHp = date.currentHp;
|
||||
info.currentMap = date.currentMap;
|
||||
|
||||
info.BagInfo = new Dictionary<int, int>();
|
||||
info.UpInfo = new Dictionary<int, bool>();
|
||||
|
||||
for (int i = 0; i < date.BagKey.Count; i++)
|
||||
{
|
||||
info.BagInfo.Add(date.BagKey[i], date.BagValue[i]);
|
||||
}
|
||||
for (int i = 0; i < date.UpKey.Count; i++)
|
||||
{
|
||||
info.UpInfo.Add(date.UpKey[i], date.UpValue[i]);
|
||||
}
|
||||
|
||||
//初始化管理器
|
||||
BagMgr = new Managers.BagManager(this);
|
||||
|
||||
Debug.Log("数据读取完毕");
|
||||
}
|
||||
public void Resrt()
|
||||
{
|
||||
info.currentHp = info.maxHp; ;
|
||||
info.testAttack = info.attack;
|
||||
if(!info.BagInfo.ContainsKey(1))
|
||||
{
|
||||
info.BagInfo.Add(1,4);
|
||||
}
|
||||
else
|
||||
{
|
||||
info.BagInfo[1] = 4;
|
||||
}
|
||||
|
||||
WriteBagInfo();
|
||||
}
|
||||
|
||||
public void KeepData(float max, float current, float attack)
|
||||
{
|
||||
info.maxHp = max;
|
||||
info.currentHp = current;
|
||||
info.testAttack = attack;
|
||||
}
|
||||
public Sprite GetSprite(string path)
|
||||
{
|
||||
return Resloader.Load<Sprite>(path);
|
||||
}
|
||||
|
||||
#region 属性更改
|
||||
public void AddHp(int num)
|
||||
{
|
||||
OnUpdateAttrbute?.Invoke(LotteryType.Hp, num);
|
||||
}
|
||||
public void AddMp(int num)
|
||||
{
|
||||
OnUpdateAttrbute?.Invoke(LotteryType.Mp, num);
|
||||
}
|
||||
public void AddAttack(int num)
|
||||
{
|
||||
OnUpdateAttrbute?.Invoke(LotteryType.Attack, num);
|
||||
}
|
||||
public void AddUpHp(int num)
|
||||
{
|
||||
OnUpdateAttrbute?.Invoke(LotteryType.UpHp, num);
|
||||
}
|
||||
public void AddUpMaxHp(int num)
|
||||
{
|
||||
OnUpdateAttrbute?.Invoke(LotteryType.UpMaxHp, num);
|
||||
}
|
||||
public void AddUpAttack(int num)
|
||||
{
|
||||
OnUpdateAttrbute?.Invoke(LotteryType.UpAttack, num);
|
||||
}
|
||||
#endregion
|
||||
#region 升级
|
||||
public void LevelUp()
|
||||
{
|
||||
int need = NeedExp();
|
||||
if (need < info.exp)
|
||||
{
|
||||
info.exp -= need;
|
||||
|
||||
info.Level++;
|
||||
LevelUp();
|
||||
|
||||
SaveSystem.SaveDate(info.DatePath, info);
|
||||
}
|
||||
}
|
||||
public int NeedExp()
|
||||
{
|
||||
int needExp;
|
||||
return needExp = info.Level * (1000);
|
||||
}
|
||||
public void ItemLevel(LevelUpDefine item)
|
||||
{
|
||||
switch (item.type)
|
||||
{
|
||||
case LotteryType.UpMaxHp:
|
||||
AddUpMaxHp(item.value);
|
||||
break;
|
||||
case LotteryType.UpAttack:
|
||||
AddUpAttack(item.value);
|
||||
break;
|
||||
case LotteryType.UpHp:
|
||||
AddUpHp(item.value);
|
||||
break;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
//重写数据
|
||||
public void WriteBagInfo()
|
||||
{
|
||||
info.BagKey.Clear();
|
||||
info.BagValue.Clear();
|
||||
foreach (var data in info.BagInfo)
|
||||
{
|
||||
info.BagKey.Add(data.Key);
|
||||
info.BagValue.Add(data.Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
11
unity/Assets/Script/GameObject/Player/PlayerInfo.cs.meta
Normal file
11
unity/Assets/Script/GameObject/Player/PlayerInfo.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d0c918ecff055954093fcb740f901146
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user