cb/assets/Script/Wall.ts
2025-10-23 18:17:40 +08:00

678 lines
25 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
import MapConroler from "./Map";
import NumberToImage from "./NumberToImage";
const { ccclass, property } = cc._decorator;
export enum WallSpecial {
/*普通门*/
"普通门" = 0,
/*星星门*/
"星星门" = 1,
/*开关门*/
"开关门" = 2,
/*冻结门 */
"冻结门" = 3,
/*冻结门 */
"旋转门顺时针" = 4,
/*旋转门逆时针*/
"旋转门逆时针" = 5,
/*上锁门*/
"上锁门" = 6,
}
export enum WallType {
/*普通地块 */
"门横向下" = 0,
/*起点地块 */
"门横向上" = 1,
/*湿地 */
"门竖向右" = 2,
/*山峰 */
"门竖向左" = 3,
/*终点地块 */
"墙横向下" = 4,
/*息壤 */
"墙横向上" = 5,
/*加固 */
"墙竖向右" = 6,
/*加固 */
"墙竖向左" = 7,
}
export enum WallColor {
/*普通地块 */
"紫色" = 0,
/*湿地 */
"黄色" = 1,
/*山峰 */
"绿色" = 2,
/*终点地块 */
"蓝色" = 3,
/*息壤 */
"粉色" = 4,
/*加固 */
"橘黄色" = 5,
/*加固 */
"青色" = 6,
/*加固 */
"白色" = 7,
/*普通地块 */
"红色" = 8,
/*普通地块 */
"灰色" = 9,
"暗" = 10,
"默认色" = 11,
}
@ccclass
export default class Wall extends cc.Component {
@property(cc.Label)
number: cc.Label = null;
@property({
tooltip: '墙或者门的方向',
type: cc.Enum(WallType),
})
type: WallType = WallType.;
@property({
tooltip: '墙或者门的方向',
type: cc.Enum(WallSpecial),
})
special: WallSpecial = WallSpecial.;
@property({
tooltip: '门的颜色',
type: cc.Enum(WallColor),
})
color: WallColor = WallColor.;
@property(cc.Node)
maskNode: cc.Node = null; // 遮罩节点
@property(cc.Float)
maskWidth: number = 138; // 遮罩宽度
@property(cc.Float)
maskHeight: number = 69; // 遮罩高度
@property(cc.SpriteAtlas)
wall_SpriteFrames: cc.SpriteAtlas = null;
@property(cc.SpriteAtlas)
down_SpriteFrames: cc.SpriteAtlas = null;
@property(cc.Prefab)
freezeSpine: cc.Prefab = null;
posX: number;
posY: number;
direction: any;
wall_Info: any;
teamDoors: any;
openNode: cc.Node;
revolvingNode: cc.Node;
freezeNode: cc.Node;
open: boolean;
freezeNumber: number;
num: number;
leftTween: any;
rightTween: any;
colorArray: any;
// LIFE-CYCLE CALLBACKS:
// onLoad () {}
start() {
// console.log(this.type);
}
jsonDeepClone<T>(obj: T): T {
return JSON.parse(JSON.stringify(obj));
}
init(wall_Info, posX: number, posY: number, direction: any) {
this.colorArray = [];
this.wall_Info = this.jsonDeepClone(wall_Info);
// this.open = true;
if (wall_Info == null) {
this.posX = posX;
this.posY = posY;
if (direction) this.direction = direction;
// console.log("门方向赋值",direction);
if (direction == "up") {
this.node.parent.zIndex = 100 + this.posX - this.posY * 3;
}
else if (direction == "down" || direction == "right") {
this.node.parent.zIndex = 20 + this.posX - this.posY * 3;
}
else if (direction == "downleft" || direction == "downright" || direction == "rightup"
|| direction == "rightdown") {
this.node.parent.zIndex = 20 + this.posX - this.posY * 3;
}
else if (direction == "left" || direction == "leftup" || direction == "upleft") {
this.node.parent.zIndex = 70 + this.posX - this.posY * 3;
}
else if (direction == "leftdown" || direction == "upright") {
this.node.parent.zIndex = 100 + this.posX - this.posY * 3;
}
else this.node.parent.zIndex = 70 + this.posX - this.posY * 3;
MapConroler._instance.mapBlocksWall[this.posX][this.posY].getComponent("MapBlock").block_Id = "Wall";
//console.log(this.posX,this.posY,MapConroler._instance.mapBlocksWall[this.posX][this.posY].getComponent("MapBlock").block_Id);
// console.log(this.posX,this.posY,this.node.zIndex);
//this.node.getChildByName("num").getComponent(cc.Label).string = direction;
//this.node.getChildByName("num").getComponent(cc.Label).string = ":" + this.node.parent.zIndex;
}
if (wall_Info != null) {
this.color = this.wall_Info.color;
this.special = this.wall_Info.special;
this.num = this.wall_Info.num;
let name2 = "rotate" + this.wall_Info.length;
this.revolvingNode = this.node.parent.getChildByName("revolving").getChildByName(name2);
if (this.wall_Info.colorArray) {
this.colorArray = this.wall_Info.colorArray.split('').map(char => parseInt(char) + 1);
MapConroler._instance.changeColor = true;
MapConroler._instance.colorDoors.push(this);
}
if (this.revolvingNode) {
this.revolvingNode.parent.zIndex = 999;
this.revolvingNode.active = false;
}
this.initType();
if (this.wall_Info.length > 0) {
this.initColor(this.wall_Info.length);
MapConroler._instance.wall_Pass.push(this);
}
else this.node.removeComponent("cc.Sprite");
if (this.posX != null) {
MapConroler._instance.mapBlocksWall[this.posX][this.posY].getComponent("MapBlock").block_Id = "Wall";
//console.log(this.posX,this.posY,MapConroler._instance.mapBlocksWall[this.posX][this.posY].getComponent("MapBlock").block_Id);
}
}
// setTimeout(() => {
// this.node.getChildByName("num").getComponent(cc.Label).string = ":" + this.node.parent.zIndex;
// }, 1000);
}
setTeamDoors() {
if (this.wall_Info.length > 0) {
MapConroler._instance.teamDoors = [];
}
MapConroler._instance.teamDoors.push(this);
this.teamDoors = MapConroler._instance.teamDoors;
}
//创建门的颜色
initColor(length: number) {
let direction = this.node.parent.name;
let double = 0;
if (direction == "left" || direction == "right") {
double = 3;
}
if (this.wall_SpriteFrames) {
let name = this.color + "color" + (length + double);
var spriteFrame = this.wall_SpriteFrames._spriteFrames[name];
if (this.node.getComponent(cc.Sprite)) this.node.getComponent(cc.Sprite).spriteFrame = spriteFrame;
if (this.node.getChildByName("icon")) this.node.getChildByName("icon").getComponent(cc.Sprite).spriteFrame = spriteFrame;
}
if (this.down_SpriteFrames) {
let name2 = this.color + "down" + (length + double);
var downFrame = this.down_SpriteFrames._spriteFrames[name2];
this.node.parent.getChildByName("down").getComponent(cc.Sprite).spriteFrame = downFrame;
}
}
//创建特殊类型门
initType() {
switch (this.special) {
case WallSpecial.星星门:
let star = cc.instantiate(MapConroler._instance.Block_Prop[this.special]);
star.parent = this.node.parent;
// console.log("门的方向",this.direction,"长度",this.wall_Info.length);
// star.scaleX = star.scaleY = 0.5;
if (this.wall_Info.length > 0) {
if (this.direction == "right" || this.direction == "left") {
star.children[this.wall_Info.length + 2].active = true;
}
else if (this.direction == "up" || this.direction == "down") {
star.children[this.wall_Info.length - 1].active = true;
}
}
star.setPosition(this.node.width / 2 + this.node.x, this.node.height / 2 + this.node.y);
break;
case WallSpecial.开关门:
let name = "open" + this.wall_Info.length;
this.openNode = this.node.parent.getChildByName("open").getChildByName(name);
this.openNode.active = true;
if (this.wall_Info.lock == false) {
this.open = true;
this.openNode.children[0].scaleX *= 0.01;
this.openNode.children[1].scaleX *= 0.01;
}
else {
this.open = false;
}
break;
case WallSpecial.冻结门:
let freeze = "freeze" + this.wall_Info.length;
this.freezeNode = this.node.parent.getChildByName("freeze").getChildByName(freeze);
this.freezeNode.active = true;
if (this.wall_Info.freeze) {
this.freezeNumber = this.wall_Info.freeze;
NumberToImage.numberToImageNodes(this.freezeNumber, 20, 8, "lock_", this.freezeNode.getChildByName("num"), false);
}
break;
case WallSpecial.旋转门顺时针: case WallSpecial.旋转门逆时针:
if (this.wall_Info.length > 0) {
// let name2 = "rotate" + this.wall_Info.length;
// this.revolvingNode = this.node.parent.getChildByName("revolving").getChildByName(name2);
this.revolvingNode.active = true;
// if (this.special == WallSpecial.旋转门逆时针) {
// this.revolvingNode.children[0].scaleX = -this.revolvingNode.children[0].scaleX;
// }
}
break;
case WallSpecial.上锁门:
let lock = cc.instantiate(MapConroler._instance.Block_Prop[3]);
lock.parent = this.node.parent;
if (this.wall_Info.length == 0) {
lock.opacity = 0;
}
lock.scaleX = lock.scaleY = 0.65;
if (this.direction == "up") {
lock.setPosition((this.wall_Info.length - 1) * 60 - 10, 38);
if (this.wall_Info.length == 1) lock.x += 5;
}
else if (this.direction == "down") {
lock.setPosition((this.wall_Info.length - 1) * 60 - 10, -38);
if (this.wall_Info.length == 1) lock.x += 5;
}
if (this.direction == "right") {
lock.setPosition(30, (this.wall_Info.length - 1) * 60);
if (this.wall_Info.length == 1) lock.y += 10;
}
else if (this.direction == "left") {
lock.setPosition(-46, (this.wall_Info.length - 1) * 60);
if (this.wall_Info.length == 1) lock.y += 10;
}
lock.getComponent("Lock").init(this.wall_Info.lockTime, "wall");
break;
}
if (this.wall_Info.colorArray) {
if (this.wall_Info.length > 0) {
this.maskNode.active = true;
this.setMask();
}
}
}
//播放星星门通过
playStarDoor() {
if (this.node.parent.getChildByName("star")) {
let star = this.node.parent.getChildByName("star");
for (let i = 0; i < star.children.length; i++) {
if (star.children[i].active == true) {
let starChild = star.children[i];
for (let j = 0; j < starChild.children.length; j++) {
starChild.children[j].active = true
starChild.children[j].getComponent(sp.Skeleton).setAnimation(1, "taopao1", false);
}
}
}
}
}
//改变开关门状态
changeLock() {
this.open = !this.open;
// console.log("开关门状态改变", this.open);
if (!this.openNode.active) {
this.openNode.active = true;
}
// 停止之前的动画
if (this.leftTween) {
this.leftTween.stop();
this.leftTween = null;
}
if (this.rightTween) {
this.rightTween.stop();
this.rightTween = null;
}
if (this.open == true) {
if (this.openNode.children[0].scaleX < 1 && this.openNode.children[0].scaleX > 0.01) {
this.openNode.children[0].scaleX = 1;
}
}
else {
if (this.openNode.children[0].scaleX < 1 && this.openNode.children[0].scaleX > 0.01) {
this.openNode.children[0].scaleX = 0.01;
}
}
let fill = this.openNode.children[0].scaleX == 1 ? 0.01 : 1;
if (this.openNode.children[0].scaleX < 0) fill = -fill;
// console.log("目标",fill);
// 存储动画实例以便后续停止
this.leftTween = cc.tween(this.openNode.children[0])
.to(0.3, { scaleX: this.openNode.children[0].scaleX < 0 ? -fill : fill })
.call(() => {
// console.log("左边完成");
this.leftTween = null; // 动画完成后清除引用
})
.start();
this.rightTween = cc.tween(this.openNode.children[1])
.to(0.3, { scaleX: this.openNode.children[1].scaleX < 0 ? -fill : fill })
.call(() => {
// console.log("右边完成");
this.rightTween = null; // 动画完成后清除引用
})
.start();
}
changeFreeze() {
this.freezeNumber -= 1;
if (this.freezeNumber == 0) {
// console.log(this.node.uuid);
// this.freezeNode.parent.active = false;
this.resetFreeze();
}
else {
NumberToImage.numberToImageNodes(this.freezeNumber, 20, 8, "lock_", this.freezeNode.getChildByName("num"), false);
}
}
downDoor() {
if (this.openNode) {
if (this.openNode.children[0].scaleX == 1)
return;
}
if (this.freezeNode) {
if (this.freezeNode.active == true)
return;
}
if (this.special == 4 || this.special == 5 || this.special == 6) {
return;
}
this.node.opacity = 0;
if (this.special == WallSpecial.) {
this.node.parent.getChildByName("star").y -= 10;
}
this.node.parent.getChildByName("down").active = true;
if (this.colorArray.length > 0) {
let direction = this.node.parent.name;
let double = 0;
if (direction == "left" || direction == "right") {
double = 3;
}
let color = this.colorArray[1];
if (this.wall_SpriteFrames) {
let name = color + "down" + (this.wall_Info.length + double);
var spriteFrame = this.down_SpriteFrames._spriteFrames[name];
this.maskNode.getChildByName("color_icon").getComponent(cc.Sprite).spriteFrame = spriteFrame;
}
}
}
upDoor() {
if (this.special == WallSpecial.) {
this.node.parent.getChildByName("star").y += 10;
}
this.node.parent.getChildByName("down").active = false;
if (this.colorArray.length > 0) {
let direction = this.node.parent.name;
let double = 0;
if (direction == "left" || direction == "right") {
double = 3;
}
if (this.colorArray.length > 1) {
let color = this.colorArray[1];
if (this.wall_SpriteFrames) {
let name = color + "color" + (this.wall_Info.length + double);
var spriteFrame = this.wall_SpriteFrames._spriteFrames[name];
this.maskNode.getChildByName("color_icon").getComponent(cc.Sprite).spriteFrame = spriteFrame;
}
}
else {
this.maskNode.getChildByName("color_icon").opacity = 0;
}
}
this.node.opacity = 250;
}
resetFreeze() {
this.special = 0;
if (this.freezeNode) {
cc.fx.AudioManager._instance.playEffect("freezeDoor", null);
this.freezeNode.active = false;
}
// if (this.wall_Info.length != 0) {
// let spine = cc.instantiate(this.freezeSpine);
// spine.parent = this.node.parent;
// spine.setPosition(this.node.width / 2 + this.node.x + 60 / 2 * (this.wall_Info.length - 1), this.node.height / 2 + this.node.y);
// spine.getComponent(sp.Skeleton).setAnimation(1, "bingkuai", false);
// console.log("添加一个动画", spine.getPosition());
// }
}
//旋转门旋转
changeRevolvingWall() {
if (this.revolvingNode) {
// console.log("自身编号", this.wall_Info.num, this.special);
if (this.special == WallSpecial. || this.special == WallSpecial.) {
this.revolvingNode.active = true;
}
else {
this.revolvingNode.active = false;
}
}
}
//变色门变色总方法
changeColorWall() {
// console.log("通过此门,调用次门数组", this.colorArray);
// console.log("改變顏色:", this.wall_Info.length);
for (let i = 0; i < this.teamDoors.length; i++) {
setTimeout(() => {
this.teamDoors[i].changeColor();
}, i * 10);
}
}
//变色门变色具体执行方法
changeColor() {
if (this.colorArray.length > 1 && this.color == this.colorArray[0]) {
let firstItem = this.colorArray.shift(); // 移除第一项
this.colorArray.push(firstItem); // 将第一项添加到数组末尾
}
// else if (this.colorArray.length == 1 && this.color != this.colorArray[0]) {
// this.updateColor();
// }
this.updateColor();
}
updateColor() {
this.color = this.colorArray[0];
// console.log("改變顏色", this.color, this.colorArray);
// debugger;
let direction = this.node.parent.name;
let double = 0;
if (direction == "left" || direction == "right") {
double = 3;
}
if (this.wall_Info.length > 0) {
this.upDoor();
this.setMask();
if (this.wall_SpriteFrames) {
let name = this.color + "color" + (this.wall_Info.length + double);
// console.log("变色名称:", name);
var spriteFrame = this.wall_SpriteFrames._spriteFrames[name];
if (this.node.getComponent(cc.Sprite)) this.node.getComponent(cc.Sprite).spriteFrame = spriteFrame;
if (this.node.getChildByName("icon")) this.node.getChildByName("icon").getComponent(cc.Sprite).spriteFrame = spriteFrame;
this.node.getComponent(cc.Sprite).fillRange = 0;
cc.tween(this.node.getComponent(cc.Sprite))
.to(0.2, { fillRange: 1 })
.start();
}
if (this.down_SpriteFrames) {
let name2 = this.color + "down" + (this.wall_Info.length + double);
// console.log("按下名称:", name2);
var downFrame = this.down_SpriteFrames._spriteFrames[name2];
this.node.parent.getChildByName("down").getComponent(cc.Sprite).spriteFrame = downFrame;
this.node.parent.getChildByName("down").getComponent(cc.Sprite).fillRange = 0;
cc.tween(this.node.parent.getChildByName("down").getComponent(cc.Sprite))
.to(0.2, { fillRange: 1 })
.start();
}
}
}
//变色门删除颜色
removeColor(color, type) {
if (this.colorArray.length > 1) {
let index = this.colorArray.indexOf(color);
if (index !== -1) {
this.colorArray.splice(index, 1);
if (type) this.updateColor();
}
}
}
//添加遮罩
setMask() {
let direction = this.node.parent.name;
let double = 0;
if (direction == "left" || direction == "right") {
double = 3;
}
if (this.colorArray.length > 1) {
let color = this.colorArray[1];
if (this.wall_SpriteFrames) {
let name = color + "color" + (this.wall_Info.length + double);
var spriteFrame = this.wall_SpriteFrames._spriteFrames[name];
this.maskNode.getChildByName("color_icon").getComponent(cc.Sprite).spriteFrame = spriteFrame;
this.maskNode.getChildByName("color_icon").getComponent(cc.Sprite).fillRange = 0;
cc.tween(this.maskNode.getChildByName("color_icon").getComponent(cc.Sprite))
.to(0.2, { fillRange: 1 })
.start();
}
if (this.down_SpriteFrames) {
let name2 = color + "down" + (this.wall_Info.length + double);
var downFrame = this.down_SpriteFrames._spriteFrames[name2];
this.maskNode.getChildByName("color_down").getComponent(cc.Sprite).spriteFrame = downFrame;
this.maskNode.getChildByName("color_down").getComponent(cc.Sprite).fillRange = 0;
cc.tween(this.maskNode.getChildByName("color_down").getComponent(cc.Sprite))
.to(0.2, { fillRange: 1 })
.start();
}
}
else {
this.maskNode.getChildByName("color_icon").opacity = 0;
}
// 设置遮罩节点大小和位置
if (this.node.parent.name == "down") {
if (this.wall_Info.length == 3) {
this.maskNode.width = 290;
this.maskNode.height = 69;
this.maskNode.position = cc.v3(116, -39, 0); // 居中
}
else if (this.wall_Info.length == 2) {
this.maskNode.width = 178;
this.maskNode.height = 69;
this.maskNode.position = cc.v3(54, -39, 0); // 居中
}
else if (this.wall_Info.length == 1) {
this.maskNode.width = 70;
this.maskNode.height = 69;
this.maskNode.position = cc.v3(-7, -39, 0); // 居中
}
}
else if (this.node.parent.name == "up") {
if (this.wall_Info.length == 3) {
this.maskNode.width = 290;
this.maskNode.height = 69;
this.maskNode.position = cc.v3(116, 34, 0); // 居中
}
else if (this.wall_Info.length == 2) {
this.maskNode.width = 178;
this.maskNode.height = 69;
this.maskNode.position = cc.v3(56, 34, 0); // 居中
}
else if (this.wall_Info.length == 1) {
this.maskNode.width = 70;
this.maskNode.height = 69;
this.maskNode.position = cc.v3(-2.44, 34, 0); // 居中
}
}
else if (this.node.parent.name == "right") {
if (this.wall_Info.length == 3) {
this.maskNode.width = 69;
this.maskNode.height = 290;
this.maskNode.position = cc.v3(37, 120, 0); // 居中
}
else if (this.wall_Info.length == 2) {
this.maskNode.width = 69;
this.maskNode.height = 178;
this.maskNode.position = cc.v3(37, 60, 0); // 居中
}
else if (this.wall_Info.length == 1) {
this.maskNode.width = 69;
this.maskNode.height = 70;
this.maskNode.position = cc.v3(37, 0, 0); // 居中
this.maskNode.anchorY = 0.35;
}
}
else if (this.node.parent.name == "left") {
if (this.wall_Info.length == 3) {
this.maskNode.width = 69;
this.maskNode.height = 290;
this.maskNode.position = cc.v3(-39.5, 120, 0); // 居中
}
else if (this.wall_Info.length == 2) {
this.maskNode.width = 69;
this.maskNode.height = 178;
this.maskNode.position = cc.v3(-39.5, 60, 0); // 居中
}
else if (this.wall_Info.length == 1) {
this.maskNode.width = 69;
this.maskNode.height = 70;
this.maskNode.position = cc.v3(-39.5, 0, 0); // 居中
this.maskNode.anchorY = 0.35;
}
}
}
// update (dt) {}
}