| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- import WealthMng from "../../manager/WealthMng";
- import Random from "../../utils/Random";
- const { ccclass, property } = cc._decorator;
- @ccclass
- export default class Bullet extends cc.Component {
- target: cc.Node = null;
- bulletSpeed: number = 1500;
- masterlv: number = 1;
- onLoad() {
- }
- update(dt) {
- if (this.target != null) {
- let targetPos: cc.Vec2 = cc.v2(this.target.getPosition().x,this.target.getPosition().y-80);// this.target.getPosition();
- let bulletPos: cc.Vec2 = this.node.getPosition();
- let normalizeVec: cc.Vec2 = targetPos.subtract(bulletPos).normalize();
- this.node.x += normalizeVec.x * this.bulletSpeed * dt;
- this.node.y += normalizeVec.y * this.bulletSpeed * dt;
- // 角度变化以y轴正方向为起点,逆时针角度递增
- //this.node.angle = cc.v2(0, 1).signAngle(normalizeVec) * 180 / Math.PI;
- this.node.angle += 10;
- let rect = this.target.getBoundingBox();
- //let len = this.target.getPosition().sub(this.node.getPosition()).len();
- let len = cc.v2(this.target.getPosition().x,this.target.getPosition().y-80).sub(this.node.getPosition()).len();
- //console.log("Len:"+len);
- if (len <= 50) {
- this.HitTheTarget();
- WealthMng.Ins.GetDamage(1, this.masterlv);
- WealthMng.Ins.ShootEft(this.node.getPosition());
- }
- //if (rect.contains(bulletPos)) this.HitTheTarget();
- //this.node.lookAt(this.target.position);
- //this.node.setPosition(this.node.right.scale(cc.v3(1, 2, 1)));
- } else {
- this.HitTheTarget();
- }
- }
- Shoot(target: cc.Node, masterlv: number) {
- this.masterlv = masterlv;
- this.node.angle = Random.Range(0, 360);
- this.target = target;
- }
- HitTheTarget() {
- this.node.destroy();
- this.destroy();
- }
- }
|