HeroControl.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import { BattleBase } from "../base/BattleBase";
  2. import { BattleEventManager } from "../base/BattleEventManager";
  3. import { BattleEventData_EnemyHurt, BattleEventData_HeroAttack, BattleEventData_HeroAttackBullet, BattleEventTarget, BattleEventType, 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. init(){
  20. this.battleEventManager = BattleEventManager.instance
  21. this.map = MapData.GetInstance()
  22. this.attackPosDataMap = this.map.attackPosDataMap
  23. this.enemyDataList = this.map.enemyDataList
  24. this.skillControl = SkillControl.GetInstance()
  25. }
  26. //每次加一帧
  27. update(){
  28. this.attackPosDataMap.forEach((posData:PosData)=>{
  29. let hero = posData.hero
  30. if(hero){
  31. hero.coolDown -= 1
  32. if(hero.coolDown <= 0){
  33. if(this.checkAttack(hero)){
  34. hero.coolDown = hero.speed
  35. }
  36. else{
  37. let eventData:BattleEventData_HeroAttack = {
  38. eventType: BattleEventType.HeroAttack,
  39. posID: hero.posID,
  40. hurt: 0,
  41. status: HurtEventState.Normal,
  42. targetID: BattleUtil.IndexID_Init
  43. }
  44. this.battleEventManager.fireEvent(BattleEventTarget.Update,eventData)
  45. }
  46. }
  47. }
  48. })
  49. }
  50. checkAttack(hero:HeroData){
  51. let bAttack = false
  52. if(hero.attackType == BulletType.Single ){
  53. for(let i = 0;i<this.enemyDataList.length;i++){
  54. let enemy = this.enemyDataList[i]
  55. if(this.isInRadius(hero.position,enemy.position,hero.attackRadius)){
  56. this.attack(hero,enemy)
  57. bAttack = true
  58. break;
  59. }
  60. }
  61. }
  62. else if(hero.attackType == BulletType.Group){
  63. // let attackList:Array<EnemyData> = []
  64. for(let i = 0;i<this.enemyDataList.length;i++){
  65. let enemy = this.enemyDataList[i]
  66. if(this.isInRadius(hero.position,enemy.position,hero.attackRadius)){
  67. this.attack(hero,enemy)
  68. bAttack = true
  69. }
  70. }
  71. }
  72. else if(hero.attackType == BulletType.SingleBullet){
  73. //创建一颗子弹
  74. for(let i = 0;i<this.enemyDataList.length;i++){
  75. let enemy = this.enemyDataList[i]
  76. if(this.isInRadius(hero.position,enemy.position,hero.attackRadius)){
  77. let bulletData:BulletData = this.map.addBullet(1,hero,enemy)
  78. this.attackBullet(hero,enemy,bulletData)
  79. bAttack = true
  80. break;
  81. }
  82. }
  83. }
  84. else if(hero.attackType == BulletType.GroupBullet){
  85. }
  86. return bAttack
  87. }
  88. //攻击直接伤害
  89. attack(hero: HeroData, enemy: EnemyData) {
  90. //先普通攻击
  91. enemy.life -= hero.attackDamage
  92. let eventData:BattleEventData_HeroAttack = {
  93. eventType: BattleEventType.HeroAttack,
  94. posID: hero.posID,
  95. hurt: hero.attackDamage,
  96. status: HurtEventState.Normal,
  97. targetID: enemy.ID
  98. }
  99. this.battleEventManager.fireEvent(BattleEventTarget.Update,eventData)
  100. let addBuffData:BuffData[] = null
  101. //在加技能攻击
  102. hero.skillList.forEach(skillID => {
  103. addBuffData = this.skillControl.useSkill(hero, enemy, skillID)
  104. })
  105. }
  106. //攻击打出子弹
  107. attackBullet(hero: HeroData, enemy: EnemyData,bulletData:BulletData) {
  108. let eventData:BattleEventData_HeroAttackBullet = {
  109. eventType: BattleEventType.HeroAttackBullet,
  110. posID: hero.posID,
  111. targetID: enemy.ID,
  112. bulletID: bulletData.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. bulletData.buffDataList = addBuffData
  121. }
  122. isInRadius(position: BattleUtil.Vector2, position1: BattleUtil.Vector2, attackRadius: number) {
  123. return BattleUtil.Vector2.Sub(position, position1).length() <= attackRadius
  124. // return true
  125. }
  126. }