2022-11-27 23:23:47 +08:00

86 lines
2.6 KiB
TypeScript

import { _decorator, Animation, AnimationClip, Node, Sprite, Vec3, Vec2, RigidBody2D, instantiate } from 'cc'
import { EntityManager } from '../../Base/EntityManager'
import { EntityTypeEnum, EntityStateEnum, EventEnum, PrefabPathEnum } from '../../Enum'
import DataManager, { IBullet, IVec2 } 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, property } = _decorator
@ccclass('BulletManager')
export class BulletManager extends EntityManager {
//静态数据
id = 1
owner = 1
type = EntityTypeEnum.Bullet1
//动态数据
position: IVec2
direction: IVec2
private angle: number
init({ id, owner, type }: IBullet) {
this.id = id
this.owner = owner
this.type = type
this.fsm = this.addComponent(BulletStateMachine)
this.fsm.init(type)
this.state = EntityStateEnum.Idle
this.node.active = false
EventManager.Instance.on(EventEnum.Explosion, this.handleExplosion, this)
}
onDisable() {
EventManager.Instance.off(EventEnum.Explosion, this.handleExplosion, this)
}
handleExplosion(id: number, { x, y }: IVec2) {
if (this.id !== id) {
return
}
const explosion = ObjectPoolManager.Instance.getPoolObject(EntityTypeEnum.Explosion)
const explosionManager = explosion.getComponent(ExplosionManager) || explosion.addComponent(ExplosionManager)
explosionManager.init(EntityTypeEnum.Explosion, {
x, y,
})
ObjectPoolManager.Instance.returnPoolObject(this.node)
}
render(data: IBullet) {
this.node.active = true
this.renderPosition(data)
this.renderDirection(data)
}
renderPosition(data: IBullet) {
this.node.setPosition(data.position.x, data.position.y)
}
renderDirection(data: IBullet) {
if (this.angle === undefined) {
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
}
this.node.setRotationFromEuler(0, 0, this.angle)
// 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)
}
}