【学习unity】【0】简易6V6回合战斗模拟逻辑

本文讲述了零基础学习者如何通过C#和Unity语言,设计简易战斗逻辑,包括角色类的创建、战斗过程中的攻击判定和血量变化,以及使用Debug日志进行调试。
摘要由CSDN通过智能技术生成

大龄失业策划 0基础初学者 从头开始通过做游戏学习c# 和unity 争取早日制作出自己的游戏

简易战斗逻辑: 按照我方1~6 敌方1~6 出手排列顺序出手 选择敌人 进行攻击 扣血 死亡判断 胜利判断

该逻辑无界面交互 在 log中表现输出。

hero1 类 定义角色

学习类的基础功能  类的定义  继承  构造函数

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// ID 角色ID;
/// </summary>
public class Hero1
{
    public string name;
    public int id;//角色ID
    public int hp;//角色血量
    public int atn;//物理攻击力
    public int def;//防御力
    public enum STATE 
    {
     health,dead, stun
    }
    public STATE state;

    public Hero1(string NAME,int ID,int HP,int ATN,int DEF) {
        name = NAME;
        id=ID;
        hp=HP;
        atn=ATN;
        def= DEF;
        STATE state = STATE.health;
    }
}
public class CombatScenesHero1 :Hero1
{
    public int position;
    public int group;
    public CombatScenesHero1(string NAME,int ID, int HP, int ATN, int DEF,int POSITION,int GROUP):base(NAME,ID,HP,ATN,DEF)
    {
        position = POSITION;
        group = GROUP;
    }
}

学习 类的实例方法及运用

学习 for循环  foreach 循环  goto等语句  

学习void 方法  和带返回值的方法

通过 debug.log   Debug.LogError 等帮助调试逻辑

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.TextCore.Text;

public class Combat_1
{
    private CombatScenesHero1[] team1;
    private CombatScenesHero1[] team2;
    private bool battleEND = false;
    public void loadingCombat()
    {
        team1 = new CombatScenesHero1[6];
        team2 = new CombatScenesHero1[6];

        team1[0] = new CombatScenesHero1("我方1", 1001, 200, 50, 12, 0, 1);
        team1[1] = new CombatScenesHero1("我方2", 1002, 220, 40, 11, 1, 1);
        team1[2] = new CombatScenesHero1("我方3", 1003, 490, 30, 14, 2, 1);
        team1[3] = new CombatScenesHero1("我方4", 1004, 200, 50, 11, 3, 1);
        team1[4] = new CombatScenesHero1("我方5", 1005, 220, 70, 15, 4, 1);
        team1[5] = new CombatScenesHero1("我方6", 1006, 120, 90, 12, 5, 1);

        team2[0] = new CombatScenesHero1("敌方1", 1007, 200, 50, 12, 0, 1);
        team2[1] = new CombatScenesHero1("敌方2", 1008, 400, 50, 11, 1, 1);
        team2[2] = new CombatScenesHero1("敌方3", 1009, 500, 50, 12, 2, 1);
        team2[3] = new CombatScenesHero1("敌方4", 1010, 700, 25, 15, 3, 1);
        team2[4] = new CombatScenesHero1("敌方5", 1011, 300, 50, 20, 4, 1);
        team2[5] = new CombatScenesHero1("敌方6", 1012, 500, 44, 12, 5, 1);

    }

    public void combatAction()
    {
        for (int a = 1; a < 21; a++)
        {
            for (int b = 0; b < 6; b++)  //我方攻击序列!!!!!!!
            {
                if(team1[b].state==Hero1.STATE.health)// 判断只有活人才能攻击
                { 
                CombatScenesHero1 attackedH = chooseAttackedHero(team2); //获取攻击对象
                attackAction(team1[b], attackedH);
                }
                if (battleEND == true) 
                {
                    goto LoopEnd;
                }
            }
            for (int b = 0; b < 6; b++)//敌方攻击序列!!!!!!!
            {
                if (team2[b].state == Hero1.STATE.health)// 判断只有活人才能攻击
                {
                    CombatScenesHero1 attackedH = chooseAttackedHero(team1); //获取攻击对象
                    attackAction(team2[b], attackedH);
                    if (battleEND == true)
                    {
                        goto LoopEnd;
                    }
                }
            }
            Debug.Log("第"+a+"回合结束");
        }
        Debug.Log("全部回合结束,无人胜利");

    LoopEnd:
        Debug.LogWarning("战斗结束 执行goto语句");
    }


    /// <summary>
    /// 选择被攻击对象
    /// </summary>
    /// <param name="ATTACKEDHERO">传入被攻击的组</param>
    /// <returns>返回组内首个血量不为0的角色 </returns>
    CombatScenesHero1 chooseAttackedHero(CombatScenesHero1[] ATTACKEDHERO)
    {
        CombatScenesHero1[] attackedHero = ATTACKEDHERO;
        for (int b = 0; b < 6; b++)
        {
            if (attackedHero[b].hp > 0)
            {
                return (attackedHero[b]);
            }
        }
        Debug.LogError("判断胜利出错 无法选择全部阵亡敌人!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
        
        return (attackedHero[0]);

    }
    /// <summary>
    /// 执行攻击 扣血 
    /// </summary>
    /// <param name="Ahero"> 攻击者 </param>
    /// <param name="Dhero"> 被攻击者 </param>
    void attackAction(Hero1 Ahero, Hero1 Dhero)
    {
        int damageValue = Ahero.atn - Dhero.def;
        Dhero.hp = Dhero.hp - damageValue;
        Debug.Log(Ahero.name + "对" + Dhero.name + "造成" + damageValue + "点伤害!" + "  " + Dhero.name + "还剩余血量" + Dhero.hp);
        if (Dhero.hp <= 0)//发生死亡后执行玩家状态操作
        {
            Dhero.state = Hero1.STATE.dead;
            Debug.Log(Dhero.name + "死了");
            winDetermine();
        }
    }/// <summary>
    /// 胜利判断
    /// </summary>
    async void winDetermine()
    {
        Debug.LogWarning("winDetermine()执行");
        bool aWin= AreAllCharactersDead(team1);
        Debug.LogWarning("aWIN 当前状态"+aWin);
        if ( aWin)
        {
            battleEND = true;
            Debug.LogWarning("我方全部死亡,战斗失败");
            Debug.LogWarning("已重置battleEND属性值" + battleEND);
        }
        bool bWin = AreAllCharactersDead(team2);
        if (bWin)
        {
            battleEND = true;
            Debug.LogWarning("敌方全部死亡,战斗胜利");
            Debug.LogWarning("已重置battleEND属性值" + battleEND);
        }
    }
    /// <summary>
    /// 全部死亡判断
    /// </summary>
    /// <param name="team">输入判断队伍</param>
    /// <returns>TRUE队伍全部死亡;FALSE 队伍有存活</returns>
   bool AreAllCharactersDead(CombatScenesHero1[] team)
    {
        foreach (var character in team)
        {
            if (character.state == Hero1.STATE.health)
            {
                Debug.LogError("全灭置false");
                return false; // 如果找到一个生命值大于0的角色,说明团队没有全部死亡  
 
            }
        }
        Debug.LogError("全灭置ture");
        return true; // 如果所有角色的生命值都不大于0,说明团队全部死亡  
 

    }
}

通过run脚本挂在 object  上运行 通过 log 观察运行结果 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class run : MonoBehaviour
{
   public Combat_1 battle = new Combat_1();
    // Start is called before the first frame update
    void Start()
    {
        battle.loadingCombat();
        battle.combatAction();
        Debug.Log("战斗结束");
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值