BlockData.ts
3.65 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
import { SoundManager } from "../../../component/SoundManager";
import { SoundName } from "../../../Global";
import { BlockColorTableMgr } from "../../table/BlockColorTableMgr";
import { ObjType } from "../BattleConst";
import { BlockSkillBase } from "./blockSkill/BlockSkillBase";
import { MapData } from "./MapData";
import { ObjData } from "./ObjData";
export class BlockData extends ObjData {
protected _hp: number = 1;
get hp(): number { return this._hp; }
protected _contentY: number = 0;
/**最大内容范围内的Y坐标 */
get contentY(): number { return this._contentY; }
skill: BlockSkillBase = null;
protected _isProtected: boolean = false;
/**是否在被闸门保护状态 */
get isProtected(): boolean { return this._isProtected; }
//#region life cycle
init(map: MapData, type: ObjType, gridX: number, gridY: number): void {
//屏蔽,调用initBlock
return;
}
initBlock(map: MapData, type: ObjType, hp: number, gridX: number, gridY: number) {
super.init(map, type, gridX, gridY);
this._contentY = gridY;
this._hp = hp;
this._imgName = this.getImgName();
}
/**子类需覆写,返回对应的类型 */
clone(): BlockData {
//XXX 新增属性需要在此处增加处理
return this.cloneValue(new BlockData());
}
/**和球发生碰撞 */
onCollisonToBall(collider: cc.Collider) {
if (!this._deaded && !this.isProtected) {
super.onCollisonToBall(collider);
this.skill && this.skill.onCollisionToBall();
if (this._hp >= 1) {
this.adJustHp(-1);
}
}
}
onDead(): void {
super.onDead();
this.skill && this.skill.onDead();
SoundManager.ins.playEffect(SoundName.BREAK);
}
protected onDestroy(): void {
this.skill && this.skill.destroy();
}
//#endregion
//#region public method
addSkill(skill: BlockSkillBase) {
this.skill = skill;
skill.onAddedToBlock();
}
adJustHp(delta: number) {
if (delta < 0) {
//先只处理扣血
this._hp += delta;
if (this._hp > 0) {
let imgName = this.getImgName();
if (this._imgName != imgName) {
this._imgName = imgName;
this._IView.onImgChanged();
}
SoundManager.ins.playEffect(SoundName.HIT);
}
if (this._hp <= 0) {
this._hp = 0;
this.dead();
}
}
}
setIsProtected(value: boolean) {
if (this._isProtected != value) {
this._isProtected = value;
}
}
//#endregion
//#region child override
protected cloneValue(target: BlockData): BlockData {
super.cloneValue(target);
target._hp = this._hp;
// target._skillImgName = this._skillImgName;
target._contentY = this._contentY;
if (this.skill) {
target.skill = this.skill;
target.skill.owner = target; //指向新的格子
target.skill.onAddedToBlock(); //重新触发一下
}
return target;
}
protected getImgName(): string {
return BlockColorTableMgr.ins.getByHp(this._hp).Img;
}
setGridPos(gridX: number, gridY: number): void {
this.map.blockData.delete(this.index); //删除旧索引
// super.setGridPos(gridX, gridY, false);
super.setGridPos(gridX, gridY);
this.map.blockData.set(this.index, this); //增加新索引
this._contentY = this.gridPos.y;
}
//#endregion
}