优化若干代码

This commit is contained in:
2024-04-17 20:15:52 +08:00
parent 54580cc1b2
commit 58cc2e3b82
66 changed files with 5821 additions and 4019 deletions

View File

@@ -1,21 +1,16 @@
import {
_decorator,
Component,
Prefab,
NodePool,
Event,
Node,
Vec3,
Vec2,
EventTouch,
UITransform,
instantiate,
sys,
error,
_decorator,
Component,
EventTouch,
instantiate,
Node,
NodePool,
Prefab,
sys,
UITransform,
Vec2,
Vec3
} from 'cc'
const { ccclass, property } = _decorator
import { Logger } from '../../engine/utils/Logger'
import FishBulletBase from '../../../fish/script/FishBulletBase'
import MathUtils from '../../engine/utils/MathUtils'
import CannonManager from './CannonManager'
@@ -25,136 +20,140 @@ import GameMusicHelper from '../utils/GameMusicHelper'
import FishUI from '../../../fish/script/FishUI'
import CommonTips from '../../engine/uicomponent/CommonTips'
const { ccclass, property } = _decorator
@ccclass('BulletManager')
export default class BulletManager extends Component {
public static instance: BulletManager = null
@property({ type: [Prefab] })
private bulletPrefabList: Prefab[] = []
private bulletPool: Array<NodePool> = []
private bulletList: Array<FishBulletBase> = []
private bulletMoveSpeed: number = 30
private _vec3Cache
private _vec2Cache
private _fireTime: number = 0
private _fireTimeNew: number
onLoad() {
this._vec3Cache = new Vec3()
this._vec2Cache = new Vec2()
BulletManager.instance = this
this.node.on(Node.EventType.TOUCH_START, this.onShootBullet, this)
// this.node.on(Node.EventType.TOUCH_MOVE, this.onShootBullet, this)
}
public static instance: BulletManager = null
@property({ type: [Prefab] })
private bulletPrefabList: Prefab[] | [] = []
private bulletPool: Array<NodePool> = []
private bulletList: Array<FishBulletBase> = []
private bulletMoveSpeed: number = 30
private _vec3Cache: Vec3
private _vec2Cache: Vec2
private _fireTime: number = 0
private _fireTimeNew: number
start() {}
onLoad() {
this._vec3Cache = new Vec3()
this._vec2Cache = new Vec2()
BulletManager.instance = this
this.node.on(Node.EventType.TOUCH_START, this.onShootBullet, this)
// this.node.on(Node.EventType.TOUCH_MOVE, this.onShootBullet, this)
}
update() {
this.checkMoveBullet()
}
start() {
}
private checkMoveBullet() {
for (let i = this.bulletList.length - 1; i >= 0; i--) {
let bullet: FishBulletBase = this.bulletList[i]
let isMoving: boolean = MoveHelper.moveNode(
bullet.node,
this.bulletMoveSpeed,
bullet.targetP.x,
bullet.targetP.y
)
if (!isMoving) {
bullet.node.getPosition(this._vec3Cache)
this._vec2Cache.x = this._vec3Cache.x
this._vec2Cache.y = this._vec3Cache.y
FishNetManager.instance.addFishNet(bullet.bulletType, this._vec2Cache)
this.bulletList.splice(i, 1)
this.destroyBullet(bullet)
}
}
}
update() {
this.checkMoveBullet()
}
private onShootBullet(event: EventTouch) {
//TOUCH_START 在Editor上连续触发2次导致发2次炮弹bug
if (sys.platform == 'EDITOR_PAGE') {
this._fireTimeNew = new Date().getTime()
if (this._fireTimeNew - this._fireTime < 100) {
return
}
this._fireTime = this._fireTimeNew
}
private checkMoveBullet() {
for (let i = this.bulletList.length - 1; i >= 0; i--) {
let bullet: FishBulletBase = this.bulletList[i]
let isMoving: boolean = MoveHelper.moveNode(
bullet.node,
this.bulletMoveSpeed,
bullet.targetP.x,
bullet.targetP.y
)
if (!isMoving) {
bullet.node.getPosition(this._vec3Cache)
this._vec2Cache.x = this._vec3Cache.x
this._vec2Cache.y = this._vec3Cache.y
FishNetManager.instance.addFishNet(bullet.bulletType, this._vec2Cache)
this.bulletList.splice(i, 1)
this.destroyBullet(bullet)
}
}
}
let tran = this.node.getComponent(UITransform)
let location = event.getUILocation()
this._vec3Cache.x = location.x
this._vec3Cache.y = location.y
this._vec3Cache.z = 0
tran.convertToNodeSpaceAR(this._vec3Cache, this._vec3Cache)
let localP: Vec2 = new Vec2(this._vec3Cache.x, this._vec3Cache.y)
FishUI.instance.playClickEffect(localP)
// 子弹发射
if (FishUI.instance.dz_score >= CannonManager.instance.cannonType) {
FishUI.instance.dz_score -= CannonManager.instance.cannonType
FishUI.instance.refreshScore()
this._vec3Cache = CannonManager.instance.getCannonPosition()
private onShootBullet(event: EventTouch) {
//TOUCH_START 在Editor上连续触发2次导致发2次炮弹bug
if (sys.platform == 'EDITOR_PAGE') {
this._fireTimeNew = new Date().getTime()
if (this._fireTimeNew - this._fireTime < 100) {
return
}
this._fireTime = this._fireTimeNew
}
let rad: number = MathUtils.p2pRad(
new Vec2(this._vec3Cache.x, this._vec3Cache.y),
localP
)
let rot: number = MathUtils.radiansToDegrees(rad)
let bullet: FishBulletBase = this.createBullet(
CannonManager.instance.cannonType - 1
)
bullet.targetP = localP
this.node.addChild(bullet.node)
bullet.node.setPosition(CannonManager.instance.getCannonPosition())
this._vec3Cache.x = 1
this._vec3Cache.y = 1
this._vec3Cache.y = 1
Vec3.multiplyScalar(this._vec3Cache, this._vec3Cache, 2)
bullet.node.setScale(this._vec3Cache)
bullet.node.angle = rot
this.bulletList.push(bullet)
GameMusicHelper.playFire()
let tran = this.node.getComponent(UITransform)
let location = event.getUILocation()
this._vec3Cache.x = location.x
this._vec3Cache.y = location.y
this._vec3Cache.z = 0
tran.convertToNodeSpaceAR(this._vec3Cache, this._vec3Cache)
let localP: Vec2 = new Vec2(this._vec3Cache.x, this._vec3Cache.y)
FishUI.instance.playClickEffect(localP)
// 子弹发射
if (FishUI.instance.dz_score >= CannonManager.instance.cannonType) {
FishUI.instance.dz_score -= CannonManager.instance.cannonType
FishUI.instance.refreshScore()
this._vec3Cache = CannonManager.instance.getCannonPosition()
//旋转炮台
CannonManager.instance.rotateCannon(location)
} else {
CommonTips.showMsg('豆子不足!')
}
}
let rad: number = MathUtils.p2pRad(
new Vec2(this._vec3Cache.x, this._vec3Cache.y),
localP
)
let rot: number = MathUtils.radiansToDegrees(rad)
let bullet: FishBulletBase = this.createBullet(
CannonManager.instance.cannonType - 1
)
bullet.targetP = localP
this.node.addChild(bullet.node)
bullet.node.setPosition(CannonManager.instance.getCannonPosition())
this._vec3Cache.x = 1
this._vec3Cache.y = 1
this._vec3Cache.y = 1
Vec3.multiplyScalar(this._vec3Cache, this._vec3Cache, 2)
bullet.node.setScale(this._vec3Cache)
bullet.node.angle = rot
this.bulletList.push(bullet)
GameMusicHelper.playFire()
private createBullet(bulletType: number) {
let bulletNode: Node
if (this.bulletPool[bulletType] && this.bulletPool[bulletType].size() > 0) {
bulletNode = this.bulletPool[bulletType].get()
} else {
bulletNode = instantiate(this.bulletPrefabList[bulletType])
}
bulletNode.getComponent(FishBulletBase).bulletType = bulletType
return bulletNode.getComponent(FishBulletBase)
}
//旋转炮台
CannonManager.instance.rotateCannon(location)
} else {
CommonTips.showMsg('豆子不足!')
}
}
public killBullet(bullet: FishBulletBase) {
let index: number = this.bulletList.indexOf(bullet)
if (index >= 0) {
this.bulletList.splice(index, 1)
this.destroyBullet(bullet)
}
}
private createBullet(bulletType: number) {
let bulletNode: Node
if (this.bulletPool[bulletType] && this.bulletPool[bulletType].size() > 0) {
bulletNode = this.bulletPool[bulletType].get()
} else {
bulletNode = instantiate(this.bulletPrefabList[bulletType])
}
bulletNode.getComponent(FishBulletBase).bulletType = bulletType
return bulletNode.getComponent(FishBulletBase)
}
private destroyBullet(bullet: FishBulletBase) {
//临时代码,因为回收在内存卡顿。后面在优化 2023-2-10
if (sys.platform == 'EDITOR_PAGE') {
bullet.node.destroy()
return
}
public killBullet(bullet: FishBulletBase) {
let index: number = this.bulletList.indexOf(bullet)
if (index >= 0) {
this.bulletList.splice(index, 1)
this.destroyBullet(bullet)
}
}
if (!this.bulletPool[bullet.bulletType]) {
this.bulletPool[bullet.bulletType] = new NodePool()
}
this.bulletPool[bullet.bulletType].put(bullet.node)
}
private destroyBullet(bullet: FishBulletBase) {
//临时代码,因为回收在内存卡顿。后面在优化 2023-2-10
if (sys.platform == 'EDITOR_PAGE') {
bullet.node.destroy()
return
}
onDestroy() {
BulletManager.instance = null
}
if (!this.bulletPool[bullet.bulletType]) {
this.bulletPool[bullet.bulletType] = new NodePool()
}
this.bulletPool[bullet.bulletType].put(bullet.node)
}
onDestroy() {
BulletManager.instance = null
}
}