WaterControl/assets/Script/ControlManager.ts
2024-08-16 14:17:58 +08:00

179 lines
5.7 KiB
TypeScript

// Learn TypeScript:
// - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
// - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
// - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html
const {ccclass, property} = cc._decorator;
@ccclass
export default class NewClass extends cc.Component {
@property(cc.Node)
Map: cc.Node = null;
@property(cc.Prefab)
tip: cc.Prefab = null;
@property(cc.Prefab)
reinforce: cc.Prefab = null;
@property(cc.Prefab)
soil: cc.Prefab = null;
tipArray:any;
controlArray:any;
canTouch:boolean;
Reinforce:boolean;
Soil:boolean;
mapHeight:number;
// LIFE-CYCLE CALLBACKS:
// onLoad () {}
start () {
this.tipArray = [];
this.controlArray = [];
this.canTouch = true;
this.Reinforce = false;
this.Soil = false;
this.mapHeight = 0;
}
setPosition(tip){
tip.setPosition(30,-25);
if(this.tipArray.length > 0){
let length = this.tipArray.length+1;
let posY = Math.ceil(length/8) - 1;
let posX = length - Math.floor(posY)*8 - 1;
tip.setPosition(30 + 48*posX,-25 -48*posY + this.mapHeight);
}
}
//清空所有
removeAllTip(){
if(!this.canTouch) return;
if(this.tipArray.length > 0){
cc.fx.AudioManager._instance.playEffect("qingkong",null);
for(let i=0; i<this.tipArray.length;i++){
let tip = this.tipArray[i];
tip.active = false;
tip.removeFromParent(this.Map);
tip = null;
}
this.tipArray = [];
this.controlArray = [];
var drawingReset = cc.fx.GameConfig.CLICK_DATA.drawingReset + 1;
cc.fx.GameConfig.CLICK_SET("drawingReset",drawingReset);
cc.fx.Notifications.emit(cc.fx.Message.removeTip,"remove");
this.mapHeight = 0;
}
}
//撤回一步
back_Click(){
if(!this.canTouch) return;
if(this.tipArray.length > 0){
cc.fx.AudioManager._instance.playEffect("chehui",null);
let tip = this.tipArray[this.tipArray.length-1];
tip.active = false;
tip.removeFromParent(this.Map);
tip = null;
this.tipArray.pop();
this.controlArray.pop();
var drawingBack = cc.fx.GameConfig.CLICK_DATA.drawingBack + 1;
cc.fx.GameConfig.CLICK_SET("drawingBack",drawingBack);
cc.fx.Notifications.emit(cc.fx.Message.removeTip,"back");
if(this.tipArray.length >= 24){
if((this.tipArray.length )%8 == 0){
this.mapMove(false);
}
}
}
}
//点击事件
btn_Click(target,data){
var GameManager = this.node.parent.getComponent("GameManager");
if(GameManager.btnClick == true){
GameManager.btnClick = false;
cc.fx.Notifications.emit(cc.fx.Message.guideNext);
}
cc.fx.Notifications.emit(cc.fx.Message.control,data);
if(!this.canTouch) return;
let prefab = this.tip;
if(data == "reinforce" || data == "soil"){
prefab = this[data];
if(data == "reinforce"){
if(this.Reinforce){
this.Reinforce = false;
this.back_Click();
return;
}else{
cc.fx.AudioManager._instance.playEffect("jineng",null);
this.Reinforce = true;
this.Soil = false;
}
}
else if(data == "soil"){
if(this.Soil){
this.Soil = false;
this.back_Click();
return;
}else{
cc.fx.AudioManager._instance.playEffect("jineng",null);
this.Soil = true;
this.Reinforce = false;
}
}
}
else{
cc.fx.AudioManager._instance.playEffect("fangxiang",null);
this.Reinforce = false;
this.Soil = false;
}
let tip = cc.instantiate(prefab);
if(data == "up") tip.angle = 180;
if(data == "left") tip.angle = -90;
if(data == "right") tip.angle = 90;
tip.parent = this.Map;
if(this.controlArray[this.controlArray.length-1] == "reinforce" ||
this.controlArray[this.controlArray.length-1] == "soil"){
if(data == "reinforce" || data == "soil"){
this.tipArray[this.tipArray.length-1].removeFromParent();
this.tipArray[this.tipArray.length-1] = null;
this.tipArray.pop();
this.controlArray.pop();
}
}
this.setPosition(tip);
this.tipArray.push(tip);
this.controlArray.push(data);
if(this.tipArray.length >= 25){
if((this.tipArray.length - 1)%8 == 0){
this.mapMove(true);
}
}
}
//地图放不下了上下移动
mapMove(type){
if(type) this.mapHeight += 48;
else this.mapHeight -= 48;
for(let i=0; i<this.tipArray.length;i++){
let tip = this.tipArray[i];
if(type)tip.y += 48;
else tip.y -= 48;
}
}
//点击开始 创建河道
start_Click(){
if(!this.canTouch) return;
this.canTouch = false;
cc.fx.AudioManager._instance.playEffect("build",null);
cc.fx.Notifications.emit(cc.fx.Message.startGame,this.controlArray);
}
// update (dt) {}
}