HeroHit.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import Hero from "./Hero";
  2. import MonsterHit from "./MonsterHit";
  3. import Weapon from "./Weapon";
  4. const {ccclass, property} = cc._decorator;
  5. @ccclass
  6. export default class HeroHit extends cc.Component {
  7. private hero: Hero = null;
  8. public get Hero(): Hero
  9. {
  10. return this.hero;
  11. }
  12. private _material;
  13. public get WPos(){
  14. return this.hero.WPos;
  15. }
  16. onLoad () {
  17. this.hero = this.node.parent.getComponent(Hero);
  18. // 获取材质
  19. if (this.node.getComponent(cc.Sprite)) {
  20. this._material = this.node.getComponent(cc.Sprite).getMaterial(0);
  21. }
  22. // 设置材质对应的属性
  23. this._material.setProperty("uSize", this.realVal);
  24. let cols = this.getComponents(cc.BoxCollider);
  25. for (let i = 0; i < cols.length; i++) {
  26. const element = cols[i];
  27. if (element.tag == 2) {
  28. this.hurtCollider = element;
  29. }
  30. }
  31. Ticker.register(this.tickFun,this);
  32. }
  33. // 碰撞回调
  34. onCollisionEnter(other:cc.BoxCollider, self:cc.BoxCollider){
  35. if(self.tag == 1 && other.node.group == 'enemy' && other.tag == 2) {//击中怪物
  36. if (self.size.width ==0 || self.size.height ==0 ) { return;}
  37. // if (this.hero.bigWeaponAtk()) {
  38. // }
  39. }
  40. else if(self.tag == 2 && other.node.group == 'enemy' && other.tag == 1) {//受击
  41. if (other.size.width ==0 || other.size.height ==0 ) { return;}
  42. let enemyHit = other.node.getComponent(MonsterHit);
  43. let eWPosX = enemyHit.WPos.x;
  44. let hWposX = this.WPos.x;
  45. this.onHurt(eWPosX < hWposX);
  46. }
  47. }
  48. public onHurt(isEnemyInleft: boolean,weapon:Weapon = null) {
  49. if (this.hero.OnHurt(isEnemyInleft,weapon)) {
  50. this.realVal = 0;
  51. this.blinkCount = 2;
  52. this.isHurt = true;
  53. }
  54. }
  55. private isHurt:boolean = false;
  56. private realVal:number = 0;
  57. private maxVal:number = 5;
  58. private dir:number = 10;
  59. private blinkCount:number;
  60. tickFun(dt) {
  61. if (!this.isHurt) {return;}
  62. this.realVal+=dt * this.dir;
  63. if (this.realVal >= this.maxVal || this.realVal <= 0) {
  64. this.dir = -this.dir;
  65. if (this.blinkCount-- <= 0) {
  66. this.realVal = 0;
  67. this.isHurt = false;
  68. }
  69. }
  70. this._material.setProperty("uSize", this.realVal);
  71. }
  72. private hurtCollider:cc.BoxCollider;
  73. public dunCollider(){
  74. if (this.hurtCollider) {
  75. this.hurtCollider.size.height = 20;
  76. this.hurtCollider.offset.y = this.hurtCollider.size.height*0.5;
  77. }
  78. }
  79. public standCollider(){
  80. if (this.hurtCollider) {
  81. this.hurtCollider.size.height = 140;
  82. this.hurtCollider.offset.y = this.hurtCollider.size.height*0.5;
  83. }
  84. }
  85. public jumpCollider(){
  86. if (this.hurtCollider) {
  87. this.hurtCollider.size.height = 60;
  88. this.hurtCollider.offset.y =180;// this.hurtCollider.size.height;
  89. }
  90. }
  91. public superSkillCollider(){
  92. if (this.hurtCollider) {
  93. this.hurtCollider.size.height = 0;
  94. this.hurtCollider.offset.y = 0;
  95. }
  96. }
  97. protected onDestroy(): void {
  98. Ticker.unregister(this.tickFun,this);
  99. }
  100. }