HeroControl.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import { BattleBase } from "../base/BattleBase";
  2. import { BattleEventManager } from "../base/BattleEventManager";
  3. import { BattleEventData_EnemyHurt, BattleEventData_HeroAction, BattleEventData_HeroAttack, BattleEventData_HeroAttackBullet, BattleEventHeroAction, BattleEventTarget, BattleEventType, HeroActionType, HurtEventState } from "../base/BattleEventUtil";
  4. import { BulletType } from "../data/BattleEnum";
  5. import { BattleUtil } from "../data/BattleUtil";
  6. import { BuffData } from "../data/BuffData";
  7. import { BulletData } from "../data/BulletData";
  8. import { EnemyData } from "../data/EnemyData";
  9. import { HeroData } from "../data/HeroData";
  10. import { MapData, PosData } from "../data/MapData";
  11. import { SkillControl } from "./SkillControl";
  12. //英雄控制类
  13. export class HeroControl extends BattleBase{
  14. map:MapData = null
  15. attackPosDataMap:Map<number,PosData> = null;
  16. enemyDataList:Array<EnemyData> = null;
  17. skillControl:SkillControl = null;
  18. battleEventManager: BattleEventManager;
  19. actionList:Array<BattleEventHeroAction> = []
  20. constructor(){
  21. super();
  22. this.battleEventManager = BattleEventManager.instance
  23. BattleEventManager.instance.addEvent(BattleEventTarget.HeroAction, this.heroActionEvent.bind(this), -1);
  24. }
  25. init(){
  26. this.map = MapData.GetInstance()
  27. this.attackPosDataMap = this.map.attackPosDataMap
  28. this.enemyDataList = this.map.enemyDataList
  29. this.skillControl = SkillControl.GetInstance()
  30. }
  31. //每次加一帧
  32. update(){
  33. this.actionList.forEach(actionData=>{
  34. let hero = this.attackPosDataMap.get(this.map.attackPosition[actionData.posIndex]).hero
  35. if(hero){
  36. this.checkAttack(hero)
  37. }
  38. })
  39. this.actionList = []
  40. this.attackPosDataMap.forEach((posData:PosData)=>{
  41. let hero = posData.hero
  42. if(hero){
  43. hero.coolDown -= 1
  44. if(hero.coolDown <= 0){
  45. let targetID = -1
  46. for(let i = 0;i<this.enemyDataList.length;i++){
  47. let enemy = this.enemyDataList[i]
  48. if(this.isInRadius(hero.position,enemy.position,hero.attackRadius)){
  49. targetID = enemy.ID
  50. hero.coolDown = hero.speed
  51. break;
  52. }
  53. }
  54. let eventData:BattleEventData_HeroAction = {
  55. eventType: BattleEventType.HeroAction,
  56. posID: hero.posID,
  57. targetID: targetID,
  58. action: HeroActionType.Normal
  59. }
  60. this.battleEventManager.fireEvent(BattleEventTarget.Update,eventData)
  61. }
  62. }
  63. })
  64. }
  65. checkAttack(hero:HeroData){
  66. let bAttack = false
  67. if(hero.attackType == BulletType.Single ){
  68. for(let i = 0;i<this.enemyDataList.length;i++){
  69. let enemy = this.enemyDataList[i]
  70. if(this.isInRadius(hero.position,enemy.position,hero.attackRadius)){
  71. this.attack(hero,enemy)
  72. bAttack = true
  73. break;
  74. }
  75. }
  76. }
  77. else if(hero.attackType == BulletType.Group){
  78. // let attackList:Array<EnemyData> = []
  79. for(let i = 0;i<this.enemyDataList.length;i++){
  80. let enemy = this.enemyDataList[i]
  81. if(this.isInRadius(hero.position,enemy.position,hero.attackRadius)){
  82. this.attack(hero,enemy)
  83. bAttack = true
  84. }
  85. }
  86. }
  87. else if(hero.attackType == BulletType.SingleBullet){
  88. //创建一颗子弹
  89. for(let i = 0;i<this.enemyDataList.length;i++){
  90. let enemy = this.enemyDataList[i]
  91. if(this.isInRadius(hero.position,enemy.position,hero.attackRadius)){
  92. let bulletData:BulletData = this.map.addBullet(1,hero,enemy)
  93. this.attackBullet(hero,enemy,bulletData)
  94. bAttack = true
  95. break;
  96. }
  97. }
  98. }
  99. else if(hero.attackType == BulletType.GroupBullet){
  100. }
  101. return bAttack
  102. }
  103. //攻击直接伤害
  104. attack(hero: HeroData, enemy: EnemyData) {
  105. //先普通攻击
  106. enemy.life -= hero.attackDamage
  107. let eventData:BattleEventData_HeroAttack = {
  108. eventType: BattleEventType.HeroAttack,
  109. posID: hero.posID,
  110. hurt: hero.attackDamage,
  111. status: HurtEventState.Normal,
  112. targetID: enemy.ID
  113. }
  114. this.battleEventManager.fireEvent(BattleEventTarget.Update,eventData)
  115. let addBuffData:BuffData[] = null
  116. //在加技能攻击
  117. hero.skillList.forEach(skillID => {
  118. addBuffData = this.skillControl.useSkill(hero, enemy, skillID)
  119. })
  120. }
  121. //攻击打出子弹
  122. attackBullet(hero: HeroData, enemy: EnemyData,bulletData:BulletData) {
  123. let eventData:BattleEventData_HeroAttackBullet = {
  124. eventType: BattleEventType.HeroAttackBullet,
  125. posID: hero.posID,
  126. targetID: enemy.ID,
  127. bulletID: bulletData.ID
  128. }
  129. this.battleEventManager.fireEvent(BattleEventTarget.Update,eventData)
  130. let addBuffData:BuffData[] = null
  131. //在加技能攻击
  132. hero.skillList.forEach(skillID => {
  133. addBuffData = this.skillControl.useSkill(hero, enemy, skillID)
  134. })
  135. bulletData.buffDataList = addBuffData
  136. }
  137. isInRadius(position: BattleUtil.Vector2, position1: BattleUtil.Vector2, attackRadius: number) {
  138. return BattleUtil.Vector2.Sub(position, position1).length() <= attackRadius
  139. // return true
  140. }
  141. heroActionEvent(eventData:BattleEventHeroAction){
  142. this.actionList.push(eventData)
  143. }
  144. }