using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class BattlePool
{
//内部属性
//--------------------------------------------------------------------------
/** 缓存池
*
*/
[SerializeField] Dictionary<string, List<GameObject>> dic = new Dictionary<string, List<GameObject>>();
//内部方法
//--------------------------------------------------------------------------
/**
*
*
*/
//公开接口
//--------------------------------------------------------------------------
/** 从缓存池获取obj
*
*/
public GameObject Get(string key)
{
if (dic.ContainsKey(key))
{
var list = dic[key];
//Debug.Log("list length " + list.Count);
if(list.Count> 0)
{
var obj = list[0];
obj.SetActive(true);
list.RemoveAt(0);
//Debug.Log("key " + key);
return obj;
}
return null;
}
return null;
}
/** 回收到缓存池
*
*/
public void Put(string key, GameObject 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(key, list);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/**
* 游戏管理器
* 职能
* 游戏操作引导、关卡管理、创建战斗
*
*/
public class GameMgr : MonoBehaviour
{
//内部属性
//--------------------------------------------------------------------------
/** 单例模式
*
*/
public static GameMgr instanse;
/** 战斗管理器
*
*/
[SerializeField] private BattleManager battleManager;
/** 全局配置
* 比如 关卡配置
*
*/
[SerializeField] private GlobleConfs globleConfs;
/** 本地存储记录
*
*/
[SerializeField] private GlobleLocalData globleLocalData;
/** 战场对象缓存池
*
*/
[SerializeField] private BattlePool pool;
// 战斗中标记
[SerializeField] private 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(globleConfs, globleLocalData, pool);
battling = true;
}
/** 选择关卡,发起战斗
*
*/
void OnUpdate(float deltaTime)
{
if (battling == false) return;
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;
// 敌人引用
[SerializeField] List<EnemyCtr> enmeyCtrs;
// 子弹引用
[SerializeField] List<BulletCtr> bulletCtrs;
// 子弹引用
[SerializeField] List<SkillCtr> skillCtrs;
// 玩家引用
[SerializeField] RoleCtr roleCtr;
// 本波死亡敌人总数
[SerializeField] int deathNum;
//内部方法
//--------------------------------------------------------------------------
/** 玩家 (不需要移动ai,仅加载攻击ai)
*
*/
private T CreateRoleCommon<T>(RoleConfig conf, Vector3 pos, Quaternion rot) where T : PlayerCtrBase
{
T ctr;
var weaponConf = battleConfs.GetConfById<WeaponConfig>(conf.weaponAiId, battleConfs.GetWeaponConfs());
// 加载模型
var obj = pool.Get(conf.id);
if(obj == null)
{
obj = UtilTool.CreateObjByName(conf.prefab, pos, rot);
// 添加武器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(conf, weaponConf);
return ctr;
}
/** 敌人 (加载移动ai, 攻击ai)
*
*/
private T CreateEnemyCommon<T>(RoleConfig conf, Vector3 pos, Quaternion rot) where 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.moveAiId, battleConfs.GetMoveAIConfs());
var weaponConf = battleConfs.GetConfById<WeaponConfig>(conf.weaponAiId, battleConfs.GetWeaponConfs());
// 加载模型
var obj = pool.Get(conf.id);
if (obj == null)
{
obj = UtilTool.CreateObjByName(conf.prefab, pos, rot);
// 添加移动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(conf, weaponConf);
return ctr;
}
/** 创建子弹(加载移动ai)
*
*/
private T CreateBulletCommon<T>(BulletConfig conf, Vector3 pos, Quaternion rot, bool isEnemy) where 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.prefab, pos, rot);
// 添加移动ai
var moveConf = battleConfs.GetConfById<MoveAIConfig>(conf.moveAiId, battleConfs.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 conf, Vector3 pos, Quaternion rot) where 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.prefab, pos, rot);
// 添加移动ai
var moveConf = battleConfs.GetConfById<MoveAIConfig>(conf.moveAiId, battleConfs.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 = 0; i < enmeyCtrs.Count; i++)
{
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 = 0; i < bulletCtrs.Count; i++)
{
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 = 0; i < skillCtrs.Count; i++)
{
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 gConfs, GlobleLocalData gLocalData, BattleConfs bConfs, BattlePool 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 = 0; i < enmeyCtrs.Count;i++)
{
enmeyCtrs[i].OnUpdate(deltaTime);
}
for (int i = 0; i < bulletCtrs.Count; i++)
{
bulletCtrs[i].OnUpdate(deltaTime);
}
for (int i = 0; i < skillCtrs.Count; i++)
{
skillCtrs[i].OnUpdate(deltaTime);
}
}
/** 创建角色
*
*/
public RoleCtr CreateRole(string roleId, Vector3 pos, Quaternion rot)
{
var conf = battleConfs.GetConfById<RoleConfig>(roleId, battleConfs.GetRoleConfs()); //currLevelConf.playerId
var ctr = CreateRoleCommon<RoleCtr>(conf, pos, rot);
ctr.OnStart();
roleCtr = ctr;
return ctr;
}
/** 创建敌人
*
*/
public EnemyCtr CreateEnemy(string enemyId, Vector3 pos, Quaternion rot)
{
var conf = battleConfs.GetConfById<RoleConfig>(enemyId, battleConfs.GetRoleConfs());
var ctr = CreateEnemyCommon<EnemyCtr>(conf, pos, rot);
ctr.OnStart();
enmeyCtrs.Add(ctr);
return ctr;
}
/** 创建子弹
*
* isEnemy 是玩家还是敌人发射的
*/
public BulletCtr CreateBullet(string bullectID, Vector3 pos, Quaternion rot, bool isEnemy)
{
var conf = battleConfs.GetConfById<BulletConfig>(bullectID, battleConfs.GetBulletConfs());
var ctr = CreateBulletCommon<BulletCtr>(conf, pos, rot, isEnemy);
ctr.OnStart();
bulletCtrs.Add(ctr);
return ctr;
}
/** 创建敌人
*
*/
public SkillCtr CreateSkill(string skillId, Vector3 pos, Quaternion rot)
{
var conf = battleConfs.GetConfById<SkillConfig>(skillId, battleConfs.GetSkillConfs());
var ctr = CreateSkillCommon<SkillCtr>(conf, pos, rot);
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.position, v.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.position, v.transform.position);
if(tempDis > dis)
{
tempDis = dis;
ctr = v;
}
}
return ctr;
}
/** 距离参考者最近的玩家
*
*/
public RoleCtr NearestPlayer(Transform refer)
{
return roleCtr;
}
/** 掉血
*
*/
public void CutHp(PlayerCtrBase role, int dhp)
{
role.GetData().CutHP(dhp);
}
/** 切换到技能
*
* 实质上是切换武器ai
*
*/
public void SwitchToSkill(PlayerCtrBase ctr, SkillConfig skillConf)
{
var oldWeaponAI = ctr.gameObject.GetComponent(typeof(ShootBaseAI));
if(oldWeaponAI!= null)
{
GameObject.Destroy(oldWeaponAI);
}
// 添加武器ai
var newWeaponConf = battleConfs.GetConfById<WeaponConfig>(skillConf.weaponAiId, battleConfs.GetWeaponConfs());
var newWeaponAI = ctr.gameObject.AddComponent(GlobalFunction.GetTypeByClassName(newWeaponConf.aiName)) as ShootBaseAI;
ctr.ReSetWeaponConfig(newWeaponConf, skillConf.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;
// 本波敌人总数
[SerializeField] private 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(0, enemyBirthTrans.Length);
battleDatas.CreateSkill(id, enemyBirthTrans[index].position, Quaternion.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(0, enemyBirthTrans.Length);
battleDatas.CreateEnemy(id, enemyBirthTrans[index].position, Quaternion.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 globleConfs, GlobleLocalData globleLocalData, BattlePool pool)
{
/** 加载战斗配置
*
*/
battleConfs = new BattleConfs();
battleConfs.Load();
/** 初始化战斗数据
*
*/
battleDatas = new BattleDatas();
battleDatas.Reset();
battleDatas.SetData(globleConfs, globleLocalData, battleConfs, pool);
currLevelConf = globleConfs.GetConfById<LevelConfig>(globleLocalData.currentLevel.ToString(), globleConfs.GetLevelConfs());
OnStart();
}
/** 开始游戏
*
*/
public void OnStart()
{
//创建角色和敌人
battleDatas.OnStart();
OnWaveStart(wave);
battleDatas.OnWaveStart(wave);
battleDatas.CreateRole(currLevelConf.playerId, Vector3.zero, Quaternion.identity);
}
/** 每帧更新
* GameMgr 驱动此心跳
*
*/
public void OnUpdate(float deltaTime)
{
if (GameMgr.instanse.Battling() == false) return;
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.x, leftTop.position.y, 0);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/** 子弹控制器
* 更新子弹移动ai,碰撞检测
*
*/
public class BulletCtrBase : CtrBase
{
//内部属性
//--------------------------------------------------------------------------
[SerializeField] protected BaseAI moveAI; // 移动脚本
[SerializeField] protected 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 == false) return;
this.CollisionDetection();
this.moveAI.OnUpdate(deltaTime);
}
//公开接口
//--------------------------------------------------------------------------
/** 移动ai
*
*/
public void SetMoveAI(BaseAI move)
{
this.moveAI = move;
}
/** 子弹配置
*
*/
public void SetConfig(BulletConfig conf)
{
this.bulletConf = conf;
}
/** 是玩家还是敌人发射的
*
*/
[SerializeField] private 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
{
//内部属性
//--------------------------------------------------------------------------
[SerializeField] protected BaseAI moveAI; // 移动脚本
[SerializeField] protected 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 == false) return;
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
{
// 战斗管理器
[SerializeField] protected BattleManager battle;
// 对象回收池
[SerializeField] protected BattlePool pool;
/** 重复移动
*
* 敌人、障碍物 出底线了,需要矫正位置后重新出现
* 子弹、技能不需要
*
*/
[SerializeField] private 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.position, transform.position);
if (hitted)
{
Debug.LogError("击中玩家 " + player.gameObject.name);
battle.battleDatas.CutHp(player, 10);
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.position, transform.position);
if (hitted)
{
Debug.LogError("击中敌人 " + enemy.gameObject.name);
battle.battleDatas.CutHp(enemy, 10);
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.moveSpeed, moveDir, null, this};
this.moveAI.OnStart(args);
base.OnStart();
}
//公开接口
//--------------------------------------------------------------------------
}