Files
jdt-fish-client/assets/FishSingle/script/game/manager/CannonManager.ts
2024-05-01 19:13:01 +08:00

73 lines
1.8 KiB
TypeScript

import {
Component,
EventMouse,
Node,
Sprite,
SpriteFrame,
UITransform,
Vec2,
Vec3,
_decorator,
} from 'cc'
import MathUtils from '../../engine/utils/MathUtils'
const { ccclass, property } = _decorator
// 炮塔管理类
@ccclass('CannonManager')
export default class CannonManager extends Component {
public static instance: CannonManager = null
@property({ type: Node })
private view: Node | null = null
@property({ type: [SpriteFrame] })
private cannonSpriteFrame: Array<SpriteFrame> | [] = []
// 炮塔倍数
public cannonType: number = 1
private _vec3Cache: Vec3
onLoad() {
this._vec3Cache = new Vec3()
CannonManager.instance = this
this.node.parent.on(Node.EventType.MOUSE_MOVE, this.onMeMove.bind(this))
this.refreshCannon()
}
// 炮塔移动
private onMeMove(event: EventMouse) {
this.rotateCannon(event.getUILocation())
}
// 炮塔旋转
public rotateCannon(uilocation: Vec2) {
const location = uilocation
this._vec3Cache.x = location.x
this._vec3Cache.y = location.y
this._vec3Cache.z = 0
const tran = this.node.getComponent(UITransform)
tran.convertToNodeSpaceAR(this._vec3Cache, this._vec3Cache)
const localTouch: Vec2 = new Vec2(this._vec3Cache.x, this._vec3Cache.y)
this.view.getPosition(this._vec3Cache)
const rad: number = MathUtils.p2pRad(new Vec2(this._vec3Cache.x, this._vec3Cache.y), localTouch)
const rot: number = MathUtils.radiansToDegrees(rad)
this.view.angle = rot - 90
}
// 刷新炮塔
public refreshCannon() {
this.view.getComponent(Sprite).spriteFrame = this.cannonSpriteFrame[this.cannonType - 1]
}
// 获取炮塔位置
public getCannonPosition() {
return this.view.getPosition()
}
onDestroy() {
CannonManager.instance = null
}
}