HeroControl.ts 6.4 KB

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