import { _decorator, Button, Collider2D, Component, Contact2DType, EPhysics2DDrawFlags, EventTouch, instantiate, IPhysics2DContact, Layout, log, Node, PhysicsSystem2D, Prefab, Sprite, SpriteAtlas, SpriteFrame, tween, UIOpacity, v3, Vec3 } from 'cc'; import { GameManager } from '../Manager/GameManager'; import { ColliderTag, Global } from '../Common/Global'; import { AudioManage } from '../Manager/AudioManage'; import { BallItem } from './BallItem'; const { ccclass, property } = _decorator; @ccclass('GameMain') export class GameMain extends Component { @property(Node) GouLanZi: Node = null; @property(Sprite) TargetColor: Sprite = null; @property(Node) Balls: Node = null; @property(Node) TargetBall: Node = null; @property(Node) WriteBall: Node = null; @property(Node) Mesh: Node = null; @property(SpriteAtlas) ItemsAtlas: SpriteAtlas = null; _targetColorIndex: number = 0; start() { // PhysicsSystem2D.instance.debugDrawFlags = EPhysics2DDrawFlags.Shape; PhysicsSystem2D.instance.enable = true; this.node.on(Node.EventType.TOUCH_MOVE, this.touchFun, this); // PhysicsSystem2D.instance } /** * 初始化逻辑 * 每次GameManager.StartGame()时都会调用 */ initUI() { this.Balls.removeAllChildren(); this._targetColorIndex = Global.getRandom(1, (this.ItemsAtlas.getSpriteFrames().length - 1) / 2 - 1); this.TargetColor.spriteFrame = this.ItemsAtlas.getSpriteFrame("Bar_" + this._targetColorIndex); this.schedule(this.tnUpdateTimer, GameManager.getInstance().curLevelData.targetUpdateTimer); this.schedule(this.wnUpdateTimer, GameManager.getInstance().curLevelData.wbUpdateTimer); } tnUpdateTimer() { let n = instantiate(this.TargetBall) if (n) { n.active = true; } n.getChildByName("BallSp").getComponent(Sprite).spriteFrame = this.ItemsAtlas.getSpriteFrame("Ball_" + this._targetColorIndex); this.Balls.addChild(n); let index = 0; do { index = Global.getRandom(0, 6) - 3; } while (!this.canCreateBall(index)); n.getComponent(BallItem)._index = index; n.position = v3(index * 166, 800, 0); this.tweenFun(n, true); } wnUpdateTimer() { let n = instantiate(this.WriteBall) if (n) { n.active = true; } let color; do { color = Global.getRandom(0, (this.ItemsAtlas.getSpriteFrames().length - 1) / 2 - 1); } while (color == this._targetColorIndex); n.getChildByName("BallSp").getComponent(Sprite).spriteFrame = this.ItemsAtlas.getSpriteFrame("Ball_" + color); this.Balls.addChild(n); let index = 0; do { index = Global.getRandom(0, 6) - 3; } while (!this.canCreateBall(index)); n.getComponent(BallItem)._index = index; n.position = v3(index * 166, 800, 0); this.tweenFun(n, false); } canCreateBall(index: number): boolean { let can = true; for (let i = 0; i < this.Balls.children.length; i++) { if (this.Balls.children[i].getComponent(BallItem)._index == index) { can = false; break; } } log(index + " " + can); return can } touchFun(e) { let deltaX = e.getUIDelta().x this.GouLanZi.position = new Vec3(this.GouLanZi.position.x + deltaX, this.GouLanZi.position.y, this.GouLanZi.position.z); if (this.GouLanZi.position.x < -570) { this.GouLanZi.position = new Vec3(-570, this.GouLanZi.position.y, this.GouLanZi.position.z); } if (this.GouLanZi.position.x > 570) { this.GouLanZi.position = new Vec3(570 + deltaX, this.GouLanZi.position.y, this.GouLanZi.position.z); } } tweenFun(node: Node, isTar): void { tween(node).to(GameManager.getInstance().curLevelData.dropTime, { position: new Vec3(node.position.x, -800, node.position.z), angle: Math.round(Math.random() * 180 - 90) }, { easing: "quadIn" }).call(() => { if (node && node.parent) { node.parent.removeChild(node); node.destroy(); } }).start(); } /** * 游戏结束后清理 */ gameOver() { this.unschedule(this.tnUpdateTimer); this.unschedule(this.wnUpdateTimer); this.Balls.removeAllChildren(); } update(deltaTime: number) { } }