106 lines
3.1 KiB
TypeScript
Raw Permalink Normal View History

2022-12-08 21:14:02 +08:00
import { Tween, tween, Vec3, _decorator } from "cc";
import { EntityManager } from "../../Base/EntityManager";
import { EntityTypeEnum, IBullet, IVec2 } from "../../Common";
import { EntityStateEnum, EventEnum } from "../../Enum";
import DataManager from "../../Global/DataManager";
import EventManager from "../../Global/EventManager";
import ObjectPoolManager from "../../Global/ObjectPoolManager";
import { rad2Angle } from "../../Utils";
import { ExplosionManager } from "../Explosion/ExplosionManager";
import { BulletStateMachine } from "./BulletStateMachine";
const { ccclass } = _decorator;
@ccclass("BulletManager")
2022-12-03 20:06:57 +08:00
export class BulletManager extends EntityManager implements IBullet {
2022-11-27 23:23:47 +08:00
//静态数据
2022-12-08 21:14:02 +08:00
id: number;
owner: number;
type: EntityTypeEnum;
2022-11-27 23:23:47 +08:00
//动态数据
2022-12-08 21:14:02 +08:00
position: IVec2;
direction: IVec2;
2022-11-27 23:23:47 +08:00
2022-12-08 21:14:02 +08:00
private angle: number;
private tw: Tween<any>;
private targetPos: Vec3;
2022-11-27 23:23:47 +08:00
init({ id, owner, type }: IBullet) {
2022-12-08 21:14:02 +08:00
this.id = id;
this.owner = owner;
this.type = type;
this.fsm = this.addComponent(BulletStateMachine);
this.fsm.init(type);
this.state = EntityStateEnum.Idle;
2022-11-27 23:23:47 +08:00
2022-12-08 21:14:02 +08:00
this.node.active = false;
this.targetPos = undefined;
this.angle = undefined;
2022-11-27 23:23:47 +08:00
2022-12-08 21:14:02 +08:00
EventManager.Instance.on(EventEnum.ExplosionBorn, this.handleExplosion, this);
2022-11-27 23:23:47 +08:00
}
handleExplosion(id: number, { x, y }: IVec2) {
if (this.id !== id) {
2022-12-08 21:14:02 +08:00
return;
2022-11-27 23:23:47 +08:00
}
2022-12-08 21:14:02 +08:00
const explosion = ObjectPoolManager.Instance.get(EntityTypeEnum.Explosion);
const explosionManager = explosion.getComponent(ExplosionManager) || explosion.addComponent(ExplosionManager);
2022-11-27 23:23:47 +08:00
explosionManager.init(EntityTypeEnum.Explosion, {
2022-12-08 21:14:02 +08:00
x,
y,
});
2022-11-27 23:23:47 +08:00
2022-12-08 21:14:02 +08:00
EventManager.Instance.off(EventEnum.ExplosionBorn, this.handleExplosion, this);
ObjectPoolManager.Instance.ret(this.node);
DataManager.Instance.bulletMap.delete(this.id);
2022-11-27 23:23:47 +08:00
}
render(data: IBullet) {
2022-12-08 21:14:02 +08:00
this.renderPosition(data);
this.renderDirection(data);
2022-11-27 23:23:47 +08:00
}
renderPosition(data: IBullet) {
2022-12-08 21:14:02 +08:00
const newPos = new Vec3(data.position.x, data.position.y);
2022-12-06 23:10:03 +08:00
if (!this.targetPos) {
2022-12-08 21:14:02 +08:00
this.node.active = true;
this.node.setPosition(newPos);
this.targetPos = new Vec3(newPos);
2022-12-05 23:04:17 +08:00
} else if (!this.targetPos.equals(newPos)) {
2022-12-08 21:14:02 +08:00
this.tw?.stop();
this.node.setPosition(this.targetPos);
this.targetPos.set(newPos);
this.tw = tween(this.node)
.to(0.1, {
position: this.targetPos,
})
.start();
2022-12-05 23:04:17 +08:00
}
// this.node.setPosition(data.position.x, data.position.y)
2022-11-27 23:23:47 +08:00
}
renderDirection(data: IBullet) {
if (this.angle === undefined) {
2022-12-08 21:14:02 +08:00
const { x, y } = data.direction;
const side = Math.sqrt(x * x + y * y);
this.angle = x > 0 ? rad2Angle(Math.asin(y / side)) : rad2Angle(Math.asin(-y / side)) + 180;
2022-11-27 23:23:47 +08:00
}
2022-12-08 21:14:02 +08:00
this.node.setRotationFromEuler(0, 0, this.angle);
2022-11-27 23:23:47 +08:00
// let angle: number, sign: number
// if (x !== 0) {
// angle = x > 0 ? rad2Angle(Math.atan(y / x)) : rad2Angle(Math.atan(-y / x)) + 180
// sign = x > 0 ? 1 : -1
// } else {
// angle = rad2Angle(Math.PI / 2)
// sign = y > 0 ? 1 : -1
// }
// this.node.setRotationFromEuler(0, 0, sign * angle)
}
}