(六) 战斗对象池 回收对象

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

[System.Serializable]
public class BattlePool 
{
    //内部属性
    //--------------------------------------------------------------------------
    /** 缓存池
     * 
     */
    [SerializeFieldDictionary<stringList<GameObject>> dic = new Dictionary<stringList<GameObject>>();


    //内部方法
    //--------------------------------------------------------------------------
    /*
      * 
      * 
      */




    //公开接口
    //--------------------------------------------------------------------------

    /** 从缓存池获取obj
     * 
     */
    public GameObject Get(string key)
    {
        if (dic.ContainsKey(key))
        {
            var list = dic[key];

            //Debug.Log("list length " + list.Count);

            if(list.Count0)
            {
                var obj = list[0];
                obj.SetActive(true);

                list.RemoveAt(0);

                //Debug.Log("key " + key);

                return obj;
            }

            return null;
        }

        return null;
    }

    /** 回收到缓存池
     * 
     */
    public void Put(string keyGameObject obj)
    {
        obj.SetActive(false);

        if(dic.ContainsKey(key))
        {
            var list = dic[key];
            list.Add(obj);
        } else {
            var list = new List<GameObject>();
            list.Add(obj);

            dic.Add(keylist);
        }
    }


}
 

 

 

 

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


/**
 * 游戏管理器
 * 职能
 * 游戏操作引导、关卡管理、创建战斗
 * 
 */
public class GameMgr : MonoBehaviour
{

    //内部属性
    //--------------------------------------------------------------------------

    /** 单例模式
     * 
     */
    public static GameMgr instanse;

