2025-04-24 17:03:28 +08:00

386 lines
14 KiB
JavaScript

import HSL from "../ts/gear_2.3.4/shader/script/HSL";
import GameModel from "../ts/core/GameModel";
import ShapeUtil from "../ts/shape/ShapeUtil";
import SKLogger from "../ts/gear_2.3.4/util/SKLogger";
import WeaponUtil from "../ts/weapon/WeaponUtil";
import WingUtil from "../ts/wing/WingUtil";
cc.Class({
extends: cc.Component,
properties: {
animationEFNode: cc.Node,
animationNode: cc.Node,
maskNode: cc.Node,
nameNode: cc.Label,
weaponNode: cc.Node,
weaponNode1: cc.Node,
effNode: cc.Node,
RoleshadowNode: cc.Node,
shadowNode: cc.Node,
},
onLoad() {
this.node.on(cc.Node.EventType.TOUCH_START, this.touchBegan.bind(this));
},
offTouchRole() {
this.node.off(cc.Node.EventType.TOUCH_START, this.touchBegan.bind(this));
},
setInfo(objinfo, nameshow = false) {
this.resid = objinfo.resid;
this.nameNode.string = objinfo.name;
this.nameNode.node.active = nameshow;
this.weapon = objinfo.weapon;
this.petColor = objinfo.petcolor;
this.bodyEffectId = objinfo.bodyEffectId
this.wingId = objinfo.wingId
let temp = GameModel.conf_res_anchor[this.resid];
if (temp) {
let anchorY = temp.anchorY;
let anchorX = temp.anchorX;
this.animationNode.anchorY = anchorY;
this.animationNode.anchorX = anchorX;
if (this.resid<=4038){
if (this.maskNode)
this.maskNode.anchorY = anchorY;
this.maskNode.anchorX = anchorX;
if (this.effNode)
this.effNode.anchorY = anchorY;
this.effNode.anchorX = anchorX;
if (this.RoleshadowNode)
this.RoleshadowNode.anchorY = anchorY;
this.RoleshadowNode.anchorX = anchorX;
if (this.weaponNode)
this.weaponNode.anchorY = anchorY;
this.weaponNode.anchorX = anchorX;
}
}
//設置寵物顏色
let hsl = this.animationNode.getComponent(HSL);
if (hsl) {
if (this.petColor != 0 && this.petColor != -1) {
hsl.dH = this.petColor;
} else {
hsl.dH = 0;
}
hsl.reset();
}
if (this.maskNode && (objinfo.color1 != 0 || objinfo.color2 != 0)) {
this.showColorMask1(this.hxcolorTransfrom(objinfo.color1), this.hxcolorTransfrom(objinfo.color2))
}
// this.animationNode.opacity = 0;
this.playAnimation('stand');
if (this.resid<=4038){
this.shadowNode.active = false
this.RoleshadowNode.active = true
ShapeUtil.playshadowAni(this.RoleshadowNode, this.resid, 1, 'stand', this);
if(this.resid == 3005){
this.effNode.position = cc.v3(15, -125, 0);
this.effNode.scale = 1.5;
}else{
this.effNode.position = cc.v3(-5, -75, 0);
this.effNode.scale = 1;
}
}else{
this.shadowNode.active = true
this.RoleshadowNode.active = false
}
if(this.bodyEffectId){
this.playBodyEff(this.bodyEffectId);
}
if(this.wingId){
console.log("play wini....", this.wingId)
WingUtil.playAni(this.node, this.resid, 1, "stand", 0, this.wingId);
}
//this.addWeaponAnimation('stand', 1);
WeaponUtil.addWeaponAnimation(this.weapon, this.weaponNode, this.animationNode, this, this.resid, 'stand', 1, 2);
},
/*
* 顏色轉換
*/
hxcolorTransfrom(color) {
let num = color;
let r = Math.floor(num / 256 / 256);
let g = Math.floor(num / 256 % 256);
let b = Math.floor(num % 256);
return new cc.Color(r, g, b);
},
showColorMask1(color1, color2) {
if (!color1 || !color2) return
if (!this.maskNode) return
if (color1.r == 0 && color1.g == 0 && color1.b == 0 && color2.r == 0 && color2.g == 0 && color2.b == 0) {
this.maskNode.active = false;
} else {
this.maskNode.active = true;
}
let logic = this.maskNode.getComponent('role_color');
if (!logic) return;
if (color1.r != 0 || color1.g != 0 || color1.b != 0) {
let t = Math.floor(color1.r) * 1000000 + Math.floor(color1.g) * 1000 + Math.floor(color1.b);
logic.dH1 = t;
} else {
logic.dH1 = 0;
}
if (color2.r != 0 || color2.g != 0 || color2.b != 0) {
let t = Math.floor(color2.r) * 1000000 + Math.floor(color2.g) * 1000 + Math.floor(color2.b);
logic.dH2 = t;
} else {
logic.dH2 = 0;
}
},
isDoubleAnimation(resid) {
if (resid == 6303||resid == 5073||resid == 5071) return true
return false
},
playBodyEff(id) {
if (!this.effNode) return
if (id == 0) {
this.effNode.active = false;
return
}
var name = `${id}`;
let anim = this.effNode.getComponent(cc.Animation);
if (!anim) {
return;
}
let clips = anim.getClips();
for (let clip of clips) {
if (clip.name == name) {
this.effNode.active = true;
this.effNode.getComponent(cc.Animation).play(name);
return;
}
}
this.effNode.active = false;
let self = this;
let url = `body_eff/${name}`;
cc.loader.loadRes(url, cc.SpriteAtlas, (error, atlas) => {
if (error) {
cc.warn(`$警告:加載角色動畫資源失敗${url}`);
return;
}
let curFrames = atlas.getSpriteFrames();
let curClip = cc.AnimationClip.createWithSpriteFrames(curFrames, 10);
curClip.name = name;
curClip.wrapMode = cc.WrapMode.Loop;
let nodeAni = self.effNode.getComponent(cc.Animation);
if (nodeAni) {
nodeAni.addClip(curClip);
nodeAni.play(name);
}
self.effNode.active = true;
});
},
playDoubleAnimation(name) {
let self = this;
if (!this.animationEFNode) return;
let url = `shap/${this.resid}/${name}_1`;
let url2 = `shap/${this.resid}/${name}_1_addon`;
if(this.resid == 5073||this.resid == 5071){
url2 = `shap/${this.resid}/${name}_1_ef`;
}
cc.loader.loadRes(url, cc.SpriteAtlas, function (error, atlas) {
if (error) {
cc.warn(`$警告:加載資源${url}失敗!`);
return;
}
let curFrames = atlas.getSpriteFrames();
let fn = 10 / 12 * curFrames.length;
if (self.resid <= 4038){
fn = 15
}
let curClip = cc.AnimationClip.createWithSpriteFrames(curFrames, fn);
curClip.name = name;
curClip.wrapMode = cc.WrapMode.Loop;
if (!self.animationNode) {
console.warn("未找到動畫節點")
return
}
let nodeAni = self.animationNode.getComponent(cc.Animation);
nodeAni.addClip(curClip);
nodeAni.play(name);
self.scheduleOnce(() => {
self.animationNode.anchorY = 0.4;
self.animationNode.scaleX = 1;
self.animationNode.scaleY = 1;
self.animationNode.opacity = 255;
}, 0);
});
cc.loader.loadRes(url2, cc.SpriteAtlas, function (error, atlas) {
if (error) {
cc.warn(`$警告:加載資源${url}失敗!`);
return;
}
let curFrames = atlas.getSpriteFrames();
let curClip = cc.AnimationClip.createWithSpriteFrames(curFrames, 10 / 12 * curFrames.length);
curClip.name = name;
curClip.wrapMode = cc.WrapMode.Loop;
if (!self.animationEFNode) {
console.warn("未找到動畫節點")
return
}
let nodeAni = self.animationEFNode.getComponent(cc.Animation);
nodeAni.addClip(curClip);
nodeAni.play(name);
self.scheduleOnce(() => {
var scale = self.animationNode.getComponent(cc.Sprite).spriteFrame._originalSize.width / curFrames[0]._originalSize.width
self.animationEFNode.anchorY = 0.4;
self.animationEFNode.scaleX = scale;
self.animationEFNode.scaleY = scale;
self.animationEFNode.opacity = 255;
self.animationEFNode.active = true
}, 0);
});
},
playAnimation(name) {
if (this.isDoubleAnimation(this.resid)) {
this.playDoubleAnimation(name);
return;
}
if (this.animationEFNode)
this.animationEFNode.active = false;
let self = this;
let url = `shap/${this.resid}/${name}_1`;
let url2 = `shape_mask/${this.resid}/${name}_1`;
cc.loader.loadRes(url, cc.SpriteAtlas, function (error, atlas) {
if (error) {
cc.warn(`$警告:加載資源${url}失敗!`);
return;
}
let curFrames = atlas.getSpriteFrames();
let fn = 10 / 12 * curFrames.length;
if (self.resid <= 4038){
fn = 15
}
if (self.resid == 20058){
if(name == "stand"){
fn = 10
}
if(name == "run"){
fn = 5
}
}
let curClip = cc.AnimationClip.createWithSpriteFrames(curFrames, fn);
curClip.name = name;
curClip.wrapMode = cc.WrapMode.Loop;
if (!self.animationNode) {
console.warn("未找到動畫節點")
return
}
let nodeAni = self.animationNode.getComponent(cc.Animation);
nodeAni.addClip(curClip);
nodeAni.play(name);
self.scheduleOnce(() => {
if (self.resid == 20058||self.resid == 5063||self.resid == 5060) return
self.animationNode.scaleX = 500 / self.animationNode.width;
self.animationNode.scaleY = 500 / self.animationNode.height;
self.animationNode.opacity = 255;
if (!self.maskNode) return
}, 0);
});
cc.loader.loadRes(url2, cc.SpriteAtlas, function (error, atlas) {
if (error) {
return;
}
let curFrames = [];
for (let i = 1; ; i++) {
let frame = atlas.getSpriteFrame(i);
if (frame) curFrames.push(frame);
else break;
}
let fn = 10 / 12 * curFrames.length;
if (self.resid <= 4038){
fn = 15
}
let curClip = cc.AnimationClip.createWithSpriteFrames(curFrames, fn);
curClip.name = name;
curClip.wrapMode = cc.WrapMode.Loop;
let nodeAni = self.maskNode.getComponent(cc.Animation);
nodeAni.addClip(curClip);
nodeAni.play(name);
});
},
clear() {
let weaponNode = this.weaponNode;
if (weaponNode) {
let clips = weaponNode.getComponent(cc.Animation).getClips();
for (const clip of clips) {
weaponNode.getComponent(cc.Animation).stop(clip.name);
weaponNode.getComponent(cc.Animation).removeClip(clip, true);
}
}
let weaponNode1 = this.weaponNode1;
if (weaponNode1) {
let clips = weaponNode1.getComponent(cc.Animation).getClips();
for (const clip of clips) {
weaponNode1.getComponent(cc.Animation).stop(clip.name);
weaponNode1.getComponent(cc.Animation).removeClip(clip, true);
}
}
let animationEFNode = this.animationEFNode;
if (animationEFNode) {
let clips = animationEFNode.getComponent(cc.Animation).getClips();
for (const clip of clips) {
animationEFNode.getComponent(cc.Animation).stop(clip.name);
animationEFNode.getComponent(cc.Animation).removeClip(clip, true);
}
}
let animationNode = this.animationNode;
if (animationNode) {
let clips = animationNode.getComponent(cc.Animation).getClips();
for (const clip of clips) {
animationNode.getComponent(cc.Animation).stop(clip.name);
animationNode.getComponent(cc.Animation).removeClip(clip, true);
}
}
let maskNode = this.maskNode;
if (maskNode) {
let clips = maskNode.getComponent(cc.Animation).getClips();
for (const clip of clips) {
maskNode.getComponent(cc.Animation).stop(clip.name);
maskNode.getComponent(cc.Animation).removeClip(clip, true);
}
}
},
touchBegan(event) {
event.stopPropagation();
this.playAnimation('run');
//this.addWeaponAnimation('run', 1);
if (this.resid<=4038){
this.shadowNode.active = false
this.RoleshadowNode.active = true
ShapeUtil.playshadowAni(this.RoleshadowNode, this.resid, 1, 'run', this);
}else{
this.shadowNode.active = true
this.RoleshadowNode.active = false
}
WeaponUtil.addWeaponAnimation(this.weapon, this.weaponNode, this.animationNode, this, this.resid, 'run', 1, 2);
this.node.stopAllActions();
this.node.runAction(cc.sequence(cc.delayTime(10), cc.callFunc(() => {
this.playAnimation('stand');
//this.addWeaponAnimation('stand', 1);
if (this.resid<=4038){
this.shadowNode.active = false
this.RoleshadowNode.active = true
ShapeUtil.playshadowAni(this.RoleshadowNode, this.resid, 1, 'stand', this);
}else{
this.shadowNode.active = true
this.RoleshadowNode.active = false
}
WeaponUtil.addWeaponAnimation(this.weapon, this.weaponNode, this.animationNode, this, this.resid, 'stand', 1, 2);
})));
},
});