| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- import { _decorator, Component, Node, Prefab, instantiate } from 'cc';
- import { DataSystem } from '../core/data/DataSystem';
- import { ResourceLoader } from '../core/resourceManager/ResourceLoader';
- import { BuffData } from './BuffData';
- import { CommandSpine } from './CommandSpine';
- import { Hero } from './Hero';
- import { HitData } from './HitData';
- import { Monster } from './Monster';
- import { TakeAWalk } from './TakeAWalk';
- const { ccclass, property } = _decorator;
- @ccclass('Buff')
- export class Buff extends Component {
- @property({ type: Node, tooltip: "特效层" }) layer: Node;
- /**当前buff */
- private curBuff: Map<number, { startTime: number, buff: any }> = new Map();
- /**当前buff特效 */
- private buffEffect: Map<number, CommandSpine> = new Map();
- update() {
- this.checkBuff();
- }
- /**移除buff */
- public removeBuff(id?: number): void {
- if (id) {
- this.cancelBuff(this.curBuff.get(id));
- this.curBuff.delete(id);
- let index = DataSystem.getData(BuffData).curBuffs.indexOf(id);
- if (index != -1) {
- DataSystem.getData(BuffData).curBuffs.splice(index, 1);
- }
- } else {
- this.curBuff.forEach((v, k) => {
- this.cancelBuff(this.curBuff.get(k));
- this.curBuff.delete(k);
- });
- DataSystem.getData(BuffData).curBuffs = [];
- }
- }
- /**添加buff */
- public addBuff(buffs: any): void {
- for (let i = 0; i < buffs.length; i++) {
- this.useBuff(buffs[i]);
- }
- }
- /**检查buff */
- private checkBuff(): void {
- if (this.curBuff.size > 0) {
- this.curBuff.forEach((v, k) => {
- let delay = this.curBuff.get(k).buff["time"];
- if (delay != -1) {
- let endTime = this.curBuff.get(k).startTime + delay * 1000;
- if (Date.now() > endTime) {
- this.cancelBuff(this.curBuff.get(k));
- this.curBuff.delete(k);
- }
- }
- });
- }
- }
- /**buff生效 */
- private useBuff(buff: any): void {
- let types = (buff["type"] + "").split(",");
- for (let i = 0; i < types.length; i++) {
- switch (parseInt(types[i])) {
- case 0:
- this.createCommandEffect("gongji", 0, true);
- this.getComponent(Hero) && (this.getComponent(Hero).data._dyAttack = buff["attackPer"] / 10000);
- break;
- case 1:
- this.createCommandEffect("baoji", 1, true);
- this.getComponent(Hero) && (this.getComponent(Hero).data._dyCri = buff["cri"]);
- break;
- case 2:
- this.getComponent(Hero) && (this.getComponent(Hero).data._dyMiss = buff["acc"]);
- this.createCommandEffect("mingzhong", 2, true);
- break;
- case 3:
- this.getComponent(Hero) && (this.getComponent(Hero).data._dyAttackSpeed = buff["attSpeed"]) / 10000;
- this.createCommandEffect("gongsu", 3, true);
- break;
- case 4:
- this.createCommandEffect("animation", 4, false, "spine/buff&die/tengman");
- this.getComponent(TakeAWalk) && (this.getComponent(TakeAWalk).dySpeed = ((10000 - buff["slow"]) / 10000));
- break;
- case 5:
- let hit = new HitData();
- hit.from = "闪电buff";
- hit.isSkill = true;
- hit.hit = Math.floor(buff["hit"] * (buff["flash"] / 10000));
- this.createFlashEffect("animation", "animation", hit);
- break;
- }
- DataSystem.getData(BuffData).curBuffs.indexOf(buff["id"]) == -1 && DataSystem.getData(BuffData).curBuffs.push(buff["id"]);
- }
- this.curBuff.set(buff["id"], { startTime: Date.now(), buff: buff });
- }
- /**buff失效 */
- private cancelBuff(buff): void {
- let types = (buff.buff["type"] + "").split(",");
- for (let i = 0; i < types.length; i++) {
- switch (parseInt(types[i])) {
- case 0:
- this.getComponent(Hero) && (this.getComponent(Hero).data._dyAttack = 0);
- break;
- case 1:
- this.getComponent(Hero) && (this.getComponent(Hero).data._dyCri = 0);
- break;
- case 2:
- this.getComponent(Hero) && (this.getComponent(Hero).data._dyMiss = 0);
- break;
- case 3:
- this.getComponent(Hero) && (this.getComponent(Hero).data._dyAttackSpeed = 0);
- break;
- case 4:
- this.buffEffect.get(4).clean();
- this.getComponent(TakeAWalk) && (this.getComponent(TakeAWalk).dySpeed = 1);
- break;
- case 5:
- break;
- }
- let index = DataSystem.getData(BuffData).curBuffs.indexOf(buff.buff["id"]);
- if (index != -1) {
- DataSystem.getData(BuffData).curBuffs.splice(index, 1);
- }
- }
- }
- /**创建持续型spine特效 */
- private async createCommandEffect(animation: string, key?: number, autoDestory = false, path = "spine/buff&die/fight_buff") {
- let res = this.node.getComponent(ResourceLoader);
- if (res && this.layer) {
- let prefab = await res.load<Prefab>("prefabs/battle/commandSpine", Prefab);
- let node = instantiate(prefab);
- node.getComponent(CommandSpine).autoDestory = autoDestory;
- node.getComponent(CommandSpine).setData(path, false, animation);
- node.setParent(this.layer);
- if (this.buffEffect.has(key)) {
- this.buffEffect.get(key).clean();
- this.buffEffect.delete(key);
- }
- this.buffEffect.set(key, node.getComponent(CommandSpine));
- }
- }
- /**创建快速型特效 */
- private async createFlashEffect(animation: string, animation2: string, hit: HitData, path = "spine/buff&die/fight_skill_dian_1", path2 = "spine/buff&die/fight_skill_dian_2") {
- let res = this.node.getComponent(ResourceLoader);
- let self = this;
- if (res && this.layer) {
- let prefab = await res.load<Prefab>("prefabs/battle/commandSpine", Prefab);
- let node = instantiate(prefab);
- let component = node.getComponent(CommandSpine);
- component.setData(path, false, animation);
- component.autoDestory = true;
- component.completehandle = async () => {
- let prefab = await res.load<Prefab>("prefabs/battle/commandSpine", Prefab);
- let node = instantiate(prefab);
- let component = node.getComponent(CommandSpine);
- component.setData(path2, false, animation2);
- component.autoDestory = true;
- component.completehandle = async () => {
- self.getComponent(Monster) && this.getComponent(Monster).data._hit.push(hit);
- }
- }
- node.setParent(this.layer);
- }
- }
- }
|