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,210 +1,272 @@
import { _decorator, Animation, Component, game, instantiate, Node, NodePool, Prefab, Vec2, Vec3 } from 'cc'
import RandomUtil from '../../engine/utils/RandomUtil'
import {
Animation,
Component,
Node,
NodePool,
Prefab,
Tween,
Vec2,
Vec3,
_decorator,
game,
instantiate,
tween,
} from 'cc'
import FishBase from '../../../fish/script/FishBase'
import { FishPathConfig } from '../config/FishPathConfig'
import FishMover from '../../../fish/script/FishMover'
import FishUI from '../../../fish/script/FishUI'
import { Logger } from '../../engine/utils/Logger'
import { FishInfo } from '../config/FishInfo'
import RandomUtil from '../../engine/utils/RandomUtil'
import { FishConfig } from '../config/FishConfig'
import GameMusicHelper from '../utils/GameMusicHelper'
import ScoreManager from './ScoreManager'
import { FishInfo } from '../config/FishInfo'
import { FishMap } from '../config/FishMap'
import { FishMapInfo } from '../config/FishMapInfo'
import FishUI from '../../../fish/script/FishUI'
import { FishPathConfig } from '../config/FishPathConfig'
import GameMusicHelper from '../utils/GameMusicHelper'
import TimeHelper from '../utils/TimeHelper'
import ScoreManager from './ScoreManager'
import WsManager from './WsManager'
import { FishPathInfo } from '../config/FishPathInfo'
const { ccclass, property } = _decorator
interface dataType {
fisn: Array<FishType>
}
interface FishType {
fishId: string
fishInfo: FishInfo
fishType: string
isDead: number
fishPathInfo: Array<fishPathInfoType>
}
interface fishPathInfoType {
pathId: string
path: Array<PathPoint>
}
interface PathPoint {
x: number
y: number
}
// 鱼管理类
@ccclass('FishManager')
export default class FishManager extends Component {
public static instance: FishManager = null
@property({ type: Node })
private fishContainer: Node | null = null
@property({ type: [Prefab] })
public fishPrefabList: Array<Prefab> = []
private fishPool: Array<NodePool> = []
private fishList: Array<FishBase> = []
private fishList: Map<string, FishBase> = new Map()
private nextRandomFishTime: number = 0
private minRandomTime: number = 2 * (game.frameRate as number)
private maxRandomTime: number = 5 * (game.frameRate as number)
private isFishMap: boolean = false
private mapCount: number = 0
private minMapCount: number = 30 * (game.frameRate as number)
private maxMapCount: number = 60 * (game.frameRate as number)
// // private minMapCount: number = 2 * cc.game.getFrameRate();
// // private maxMapCount: number = 5 * cc.game.getFrameRate();
private _fishPosCache: Vec3
onLoad() {
WsManager.instance.on(100, this.randomFish, this)
FishManager.instance = this
this._fishPosCache = new Vec3()
Logger.log(
'maxRandomTime=',
this.minRandomTime,
this.maxRandomTime,
game.frameRate
)
Logger.log('maxRandomTime=', this.minRandomTime, this.maxRandomTime, game.frameRate)
}
start() {
this.randomFish()
// this.randomFish()
}
update() {
this.checkRandomFish()
// this.checkRandomFish()
this.checkFishMoveEnd()
this.checkFishMap()
// this.checkFishMap()
}
private checkFishMap() {
if (!this.isFishMap) {
if (this.mapCount > 0) {
this.mapCount--
if (this.mapCount <= 0) {
FishUI.instance.playWaveEffect()
}
if (this.mapCount <= 0) FishUI.instance.playWaveEffect()
}
}
}
// 检测是否随机鱼
private checkRandomFish() {
if (!this.isFishMap) {
if (this.nextRandomFishTime > 0) {
this.nextRandomFishTime--
if (this.nextRandomFishTime == 0) {
this.randomFish()
}
// if (this.nextRandomFishTime === 0) this.randomFish()
}
}
}
// 检测鱼是否移动结束
private checkFishMoveEnd() {
for (let i = this.fishList.length - 1; i >= 0; i--) {
let fish: FishBase = this.fishList[i]
this.fishList.forEach(async (item: FishBase, key: string) => {
if (this.isFishMap) {
if (!fish.isDead) {
fish.node.getPosition(this._fishPosCache)
// 鱼阵回收
if (item.isDead === 2) {
item.node.getPosition(this._fishPosCache)
this._fishPosCache.x -= 2
fish.node.setPosition(this._fishPosCache)
item.node.setPosition(this._fishPosCache)
if (this._fishPosCache.x <= -screen.width / 2) {
//winSize.width
this.destroyFish(fish)
this.fishList.splice(i, 1)
// winSize.width
await WsManager.instance.onSend({
type: 102,
fish_id: item.fishId,
})
// this.fishList.splice(index, 1)
this.destroyFish(item)
// this.fishList.delete(item.fishId)
this.checkEndFishMap()
}
}
} else if (!fish.getComponent(FishMover).isMoving) {
this.destroyFish(fish)
this.fishList.splice(i, 1)
} else if (!item.getComponent(FishMover).isMoving) {
// 普通鱼回收
await WsManager.instance.onSend({
type: 102,
fish_id: item.fishId,
})
// this.fishList.splice(index, 1)
this.destroyFish(item)
// this.fishList.delete(item.fishId)
}
}
})
}
private checkEndFishMap() {
Logger.log('checkEndFishMap==', this.isFishMap, this.fishList)
if (this.isFishMap && this.fishList.length <= 0) {
// Logger.log('checkEndFishMap==', this.isFishMap, this.fishList)
if (this.isFishMap && this.fishList.size <= 0) {
this.isFishMap = false
this.randomFish()
// this.randomFish()
}
}
private randomFish() {
if (this.isFishMap) return
let randomNum: number = RandomUtil.nextInt(1, 10)
// let randomNum: number = RandomUtil.nextInt(1, 1);
for (let i = 0; i < randomNum; i++) {
let fishType: number = RandomUtil.nextInt(1, 29)
// let fishType: number = RandomUtil.nextInt(1, 1);
let fish: FishBase = this.createFishByType(fishType)
fish.fishPathInfo = FishPathConfig.randomPathInfo()
/**
* 原:本地随机生成鱼
* 新:服务端生成鱼
*/
private async randomFish(data: dataType) {
const arr = data.fisn
const paths: Array<FishPathInfo> = []
if (!Array.isArray(arr) || this.isFishMap) return
for (let i = 0; i < arr.length; i++) {
const fish: FishBase = this.createFishByType(arr[i])
for (let k = 0; k < arr[i].fishPathInfo.length; k++) {
const path: Array<Vec2> = []
for (let j = 0; j < arr[i].fishPathInfo[k].path.length; j++) {
const p: Vec2 = new Vec2(
arr[i].fishPathInfo[k].path[j].x,
arr[i].fishPathInfo[k].path[j].y,
)
path.push(p)
}
paths.push(new FishPathInfo(arr[i].fishPathInfo[k].pathId, path))
}
fish.fishPathInfo = paths[0]
this._fishPosCache.z = 0
this._fishPosCache.x = fish.fishPathInfo.path[0].x
this._fishPosCache.y = fish.fishPathInfo.path[0].y
fish.node.setPosition(this._fishPosCache)
fish.getComponent(FishMover).bezierPList = fish.fishPathInfo.path
fish.getComponent(FishMover).startMove()
this.fishList.push(fish)
this.fishContainer.addChild(fish.node)
}
Logger.log('checkFishMoveEnd=randomFish=', this.fishList)
this.nextRandomFishTime = RandomUtil.nextInt(
this.minRandomTime,
this.maxRandomTime
)
if (this.mapCount <= 0) {
this.mapCount = RandomUtil.nextInt(this.minMapCount, this.maxMapCount)
// this.fishList.push(fish)
this.fishList.set(fish.fishId, fish)
// this.fishContainer.addChild(fish.node)
this.fishContainer.addChild(this.fishList.get(fish.fishId).node)
}
}
public createFishByType(fishType: number): FishBase {
// 创建鱼类
public createFishByType(data: FishType | any): FishBase {
let fishNode: Node
const fishType: number = Number(data.fishType)
if (this.fishPool[fishType - 1] && this.fishPool[fishType - 1].size() > 0) {
fishNode = this.fishPool[fishType - 1].get()
} else {
fishNode = instantiate(this.fishPrefabList[fishType - 1])
}
//fishNode.getComponent(Animation).play() //v3 当前帧 不能播放
TimeHelper.exeNextFrame(fishNode, () =>
fishNode.getComponent(Animation).play()
)
let fishInfo: FishInfo = FishConfig.getFishInfoByType(fishType)
TimeHelper.exeNextFrame(fishNode, () => fishNode.getComponent(Animation).play())
const fishInfo: FishInfo = FishConfig.getFishInfoByType(fishType)
fishNode.getComponent(FishBase).fishInfo = fishInfo
fishNode.getComponent(FishBase).fishType = fishType
fishNode.getComponent(FishBase).blood = fishInfo.blood
fishNode.getComponent(FishBase).isDead = false
fishNode.getComponent(FishBase).fishId = data.fishId
fishNode.getComponent(FishBase).isDead = 2
return fishNode.getComponent(FishBase)
}
public killFish(fish: FishBase) {
let index: number = this.fishList.indexOf(fish)
if (index >= 0) {
// console.log("鱼挂了")
GameMusicHelper.playFishDead(fish.fishType)
fish.node.getPosition(this._fishPosCache)
let vec2 = new Vec2(this._fishPosCache.x, this._fishPosCache.y)
ScoreManager.instance.addScore(fish.fishInfo.blood, vec2)
this.fishList.splice(index, 1)
this.destroyFish(fish)
this.checkEndFishMap()
// 销毁鱼类
public killFish(res: any) {
const fishCheck = this.fishList.get(res.fish_id)
// console.log('正在执行销毁=', fishCheck.fishId, res.fish_id, res.fish_status, fishCheck.isDead)
if (fishCheck) {
fishCheck.isDead = 1
setTimeout(() => {
GameMusicHelper.playFishDead(fishCheck.fishType)
fishCheck.node.getPosition(this._fishPosCache)
const vec2 = new Vec2(this._fishPosCache.x, this._fishPosCache.y)
ScoreManager.instance.addScore(Number(res.fish_number), vec2)
this.destroyFish(fishCheck)
// console.log('killFish=', fishCheck.fishId, fishCheck.fishInfo.name)
this.checkEndFishMap()
}, 500)
tween(fishCheck.node)
.repeatForever(tween().by(0.6, { angle: -360 }))
.start()
// this.fishList.splice(index, 1)
}
}
private destroyFish(fish: FishBase) {
if (!this.fishPool[fish.fishType - 1]) {
this.fishPool[fish.fishType - 1] = new NodePool()
const f = this.fishList.get(fish.fishId)
if (!f) return
if (!this.fishPool[f.fishType - 1]) {
this.fishPool[f.fishType - 1] = new NodePool()
}
this.fishPool[fish.fishType - 1].put(fish.node)
this.fishPool[f.fishType - 1].put(f.node)
this.fishList.delete(f.fishId)
}
public playFishMap() {
this.isFishMap = true
for (let i = this.fishList.length - 1; i >= 0; i--) {
let fish: FishBase = this.fishList[i]
this.fishList.forEach((fish: FishBase, key: string) => {
this.destroyFish(fish)
this.fishList.splice(i, 1)
}
})
}
public startFishMap() {
// this.playFishMap();
// this.fishList = [];
let map: FishMap = FishPathConfig.randomFishMap()
let fishMapInfoList: Array<FishMapInfo> = map.fishMapInfoList
const map: FishMap = FishPathConfig.randomFishMap()
const fishMapInfoList: Array<FishMapInfo> = map.fishMapInfoList
Logger.log('startFishMap==', this.isFishMap, this.fishList, map)
for (let i = 0; i < fishMapInfoList.length; i++) {
let fishMapInfo: FishMapInfo = fishMapInfoList[i]
let fish: FishBase = this.createFishByType(fishMapInfo.fishType)
const fishMapInfo: FishMapInfo = fishMapInfoList[i]
// 暂时屏蔽
// @ts-ignore
const fish: FishBase = this.createFishByType(fishMapInfo.fishType)
fish.node.angle = 0
// fish.node.setScale(fishMapInfo.scale);
this.fishContainer.addChild(fish.node)
fish.node.setPosition(fishMapInfo.x + screen.width, fishMapInfo.y)
this.fishList.push(fish)
this.fishList.set(fish.fishId, fish)
}
}
onDestroy() {
FishManager.instance = null
WsManager.instance.off(100)
}
protected onDisable(): void {}
}