ObjBaseView.ts
4.87 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import { tMgr } from "../Global";
import { IObjView } from "../kernel/battle/inter/IObjView";
import { MapView } from "../kernel/battle/map/MapView";
import { ObjData } from "../kernel/battle/map/ObjData";
import { TableName, GamePlayTable } from "../kernel/table/TableDefine";
const { ccclass, property } = cc._decorator;
@ccclass
export default class ObjBaseView extends cc.Component implements IObjView {
protected _owner: ObjData = null;
get owner(): ObjData { return this._owner; }
/**主体图像 */
protected _img: cc.Node = null;
protected _imgOffset: cc.Vec2 = cc.v2();
protected _visible: boolean;
protected _needMove: boolean = false;
protected _destroyed: boolean = false;
//#region interface
onImgChanged() {
this.updateImg();
}
onDroped() {
this.setColliderEnable(false);
}
onDead() {
this._img.destroy();
this.node.destroy();
}
onHit() {
}
//#endregion
//#region life cycle
protected start(): void {
this.schedule(this.onLazyUpdate, 1 / 20);
}
init(owner: ObjData, mapView: MapView) {
this._owner = owner;
this._owner.bindView(this);
this._img = this.node.getChildByName('img');
this._imgOffset.x = this._img.x;
this._imgOffset.y = this._img.y;
//分层显示
this._img.removeFromParent();
mapView.colliderLayer.addChild(this.node);
this.node.active = true;
mapView.blockImgLayer.addChild(this._img);
// this.updateImg();
this.onImgChanged();
this.onPositionChange(this._owner.pixelPos.x, this._owner.pixelPos.y);
}
onPreview() {
//预览状态下,在init后被调用
this._visible = true;
this._img.active = true;
this.setColliderEnable(false);
this.onVisibleChange();
}
protected update(dt: number): void {
if (this._destroyed) {
return;
}
// let needMove = (this.node.position.y != this._owner.pixelPos.y);
this.onUpdateMoveState();
if (this._needMove) {
let speed = this._owner.droped ? 600 : (tMgr.getConfig(TableName.GAME_PLAY, 1) as GamePlayTable).Value;
let step = -speed * dt;
let dis = this._owner.pixelPos.y - this.node.y;
if (Math.abs(step) >= Math.abs(dis)) {
step = dis;
}
this.onPositionChange(this._owner.pixelPos.x, this.node.position.y + step);
}
}
/**更改是否需要移动的接口,更改 _needMove变量 */
protected onUpdateMoveState() {
this._needMove = (this.node.position.y != this._owner.pixelPos.y);
}
// onEndContact(contact: cc.PhysicsContact, selfCollider: cc.Collider, otherCollider: cc.Collider) {
// if (otherCollider.node.name == "ball") {
// contact.disabled = true;
// this._owner.onCollisonToBall(otherCollider);
// }
// }
onBeginContact(contact: cc.PhysicsContact, selfCollider: cc.Collider, otherCollider: cc.Collider) {
if (otherCollider.node.name == "ball") {
this._owner.onCollisonToBall(otherCollider);
}
}
protected onDestroy(): void {
this._destroyed = true;
this._owner = null;
this._img = null;
}
//#endregion
//#region public method
setColliderEnable(value: boolean) {
this.node.getComponent(cc.PhysicsCollider).enabled = value;
}
//#endregion
//#region children override
protected onLazyUpdate() {
}
protected onImageLoaded() {
}
protected updateImg() {
if (this._owner.imgName != "") {
// this._img.active = true;
cc.resources.load("battle/" + this._owner.imgName, cc.SpriteFrame, (err, spf) => {
// const frame = (spf as cc.SpriteAtlas).getSpriteFrame(this._owner.iconName);
if (!err) {
if (!this._destroyed) {
this._img.getComponent(cc.Sprite).spriteFrame = spf as cc.SpriteFrame;
this.onImageLoaded();
}
}
else {
console.error(err.message);
}
});
}
else {
this._img.active = false;
}
}
protected onVisibleChange() {
}
protected onPositionChange(x: number, y: number) {
this._img.setPosition(x + this._imgOffset.x, y + this._imgOffset.y);
this.node.setPosition(x, y);
let visible = (this._owner.gridPos.y > this._owner.map.viewSplitRow);
if (this._visible != visible) {
this._visible = visible;
this.setColliderEnable(visible);
this._img.active = visible;
this.onVisibleChange();
}
}
//#endregion
//#region private method
//#endregion
}