import GamePage from "../GamePage"; import GameConfig from "../../GameConfig"; const {ccclass, property} = cc._decorator; /**方块基类 */ @ccclass export default class Block extends cc.Component { @property({type: cc.Sprite}) private face: cc.Sprite = null; @property({type: cc.SpriteFrame}) private face_sp: cc.SpriteFrame[] = []; private type: Type; private config: BlockConfig; beat: number;//节奏时间点 index: number; startTime: number = 0; existTime: number = 0; hasLogReachTime: boolean = false; static seed: number = 0; init(beat: number) { this.beat = beat; let random = Math.random(); this.index = random < 0.3 ? 0 : (random < 0.8 ? 1 : 2); this.startTime = beat - GameConfig.block_come_time; this.existTime = 0; this.hasLogReachTime = false; //如果长节拍存在,之后的方块节拍会在别的道路产出,不会占用同一道路 if (GamePage.instance.block_long.visiable) { let indexes = []; for (let i = 0; i < 3; i++) { if (i != GamePage.instance.block_long.index) { indexes.push(i); } } this.index = indexes[Math.random() * indexes.length << 0]; } this.config = GameConfig.block_configs[this.index]; this.type = Block.seed % 2 == 0 ? Type.blue : Type.orange; Block.seed++; if (this.index == 1) { if (this.type == Type.blue) { this.face.spriteFrame = this.face_sp[0]; this.face.node.setPosition(21.216, -104.576); //纠正新美术资源偏差 } else if (this.type == Type.orange) { this.face.spriteFrame = this.face_sp[2]; this.face.node.setPosition(0, -97.419); //纠正新美术资源偏差 } } else { if (this.type == Type.blue) { this.face.spriteFrame = this.face_sp[1]; this.face.node.setPosition(-8, -104.69); //纠正新美术资源偏差 } else if (this.type == Type.orange) { this.face.spriteFrame = this.face_sp[3]; this.face.node.setPosition(0, -97.69); //纠正新美术资源偏差 } } this.node.setPosition(this.config.first_pos); this.node.setScale(this.config.first_scale); if (GameConfig.block_motion_type < 0) { this.node.runAction(cc.sequence( cc.spawn( cc.moveTo( this.config.move_time, this.config.final_pos ).easing(cc.easeIn(this.config.ease_rate)), cc.scaleTo( this.config.move_time, this.config.final_scale.x, this.config.final_scale.y ).easing(cc.easeIn(this.config.ease_rate)) ), cc.callFunc(this.removeSelf, this) )); } } update(dt: number) { this.existTime += dt; this.node.zIndex = cc.winSize.height / 2 - this.node.y; if (GameConfig.block_motion_type >= 0) { let audioCurrentTime = cc.audioEngine.getCurrentTime(GamePage.instance.music_id); let t = 0; if (audioCurrentTime == 0) { t = Date.now() - GamePage.instance.startTime - this.startTime; } else { t = audioCurrentTime - this.startTime; } let distance = GameConfig.seekDistance(this.config.acc, t); let vector = this.config.final_pos.sub(this.config.first_pos); let mul = distance / vector.mag(); if (mul < 1) { this.node.setPosition(this.config.first_pos.add(vector.mul(mul))); this.node.setScale(this.config.final_scale.sub(this.config.first_scale).mul(mul)); if (this.existTime > 5) { this.removeSelf(); } } else { this.removeSelf(); } } //输出方块到达击打线的所需时间 if (CC_DEBUG && !this.hasLogReachTime && this.node.y <= GameConfig.end_line_y + 3) { this.hasLogReachTime = true; // console.log(this.existTime); } } removeSelf() { GamePage.instance.putBlock(this.node); GamePage.instance.miss(); GamePage.instance.countOverBeat(1); } /**判断是否在击打区域 */ canHit(index: number) { let faultTolerance = 120;//容错距离 if ( this.index == index && this.node.y <= GameConfig.end_line_y + faultTolerance * 1.5 && this.node.y > GameConfig.fianl_line_y - faultTolerance * 0.5 ) { return true; } else { return false; } } /**添加打击碰撞特效 */ addCollision() { this.node.stopAllActions(); GamePage.instance.putBlock(this.node); // GamePage.instance.hit(); // GamePage.instance.countHitBeat(1); GamePage.instance.countOverBeat(1); let c: dragonBones.ArmatureDisplay = null; if (this.type == Type.blue) { c = GamePage.instance.collision1s[this.index]; } else if (this.type == Type.orange) { c = GamePage.instance.collision2s[this.index]; } c.node.active = true; let config = GameConfig.collision_configs[this.index]; // let rate = (GameConfig.end_line_y - this.node.y) / (GameConfig.end_line_y + cc.winSize.height / 2); // let scale = rate * 0.2 + 1; // c.node.setPosition(this.node.getPosition()); // c.node.setScale(config.scaleX * scale, config.scaleY * scale); // c.node.setScale(config.scaleX, config.scaleY); // c.node.setPosition(this.node.x, GameConfig.end_line_y); c.node.setScale(config.scaleX * this.node.scaleY, config.scaleY * this.node.scaleY); c.node.setPosition(this.node.x, this.node.y + 50 * this.node.scaleY); c.playAnimation("01", 1); } } enum Type { blue, orange }