
import { _decorator, Component, Node, Prefab } from 'cc';
import { EventListener } from './EventListener';
import { Constants } from './data/Constants';
const { ccclass, property } = _decorator;
@ccclass('EffectManager')
export class EffectManager extends Component {
@property({
type:Prefab
})
brakeTrail : Prefab |null = null;
@property({
type:Prefab
})
conin : Prefab |null = null;
private followTarget:Node |null = null;//目标,刹车的目标
public start() {
EventListener.on(Constants.EventName.START_BRAKING, this._start_braking, this);
EventListener.on(Constants.EventName.END_BRAKING, this._end_braking, this);
EventListener.on(Constants.EventName.SHOW_COIN, this._show_coin, this);
}
public _start_braking(...args: any[]) {
const follow = this.followTarget = args[0];
}
public _end_braking(...args: any[]) {
}
public _show_coin(...args: any[]) {
}
}

import { _decorator, Component, Node, Prefab, instantiate, TiledMap } from 'cc';
const { ccclass, property } = _decorator;
@ccclass('PoolManager')
export class PoolManager extends Component {
public static handle = new Map<string, Node[]>();
public static getNode(prefab: Prefab, parent: Node) {
const name = prefab.name;
console.log("prefab.name:" + name);
var node: Node | null = null;//这里之前用的不是let 小车没有回收。
if (this.handle.has(name)) {
node = this.handle.get(name)?.pop();
} else {
node = instantiate(prefab) as Node;
}
node?.setParent(parent);
return node;
}
public static setNode(target: Node) {
const name = target.name;
target.parent = null;
if (this.handle.has(name)) {
this.handle.get(name)?.push(target);
} else {
let array:Node[] = [];
array.push(target);
this.handle.set(name, array);
}
}
}小车初始(car.setEntry方法)化时判断 是不是主车播放尾气。


拉送客 停止 尾气

public stopRunning() {
console.log("stop")
this._acceleration = -0.2;
//事件传递到 EffectManage里面。 播放尾气
EventListener.dispatchEvent(Constants.EventName.START_BRAKING,this.node);
this._isBraking =true;
//this._isMove = false;
}




送客到位后,播放

特效管理去播放

import { _decorator, Component, Node, Prefab, ParticleUtils, ParticleSystemComponent, instantiate } from 'cc';
import { EventListener } from './EventListener';
import { Constants } from './data/Constants';
import { PoolManager } from './data/PoolManager';
const { ccclass, property } = _decorator;
@ccclass('EffectManager')
export class EffectManager extends Component {
@property({
type:Prefab
})
prefab_brakeTrail : Prefab |null = null!;
@property({
type:Prefab
})
prefab_coin : Prefab |null = null;
private followTarget:Node |null = null;//目标,刹车的目标
private _currentBraking : Node |null= null;
private _coin :ParticleSystemComponent |null = null
public update(dt:number){
if(this._currentBraking&&this.followTarget){
this._currentBraking.setWorldPosition(this.followTarget.worldPosition);
}
}
public start() {
//接受car停车时候的事件。
EventListener.on(Constants.EventName.START_BRAKING, this._start_braking, this);
EventListener.on(Constants.EventName.END_BRAKING, this._end_braking, this);
EventListener.on(Constants.EventName.SHOW_COIN, this._show_coin, this);
}
public _start_braking(...args: any[]) {
if(this.prefab_brakeTrail!=null){
const follow = this.followTarget = args[0];
this._currentBraking = PoolManager.getNode(this.prefab_brakeTrail,this.node);
this._currentBraking?.setWorldPosition(follow);
if(this._currentBraking){
ParticleUtils.play(this._currentBraking);//执行节点下面的所有粒子系统 执行play操作。
}
}
}
public _end_braking(...args: any[]) {
const currentBraking = this._currentBraking;
if(this._currentBraking){
ParticleUtils.stop(this._currentBraking);
}
this.scheduleOnce(()=>{
if(currentBraking){
PoolManager.setNode(currentBraking);
}
},2);//2秒后回收
this._currentBraking = null;
this.followTarget = null;
}
public _show_coin(...args: any[]) {
const pos = args[0];
if(this._coin==null){
const coin = instantiate(this.prefab_coin) as Node ;
coin.setParent(this.node);
this._coin = coin.getComponent(ParticleSystemComponent);
}
this._coin?.node.setWorldPosition(pos);
this._coin?.play();
}
}站长微信:xiaomao0055
站长QQ:14496453