优化若干代码
This commit is contained in:
@@ -1,66 +1,58 @@
|
||||
import {
|
||||
_decorator,
|
||||
Component,
|
||||
Node,
|
||||
SpriteFrame,
|
||||
Event,
|
||||
EventMouse,
|
||||
Sprite,
|
||||
Vec2,
|
||||
UITransform,
|
||||
Vec3,
|
||||
} from 'cc'
|
||||
const { ccclass, property } = _decorator
|
||||
|
||||
import { _decorator, Component, EventMouse, Node, Sprite, SpriteFrame, UITransform, Vec2, Vec3 } 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
|
||||
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())
|
||||
}
|
||||
onLoad() {
|
||||
this._vec3Cache = new Vec3()
|
||||
CannonManager.instance = this
|
||||
this.node.parent.on(Node.EventType.MOUSE_MOVE, this.onMeMove.bind(this))
|
||||
this.refreshCannon()
|
||||
}
|
||||
|
||||
public rotateCannon(uilocation: Vec2) {
|
||||
let location = uilocation
|
||||
this._vec3Cache.x = location.x
|
||||
this._vec3Cache.y = location.y
|
||||
this._vec3Cache.z = 0
|
||||
let tran = this.node.getComponent(UITransform)
|
||||
tran.convertToNodeSpaceAR(this._vec3Cache, this._vec3Cache)
|
||||
private onMeMove(event: EventMouse) {
|
||||
this.rotateCannon(event.getUILocation())
|
||||
}
|
||||
|
||||
let localTouch: Vec2 = new Vec2(this._vec3Cache.x, this._vec3Cache.y)
|
||||
this.view.getPosition(this._vec3Cache)
|
||||
let rad: number = MathUtils.p2pRad(
|
||||
new Vec2(this._vec3Cache.x, this._vec3Cache.y),
|
||||
localTouch
|
||||
)
|
||||
let rot: number = MathUtils.radiansToDegrees(rad)
|
||||
this.view.angle = rot - 90
|
||||
}
|
||||
public rotateCannon(uilocation: Vec2) {
|
||||
let location = uilocation
|
||||
this._vec3Cache.x = location.x
|
||||
this._vec3Cache.y = location.y
|
||||
this._vec3Cache.z = 0
|
||||
let tran = this.node.getComponent(UITransform)
|
||||
tran.convertToNodeSpaceAR(this._vec3Cache, this._vec3Cache)
|
||||
|
||||
public refreshCannon() {
|
||||
this.view.getComponent(Sprite).spriteFrame =
|
||||
this.cannonSpriteFrame[this.cannonType - 1]
|
||||
}
|
||||
public getCannonPosition() {
|
||||
return this.view.getPosition()
|
||||
}
|
||||
onDestroy() {
|
||||
CannonManager.instance = null
|
||||
}
|
||||
let localTouch: Vec2 = new Vec2(this._vec3Cache.x, this._vec3Cache.y)
|
||||
this.view.getPosition(this._vec3Cache)
|
||||
let rad: number = MathUtils.p2pRad(
|
||||
new Vec2(this._vec3Cache.x, this._vec3Cache.y),
|
||||
localTouch
|
||||
)
|
||||
let 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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user