Bullet.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import WealthMng from "../../manager/WealthMng";
  2. import Random from "../../utils/Random";
  3. const { ccclass, property } = cc._decorator;
  4. @ccclass
  5. export default class Bullet extends cc.Component {
  6. target: cc.Node = null;
  7. bulletSpeed: number = 1500;
  8. masterlv: number = 1;
  9. onLoad() {
  10. }
  11. update(dt) {
  12. if (this.target != null) {
  13. let targetPos: cc.Vec2 = cc.v2(this.target.getPosition().x,this.target.getPosition().y-80);// this.target.getPosition();
  14. let bulletPos: cc.Vec2 = this.node.getPosition();
  15. let normalizeVec: cc.Vec2 = targetPos.subtract(bulletPos).normalize();
  16. this.node.x += normalizeVec.x * this.bulletSpeed * dt;
  17. this.node.y += normalizeVec.y * this.bulletSpeed * dt;
  18. // 角度变化以y轴正方向为起点,逆时针角度递增
  19. //this.node.angle = cc.v2(0, 1).signAngle(normalizeVec) * 180 / Math.PI;
  20. this.node.angle += 10;
  21. let rect = this.target.getBoundingBox();
  22. //let len = this.target.getPosition().sub(this.node.getPosition()).len();
  23. let len = cc.v2(this.target.getPosition().x,this.target.getPosition().y-80).sub(this.node.getPosition()).len();
  24. //console.log("Len:"+len);
  25. if (len <= 50) {
  26. this.HitTheTarget();
  27. WealthMng.Ins.GetDamage(1, this.masterlv);
  28. WealthMng.Ins.ShootEft(this.node.getPosition());
  29. }
  30. //if (rect.contains(bulletPos)) this.HitTheTarget();
  31. //this.node.lookAt(this.target.position);
  32. //this.node.setPosition(this.node.right.scale(cc.v3(1, 2, 1)));
  33. } else {
  34. this.HitTheTarget();
  35. }
  36. }
  37. Shoot(target: cc.Node, masterlv: number) {
  38. this.masterlv = masterlv;
  39. this.node.angle = Random.Range(0, 360);
  40. this.target = target;
  41. }
  42. HitTheTarget() {
  43. this.node.destroy();
  44. this.destroy();
  45. }
  46. }