BlockView.ts
5.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// 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 { BlockSkill } from "../kernel/battle/BattleConst";
import { BlockData } from "../kernel/battle/map/BlockData";
import { MapView } from "../kernel/battle/map/MapView";
import { BlockSkillFireworkView } from "./BlcokSkillFireworkView";
import { BlockSkillBombView } from "./BlockSkillBombView";
import { BlockSkillSwitcherView } from "./BlockSkillSwitcherView";
import { BlcokSkillViewBase } from "./BlockSkillViewBase";
import ObjBaseView from "./ObjBaseView";
const { ccclass, property } = cc._decorator;
@ccclass
export default class BlockView extends ObjBaseView {
protected _imgSkillIcon: cc.Node = null;
protected _imgEffect: cc.Node = null;
protected _txt: cc.Node = null;
protected _light: cc.Node = null;
get owner(): BlockData { return this._owner as BlockData; }
private _mapView: MapView = null;
protected _txtOffset: cc.Vec2 = new cc.Vec2();
private _skillView: BlcokSkillViewBase = null;
//#region interface
onHit(): void {
super.onHit();
let ani = this._txt.getComponent(cc.Animation);
ani && !ani.getAnimationState('fx_block_txt_hit').isPlaying && ani.play('fx_block_txt_hit', 0);
if (this._light) {
let lightAni = this._light.getComponent(cc.Animation);
lightAni && !lightAni.getAnimationState('fx_block_light_hit').isPlaying && lightAni.play('fx_block_light_hit', 0) && (this._light.active = true)
}
this._skillView && this._skillView.onCollision();
}
onDead() {
this._mapView.playBlockParticle(this.node);
this._skillView && this._skillView.onDead();
this._imgEffect.destroy();
this._imgSkillIcon.destroy();
this._txt.destroy();
this._light && this._light.destroy();
super.onDead();
}
//#endregion
//#region life cycle
init(owner: BlockData, mapView: MapView) {
this._imgSkillIcon = this.node.getChildByName('imgIcon');
this._imgEffect = this.node.getChildByName('imgEffect');
this._imgEffect.active = false;
this._txt = this.node.getChildByName('txt');
this._txtOffset.x = this._txt.x;
this._txtOffset.y = this._txt.y;
this._mapView = mapView;
this._light = this.node.getChildByName('light');
this._light && (this._light.active = false);
super.init(owner, mapView);
//分层显示
this._imgEffect.removeFromParent();
this._txt.removeFromParent();
this._imgSkillIcon.removeFromParent();
this._light && this._light.removeFromParent();
mapView.effectLayer.addChild(this._imgEffect);
mapView.txtLayer.addChild(this._txt);
mapView.blockIconLayer.addChild(this._imgSkillIcon);
this._light && mapView.lightLayer.addChild(this._light)
if (owner.skill && owner.skill.skillId == BlockSkill.BOMB) {
this._skillView = new BlockSkillBombView(this, this._mapView);
}
else if (owner.skill && owner.skill.skillId >= BlockSkill.FIREWORK_VERTICAL && owner.skill.skillId <= BlockSkill.FIREWORK_DOUBLE) {
this._skillView = new BlockSkillFireworkView(this, this._mapView);
}
else if (owner.skill && owner.skill.skillId == BlockSkill.SWITCHER) {
this._skillView = new BlockSkillSwitcherView(this, mapView);
}
this.updateSkillIcon();
// this.onVisibleChange()
}
onPreview(): void {
super.onPreview();
this._txt.active = false;
}
protected onDestroy(): void {
super.onDestroy();
this._skillView && this._skillView.onDestroy();
this._skillView = null;
this._imgEffect = null;
this._imgSkillIcon = null;
this._txt = null;
}
//#endregion
//#region override method
protected onPositionChange(x: number, y: number): void {
super.onPositionChange(x, y);
this._imgEffect.setPosition(x, y);
this._txt.setPosition(x + this._txtOffset.x, y + this._txtOffset.y);
this._imgSkillIcon.setPosition(x, y);
this._light && this._light.setPosition(x, y);
this._skillView && this._skillView.onPositionChange(x, y);
}
protected onLazyUpdate(): void {
this._txt.getComponent(cc.Label).string = this.owner.hp.toString();
}
protected onVisibleChange(): void {
super.onVisibleChange();
this._imgEffect.active = this._visible;
this._imgEffect.active = false; //暂时屏蔽
this._imgSkillIcon.active = (this._visible && this.owner.skill && this.owner.skill.skillImg != '');
this._txt.active = this._visible && this.owner.hp > 0;
}
//#endregion
//#region private method
private updateSkillIcon() {
if (this.owner.skill && this.owner.skill.skillImg != "") {
// this._imgSkillIcon.active = true;
cc.resources.load("battle/" + this.owner.skill.skillImg, cc.SpriteFrame, (err, spf) => {
// const frame = (spf as cc.SpriteAtlas).getSpriteFrame(this._owner.iconName);
if (!err && this.isValid) { //有可能销毁后进入回调,增加判断 isValid
this._imgSkillIcon.getComponent(cc.Sprite).spriteFrame = spf as cc.SpriteFrame;
}
else {
// console.error(err.message);
}
});
}
else {
this._imgSkillIcon.active = false;
}
}
//#endregion
}