123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- import { _decorator, Component, EventTouch, Prefab, Node, instantiate, AudioSource, AudioClip, math } from 'cc';
- import { GameManager } from './Manager/GameManager';
- import LevelManager from './Manager/LevelManager';
- import { EventCustom } from './Common/EventCustom';
- import { Global } from './Common/Global';
- import { GameConfig } from './Game/GameConfig';
- import { EventUtils } from './Game/EventUtils';
- import { PlayerItem } from './Game/PlayerItem';
- const { ccclass, property } = _decorator;
- @ccclass('MainGame')
- export class MainGame extends Component {
- @property(Prefab)
- playerItemPrefab: Prefab;
- @property(Node)
- middle: Node = null;
- @property(Node)
- top: Node = null;
- @property(Node)
- bottom: Node = null;
- @property(Node)
- left: Node = null;
- @property(Node)
- right: Node = null;
- @property(Node)
- gameRoot: Node = null;
- @property(Node)
- nextPolice: Node = null;
- @property(Node)
- nextThief: Node = null;
- @property(Node)
- tmiddle: Node = null;
- @property(Node)
- gameMask: Node = null;
- @property(AudioClip)
- audioClipList: AudioClip[] = [];
- gameSaveData: object = {
- gender: 0,
- thiefDir: 0,
- theMassesDir: []
- };
- audioSource: AudioSource = null;
- _currentLevel: number = 1;
- theMassesNum: number = 1;
- onLoad() {
- this.node.on(Global.EventTypeSelectPolice, (e) => {
- this.selectPoliceComplete(e.data);
- e.propagationStopped = false;
- });
- this.node.on(Global.EventTypeSelectThief, (e) => {
- this.selectThiefComplete(e.data);
- e.propagationStopped = false;
- });
- this.node.on(Global.EventGameContinue, (e) => {
- this.gameContinue(e.data);
- e.propagationStopped = false;
- });
- EventUtils.on("GameStart", this.onGameStart, this);
- EventUtils.on("SelectEffect", this.selectEffect, this);
- this.audioSource = this.node.getComponent(AudioSource);
- }
- onGameStart(levelData) {
- this.theMassesNum = levelData["theMassesNum"] + 1;
- this.generateCurrentGameData(this.theMassesNum);
- }
- start() {
- }
- onDestroy() {
- EventUtils.off("GameStart", this.onGameStart, this);
- EventUtils.off("SelectEffect", this.selectEffect, this);
- }
- generateCurrentGameData(theMassesNum) {
- this.showTips("请记住警察和小偷和平民出现的方向")
- let gender: number = Math.random() > 0.5 ? 5 : 6;
- let randomArray = this.randomData(theMassesNum);
- let targetID: number = randomArray[math.randomRangeInt(0, randomArray.length)];
- GameConfig.gameSaveData = {
- gender: gender,
- thiefDir: randomArray[0],
- theMassesDir: randomArray.filter(item => { return item != randomArray[0] }),
- targetDir: targetID
- }
- this.generaPlayer();
- setTimeout(() => {
- this.gameRoot.active = false;
- this.nextThief.active = false;
- this.gameMask.active = false;
- this.nextPolice.active = true;
- this.showTips("请选择刚才出现的警察");
- }, 2000 + (5000 - (GameManager.getInstance().curLevel * 200 + Global.DifficultyRate * 100)));
- }
- generaPlayer() {
- this.clearNode();
- let middleNode = instantiate(this.playerItemPrefab);
- this.middle.addChild(middleNode);
- middleNode.emit("init", GameConfig.PlayerType.Police, GameConfig.gameSaveData["gender"]);
- let tmiddleNode = instantiate(this.playerItemPrefab);
- this.tmiddle.addChild(tmiddleNode);
- tmiddleNode.getComponent(PlayerItem).init(GameConfig.PlayerType.Police, GameConfig.gameSaveData["gender"]);
- // tmiddleNode.emit("init", GameConfig.PlayerType.Police, GameConfig.gameSaveData["gender"]);
- let thiefNode = instantiate(this.playerItemPrefab);
- let thiefParent = this.getTargetNode(GameConfig.gameSaveData["thiefDir"]);
- thiefParent.addChild(thiefNode);
- thiefNode.emit("init", GameConfig.PlayerType.Thief);
- let allTheMassesData = GameConfig.gameSaveData["theMassesDir"];
- for (let i = 0; i < allTheMassesData.length; ++i) {
- let item = instantiate(this.playerItemPrefab);
- let itemParent = this.getTargetNode(allTheMassesData[i]);
- if (itemParent) {
- itemParent.addChild(item);
- item.emit("init", GameConfig.PlayerType.TheMasses);
- }
- }
- }
- clearNode() {
- this.middle.destroyAllChildren();
- this.tmiddle.destroyAllChildren();
- this.top.destroyAllChildren();
- this.bottom.destroyAllChildren();
- this.left.destroyAllChildren();
- this.right.destroyAllChildren();
- this.gameRoot.active = true;
- this.nextThief.active = false;
- this.gameMask.active = false;
- this.nextPolice.active = false;
- }
- getTargetNode(index) {
- let targetNode: Node = null;
- switch (index) {
- case 1:
- targetNode = this.top;
- break;
- case 2:
- targetNode = this.right;
- break;
- case 3:
- targetNode = this.bottom;
- break;
- case 4:
- targetNode = this.left;
- break;
- }
- return targetNode;
- }
- randomData(num) {
- let arr = [];
- while (arr.length < num) {
- let randomNumber = Math.floor(Math.random() * 4) + 1;
- if (arr.indexOf(randomNumber) == -1) {
- arr.push(randomNumber);
- }
- }
- return arr;
- }
- onTouchStart(event: EventTouch) {
- }
- getTheMassesNumFromServer() {
- let currentLevelData = LevelManager.getInstance().getLevelData(this._currentLevel);
- return currentLevelData.theMassesNum || 1;
- }
- selectEffect() {
- this.audioSource.playOneShot(this.audioClipList[0], 1);
- }
- selectPoliceComplete(data) {
- if (data == GameConfig.selectType.Success) {
- this.gameRoot.active = false;
- this.nextThief.active = true;
- this.nextPolice.active = false;
- this.gameMask.active = false;
- let targetParent = this.getTargetNode(GameConfig.gameSaveData["targetDir"]);
- if (targetParent) {
- let playerItemSc = targetParent.getComponentInChildren(PlayerItem);
- if (playerItemSc.curPlayerType == GameConfig.PlayerType.Thief) {
- this.showTips("请选择小偷出现的方向");
- } else {
- switch (playerItemSc.massesID) {
- case 0:
- this.showTips("请选择小摊车出现的方向");
- break;
- case 1:
- this.showTips("请选择老爷爷和老奶奶出现的方向");
- break;
- case 2:
- this.showTips("请选择悲伤老爷爷出现的方向");
- break;
- case 3:
- this.showTips("请选择卖煎饼师傅出现的方向");
- break;
- case 4:
- this.showTips("请选择微笑老爷爷出现的方向");
- break;
- }
- }
- }
- } else {
- this.gameRoot.active = false;
- this.nextThief.active = false;
- this.nextPolice.active = false;
- this.gameMask.active = true;
- this.gameMask.emit("init", GameConfig.selectType.Failed);
- }
- }
- selectThiefComplete(data) {
- if (data == GameConfig.selectType.Success) {
- this.gameRoot.active = false;
- this.nextThief.active = false;
- this.nextPolice.active = false;
- this.gameMask.active = true;
- this.gameMask.emit("init", GameConfig.selectType.Success);
- GameManager.getInstance().addScore(20);
- setTimeout(() => { this.audioSource.playOneShot(this.audioClipList[1], 1); }, 200);
- } else {
- this.gameRoot.active = false;
- this.nextThief.active = false;
- this.nextPolice.active = false;
- this.gameMask.active = true;
- this.gameMask.emit("init", GameConfig.selectType.Failed);
- }
- }
- gameContinue(data) {
- this.gameRoot.active = true;
- this.nextThief.active = false;
- this.nextPolice.active = false;
- this.gameMask.active = false;
- if (GameManager.getInstance().isStartGame) {
- this.generateCurrentGameData(this.theMassesNum);
- }
- }
- showTips(str: string) {
- let tip = new EventCustom(Global.EventType_ShowTips, true, str);
- this.node.dispatchEvent(tip);
- }
- update(deltaTime: number) {
- }
- }
|