LogicBattleField.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System;
  5. public class LogicBattleField : BattleField
  6. {
  7. public const float default_size_x = 8;
  8. public const float default_size_y = 0;
  9. public const float default_size_z = 10;
  10. LogicBattle mBattle;
  11. LevelItem mBattleInfo;
  12. LogicBattleFieldState mState;
  13. SpawnPointCfg mCurrentSpawnCfg = null;
  14. float mHalfHeight;
  15. public bool muteMusic = false;
  16. public bool playingBattleMusic = false;
  17. public float rushLeftTime = 0;
  18. private bool bKillBoss = false;
  19. public override bool killBoss
  20. {
  21. get { return bKillBoss; }
  22. set
  23. {
  24. if(bKillBoss !=value)
  25. {
  26. bKillBoss = value;
  27. if(mState!=null)
  28. mState.OnDataChanged();
  29. }
  30. }
  31. }
  32. public override float FloorY
  33. {
  34. get
  35. {
  36. return mCurrentSpawnCfg != null ? Math.Max(mCurrentSpawnCfg.Pos.y,0) : 0;
  37. }
  38. }
  39. public override bool IsIdleState
  40. {
  41. get { return CurrentState == LogicBattleFieldStateType.None; }
  42. }
  43. public override bool IsFightingState
  44. {
  45. get
  46. {
  47. return CurrentState == LogicBattleFieldStateType.Fighting || CurrentState == LogicBattleFieldStateType.EditorFighting;
  48. }
  49. }
  50. public override int CurrentFieldState
  51. {
  52. get { return (int)CurrentState; }
  53. }
  54. public LogicBattleField (LogicBattle battle, LevelItem bi)
  55. : base (battle,Vector3.zero, new Vector3 (default_size_x, default_size_y, default_size_z), Vector3.forward)
  56. {
  57. Init (battle, bi);
  58. }
  59. private void Init(LogicBattle battle, LevelItem bi)
  60. {
  61. mBattle = battle;
  62. mBattleInfo = bi;
  63. mHalfHeight = Size.y * 0.5f;
  64. FightingTime = 0;
  65. DisableWaitFighterReadyOnWin = true;
  66. if(battle!=null)
  67. {
  68. killBoss = battle.ChallengeBoss;
  69. battle.ChallengeBoss = false;
  70. }
  71. mEnemyActors.Clear();
  72. if (killBoss)
  73. {
  74. mEnemyActors.Add(bi.BossActor);
  75. if (bi.Monsters != null)
  76. {
  77. for (int idx = 0; idx < bi.Monsters.Count; idx++)
  78. {
  79. mEnemyActors.Add(bi.Monsters[idx]);
  80. }
  81. }
  82. }
  83. }
  84. public LogicBattle Battle { get { return mBattle; } }
  85. public override LevelItem BattleInfo { get { return mBattleInfo; } }
  86. public LogicBattleScene Scene { get { return mBattle.BattleScene; } }
  87. public int BattleIdx { get { return mBattle.BattleIdx; } }
  88. public bool DisableWaitFighterReadyOnWin { get; set; }
  89. public LogicBattleFieldStateType CurrentState {
  90. get { return mState != null ? mState.State : LogicBattleFieldStateType.None; }
  91. set {
  92. if (CurrentState == value)
  93. return;
  94. int oldState = (int)CurrentState;
  95. if (mState != null)
  96. mState.OnLeave ();
  97. mState = LogicBattleFieldState.Create (this, value);
  98. mCurrentStateFrame = 0;
  99. if (mState != null)
  100. {
  101. mState.OnEnter ();
  102. }
  103. }
  104. }
  105. private Vector3 mFieldCenter = Vector3.zero;
  106. public override Vector3 FieldCenter
  107. {
  108. get
  109. {
  110. return mFieldCenter;
  111. }
  112. set
  113. {
  114. mFieldCenter = value;
  115. }
  116. }
  117. public override Vector3 FieldFocus
  118. {
  119. get
  120. {
  121. if (Fighters.Count == 0)
  122. return mCurrentSpawnCfg.Pos;
  123. Vector3 min = Vector3.zero;
  124. Vector3 max = Vector3.zero;
  125. bool hasSet = false;
  126. for (int idx = 0; idx < Fighters.Count; idx++)
  127. {
  128. Fighter f = Fighters[idx];
  129. if(f.IsFront)
  130. {
  131. if (!hasSet)
  132. {
  133. min = f.Position;
  134. max = f.Position;
  135. hasSet = true;
  136. }
  137. else
  138. {
  139. min = Vector3.Min(f.Position, min);
  140. max = Vector3.Max(f.Position, max);
  141. }
  142. }
  143. }
  144. return 0.5f * (min + max) + Vector3.up * 1.0f;
  145. }
  146. }
  147. public Vector3 MosterFighterFocus
  148. {
  149. get
  150. {
  151. if (EnemyFighters.Count == 0)
  152. return mCurrentSpawnCfg.Pos;
  153. Vector3 min = Vector3.zero;
  154. Vector3 max = Vector3.zero;
  155. bool hasSet = false;
  156. for (int idx = 0; idx < EnemyFighters.Count; idx++)
  157. {
  158. Fighter f = EnemyFighters[idx];
  159. if (!hasSet)
  160. {
  161. min = f.Position;
  162. max = f.Position;
  163. hasSet = true;
  164. }
  165. else
  166. {
  167. min = Vector3.Min(f.Position, min);
  168. max = Vector3.Max(f.Position, max);
  169. }
  170. }
  171. return 0.5f * (min + max) + Vector3.up * 1.0f;
  172. }
  173. }
  174. public override Vector3 FighterForward
  175. {
  176. get
  177. {
  178. if (EnemyFighters.Count == 0)
  179. return Vector3.forward;
  180. return EnemyFighters[0].Ctrl != null ? EnemyFighters[0].Ctrl.transform.forward : EnemyFighters[0].Forward;
  181. }
  182. }
  183. public override bool CanKillBoss { get { return CurrentState <= LogicBattleFieldStateType.Search; } }
  184. private Vector3 mPreSpawnPos;
  185. public Vector3 PreSpawnPos
  186. {
  187. get { return mPreSpawnPos; }
  188. }
  189. public SpawnPointCfg CurrentWavePoint
  190. {
  191. get { return mCurrentSpawnCfg; }
  192. }
  193. public override string Name
  194. {
  195. get { return "LogicBattleField"; }
  196. }
  197. public void Start ()
  198. {
  199. Result = FightingResult.None;
  200. FightingTime = 0;
  201. CurrentState = LogicBattleFieldStateType.DialogBeforeSearch;
  202. }
  203. public void BeginReplay()
  204. {
  205. Result = FightingResult.None;
  206. FightingTime = 0;
  207. CurrentState = LogicBattleFieldStateType.Search;
  208. }
  209. public override void Update (float deltaTime)
  210. {
  211. base.Update(deltaTime);
  212. if (mState != null)
  213. {
  214. #if PROFILE
  215. UnityEngine.Profiling.Profiler.BeginSample("LogicBattleField update:" + mState.State);
  216. #endif
  217. mState.Update (deltaTime);
  218. #if PROFILE
  219. UnityEngine.Profiling.Profiler.EndSample();
  220. #endif
  221. }
  222. }
  223. public override void AddFighter (Fighter fighter)
  224. {
  225. base.AddFighter(fighter);
  226. if (mBattle != null)
  227. mBattle.FighterMgr.AddFighterToBattleField(fighter);
  228. if (fighter.IsTeamMember)
  229. {
  230. SortTeamFighters();
  231. }
  232. }
  233. public override void RemoveFighter (Fighter fighter)
  234. {
  235. base.RemoveFighter(fighter);
  236. if (mBattle != null)
  237. mBattle.FighterMgr.RemoveFighterFromBattleField(fighter);
  238. if (fighter.IsTeamMember)
  239. {
  240. SortTeamFighters();
  241. }
  242. }
  243. public bool bIsAllFighterIdle()
  244. {
  245. if (mBattle == null)
  246. return false;
  247. for (int i = 0; i < mFighters.Count; i++)
  248. {
  249. Fighter fighter = mFighters[i];
  250. if (!fighter.IsSpawned || (!fighter.IsDisposed && fighter.IsAlive))
  251. {
  252. if(!fighter.IsIdle)
  253. {
  254. return false;
  255. }
  256. }
  257. }
  258. return true;
  259. }
  260. public override void GetFieldSummonPos(int posVal, out Vector3 pos, out Vector3 rot)
  261. {
  262. if (CurrentWavePoint == null)
  263. {
  264. pos = Vector3.zero;
  265. rot = Vector3.zero;
  266. return;
  267. }
  268. MonsterSpawnLoc loc = CurrentWavePoint.PointList[posVal - 1];
  269. pos = loc.pos;
  270. rot = loc.rot;
  271. }
  272. public bool HasTeamMemberDead()
  273. {
  274. if (mTeamFighters == null) return false;
  275. for(int idx =0; idx < mTeamFighters.Count;idx++)
  276. {
  277. if (!mTeamFighters[idx].IsAlive) return true;
  278. }
  279. return false;
  280. }
  281. public void RemoveMonsters()
  282. {
  283. for(int idx =0; idx < mEnemyFighters.Count;idx++)
  284. {
  285. mFighters.Remove(mEnemyFighters[idx]);
  286. mEnemyFighters[idx].Dispose();
  287. }
  288. mEnemyFighters.Clear();
  289. }
  290. public void SpawnMonster()
  291. {
  292. RecordSpawnCfg();
  293. Vector3 forward = Vector3.forward;
  294. Vector3 rot = mCurrentSpawnCfg.Rot;
  295. rot.y = 180 + rot.y;
  296. for(int idx =0; idx < mCurrentSpawnCfg.PointList.Count;idx++)
  297. {
  298. MonsterSpawnLoc loc = mCurrentSpawnCfg.PointList[idx];
  299. if(loc.npcInstanceId > 0)
  300. {
  301. Fighter fighter = mBattle.FighterMgr.GetFighterByID(loc.npcInstanceId,eTeamType.Enemy);
  302. fighter.Actor.SetLevel(BattleInfo.MonsterLv);
  303. fighter.Spawn(loc.pos, Vector3.forward, Quaternion.Euler(loc.rot));
  304. //fighter.SetRotation(Quaternion.Euler(loc.rot));
  305. forward = fighter.Ctrl.transform.forward;
  306. }
  307. }
  308. Vector3 center = mCurrentSpawnCfg.Pos + forward * mCurrentSpawnCfg.ActorReadyDist;
  309. mBattle.BattleScene.ActorBornPoint.CalcNextBattleFieldPoints(center, rot);
  310. }
  311. public void RecordSpawnCfg()
  312. {
  313. mCurrentSpawnCfg = mBattle.BattleScene.MapMonsterSpawnPoint;
  314. }
  315. public void SpawnBoss()
  316. {
  317. Battle.BattleScene.SetEffectVisible(true);
  318. mBattle.StartStatistics();
  319. if (mBattle.IsPlayRecord)
  320. {
  321. mPreSpawnPos = mCurrentSpawnCfg.Pos;
  322. mCurrentSpawnCfg = mBattle.BattleScene.BossSpawnPoint;
  323. mBattle.Recorder.ProcessFrameRecord(BattleRecorder.RecordType.FighterSpawn);
  324. mBattle.BattleScene.ActorBornPoint.CalcNextBattleFieldPoints(mCurrentSpawnCfg.TransferPos, mCurrentSpawnCfg.TransferRot);
  325. mBattle.CheckTeamPlayerActors();
  326. EventMgr.DispatchEvent<Fighter>(new CoreEvent<Fighter>(ECoreEventType.EID_BOSS_SPAWNED, EnemyFighters[0]));
  327. }
  328. else
  329. {
  330. mPreSpawnPos = mCurrentSpawnCfg.Pos;
  331. mCurrentSpawnCfg = mBattle.BattleScene.BossSpawnPoint;
  332. ActorData ad = BattleInfo.BossActor;
  333. Fighter fighter = Battle.FighterMgr.GetFighterByInstanceID(ad.FighterInstanceId);
  334. if (fighter != null)
  335. {
  336. MonsterSpawnLoc loc = mCurrentSpawnCfg.PointList[ad.PositionValue - 1];
  337. fighter.Spawn(loc.pos, Vector3.forward, Quaternion.Euler(loc.rot));
  338. //fighter.SetRotation(Quaternion.Euler(loc.rot));
  339. }
  340. if (BattleInfo.Monsters != null)
  341. {
  342. for (int idx = 0; idx < BattleInfo.Monsters.Count; idx++)
  343. {
  344. ActorData npcAd = BattleInfo.Monsters[idx];
  345. Fighter f = Battle.FighterMgr.GetFighterByInstanceID(npcAd.FighterInstanceId);
  346. if (f != null)
  347. {
  348. MonsterSpawnLoc loc = mCurrentSpawnCfg.PointList[npcAd.PositionValue - 1];
  349. f.Actor.SetLevel(BattleInfo.MonsterLv);
  350. f.Spawn(loc.pos, Vector3.forward, Quaternion.Euler(loc.rot));
  351. //f.SetRotation(Quaternion.Euler(loc.rot));
  352. }
  353. }
  354. }
  355. mBattle.BattleScene.ActorBornPoint.CalcNextBattleFieldPoints(mCurrentSpawnCfg.TransferPos, mCurrentSpawnCfg.TransferRot);
  356. mBattle.CheckTeamPlayerActors();
  357. EventMgr.DispatchEvent<Fighter>(new CoreEvent<Fighter>(ECoreEventType.EID_BOSS_SPAWNED, fighter));
  358. }
  359. }
  360. public override void PeaceEnd()
  361. {
  362. if (bKillBoss)
  363. {
  364. Result = FightingResult.Peace;
  365. if (CurrentState == LogicBattleFieldStateType.Failed || CurrentState == LogicBattleFieldStateType.Win)
  366. {
  367. CurrentState = LogicBattleFieldStateType.End;
  368. CurrentState = LogicBattleFieldStateType.None;
  369. }
  370. else if (CurrentState == LogicBattleFieldStateType.End)
  371. {
  372. BattleMgr.Instance.OnBattleForceStop(killBoss);
  373. CurrentState = LogicBattleFieldStateType.None;
  374. }
  375. else
  376. {
  377. CurrentState = LogicBattleFieldStateType.End;
  378. CurrentState = LogicBattleFieldStateType.None;
  379. }
  380. }
  381. else
  382. {
  383. Result = FightingResult.Peace;
  384. if (CurrentState == LogicBattleFieldStateType.Failed || CurrentState == LogicBattleFieldStateType.Win)
  385. {
  386. CurrentState = LogicBattleFieldStateType.End;
  387. CurrentState = LogicBattleFieldStateType.None;
  388. }
  389. else if (CurrentState == LogicBattleFieldStateType.End)
  390. {
  391. BattleMgr.Instance.OnBattleForceStop(killBoss);
  392. CurrentState = LogicBattleFieldStateType.None;
  393. }
  394. else if (CurrentState == LogicBattleFieldStateType.None)
  395. {
  396. }
  397. else
  398. {
  399. CurrentState = LogicBattleFieldStateType.End;
  400. CurrentState = LogicBattleFieldStateType.None;
  401. }
  402. }
  403. }
  404. public void DropItems()
  405. {
  406. if (!IsWin) return;
  407. int gold = BattleInfo.Zeny;
  408. int dropCnt = 0;
  409. if (gold > 0)
  410. {
  411. dropCnt = GlobalConfig.Instance.GetConfigIntValue(GlobalConfig.c_default_dropmoney_cnt);
  412. }
  413. if (BattleInfo.DropItems != null)
  414. {
  415. dropCnt += BattleInfo.DropItems.Length;
  416. }
  417. Vector3 camPos = BattleCamera.Instance.CamPosition;
  418. Vector3[] posList = new Vector3[dropCnt];
  419. for (int idx = 0; idx < dropCnt; idx++)
  420. {
  421. Vector3 pos = MosterFighterFocus;
  422. pos.x += UnityEngine.Random.Range(-CurrentWavePoint.HorSpace, CurrentWavePoint.HorSpace);
  423. pos.z += UnityEngine.Random.Range(-2 * CurrentWavePoint.VerSpace, CurrentWavePoint.VerSpace);
  424. posList[idx] = pos;
  425. }
  426. EventMgr.DispatchEvent<Vector3[]>(new CoreEvent<Vector3[]>(ECoreEventType.EID_DROP_ITEMS, posList));
  427. }
  428. }