public object Clone() { return this as object; } }
上面的战斗状态类已经可以被克隆了,接着还有一个读取状态的方法:
1 2 3 4 5 6 7 8 9
public FormatterBattleState GetState(string id) { var states = TableLoader.GetInstance().GetStateTable(); var state = states.ContainsKey(id) ? states[id] : null;
if (state == null) return null;
return state.Clone() as FormatterBattleState; }
然后完成技能附加状态的效果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
protected override void OnSkillHit() { // 这里是一些业务逻辑,不用管 int maxCount = 10; int currentCount = battleSkillData.user.info.GetStateCount("jianyi"); int spentCount = Mathf.Min(currentCount, maxCount);
public FormatterBattleState GetState(string id) { var states = TableLoader.GetInstance().GetStateTable(); var state = states.ContainsKey(id) ? states[id] : null;
if (state == null) return null;
// 注释掉,看看是不是因为克隆的问题 //return state.Clone() as FormatterBattleState;
// 创建一个新的对象,然后手动赋值 var temp = new FormatterBattleState { id = state.id, icon = state.icon, attributes = new Dictionary<string, decimal>(state.attributes), };
using System; using UnityEngine; using System.Collections; using System.Collections.Generic; using System.IO; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary;
// 序列化注解 [Serializable] public class FormatterBattleState : FormatterData, ICloneable { public string icon; public string name;
// 持续回合数,-1代表无限回合 public int remain_time;
// 状态的最大层数,达到该值后则无法继续叠加,只会刷新回合 public int max_count = 1;