import SKDataUtil from "../gear_2.3.4/util/SKDataUtil"; import SKLogger from "../gear_2.3.4/util/SKLogger"; import Horse from "./Horse"; export default class HorseList { currentHorse: Horse; horseIndex: number = 0; dict: { [key: number]: Horse }; horseChange: Function; race: number; constructor() { this.dict = {}; } // 獲得資源索引 get resId(): number { if (this.currentHorse == null) { return 0; } return this.currentHorse.resId; } reset(data: any) { console.warn("reset") if (this.horseIndex != data.horseIndex) { if (data.horseIndex < 1) { this.getDown(); } else { this.ride(data.horseIndex); } } for (let item of data.list) { let horse: Horse = this.dict[item.position]; if (horse != null) { horse.parse(item,this.race); } } } parse(horseData: any, race: number) { if (horseData == null) { return; } let horseIndex = horseData.horseIndex; this.race = race; for (let item of horseData.list) { let horse = this.dict[item.position]; if (horse == null) { horse = new Horse(item.position, item.name, item.level, item.exp, this.race); } else { horse.parse(item,race); } if (horse.position == horseIndex) { this.currentHorse = horse; } this.dict[horse.position] = horse; } if (this.horseIndex != horseIndex) { if (horseIndex < 1) { this.getDown(); } else { this.ride(horseIndex); } } } getCurrentPosition(): number { if (this.currentHorse == null) { return 0; } return this.currentHorse.position; } // 獲得速度加成 getMoveSpeedScale(): number { if (this.currentHorse == null) { return 1; } return 1.25; } getHorseResId(position: number): number { let horse = this.dict[position]; if (horse == null) { return 0; } return horse.resId; } getHorseBy(position: number): Horse { let horse = this.dict[position]; return horse; } // 騎乘 ride(horseIndex: number) { if (this.horseIndex == horseIndex) { return; } this.horseIndex = horseIndex; this.currentHorse = this.dict[horseIndex]; if (this.horseChange) { this.horseChange(); } } // 同步騎乘 syncRide(horseIndex: number) { horseIndex = SKDataUtil.clamp(horseIndex, 0, 4); if (this.horseIndex == horseIndex) { return; } this.horseIndex = horseIndex; this.currentHorse = this.dict[this.horseIndex]; if (this.currentHorse != null) { SKLogger.debug(`同步[${this.currentHorse.name}]騎乘`); } if (this.horseChange) { this.horseChange(); } } // 下馬 getDown() { if (this.horseIndex == 0) { return; } if (this.currentHorse != null) { SKLogger.debug(`同步[${this.currentHorse.name}]下馬`); } this.horseIndex = 0; this.currentHorse = null; if (this.horseChange) { this.horseChange(); } } // 同步下騎 syncGetDown() { this.horseIndex = 0; this.currentHorse = null; if (this.horseChange) { this.horseChange(); } } }