2025-06-16 12:47:55 +08:00

110 lines
3.2 KiB
TypeScript

import getDecorators from "inversify-inject-decorators";
import { Chip } from "../../chip/scripts/chip";
import { container, ListBetOptionModel, MODEL } from "../../plugins/core";
import { ChipHandlerAnim } from "./chip-handler-anim";
import { ChipHandlerCalculator } from "./chip-handler-calculator";
const { ccclass, property } = cc._decorator;
const { lazyInject } = getDecorators(container, false);
@ccclass
export class ChipHandler extends cc.Component
{
@lazyInject(MODEL.ListBetOption)
private listBetOptionModel: ListBetOptionModel;
@property(cc.Prefab)
private chipPrefab: cc.Prefab = null;
@property(ChipHandlerAnim)
private animChip: ChipHandlerAnim = null;
private listChips: Chip[] = [];
private totalChipValue: number = 0;
public updateChipBetValue(newValue: number): void
{
if (this.totalChipValue == newValue) return;
if (this.totalChipValue > 0 && newValue == 0)
{
this.collectAll();
return;
}
if (newValue > this.totalChipValue)
{
this.addBet(newValue - this.totalChipValue);
}
else if (newValue < this.totalChipValue)
{
this.collect(this.totalChipValue - newValue);
}
this.totalChipValue = newValue;
}
public hideAll(): void
{
if (!this.listChips) return;
this.listChips.forEach(chip =>
{
this.animChip.hide(chip.node);
});
this.listChips = [];
this.totalChipValue = 0;
}
private collectAll(): void
{
this.listChips.forEach(chip =>
{
this.animChip.collect(chip, true);
});
this.listChips = [];
this.totalChipValue = 0;
}
private collect(value: number)
{
var arrayChipReturn = ChipHandlerCalculator.getChipsToCollectByValue(value, this.listChips);
arrayChipReturn.forEach(chip =>
{
this.animChip.collect(chip, false);
});
}
private addWin(totalWinPoint: number): void
{
if (totalWinPoint <= 0) return;
if (totalWinPoint < this.totalChipValue) return;
var chipValue = totalWinPoint - this.totalChipValue;
this.totalChipValue = totalWinPoint;
var listChipValue = ChipHandlerCalculator.getChipValuesToSpawn(chipValue, this.listBetOptionModel.listBetOption);
listChipValue.forEach(chipValue =>
{
var chip = this.spawn();
chip.setChipValue(chipValue);
this.animChip.win(chip.node);
});
}
private addBet(value: number): void
{
var arrayChipValue = ChipHandlerCalculator.getChipValuesToSpawn(value, this.listBetOptionModel.listBetOption);
arrayChipValue.forEach(chipValue =>
{
var chip = this.spawn();
chip.setChipValue(chipValue);
this.animChip.place(chip.node);
});
}
private spawn(): Chip
{
var chip = cc.instantiate(this.chipPrefab).getComponent(Chip);
chip.node.active = true;
this.listChips.push(chip);
this.node.addChild(chip.node);
chip.node.setPosition(cc.v2(0, 0));
return chip;
}
}