勇者与羁绊 游戏开发日志(九)

勇者与羁绊 游戏开发日志(九)

  • 开发进度
    • 实现思路
    • 具体实现
      • Action类
      • ActionManager类
      • 卡牌编写范例

开发进度

实现三种基础的卡牌功能:
抽卡
对敌人造成伤害
获得护盾

实现思路

参照了杀戮尖塔的mod制作方法。
每一种基本功能,即是一个Action,比如说对敌人造成伤害,就是一种基本功能。
每一张卡牌的效果,是由一个或多个Action组合而成的。
Action有三种状态:
等待中,执行中,完成。
每个Action有三个接口:
OnBegining():当其开始执行时,进行一次调用。
OnGoing():当其处于执行中时,每帧调用。
OnFinish():当其执行完成时,进行一次调用。
每个Action其实现其自身的功能,就是在这三个接口函数中进行编写。

有一个ActionManager来负责管理执行卡牌的功能,其自身是一个单例类。
ActionManager持有一个Action的List,并且提供方法来给List添加成员。
ActionManager的Update方法会获取ActionList的第一个元素,并进行判断:
若其为等待中,则将其标记为执行中,并触发一次该Action的OnBegining()方法。
若其为执行中,则触发一次该Action的OnGoing()方法。
若其为完成,则触发一次该Action的OnFinish()方法,然后将该Action移除。

具体实现

Action类

public class Action
{
    public ActionState _state;

    public virtual void OnBegining() { }
    public virtual void OnGoing() { }
    public virtual void OnFinish() { }

    public void ChangeStatus(ActionState state)
    {
        _state = state;
    }

	//每一个Action,都必须在其方法主体中调用到Finish()
    public void Finish()
    {
        _state = ActionState.Finish;
    }
}

public enum ActionState
{
    Wait,
    Going,
    Finish,
}

ActionManager类

public class ActionManager : SingletonMonoBehaviour<ActionManager>
{
    public List<Action> ActionList = new List<Action>();
	
	//提供给List添加Action的方法
    public void AddActionOnBottom(Action action)
    {
        ActionList.Add(action);
        action._state = ActionState.Wait;
    }
	
	//提供移除Action的方法
    public Action RemoveActionOnTop()
    {
        if (ActionList.Count == 0)
        {
            Debug.Log("当前没有可移除的action");
            return null;
        }
        Action result = ActionList[0];
        ActionList.Remove(result);
        return result;
    }
	
	//每帧执行
    private void Update()
    {
        if (ActionList.Count == 0) return;
        Action _action = ActionList[0];
        switch (_action._state)
        {
            case ActionState.Wait:
                _action.ChangeStatus(ActionState.Going);
                _action.OnBegining();
                break;
            case ActionState.Going:
                _action.OnGoing();
                break;
            case ActionState.Finish:
                _action.OnFinish();
                ActionList.Remove(_action);
                break;
        }
    }
}

卡牌编写范例

首先是三种基础的Action
攻击Action:

public class AttackAction: Action
{
    public GameObject target;
    public GameObject from;
    public GameObject card;
    public int count;

    public AttackAction(GameObject target)
    {
        this.target = target;
    }

    public AttackAction(GameObject from, GameObject target,int count)
    {
        this.from = from;
        this.target = target;
        this.count = count;
    }

    public override void OnBegining()
    {
        target.GetComponent<BattleUnitContainer>().battleUnit.ReceiveDamageInfo(from,count);
        Finish();
    }
}

抽卡Action

public class DrawCardAction:Action
{
    public int DrawCount;

    public DrawCardAction(int count)
    {
        this.DrawCount = count;
    }

    public override void OnBegining()
    {
        CardManager._instance.DrawCard(DrawCount);
        Finish();
    }
}

护盾Action

public class GetBarrierAction:Action
{
    public int count;
    public GameObject target;
    public GetBarrierAction(GameObject target, int count)
    {
        this.count = count;
        this.target = target;
    }

    public override void OnBegining()
    {
        target.GetComponent<BattleUnitContainer>().battleUnit.GetBarrier(count);
        Finish();
    }
}

攻击卡牌

public class CardScript_10000 : CardScript
{
    public CardScript_10000()
    {
        ID = 10000;
        name = "普通攻击";
        desc = "进行一次普通攻击";
        cost = 1;
        type = CardType.Attack;
        needTarget = true;
        canUse = true;
    }

    public override void OnUse()
    {
        GameObject target = this.owner.gameObject.GetComponent<PlayMakerFSM>().Fsm.GetFsmGameObject("Target").Value;
        ActionManager._instance.AddActionOnBottom(new AttackAction(BattleManager._instance.playerHeroUnit,target,100));
    }
}

抽牌卡牌

public class CardScript_30000 : CardScript
{
    public CardScript_30000()
    {
        ID = 30000;
        name = "智慧祝福";
        desc = "抽一张牌";
        cost = 1;
        type = CardType.Magic;
        needTarget = false;
        canUse = true;
    }

    public override void OnUse()
    {
        ActionManager._instance.AddActionOnBottom(new DrawCardAction(1));
    }
}

护盾卡牌

public class CardScript_20000 : CardScript
{
    public CardScript_20000()
    {
        ID = 20000;
        name = "护盾";
        desc = "为自己添加护盾";
        cost = 1;
        type = CardType.Defend;
        needTarget = false;
        canUse = true;
    }

    public override void OnUse()
    {
        GameObject target = BattleManager._instance.playerHeroUnit;
        ActionManager._instance.AddActionOnBottom(new GetBarrierAction(target,10));
    }
}

你可能感兴趣的:(独立游戏,Unity,游戏策划)