This commit is contained in:
2024-05-01 19:13:01 +08:00
parent cf008327aa
commit 80a32d9b1b
150 changed files with 8561 additions and 5045 deletions

View File

@@ -1,32 +1,39 @@
import {
_decorator,
Component,
EventTouch,
instantiate,
Node,
NodePool,
Prefab,
sys,
UITransform,
Vec2,
Vec3
Vec3,
_decorator,
instantiate,
sys,
} from 'cc'
import FishBulletBase from '../../../fish/script/FishBulletBase'
import MathUtils from '../../engine/utils/MathUtils'
import CannonManager from './CannonManager'
import { MoveHelper } from '../../engine/utils/MoveHelper'
import FishNetManager from './FishNetManager'
import GameMusicHelper from '../utils/GameMusicHelper'
import FishUI from '../../../fish/script/FishUI'
import CommonTips from '../../engine/uicomponent/CommonTips'
import MathUtils from '../../engine/utils/MathUtils'
import { MoveHelper } from '../../engine/utils/MoveHelper'
import GameMusicHelper from '../utils/GameMusicHelper'
import CannonManager from './CannonManager'
import FishNetManager from './FishNetManager'
import WsManager from './WsManager'
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
@@ -40,24 +47,23 @@ export default class BulletManager extends Component {
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)
}
start() {
}
start() {}
update() {
this.checkMoveBullet()
}
// 检测子弹是否移动完成
private checkMoveBullet() {
for (let i = this.bulletList.length - 1; i >= 0; i--) {
let bullet: FishBulletBase = this.bulletList[i]
let isMoving: boolean = MoveHelper.moveNode(
const bullet: FishBulletBase = this.bulletList[i]
const isMoving: boolean = MoveHelper.moveNode(
bullet.node,
this.bulletMoveSpeed,
bullet.targetP.x,
bullet.targetP.y
bullet.targetP.y,
)
if (!isMoving) {
bullet.node.getPosition(this._vec3Cache)
@@ -70,38 +76,31 @@ export default class BulletManager extends Component {
}
}
// 发射炮弹
private onShootBullet(event: EventTouch) {
//TOUCH_START 在Editor上连续触发2次导致发2次炮弹bug
if (sys.platform == 'EDITOR_PAGE') {
// TOUCH_START 在Editor上连续触发2次导致发2次炮弹bug
if (sys.platform === 'EDITOR_PAGE') {
this._fireTimeNew = new Date().getTime()
if (this._fireTimeNew - this._fireTime < 100) {
return
}
if (this._fireTimeNew - this._fireTime < 15000) return
this._fireTime = this._fireTimeNew
}
let tran = this.node.getComponent(UITransform)
let location = event.getUILocation()
const tran = this.node.getComponent(UITransform)
const 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)
const 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()
// FishUI.instance.refreshScore()
this._vec3Cache = CannonManager.instance.getCannonPosition()
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
)
const rad: number = MathUtils.p2pRad(new Vec2(this._vec3Cache.x, this._vec3Cache.y), localP)
const rot: number = MathUtils.radiansToDegrees(rad)
const bullet: FishBulletBase = this.createBullet(CannonManager.instance.cannonType - 1)
bullet.targetP = localP
this.node.addChild(bullet.node)
bullet.node.setPosition(CannonManager.instance.getCannonPosition())
@@ -113,14 +112,14 @@ export default class BulletManager extends Component {
bullet.node.angle = rot
this.bulletList.push(bullet)
GameMusicHelper.playFire()
//旋转炮台
// 旋转炮台
CannonManager.instance.rotateCannon(location)
} else {
CommonTips.showMsg('豆子不足!')
}
}
// 创建子弹
private createBullet(bulletType: number) {
let bulletNode: Node
if (this.bulletPool[bulletType] && this.bulletPool[bulletType].size() > 0) {
@@ -133,23 +132,21 @@ export default class BulletManager extends Component {
}
public killBullet(bullet: FishBulletBase) {
let index: number = this.bulletList.indexOf(bullet)
const index: number = this.bulletList.indexOf(bullet)
if (index >= 0) {
this.bulletList.splice(index, 1)
this.destroyBullet(bullet)
}
}
// 销毁子弹
private destroyBullet(bullet: FishBulletBase) {
//临时代码,因为回收在内存卡顿。后面在优化 2023-2-10
if (sys.platform == 'EDITOR_PAGE') {
// 临时代码,因为回收在内存卡顿。后面在优化 2023-2-10
if (sys.platform === 'EDITOR_PAGE') {
bullet.node.destroy()
return
}
if (!this.bulletPool[bullet.bulletType]) {
this.bulletPool[bullet.bulletType] = new NodePool()
}
if (!this.bulletPool[bullet.bulletType]) this.bulletPool[bullet.bulletType] = new NodePool()
this.bulletPool[bullet.bulletType].put(bullet.node)
}