unity 战斗系统框架设计数值系统

战斗系统框架设计(一)
1.战斗的基础数值系统
战斗里面是由很多数值拼出的数值组组成,整个战斗运行就是改变这些数值。

a .数值类型:

public enum SVType
{
    None = 0,
    Hp = 1,//血量
}

b.数值的值:

public float curValue;

c.数值的时间轴性:

public float curTime;

d.数值的变化性:

public float deltaValue;

e.数值的范围性:

private float min;//最小数值
private float max;//最大数值
private bool rangeEnable = false;//启动范围限制

f.数值变化的活性(数值发生变化的时候,应该有主动抛出自己变化数值和发生变化时间事件的能力):

private OneSender _sender;

附加事件系统代码:

public class OneSender
{
    private  EventSender<Enum, object> sender = new EventSender<Enum, object>();

    public  void AddListener(Enum eventType, Action<object> eventHandler)
    {
        sender.AddListener(eventType, eventHandler);
    }
    public  void RemoveListener(Enum eventType, Action<object> eventHandler)
    {
        sender.RemoveListener(eventType, eventHandler);
    }
    public  void SendMessage(Enum eventType, object pObj)
    {
        sender.SendMessage(eventType, pObj);
    }
}
using System;
using System.Collections.Generic;
public class EventSender<TKey,TValue> 
{
	private Dictionary<TKey, Action<TValue>> dic = new Dictionary<TKey, Action<TValue>>();
	public void AddListener(TKey pKey,Action<TValue> pAction)
	{
		Action<TValue> _cbs;
		if (dic.TryGetValue(pKey, out _cbs))
		{
			dic[pKey] = _cbs + pAction;
		}
		else
		{
			dic.Add(pKey, pAction);
		}
	}
	public void RemoveListener(TKey pKey, Action<TValue> pAction)
	{
		Action<TValue> _cbs;
		if (dic.TryGetValue(pKey, out _cbs))
		{
			_cbs = (Action<TValue>)Delegate.Remove(_cbs, pAction);
			if (_cbs == null)
			{
				dic.Remove(pKey);
			}
			else
			{
				dic[pKey] = _cbs;
			}
		}
	}
	public bool HasListener(TKey pKey)
	{
		return dic.ContainsKey(pKey);
	}
	public void SendMessage(TKey pKey,TValue pValue)
	{
		Action<TValue> _cbs;
		if (dic.TryGetValue(pKey, out _cbs))
		{
			_cbs.Invoke(pValue);
		}
	}
	public void Clear()
	{
		dic.Clear();
	}	
}

2.战斗系统的数值组:
战斗里面的每一个实例都是有很多数值的组合,不会单个存在,所有数值组才是战斗里面的数值灵魂。
a.基础数值组:

using System.Collections.Generic;

public class SVGroup
{
	private Dictionary<SVType, SVBase> values = new Dictionary<SVType, SVBase>();
	public int BodyId;//每个实例的个体编号
	public SVGroup() { }
	public SVGroup(int pBodyId) { BodyId = pBodyId; }
	public void Load(){
		OnLoad();
	}
	//添加一个数值
	protected void AddSVBase(SVBase pSVBase){
		if (!values.ContainsKey(pSVBase.VType))
		{
			values.Add(pSVBase.VType, pSVBase);
		}
	}
	//该数值组组装的时候的扩展接口
	protected virtual void OnLoad(){ }
	//该数值组卸载的时候的扩展接口
	protected virtual void OnUnLoad(){ }
	//获取某一个类型的具体数值
	public SVBase GetValue(SVType pSVType){
		SVBase temp;
		values.TryGetValue(pSVType,out temp);
		return temp;
	}
}

b.数值组扩展(扩展成普通数值组):

using UnityEngine;

public class VNormalGroup : SVGroup
{
    private Vector3 bornPosition  = Vector3.zero;
    private CampType campType = CampType.Player0;
    private int hp_ = 1;
    private float speed_= 2f;
    private int model_ = 1;
    private int atk_ = 1;

    public VNormalGroup(int pBodyId, CampType pCampType,int Hp,int Atk,float pSpeed =2,int pModel = 1)
    {
        BodyId = pBodyId;
        campType = pCampType;
        hp_ = Hp;
        speed_ = pSpeed;
        model_ = pModel;
        atk_ = Atk;
    }

    public void SetBornPosition(Vector3 pPos)
    {
        bornPosition = pPos;
    }

    protected override void OnLoad()
    {
        var model = new SVBase(BodyId, SVType.Model, model_);
        AddSVBase(model);
        var hp = new SVBase(BodyId, SVType.Hp, hp_);
        AddSVBase(hp);
        var atk = new SVBase(BodyId, SVType.Atk, atk_);
        AddSVBase(atk);
        var speed = new SVBase(BodyId, SVType.Speed, speed_);
        AddSVBase(speed);
        var posX = new SVBase(BodyId, SVType.PosX, bornPosition.x);
        AddSVBase(posX);
        var posY = new SVBase(BodyId, SVType.PosY, bornPosition.y);
        AddSVBase(posY);
        var camp = new SVBase(BodyId, SVType.Camp, (int)campType);
        AddSVBase(camp);
        var forcus = new SVBase(BodyId, SVType.ForcusId,0);
        AddSVBase(forcus);
    }
}

你可能感兴趣的:(游戏,程序人生,面试,经验分享,恰饭,其他)