(五) 武器切换(使用技能)
(五) 武器切换(使用技能)
using System.Collecti;
using System.Collecti.Generic;
using UnityEngine;
/**
* 战斗管理器
* 职能 : 仅负责战斗逻辑
* (开始、结束、暂停、加速、广告逻辑)
* 核心玩法 :
* 一局游戏大致分钟,有三波敌人.
* 一波普通攻击,
* 一波大波攻击,
* 最后一波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 totalum;
//外部部属性
//--------------------------------------------------------------------------
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.skill1um);
break;
case 2:
id = currLevelConf.skill2Id;
num = currLevelConf.GetInt(currLevelConf.skill2um);
break;
case :
id = currLevelConf.skillId;
num = currLevelConf.GetInt(currLevelConf.skillum);
break;
default:
break;
}
totalum = num;
yield return new WaitForSeconds(f);
while (num > 0)
{
int index = Random.Range(0, enemyBirthTrans.Length);
battleDatas.CreateSkill(id, enemyBirthTrans[index].position, Quaternion.identity);
Debug.LogError(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 = ;
num = currLevelConf.GetInt();
break;
case 2:
id = ;
num = currLevelConf.GetInt();
break;
case :
id = ;
num = currLevelConf.GetInt();
break;
default:
break;
}
totalum = num;
yield return new WaitForSeconds(1f);
while (num > 0)
{
//battleDatas.CreateEnemy(id, new Vector(0, 4f, 0), Quaternion.identity);
int index = Random.Range(0, enemyBirthTrans.Length);
battleDatas.CreateEnemy(id, enemyBirthTrans[index].position, Quaternion.identity);
Debug.LogError(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)
{
/** 加载战斗配置
*
*/
battleConfs = new BattleConfs();
battleConfs.Load();
/** 初始化战斗数据
*
*/
battleDatas = new BattleDatas();
battleDatas.Reset();
battleDatas.SetData(globleConfs, globleLocalData, battleConfs);
currLevelConf = globleConfs.GetConfById<LevelConfig>(ToString(), globleConfs.GetLevelConfs());
OnStart();
}
/** 开始游戏
*
*/
public void OnStart()
{
//创建角和敌人
battleDatas.OnStart();
OnWaveStart(wave);
battleDatas.OnWaveStart(wave);
battleDatas.CreateRole(currLevelConf.playerId, Vector.zero, Quaternion.identity);
}
/** 每帧更新
* GameMgr 驱动此心跳
*
*/
public void OnUpdate(float deltaTime)
{
if (GameMgr.instanse.Battling() == false) return;
if(battleDatas.EnemyAllDeath(totalum))
{
if(switching == false)
{
if(wave == )
{
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(Vector 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 Vector CorrectOutSide(Vector pos)
{
return new Vector(pos.x, leftTop.position.y, 0);
}
}
using UnityEngine;
using Excel;
using System.Data;
using System.IO;
using System.Collecti.Generic;
using OfficeOpenXml;
using UnityEditor;
// TODO:重构
public static class ExcelTool
{
//TODO:内部方法
//--------------------------------------------------------------------------
// 定义数组
// 关卡配置
// ID: 1-10
static string[,] levels = new string[,]
{
{ id,playerId,
enemy1Id,enemy1um,skill1Id,skill1um, obstacles1Id,obstacles1um,
enemy2Id,enemy2um,skill2Id,skill2um,obstacles2Id,obstacles2um,
enemyId,enemyum,skillId,skillum,obstaclesId,obstaclesum,
},
{ 1,10010001,
10010002,1,50010001,2, obstacles1Id,obstacles1um,
10010002,,50010001,4,obstacles2Id,obstacles2um,
10010002,5,50010001,6,obstaclesId,obstaclesum,
},
};
// 角配置
// ID: 1001
static string[,] roles = new string[,]
{
{ id,prefab,moveAiId,weaponAiId,roleType,maxHP, moveSpeed},
{ 10010001,role1,,40010001,1,1000,}, //玩家不需要移动ai,和 初始移动速度
{ 10010002,role2,20010001,40010001,2,50,1},
};
// 移动ai配置
// ID: 2001
static string[,] moveAIs = new string[,]
{
{ id,aiame},
{ 20010001,SingleForwardAI},
};
// 子弹配置
// ID: 001
static string[,] bullets = new string[,]
{
{ id,prefab,moveAiId,attack,explosionEffect,hitEffect, fireEffect, moveSpeed},
{ 0010001,bullet1,20010001,15,explosionEffect,hitEffect, fireEffect, 5},
};
// 武器配置
// ID: 4001
static string[,] weap = new string[,]
{
{ id,aiame, shootInterval,bulletID},
{ 40010001,SingleShootAI, 0.2 ,0010001},
};
// 技能配置
// ID: 5001
static string[,] skills = new string[,]
{
{ id,prefab,moveAiId,weaponAiId,moveSpeed,lifeTime},
{ 50010001,bullet1,20010001,40010001, 5,},
};
// 向Excel写配置数据(数组)
//--------------------------------------------------------------------------
static void WriteConfigTable(ExcelWorksheet worksheet, string[,] contents)
{
int rowCount = contents.GetLength(0);
int colCount = contents.GetLength(1);
for (int i = 0; i < rowCount; i)
{
for (int j = 0; j < colCount; j)
{
var v = contents[i, j];
worksheet.Cells[i 1, j 1].Value = v;
}
}
}
static void TryWriteConfig(ExcelWorksheet worksheet, string sheetame)
{
if (sheetame == GlobleStr.LevelConf)
WriteConfigTable(worksheet, levels);
else if (sheetame == GlobleStr.RoleConf)
WriteConfigTable(worksheet, roles);
else if (sheetame == GlobleStr.MoveAiConf)
WriteConfigTable(worksheet, moveAIs);
else if (sheetame == GlobleStr.WeaponConf)
WriteConfigTable(worksheet, weap);
else if (sheetame == GlobleStr.BulletConf)
WriteConfigTable(worksheet, bullets);
else if (sheetame == GlobleStr.SkillConf)
WriteConfigTable(worksheet, skills);
}
//--------------------------------------------------------------------------
// 读取 Excel ; 需要添加 Excel.dll; System.Data.dll;
// excel文件名
// sheet名称
// DataRow的集合
static DataRowCollection TryReadExcel(string excelame, string sheetame)
{
DataSet result = ReadExcel(excelame);
return result.Tables[sheetame].Rows;
}
static DataSet ReadExcel(string excelame)
{
string path = Application.dataPath / excelame;
FileStream stream = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read);
IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
DataSet result = excelReader.AsDataSet();
Debug.Log(path: path);
Debug.Log(result: result.Tables.Count);
Debug.Log(excelReader: excelReader.ResultsCount);
return result;
}
static int GetFieldId(DataRowCollection collect, string fieldame)
{
// 标题名称
var titles = collect[0];
for (int j = 0; j < titles.ItemArray.Length; j)
{
var title = titles[j];
if(title.ToString() == fieldame)
return j;
}
return -1;
}
//TODO:外部接口
//--------------------------------------------------------------------------
// 从Excel读配置数据(反射)
public static List<T> ReadConfigTable<T>(string excelame, string sheetame) where T:CCObj,new()
{
List<T> list = new List<T>();
DataRowCollection collect = TryReadExcel(excelame, sheetame);
if (collect.Count == 0) return null;
//Debug.Log(collect.Count : collect.Count);
// i 从1开始读取数据,0行是表头
for (int i = 1; i < collect.Count; i)
{
if (collect[i][0].ToString() == ) continue;
T t = new T();
var fields = t.getFieldames();
//Debug.Log(fields.Length : fields.Length);
for (int j = 0; j < fields.Length;j)
{
var fieldame = fields[j];
int id = GetFieldId(collect, fieldame);
var val = collect[i][id];
t.setValue(fields[j], val);
//Debug.Log(field : fields[j]);
//Debug.Log(index : id);
//Debug.Log(field val : val);
}
list.Add(t);
}
return list;
}
// 写入 Excel , 需要添加 OfficeOpenXml.dll
// excel 文件名
// sheet 名称
public static void WriteExcel(string excelame, string sheetame)
{
//自定义excel的路径
string path = Application.dataPath / excelame;
FileInfo newFile = new FileInfo(path);
if (newFile.Exists)
{
Debug.Log(exists);
DataSet result = ReadExcel(excelame);
var oldSheet = result.Tables[sheetame];
foreach(var v in result.Tables)
{
Debug.Log(v: v);
}
Debug.Log(oldSheet Contains: result.Tables.Contains(sheetame));
Debug.Log(oldSheet: oldSheet);
if (oldSheet != null) {
ExcelPackage package = new ExcelPackage(newFile);
package.Workbook.Worksheets.Delete(sheetame);
//保存excel
package.Save();
AssetDatabase.Refresh();
Debug.Log(delete sheetame: sheetame);
}
}
else
{
Debug.Log(not exists);
//创建一个新的excel文件
newFile = new FileInfo(path);
}
//通过ExcelPackage打开文件
using (ExcelPackage package = new ExcelPackage(newFile))
{
//在excel空文件添加新sheet
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add(sheetame);
TryWriteConfig(worksheet, sheetame);
//保存excel
package.Save();
AssetDatabase.Refresh();
Debug.Log(save);
}
}
}
using System.Collecti;
using System.Collecti.Generic;
using UnityEngine;
/**
* 运行时数据
* 职能: 管理(增、删、改、查操作) 角、敌人、技能、金币 等游戏对象
*
* 玩家 controller
* 敌人 controller (数组)
* 技能 controller (数组)
* 障碍物 controller (数组)
* 子弹移动ai (数组, 子弹会持有发射源的controller, 攻击目标的transform, 子弹有个碰撞检测脚本)
*
*
*
**/
[System.Serializable]
public class BattleDatas
{
//内部属性
//--------------------------------------------------------------------------
GlobleLocalData globleLocalData;
GlobleConfs globleConfs;
BattleConfs battleConfs;
// 敌人引用
[SerializeField] List<EnemyCtr> enmeyCtrs;
// 子弹引用
[SerializeField] List<BulletCtr> bulletCtrs;
// 子弹引用
[SerializeField] List<SkillCtr> skillCtrs;
// 玩家引用
[SerializeField] RoleCtr roleCtr;
// 本波死亡敌人总数
[SerializeField] int deathum;
//内部方法
//--------------------------------------------------------------------------
/** 玩家 (不需要移动ai,仅加载攻击ai)
*
*/
private T CreateRoleCommon<T>(RoleConfig conf, Vector pos, Quaternion rot) where T : PlayerCtrBase
{
// 加载模型
var obj = UtilTool.CreateObjByame(conf.prefab, pos, rot);
// 添加武器ai
var weaponConf = battleConfs.GetConfById<WeaponConfig>(conf.weaponAiId, battleConfs.GetWeaponConfs());
var weaponAI = obj.AddComponent(GlobalFunction.GetTypeByClassame(weaponConf.aiame)) as ShootBaseAI;
// 添加controller
var ctr = obj.AddComponent(typeof(T)) as T;
ctr.SetConfig(conf, weaponConf);
ctr.SetWeaponAI(weaponAI);
ctr.IsEnemy = false;
return ctr;
}
/** 敌人 (加载移动ai, 攻击ai)
*
*/
private T CreateEnemyCommon<T>(RoleConfig conf, Vector pos, Quaternion rot) where T : PlayerCtrBase
{
// 加载模型
var obj = UtilTool.CreateObjByame(conf.prefab, pos, rot);
// 添加移动ai
var moveConf = battleConfs.GetConfById<MoveAIConfig>(, battleConfs.GetMoveAIConfs());
var moveAI = obj.AddComponent(GlobalFunction.GetTypeByClassame(moveConf.aiame)) as BaseAI;
// 添加武器ai
var weaponConf = battleConfs.GetConfById<WeaponConfig>(conf.weaponAiId, battleConfs.GetWeaponConfs());
var weaponAI = obj.AddComponent(GlobalFunction.GetTypeByClassame(weaponConf.aiame)) 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;
}
/** 创建子弹(加载移动ai)
*
*/
private T CreateBulletCommon<T>(BulletConfig conf, Vector pos, Quaternion rot, bool isEnemy) where T : BulletCtrBase
{
// 加载模型
var obj = UtilTool.CreateObjByame(conf.prefab, pos, rot);
// 添加移动ai
var moveConf = battleConfs.GetConfById<MoveAIConfig>(, battleConfs.GetMoveAIConfs());
var moveAI = obj.AddComponent(GlobalFunction.GetTypeByClassame(moveConf.aiame)) as BaseAI;
// 添加controller
var ctr = obj.AddComponent(typeof(T)) as T;
ctr.SetMoveAI(moveAI);
ctr.SetConfig(conf);
ctr.IsEnemy = isEnemy;
return ctr;
}
/** 创建技能(加载移动ai)
*
*/
private T CreateSkillCommon<T>(SkillConfig conf, Vector pos, Quaternion rot) where T : SkillCtrBase
{
// 加载模型
var obj = UtilTool.CreateObjByame(conf.prefab, pos, rot);
// 添加移动ai
var moveConf = battleConfs.GetConfById<MoveAIConfig>(, battleConfs.GetMoveAIConfs());
var moveAI = obj.AddComponent(GlobalFunction.GetTypeByClassame(moveConf.aiame)) as BaseAI;
// 添加controller
var ctr = obj.AddComponent(typeof(T)) as T;
ctr.SetMoveAI(moveAI);
ctr.SetConfig(conf);
return ctr;
}
/** 回收敌人
*
*/
private void ClearEnemys()
{
for (int i = 0; i < enmeyCtrs.Count; i)
{
enmeyCtrs[i] = null;
}
enmeyCtrs.Clear();
}
/** 回收子弹
*
*/
private void ClearBullets()
{
for (int i = 0; i < bulletCtrs.Count; i)
{
var obj = bulletCtrs[i];
if (obj != null)
{
GameObject.Destroy(obj.gameObject);
}
bulletCtrs[i] = null;
}
bulletCtrs.Clear();
}
/** 回收技能
*
*/
private void ClearSkills()
{
for (int i = 0; i < skillCtrs.Count; i)
{
var obj = skillCtrs[i];
if (obj != null)
{
GameObject.Destroy(obj.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)
{
globleLocalData = gLocalData;
globleConfs = gConfs;
battleConfs = bConfs;
}
/** 开始游戏
*
*/
public void OnStart()
{
}
/** 每一波开始
*
*/
public void OnWaveStart(int currentWave)
{
deathum = 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, Vector 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, Vector 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, Vector 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, Vector 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 earestSkill(Transform refer)
{
EnemyCtr ctr = null;
float tempDis = float.MaxValue;
float dis;
foreach (var v in enmeyCtrs)
{
dis = Vector.Distance(refer.position, position);
if (tempDis > dis)
{
tempDis = dis;
ctr = v;
}
}
return ctr;
}
/** 距离参考者最近的敌人
*
*/
public EnemyCtr earestEnemy(Transform refer)
{
EnemyCtr ctr = null;
float tempDis = float.MaxValue;
float dis;
foreach(var v in enmeyCtrs)
{
dis = Vector.Distance(refer.position, position);
if(tempDis > dis)
{
tempDis = dis;
ctr = v;
}
}
return ctr;
}
/** 距离参考者最近的玩家
*
*/
public RoleCtr earestPlayer(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.GetTypeByClassame(newWeaponConf.aiame)) as ShootBaseAI;
ctr.ReSetWeaponConfig(newWeaponConf, skillConf.GetFloat(skillConf.lifeTime));
ctr.SetWeaponAI(newWeaponAI);
}
/** 玩家角死亡
*
*/
public void RoleDeath(RoleCtr role)
{
if(role == roleCtr)
{
//roleCtr.IsDead = true;
roleCtr = null;
}
}
/** 敌人死亡
*
*/
public void EnemyDeath(EnemyCtr enemy)
{
if(enmeyCtrs.Contains(enemy))
{
//enemy.IsDead = true;
deathum;
GameObject.Destroy(enemy.gameObject);
enmeyCtrs.Remove(enemy);
}
}
/** 敌人是否全部消灭
* 用来判断下一波进行何时进行
* 或者比赛胜利
*
*/
public bool EnemyAllDeath(int totalum)
{
return deathum == totalum;
}
/** 清理上一波数据
*
*/
public void ClearBattleWave()
{
ClearEnemys();
ClearBullets();
}
}
using System.Collecti;
using System.Collecti.Generic;
using UnityEngine;
using System;
/** 玩家控制器
* 持有武器的引用,负责角行为逻辑(攻击、移动)
*
*
* 玩家不做碰撞检测,检测为单向,敌人、技能、子弹等负责检测碰撞
*
*
*/
[System.Serializable]
public class RoleCtr : PlayerCtrBase
{
//内部属性
//--------------------------------------------------------------------------
Vector lastPosition_; // 上次位置
Vector clickPosition_; // 点击位置
//内部方法
//--------------------------------------------------------------------------
/** 玩家操作 滑动(或者点击鼠标)
*
*/
private bool IsOperating()
{
#if UITY_EDITOR || UITY_STADALOE_OSX || UITY_STADALOE
if (Input.GetMouseButton(0))
{
return true;
}
#elif UITY_ADROID || UITY_IPHOE
if( > 0 && Input.GetTouch(0).phase == TouchPhase.Moved){
return true;
}
#endif
return false;
}
// z轴为10 : 是摄像机z轴取相反数
private Vector GetPosition()
{
Vector v = ScreenToWorldPoint( new Vector(0f, 0f, 10f));
return v;
}
/** 角移动
*
*/
private void UpdateMove(float deltaTime)
{
lastPosition_ = transform.position; //存储上一次位置
clickPosition_ = GetPosition();
transform.position = clickPosition_;
}
/** 更新武器攻击时间、执行攻击行为
*
*/
private void UpdateWeapon(float deltaTime)
{
this.weaponAI.OnUpdate(deltaTime);
}
/** 仅更新武器攻击时间、不执行攻击行为
*
*/
private void UpdateWeaponTimeOnly(float deltaTime)
{
this.weaponAI.OnUpdateTimeOnly(deltaTime);
}
//override方法
//--------------------------------------------------------------------------
public override void OnStart()
{
object[] args = { this, , this.weaponConf};
this.weaponAI.OnStart(args);
base.OnStart();
}
/** 每帧更新
* BattleDatas 驱动此心跳
*
*/
public override void OnUpdate(float deltaTime)
{
base.OnUpdate(deltaTime);
if (IsOperating())
{
//Debug.LogError(IsOperating deltaTime: deltaTime);
UpdateMove(deltaTime);
UpdateWeapon(deltaTime);
}
else
{
//Debug.LogError(UpdateWeaponTimeOnly: deltaTime);
UpdateWeaponTimeOnly(deltaTime);
}
}
//公开接口
//--------------------------------------------------------------------------
}
using System.Collecti;
using System.Collecti.Generic;
using UnityEngine;
/** 角、敌人的controller
* 负责 :
* 初始化对象的配置、记录对象运行数据、动画控制器、移动ai引用、
* 武器ai引用、子弹数组引用等.
* (controller : config、data、animator、移动ai引用、武器ai引用、子弹ai引用(数组) 等)
*
**/
public class PlayerCtrBase : CtrBase
{
//内部属性
//--------------------------------------------------------------------------
[SerializeField] protected WeaponConfig tempWeaponConf; // 武器配置(技能等意外获得武器)
[SerializeField] protected float skillTime; // 技能生效时间
[SerializeField] protected float skillDelta; //
[SerializeField] protected bool skilling = false; // 技能生效中
[SerializeField] protected WeaponConfig weaponConf; // 武器配置
[SerializeField] protected RoleConfig conf; // 角配置
[SerializeField] protected RoleData data; // 角数据
[SerializeField] protected BaseAI moveAI; // 移动脚本
[SerializeField] protected ShootBaseAI weaponAI; // 武器脚本 (使用技能能,换成技能脚本)
protected bool isEnemy = false;
protected bool isStart = false;
//内部方法
//--------------------------------------------------------------------------
/** 碰撞检测
*
*/
protected virtual void CollisionDetection() { }
//override方法
//--------------------------------------------------------------------------
/** 技能生效
*
*/
public virtual void OnSkillStart()
{
object[] args = { this, , };
this.weaponAI.OnStart(args);
OnStart();
this.skilling = true;
}
/**
*
*/
public virtual void OnStart()
{
battle = GameMgr.instanse.BattleMgr();
isStart = true;
}
/** 每帧更新
* BattleDatas 驱动此心跳
*
*/
public virtual void OnUpdate(float deltaTime)
{
if (this.data == null) return;
if ( == null) return;
if (isStart == false) return;
// 碰撞检测
this.CollisionDetection();
// 技能倒计时
if (this.skilling == true) {
if(this.skillDelta > this.skillTime)
{
Debug.LogError(技能失效 );
this.skilling = false;
// 技能失效,恢复初始武器
this.OnStart();
} else {
this.skillDelta = deltaTime;
}
}
}
//公开接口
//--------------------------------------------------------------------------
/** 切换武器时,临时武器配置
*
*/
public void ReSetWeaponConfig(WeaponConfig wc, float time)
{
= wc;
this.skillTime = time;
this.skillDelta = 0f;
this.OnSkillStart();
}
/** 初始化 config 和 data(血量最大值等)
*
*/
public void SetConfig(RoleConfig c, WeaponConfig wc)
{
= c;
this.weaponConf = wc;
this.data = new RoleData();
this.data.SetData(c.GetInt());
}
/** 移动ai
*
*/
public void SetMoveAI(BaseAI move)
{
= move;
}
/** 武器ai
*
*/
public void SetWeaponAI(ShootBaseAI weapon)
{
this.weaponAI = weapon;
}
/** 是玩家还是敌人
*
*/
[SerializeField] protected bool enemy;
public bool IsEnemy
{
set { = value; }
get { return ; }
}
/** 死亡标记
*
*/
[SerializeField] protected bool dead;
public bool IsDead
{
set { this.dead = value; }
get { return this.dead; }
}
/** 角数据(血量)
*
*/
public RoleData GetData()
{
return data;
}
}
using System.Collecti;
using System.Collecti.Generic;
using UnityEngine;
using System;
/** 技能控制器
*
* 更新技能移动ai
*
* 碰撞检测
*
*
*/
[System.Serializable]
public class SkillCtr : SkillCtrBase
{
//内部属性
//--------------------------------------------------------------------------
//内部方法
//--------------------------------------------------------------------------
/** 碰撞检测
* 子弹ctr 有个属性IsEnemy,标记子弹是玩家还是敌人发出的.
* 如果是玩家发出的,子弹会检测所有敌人.
* 如果是敌人发出的,子弹会检测所有玩家.
*
*
*/
protected override void CollisionDetection()
{
// 检测玩家
var player = battle.(transform);
if(player == null)
{
Debug.LogError(没有检测到玩家);
return;
}
bool inScope = GlobalFunction.InFieldOfVision(position, transform.position);
if (inScope)
{
Debug.LogError(玩家获得技能 player.);
battle.battleDatas.SwitchToSkill(player, this.skillConf);
}
}
//override方法
//--------------------------------------------------------------------------
/** BattleDatas 创建时,完成初始化(OnStart)
*
*/
public override void OnStart()
{
var moveDir = Vector.down;
object[] args = { ,this., moveDir, null};
.OnStart(args);
base.OnStart();
}
//公开接口
//--------------------------------------------------------------------------
}
#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格
推荐阅读
留言与评论(共有 9 条评论) |
本站网友 太平二手房 | 0秒前 发表 |
移动到屏幕外会自动循环 | |
本站网友 大红袍的功效 | 29分钟前 发表 |
Vector.zero | |
本站网友 浦科特 | 23分钟前 发表 |
敌人的controller * 负责 | |
本站网友 杭州无痛人流多少钱 | 8分钟前 发表 |
不执行攻击行为 * */ private void UpdateWeaponTimeOnly(float deltaTime) { this.weaponAI.OnUpdateTimeOnly(deltaTime); } //override方法 //-------------------------------------------------------------------------- public override void OnStart() { object[] args = { this | |
本站网友 牟平区二手房 | 25分钟前 发表 |
仅负责战斗逻辑 * (开始 | |
本站网友 hello树先生 | 22分钟前 发表 |
//玩家不需要移动ai | |
本站网友 处和非处图片区别 | 23分钟前 发表 |
fireEffect | |
本站网友 钱龙旗舰 | 17分钟前 发表 |
移动ai引用 |