feat: reset weight of spawn object when restart

main
tiendat3699 2024-03-13 11:47:07 +07:00
parent 8f4734cee6
commit d1a218f16b
1 changed files with 12 additions and 6 deletions

View File

@ -13,13 +13,15 @@ class weightedObject {
@property(Prefab) @property(Prefab)
public prefab: Prefab; public prefab: Prefab;
@property(CCInteger) @property(CCInteger)
public weight = 0; public readonly weight = 0;
@property(CCInteger) @property(CCInteger)
public weightStepOnGoal = 0; public weightStepOnGoal = 0;
@property(CCInteger) @property(CCInteger)
public maxWeight = 0; public maxWeight = 0;
@property(CCInteger) @property(CCInteger)
public maxObjects = -1; public maxObjects = -1;
@property({ readonly: true })
public currentWeight;
} }
@ccclass('SpawnObjectManager') @ccclass('SpawnObjectManager')
@ -55,7 +57,10 @@ export class SpawnObjectManager extends Component {
this._pools[i] = new ObjectPool(prefab, 10, true, ScoreObject); this._pools[i] = new ObjectPool(prefab, 10, true, ScoreObject);
} }
this._weights = this._objects.map((obj) => obj.weight); this._weights = this._objects.map((obj) => {
obj.currentWeight = obj.weight;
return obj.currentWeight;
});
} }
protected update(dt: number): void { protected update(dt: number): void {
@ -87,14 +92,14 @@ export class SpawnObjectManager extends Component {
private onScore(score: number, type: ScoreType) { private onScore(score: number, type: ScoreType) {
if (type == ScoreType.Goal) { if (type == ScoreType.Goal) {
this._objects.forEach((object) => { this._objects.forEach((object) => {
if (object.weight >= object.maxWeight) { if (object.currentWeight >= object.maxWeight) {
object.weight = object.maxWeight; object.currentWeight = object.maxWeight;
return; return;
} }
object.weight += object.weightStepOnGoal; object.currentWeight += object.weightStepOnGoal;
}); });
this._weights = this._objects.map((obj) => obj.weight); this._weights = this._objects.map((obj) => obj.currentWeight);
} }
} }
@ -110,6 +115,7 @@ export class SpawnObjectManager extends Component {
break; break;
case GameState.GameOver: case GameState.GameOver:
this._playing = false; this._playing = false;
this._objects.forEach((object) => (object.currentWeight = object.weight));
this._pools.forEach((pool) => pool.releaseAll()); this._pools.forEach((pool) => pool.releaseAll());
break; break;
} }