    /** 战斗管理器
     * 
     */
    [SerializeFieldprivate BattleManager battleManager;

    /** 全局配置
     *  比如 关卡配置
     * 
     */
    [SerializeFieldprivate GlobleConfs globleConfs;

    /** 本地存储记录
     * 
     */
    [SerializeFieldprivate GlobleLocalData globleLocalData;

    /** 战场对象缓存池
     * 
     */
    [SerializeFieldprivate BattlePool pool;


    // 战斗中标记
    [SerializeFieldprivate bool battling = false;



    //内部方法
    //--------------------------------------------------------------------------

    private void Awake()
    {
        instanse = this;

        // 初始化配置等
        Init();

        //TODO:初始化战斗,后面改成开始按钮触发
        OnBattle();
    }

    private void Update()
    {
        OnUpdate(Time.deltaTime);
    }


    /** 初始化,包括全局配置
     * 
     */
    void Init()
    {
        // 关卡
        globleConfs = new GlobleConfs();
        globleConfs.Load();

        // 本地缓存数据
        globleLocalData = new GlobleLocalData();
        globleLocalData = globleLocalData.GetLocal();
    }

    /** 选择关卡,发起战斗
     * 
     */
    void OnBattle()
    {
        // 初始化缓存池
        pool = new BattlePool();

        // 初始化战斗配置等
        battleManager = FindObjectOfType<BattleManager>();
        battleManager.InitBattle(globleConfsglobleLocalDatapool);

        battling = true;
    }

    /** 选择关卡,发起战斗
   * 
   */
    void OnUpdate(float deltaTime)
    {
        if (battling == falsereturn;

        battleManager.OnUpdate(deltaTime);
    }



    //公开接口
    //--------------------------------------------------------------------------

    /** 暂时所有游戏对象、或者部分游戏对象
    * 
    */
    public void Pause()
    {
        // 暂时所有游戏对象
        Time.timeScale = 0;

        // 遍历数组控制部分对象停止运动

    }

    /** 游戏胜利
     * 
     */
    public void Success()
    {
        battling = false;

    }

    /* 是否战斗中
     * 
     */
    public bool Battling()
    {
        return battling;
    }

    public BattleManager BattleMgr()
    {
        return battleManager;
    }

    public BattlePool BattlePool()
    {
        return pool;
    }

}
 

 

 

 

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


/**
 * 运行时数据 
 * 职能: 管理(增、删、改、查操作) 角色、敌人、技能、金币 等游戏对象
 * 
 * 玩家   controller
 * 敌人   controller   (数组)
 * 技能   controller   (数组)
 * 障碍物  controller   (数组)
 * 子弹移动ai   (数组, 子弹会持有发射源的controller, 攻击目标的transform, 子弹有个碰撞检测脚本)
 *   
 * 
 * 
**/

[System.Serializable]
public class BattleDatas
{
    //内部属性
    //--------------------------------------------------------------------------
    GlobleLocalData globleLocalData;
    GlobleConfs globleConfs;
    BattleConfs battleConfs;
    BattlePool pool;

    // 敌人引用
    [SerializeFieldList<EnemyCtrenmeyCtrs;
    // 子弹引用
    [SerializeFieldList<BulletCtrbulletCtrs;
    // 子弹引用
    [SerializeFieldList<SkillCtrskillCtrs;
    // 玩家引用
    [SerializeFieldRoleCtr roleCtr;
    // 本波死亡敌人总数
    [SerializeFieldint deathNum;




    //内部方法
    //--------------------------------------------------------------------------

    /** 玩家 (不需要移动ai,仅加载攻击ai)
     * 
     */
    private T CreateRoleCommon<T>(RoleConfig confVector3 posQuaternion rotwhere T : PlayerCtrBase
    {
        T ctr
        var weaponConf = battleConfs.GetConfById<WeaponConfig>(conf.weaponAiIdbattleConfs.GetWeaponConfs());

        // 加载模型
        var obj = pool.Get(conf.id);
        if(obj == null
        {
            obj = UtilTool.CreateObjByName(conf.prefabposrot);

            // 添加武器ai

            var weaponAI = obj.AddComponent(GlobalFunction.GetTypeByClassName(weaponConf.aiName)) as ShootBaseAI;

            // 添加controller
            ctr = obj.AddComponent(typeof(T)) as T;

            ctr.SetWeaponAI(weaponAI);
            //ctr.IsRepeatMove = false; 玩家用不到移动ai,用不到这个参数
            ctr.IsEnemy = false;

        } else {
            ctr = obj.GetComponent(typeof(T)) as T;
            ctr.transform.position = pos;
            ctr.transform.rotation = rot;
        }

        ctr.SetConfig(confweaponConf);

        return ctr;
    }

    /** 敌人 (加载移动ai, 攻击ai)
     * 
     */
    private T CreateEnemyCommon<T>(RoleConfig confVector3 posQuaternion rotwhere T : PlayerCtrBase
    {
        //// 加载模型
        //var obj = UtilTool.CreateObjByName(conf.prefab, pos, rot);

        //// 添加移动ai
        //var moveConf = battleConfs.GetConfById<MoveAIConfig>(conf.moveAiId, battleConfs.GetMoveAIConfs());
        //var moveAI = obj.AddComponent(GlobalFunction.GetTypeByClassName(moveConf.aiName)) as BaseAI;

        //// 添加武器ai
        //var weaponConf = battleConfs.GetConfById<WeaponConfig>(conf.weaponAiId, battleConfs.GetWeaponConfs());
        //var weaponAI = obj.AddComponent(GlobalFunction.GetTypeByClassName(weaponConf.aiName)) as ShootBaseAI;

        //// 添加controller
        //var ctr = obj.AddComponent(typeof(T)) as T;

        //ctr.SetConfig(conf, weaponConf);
        //ctr.SetMoveAI(moveAI);
        //ctr.SetWeaponAI(weaponAI);

        //ctr.IsEnemy = true;

        //return ctr;



        T ctr;
        var moveConf = battleConfs.GetConfById<MoveAIConfig>(conf.moveAiIdbattleConfs.GetMoveAIConfs());
        var weaponConf = battleConfs.GetConfById<WeaponConfig>(conf.weaponAiIdbattleConfs.GetWeaponConfs());

        // 加载模型
        var obj = pool.Get(conf.id);
        if (obj == null)
        {
            obj = UtilTool.CreateObjByName(conf.prefabposrot);

            // 添加移动ai
            var moveAI = obj.AddComponent(GlobalFunction.GetTypeByClassName(moveConf.aiName)) as BaseAI;
            // 添加武器ai
            var weaponAI = obj.AddComponent(GlobalFunction.GetTypeByClassName(weaponConf.aiName)) as ShootBaseAI;

            // 添加controller
            ctr = obj.AddComponent(typeof(T)) as T;

            ctr.SetMoveAI(moveAI);
            ctr.SetWeaponAI(weaponAI);
            ctr.IsRepeatMove = true;
            ctr.IsEnemy = true;
        }
        else
        {
            ctr = obj.GetComponent(typeof(T)) as T;
            ctr.transform.position = pos;
            ctr.transform.rotation = rot;
        }

        ctr.SetConfig(confweaponConf);


        return ctr;
    }

    /** 创建子弹(加载移动ai)
     * 
     */
    private T CreateBulletCommon<T>(BulletConfig confVector3 posQuaternion rotbool isEnemywhere T : BulletCtrBase
    {
        //// 加载模型
        //var obj = UtilTool.CreateObjByName(conf.prefab, pos, rot);

        //// 添加移动ai
        //var moveConf = battleConfs.GetConfById<MoveAIConfig>(conf.moveAiId, battleConfs.GetMoveAIConfs());
        //var moveAI = obj.AddComponent(GlobalFunction.GetTypeByClassName(moveConf.aiName)) as BaseAI;

        //// 添加controller
        //var ctr = obj.AddComponent(typeof(T)) as T;

        //ctr.SetMoveAI(moveAI);
        //ctr.SetConfig(conf);

        //ctr.IsEnemy = isEnemy;

        //return ctr;



        T ctr;
      
        // 加载模型
        var obj = pool.Get(conf.id);
        if (obj == null)
        {
            obj = UtilTool.CreateObjByName(conf.prefabposrot);

            // 添加移动ai
            var moveConf = battleConfs.GetConfById<MoveAIConfig>(conf.moveAiIdbattleConfs.GetMoveAIConfs());
            var moveAI = obj.AddComponent(GlobalFunction.GetTypeByClassName(moveConf.aiName)) as BaseAI;
            // 添加controller
            ctr = obj.AddComponent(typeof(T)) as T;

            ctr.SetMoveAI(moveAI);
            ctr.IsRepeatMove = false;
        }
        else
        {
            ctr = obj.GetComponent(typeof(T)) as T;
            ctr.transform.position = pos;
            ctr.transform.rotation = rot;
        }

        ctr.SetConfig(conf);
        ctr.IsEnemy = isEnemy;

        return ctr;
    }

    /** 创建技能(加载移动ai)
     * 
     */
    private T CreateSkillCommon<T>(SkillConfig confVector3 posQuaternion rotwhere T : SkillCtrBase
    {
        //// 加载模型
        //var obj = UtilTool.CreateObjByName(conf.prefab, pos, rot);

        //// 添加移动ai
        //var moveConf = battleConfs.GetConfById<MoveAIConfig>(conf.moveAiId, battleConfs.GetMoveAIConfs());
        //var moveAI = obj.AddComponent(GlobalFunction.GetTypeByClassName(moveConf.aiName)) as BaseAI;

        //// 添加controller
        //var ctr = obj.AddComponent(typeof(T)) as T;

        //ctr.SetMoveAI(moveAI);
        //ctr.SetConfig(conf);

        //return ctr;

        T ctr;

        // 加载模型
        var obj = pool.Get(conf.id);
        if (obj == null)
        {
            obj = UtilTool.CreateObjByName(conf.prefabposrot);

            // 添加移动ai
            var moveConf = battleConfs.GetConfById<MoveAIConfig>(conf.moveAiIdbattleConfs.GetMoveAIConfs());
            var moveAI = obj.AddComponent(GlobalFunction.GetTypeByClassName(moveConf.aiName)) as BaseAI;
            // 添加controller
            ctr = obj.AddComponent(typeof(T)) as T;

            ctr.SetMoveAI(moveAI);
            ctr.IsRepeatMove = false;

        }
        else
        {
            ctr = obj.GetComponent(typeof(T)) as T;
            ctr.transform.position = pos;
            ctr.transform.rotation = rot;
        }

        ctr.SetConfig(conf);

        return ctr;
    }

    /** 回收敌人
     * 
     */
    private void ClearEnemys()
    {
        for (int i = 0i < enmeyCtrs.Counti++)
        {
            var ctr = enmeyCtrs[i];
            if (ctr != null)
            {
                //GameObject.Destroy(ctr.gameObject);

                pool.Put(ctr.Id(), ctr.gameObject);
            }

            enmeyCtrs[i] = null;
        }
        enmeyCtrs.Clear();
    }

    /** 回收子弹
     * 
     */
    private void ClearBullets()
    {

        for (int i = 0i < bulletCtrs.Counti++)
        {
            var ctr = bulletCtrs[i];
            if (ctr != null)
            {
                //GameObject.Destroy(ctr.gameObject);

                pool.Put(ctr.Id(), ctr.gameObject);
            }

            bulletCtrs[i] = null;
        }
        bulletCtrs.Clear();
    }

    /** 回收技能
    * 
    */
    private void ClearSkills()
    {

        for (int i = 0i < skillCtrs.Counti++)
        {
            var ctr = skillCtrs[i];
            if (ctr != null)
            {
                //GameObject.Destroy(obj.gameObject);

                pool.Put(ctr.Id(), ctr.gameObject);
            }

            skillCtrs[i] = null;
        }
        skillCtrs.Clear();
    }


    //公开接口
    //--------------------------------------------------------------------------

    /** 重置
     * 
     */
    public void Reset()
    {
        roleCtr = null;
        enmeyCtrs = new List<EnemyCtr>();
        bulletCtrs = new List<BulletCtr>();
        skillCtrs = new List<SkillCtr>();
    }

    /** 初始化
     * 
     */
    public void SetData(GlobleConfs gConfsGlobleLocalData gLocalDataBattleConfs bConfsBattlePool battlePool)
    {
        globleLocalData = gLocalData;
        globleConfs = gConfs;
        battleConfs = bConfs;
        pool = battlePool;
    }

    /** 开始游戏
      * 
      */
    public void OnStart()
    {
    }

    /** 每一波开始
     * 
     */
    public void OnWaveStart(int currentWave)
    {
        deathNum = 0;

    }


    /** 每帧更新
      * BattleManager 驱动此心跳
      * 
      */
    public void OnUpdate(float deltaTime)
    {

        roleCtr.OnUpdate(deltaTime);

        for (int i = 0i < enmeyCtrs.Count;i++)
        {
            enmeyCtrs[i].OnUpdate(deltaTime);
        }

        for (int i = 0i < bulletCtrs.Counti++)
        {
            bulletCtrs[i].OnUpdate(deltaTime);
        }

        for (int i = 0i < skillCtrs.Counti++)
        {
            skillCtrs[i].OnUpdate(deltaTime);
        }
    }
   

    /** 创建角色
     * 
     */
    public RoleCtr CreateRole(string roleIdVector3 posQuaternion rot)
    {
        var conf = battleConfs.GetConfById<RoleConfig>(roleIdbattleConfs.GetRoleConfs()); //currLevelConf.playerId

        var ctr = CreateRoleCommon<RoleCtr>(confposrot);
        ctr.OnStart();

        roleCtr = ctr;
            
        return ctr;
    }

    /** 创建敌人
     * 
     */
    public EnemyCtr CreateEnemy(string enemyIdVector3 posQuaternion rot)
    {
        var conf = battleConfs.GetConfById<RoleConfig>(enemyIdbattleConfs.GetRoleConfs());

        var ctr = CreateEnemyCommon<EnemyCtr>(confposrot);
        ctr.OnStart();

        enmeyCtrs.Add(ctr);

        return ctr;
    }


    /** 创建子弹
    * 
    *  isEnemy 是玩家还是敌人发射的
    */
    public BulletCtr CreateBullet(string bullectIDVector3 posQuaternion rotbool isEnemy)
    {
        var conf = battleConfs.GetConfById<BulletConfig>(bullectIDbattleConfs.GetBulletConfs());

        var ctr = CreateBulletCommon<BulletCtr>(confposrotisEnemy);
        ctr.OnStart();

        bulletCtrs.Add(ctr);

        return ctr;
    }

    /** 创建敌人
    * 
    */
    public SkillCtr CreateSkill(string skillIdVector3 posQuaternion rot)
    {
        var conf = battleConfs.GetConfById<SkillConfig>(skillIdbattleConfs.GetSkillConfs());

        var ctr = CreateSkillCommon<SkillCtr>(confposrot);
        ctr.OnStart();

        skillCtrs.Add(ctr);

        return ctr;
    }

    /** 距离参考者最近的技能
      *  
      */
    public EnemyCtr NearestSkill(Transform refer)
    {
        EnemyCtr ctr = null;
        float tempDis = float.MaxValue;
        float dis;
        foreach (var v in enmeyCtrs)
        {
            dis = Vector3.Distance(refer.positionv.transform.position);
            if (tempDis > dis)
            {
                tempDis = dis;
                ctr = v;
            }
        }

        return ctr;
    }

    /** 距离参考者最近的敌人
     *  
     */
    public EnemyCtr NearestEnemy(Transform refer)
    {
        EnemyCtr ctr = null;
        float tempDis = float.MaxValue;
        float dis;
        foreach(var v in enmeyCtrs)
        {
            dis = Vector3.Distance(refer.positionv.transform.position);
            if(tempDis > dis)
            {
                tempDis = dis;
                ctr = v;
            }
        }

        return ctr;
    }


    /** 距离参考者最近的玩家
     *  
     */
    public RoleCtr NearestPlayer(Transform refer)
    {
        return roleCtr;
    }

    /** 掉血
     * 
     */
    public void CutHp(PlayerCtrBase roleint dhp)
    {
        role.GetData().CutHP(dhp);
    }

    /** 切换到技能
      * 
      * 实质上是切换武器ai
      * 
      */
    public void SwitchToSkill(PlayerCtrBase ctrSkillConfig skillConf)
    {
        var oldWeaponAI = ctr.gameObject.GetComponent(typeof(ShootBaseAI));
        if(oldWeaponAI!= null)
        {
            GameObject.Destroy(oldWeaponAI);
        }

        // 添加武器ai
        var newWeaponConf = battleConfs.GetConfById<WeaponConfig>(skillConf.weaponAiIdbattleConfs.GetWeaponConfs());
        var newWeaponAI = ctr.gameObject.AddComponent(GlobalFunction.GetTypeByClassName(newWeaponConf.aiName)) as ShootBaseAI;

        ctr.ReSetWeaponConfig(newWeaponConfskillConf.GetFloat(skillConf.lifeTime));
        ctr.SetWeaponAI(newWeaponAI);
    }

    /** 玩家角色死亡
     * 
     */
    public void RoleDeath(RoleCtr role)
    {
        if(role == roleCtr)
        {
            //roleCtr.IsDead = true;
            pool.Put(roleCtr.Id(), roleCtr.gameObject);

            roleCtr = null;
        }
    }

    /** 敌人死亡
     * 
     */
    public void EnemyDeath(EnemyCtr enemy)
    {
        if(enmeyCtrs.Contains(enemy))
        {
            //enemy.IsDead = true;

            deathNum++;

            //GameObject.Destroy(enemy.gameObject);

            pool.Put(enemy.Id(), enemy.gameObject);

            enmeyCtrs.Remove(enemy);
        }
    }

    /** 敌人死亡
      * 
      */
    public void SkillDeath(SkillCtr skill)
    {
        if (skillCtrs.Contains(skill))
        {
            //GameObject.Destroy(skill.gameObject);

            pool.Put(skill.Id(), skill.gameObject);

            skillCtrs.Remove(skill);
        }
    }

    /** 子弹死亡
     * 
     */
    public void BulletDeath(BulletCtr bullet)
    {
        if (bulletCtrs.Contains(bullet))
        {
            //GameObject.Destroy(skill.gameObject);

            pool.Put(bullet.Id(), bullet.gameObject);

            bulletCtrs.Remove(bullet);
        }
    }

    public void ObjDeath(CtrBase ctr)
    {
        RoleDeath(ctr as RoleCtr);
        EnemyDeath(ctr as EnemyCtr);
        SkillDeath(ctr as SkillCtr);
        BulletDeath(ctr as BulletCtr);
    }

    /** 敌人是否全部消灭
     *  用来判断下一波进行何时进行
     *  或者比赛胜利
     * 
     */
    public bool EnemyAllDeath(int totalNum)
    {
        return deathNum == totalNum;
    }

    /** 清理上一波数据
     * 
     */
    public void ClearBattleWave()
    {
        ClearEnemys();
        ClearBullets();
    }
}

 

 

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

/*
 * 战斗管理器 
 * 职能 : 仅负责战斗逻辑
 *       (开始、结束、暂停、加速、广告逻辑)
 * 核心玩法 :
 *       一局游戏大致3分钟,有三波敌人.
 *       一波普通攻击,
 *       一波大波攻击,
 *       最后一波boss攻击.
 *       敌人没被打死, 移动到屏幕外会自动循环,再次从出生点开始移动, 直到被打死.
 *       一波敌人全部被消灭,敌人才会开始第二波进攻.
 * 
**/

[System.Serializable]
public class BattleManager : MonoBehaviour 
{
    //内部属性
    //--------------------------------------------------------------------------

    // TODO:以后做成读取配置表
    public Transform[] enemyBirthTrans;                // 敌人出生点
    public Transform[] skillBirthTrans;                // 技能出生点
    public Transform leftTop;                          // 左上角位置
    public Transform rightBottom;                      // 右下角位置

    /** 记录当前第几波
     * 
     */
    private int wave = 1;

    /** 切换到下一波之前的准备时间,可以进行一些弹字等
     * 
     */
    private bool switching = false;
    private float switchDelta = 0f;
    private float switchTime = 2f;
    private LevelConfig currLevelConf;
    private Coroutine enemyCorotine;
    private Coroutine skillCorotine;

    // 本波敌人总数
    [SerializeFieldprivate int totalNum;


    //外部部属性
    //--------------------------------------------------------------------------
    public BattleConfs battleConfs
    public BattleDatas battleDatas;

    //内部方法
    //--------------------------------------------------------------------------


    /** 技能出生逻辑
  * 
  */
    private IEnumerator InvokeSkill(int curentWave)
    {

        //Debug.LogError("curentWave; " + curentWave);

        int num = 0;
        string id = "";
        switch (curentWave)
        {
            case 1:
                id = currLevelConf.skill1Id;
                num = currLevelConf.GetInt(currLevelConf.skill1Num);
                break;
            case 2:
                id = currLevelConf.skill2Id;
                num = currLevelConf.GetInt(currLevelConf.skill2Num);
                break;
            case 3:
                id = currLevelConf.skill3Id;
                num = currLevelConf.GetInt(currLevelConf.skill3Num);
                break;
            default:
                break;
        }

        yield return new WaitForSeconds(3f);

        while (num > 0)
        {
       
            int index = Random.Range(0enemyBirthTrans.Length);

            battleDatas.CreateSkill(idenemyBirthTrans[index].positionQuaternion.identity);

            //Debug.LogError("skill num; " + num);


            yield return new WaitForSeconds(2f);
            num--;
        }


    }

    /** 停止出生敌人
     * 
     */
    private void StopInvokeSkill()
    {
        if (skillCorotine != null)
        {
            StopCoroutine("InvokeSkill");
            skillCorotine = null;

            //Debug.LogError("StopCoroutine");
        }
    }


    /** 敌人出生逻辑
     * 
     */
    private IEnumerator InvokeEnemy(int curentWave)
    {
        Debug.LogError("curentWave; " + curentWave);

        int num = 0;
        string id = "";
        switch (curentWave)
        {
            case 1:
                id = currLevelConf.enemy1Id;
                num = currLevelConf.GetInt(currLevelConf.enemy1Num);
                break;
            case 2:
                id = currLevelConf.enemy2Id;
                num = currLevelConf.GetInt(currLevelConf.enemy2Num);
                break;
            case 3:
                id = currLevelConf.enemy3Id;
                num = currLevelConf.GetInt(currLevelConf.enemy3Num);
                break;
            default:
                break;
        }

        totalNum = num;

        yield return new WaitForSeconds(1f);

        while (num > 0)
        {
            //battleDatas.CreateEnemy(id, new Vector3(0, 4f, 0), Quaternion.identity);

            int index = Random.Range(0enemyBirthTrans.Length);

            battleDatas.CreateEnemy(idenemyBirthTrans[index].positionQuaternion.identity);

            //Debug.LogError("1 num; " + num);


            yield return new WaitForSeconds(2f);
            num--;
        }

    }

    /** 停止出生敌人
     * 
     */
    private void StopInvokeEnemy()
    {
        if (enemyCorotine != null)
        {
            StopCoroutine("InvokeEnemy");
            enemyCorotine = null;

            //Debug.LogError("StopCoroutine");
        }
    }


    /** 敌人产生
    * 
    */
    private void OnWaveStart(int currentWave)
    {
        StopInvokeEnemy();
        //创建每一波敌人
        enemyCorotine = StartCoroutine("InvokeEnemy",currentWave);
    

        StopInvokeSkill();
        //创建每一波技能
        skillCorotine = StartCoroutine("InvokeSkill"currentWave);
    }


   
    /** 玩家胜利
     * 
     */
    private void OnSucces()
    {
        StopInvokeEnemy();
        battleDatas.ClearBattleWave();

        GameMgr.instanse.Success();
    }

    //公开接口
    //--------------------------------------------------------------------------

    // 初始化战斗
    public void InitBattle(GlobleConfs globleConfsGlobleLocalData globleLocalDataBattlePool pool)
    {
        /** 加载战斗配置
         * 
         */
        battleConfs = new BattleConfs();
        battleConfs.Load();

        /** 初始化战斗数据
         * 
         */
        battleDatas = new BattleDatas();
        battleDatas.Reset();
        battleDatas.SetData(globleConfsglobleLocalDatabattleConfspool);

        currLevelConf = globleConfs.GetConfById<LevelConfig>(globleLocalData.currentLevel.ToString(), globleConfs.GetLevelConfs());

        OnStart();
    }


    /** 开始游戏
      * 
      */
    public void OnStart()
    {
        //创建角色和敌人

        battleDatas.OnStart();

        OnWaveStart(wave);
        battleDatas.OnWaveStart(wave);

        battleDatas.CreateRole(currLevelConf.playerIdVector3.zeroQuaternion.identity);
    }

    /** 每帧更新
      * GameMgr 驱动此心跳
      * 
      */
    public void OnUpdate(float deltaTime)
    {
        if (GameMgr.instanse.Battling() == falsereturn;

        if(battleDatas.EnemyAllDeath(totalNum))
        {
            if(switching == false)
            {
                if(wave == 3
                {
                    
                    Debug.LogError("success!");

                    OnSucces();

                } else {
                    
                    switching = true;
                    wave++;

                    battleDatas.ClearBattleWave();
                }

            } else {
                if(switchDelta > switchTime)
                {
                    switchDelta = 0f;
                    switching = false;

                    OnWaveStart(wave);
                    battleDatas.OnWaveStart(wave);

                } else {
                    
                    switchDelta += deltaTime;
                }
            }
        }
        else
        {
            battleDatas.OnUpdate(deltaTime);
        }
    }

    //公开接口
    //--------------------------------------------------------------------------

    /** 收否出界
     *  左右下方判断,上方不用判断
     * 
     */
    public bool IsOutSide(Vector3 pos)
    {
        if (pos.x < leftTop.position.x)
            return true;

        if (pos.x > rightBottom.position.x)
            return true;

        if (pos.y < rightBottom.position.y)
            
            return true;
        
        return false;
    }

    /** 矫正出界点
     * 
     */
    public Vector3 CorrectOutSide(Vector3 pos)
    {
        return new Vector3(pos.xleftTop.position.y0);
    }

}
 

 

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

/** 子弹控制器
 *  更新子弹移动ai,碰撞检测
 * 
 */

public class BulletCtrBase : CtrBase
{
    //内部属性
    //--------------------------------------------------------------------------

    [SerializeFieldprotected BaseAI moveAI;            // 移动脚本
    [SerializeFieldprotected BulletConfig bulletConf;    // 子弹配置

    protected bool isStart = false;

    //内部方法
    //--------------------------------------------------------------------------
 
    /** 碰撞检测
     * 
     */
    protected virtual void CollisionDetection() {}


    //override方法 
    //--------------------------------------------------------------------------
    public virtual void OnStart()
    {
        battle = GameMgr.instanse.BattleMgr();

        isStart = true;
    }

    /** 每帧更新
     *  BattleDatas 驱动此心跳
     *  
     *  子弹移动
     *  碰撞检测逻辑
     * 
     */
    public virtual void OnUpdate(float deltaTime)
    {
        if (isStart == falsereturn;

        this.CollisionDetection();

        this.moveAI.OnUpdate(deltaTime);
      
    }



    //公开接口
    //--------------------------------------------------------------------------

    /** 移动ai
     * 
     */
    public void SetMoveAI(BaseAI move)
    {
        this.moveAI = move;
    }

    /** 子弹配置
     * 
     */
    public void SetConfig(BulletConfig conf)
    {
        this.bulletConf = conf;
    }

    /** 是玩家还是敌人发射的
     * 
     */
    [SerializeFieldprivate bool enemy;
    public bool IsEnemy
    {
        set { this.enemy = value; }
        get { return this.enemy; }
    }


    /** 角色id
     * 
     */
    public string Id()
    {
        return this.bulletConf.id;
    }

}
 

 

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

/** 技能控制器
 *  更新移动ai,碰撞检测
 * 
 */

public class SkillCtrBase : CtrBase
{
    //内部属性
    //--------------------------------------------------------------------------

    [SerializeFieldprotected BaseAI moveAI;              // 移动脚本
    [SerializeFieldprotected SkillConfig skillConf;      // 技能配置

    protected bool isStart = false;

    //内部方法
    //--------------------------------------------------------------------------
 
    /** 碰撞检测
     * 
     */
    protected virtual void CollisionDetection() {}


    //override方法 
    //--------------------------------------------------------------------------
    public virtual void OnStart()
    {
        battle = GameMgr.instanse.BattleMgr();

        isStart = true;
    }

    /** 每帧更新
     *  BattleDatas 驱动此心跳
     *  
     *  移动
     *  碰撞检测逻辑
     * 
     */
    public virtual void OnUpdate(float deltaTime)
    {
        if (isStart == falsereturn;

        this.CollisionDetection();

        this.moveAI.OnUpdate(deltaTime);
      
    }



    //公开接口
    //--------------------------------------------------------------------------

    /** 移动ai
     * 
     */
    public void SetMoveAI(BaseAI move)
    {
        this.moveAI = move;
    }

    /** 技能配置
     * 
     */
    public void SetConfig(SkillConfig conf)
    {
        this.skillConf = conf;
    }

    /** 角色id
     * 
     */
    public string Id()
    {
        return this.skillConf.id;
    }

}
 

 

 

 

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

public class CtrBase : MonoBehaviour 
{
    // 战斗管理器
    [SerializeFieldprotected BattleManager battle;
    // 对象回收池
    [SerializeFieldprotected BattlePool pool;

    /** 重复移动
     * 
     *  敌人、障碍物 出底线了,需要矫正位置后重新出现
     *  子弹、技能不需要
     * 
     */
    [SerializeFieldprivate bool repeatMove;
    public bool IsRepeatMove
    {
        set { this.repeatMove = value; }
        get { return this.repeatMove; }
    }

}

 

 

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

/** 子弹控制器
 * 
 *  更新子弹移动ai
 *  
 *  碰撞检测
 *
 *
 */


[System.Serializable]
public class BulletCtr : BulletCtrBase
{
    //内部属性
    //--------------------------------------------------------------------------


    //内部方法
    //--------------------------------------------------------------------------
    /** 碰撞检测
    *   子弹ctr 有个属性IsEnemy,标记子弹是玩家还是敌人发出的.
    *   如果是玩家发出的,子弹会检测所有敌人.
    *   如果是敌人发出的,子弹会检测所有玩家.
    * 
    * 
    */
    protected override void CollisionDetection()
    {
        if (this.IsEnemy)
        {
            // 检测玩家
            var player = battle.battleDatas.NearestPlayer(transform);

            if(player == null)
            {
                Debug.LogError("没有检测到玩家");

                return;
            }
            bool hitted = GlobalFunction.InFieldOfVision(player.transform.positiontransform.position);
            if (hitted)
            {
                Debug.LogError("击中玩家 " + player.gameObject.name);

                battle.battleDatas.CutHp(player10);

                if(player.GetData().Death())
                {
                    Debug.Log("玩家 " + player.gameObject.name + " 死亡!");

                    battle.battleDatas.RoleDeath(player);
                }
            } 
        } 
        else
        {
            // 检测敌人
            var enemy = battle.battleDatas.NearestEnemy(transform);

            if(enemy == null)
            {
                //Debug.LogError("没有检测到敌人");

                return;
            }
            bool hitted = GlobalFunction.InFieldOfVision(enemy.transform.positiontransform.position);
            if (hitted)
            {
                Debug.LogError("击中敌人 " + enemy.gameObject.name);

                battle.battleDatas.CutHp(enemy10);

                if (enemy.GetData().Death())
                {
                    Debug.Log("敌人 " + enemy.gameObject.name + " 死亡!");

                    battle.battleDatas.EnemyDeath(enemy);
                }
            } 
        }
    }

    //override方法
    //--------------------------------------------------------------------------
    /** BattleDatas 创建时,完成初始化(OnStart)
     * 
     */
    public override void OnStart()
    {
        var moveDir = this.IsEnemy ? Vector3.down : Vector3.up;
        object[] args = { this.transform ,this.bulletConf.moveSpeedmoveDirnullthis};

        this.moveAI.OnStart(args);

        base.OnStart();
    }

    //公开接口
    //--------------------------------------------------------------------------

 

}

 

 

 

 

 

 

 

 


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值