-
Notifications
You must be signed in to change notification settings - Fork 964
/
Copy pathlightruntimeobject.ts
205 lines (180 loc) · 5.82 KB
/
lightruntimeobject.ts
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
199
200
201
202
203
204
205
namespace gdjs {
export type LightObjectDataType = {
/** The base parameters of light object. */
content: {
/** The radius of light object. */
radius: number;
/** A string representing color in hexadecimal format. */
color: string;
/** A string representing the name of texture used for light object. */
texture: string;
/** true if the light objects shows debug graphics, false otherwise. */
debugMode: boolean;
};
};
export type LightObjectData = ObjectData & LightObjectDataType;
export type LightNetworkSyncDataType = {
rad: number;
col: string;
};
export type LightNetworkSyncData = ObjectNetworkSyncData &
LightNetworkSyncDataType;
/**
* Displays a Light object.
*/
export class LightRuntimeObject extends gdjs.RuntimeObject {
_radius: number;
/** color in format [r, g, b], where each component is in the range [0, 255] */
_color: integer[];
_debugMode: boolean;
_texture: string;
_obstaclesManager: gdjs.LightObstaclesManager;
_renderer: gdjs.LightRuntimeObjectRenderer;
_instanceContainer: gdjs.RuntimeScene;
constructor(
runtimeScene: gdjs.RuntimeScene,
lightObjectData: LightObjectData
) {
super(runtimeScene, lightObjectData);
this._radius =
lightObjectData.content.radius > 0 ? lightObjectData.content.radius : 1;
this._color = gdjs.rgbOrHexToRGBColor(lightObjectData.content.color);
this._debugMode = lightObjectData.content.debugMode;
this._texture = lightObjectData.content.texture;
this._obstaclesManager =
gdjs.LightObstaclesManager.getManager(runtimeScene);
this._renderer = new gdjs.LightRuntimeObjectRenderer(this, runtimeScene);
this._instanceContainer = runtimeScene;
// *ALWAYS* call `this.onCreated()` at the very end of your object constructor.
this.onCreated();
}
static hexToRGBColor(hex) {
const hexNumber = parseInt(hex.replace('#', ''), 16);
return [(hexNumber >> 16) & 255, (hexNumber >> 8) & 255, hexNumber & 255];
}
getRendererObject() {
return this._renderer.getRendererObject();
}
updateFromObjectData(
oldObjectData: LightObjectData,
newObjectData: LightObjectData
): boolean {
if (oldObjectData.content.radius !== newObjectData.content.radius) {
this.setRadius(newObjectData.content.radius);
}
if (oldObjectData.content.color !== newObjectData.content.color) {
this._color = gdjs.rgbOrHexToRGBColor(newObjectData.content.color);
this._renderer.updateColor();
}
if (oldObjectData.content.texture !== newObjectData.content.texture) {
this._texture = newObjectData.content.texture;
this._renderer.updateMesh();
}
if (oldObjectData.content.debugMode !== newObjectData.content.debugMode) {
this._debugMode = newObjectData.content.debugMode;
this._renderer.updateDebugMode();
}
return true;
}
getNetworkSyncData(): LightNetworkSyncData {
return {
...super.getNetworkSyncData(),
rad: this.getRadius(),
col: this.getColor(),
};
}
updateFromNetworkSyncData(
networkSyncData: LightNetworkSyncData,
options?: { skipMultiplayerInstructions: boolean }
): void {
super.updateFromNetworkSyncData(networkSyncData, options);
if (networkSyncData.rad !== undefined) {
this.setRadius(networkSyncData.rad);
}
if (networkSyncData.col !== undefined) {
this.setColor(networkSyncData.col);
}
}
updatePreRender(): void {
this._renderer.ensureUpToDate();
}
/**
* Get the radius of the light object.
* @returns radius of the light object.
*/
getRadius(): number {
return this._radius;
}
/**
* Set the radius of the light object.
*/
setRadius(radius: number): void {
this._radius = radius > 0 ? radius : 1;
this._renderer.updateRadius();
}
/**
* Get the height of the light object.
* @returns height of light object.
*/
getHeight(): float {
return 2 * this._radius;
}
/**
* Get the width of the light object.
* @returns width of light object.
*/
getWidth(): float {
return 2 * this._radius;
}
/**
* Get the x co-ordinate of the top-left vertex/point of light object.
* @returns x co-ordinate of the top-left vertex/point.
*/
getDrawableX(): float {
return this.x - this._radius;
}
/**
* Get the y co-ordinate of the top-left vertex/point of light object.
* @returns y co-ordinate of the top-left vertex/point.
*/
getDrawableY(): float {
return this.y - this._radius;
}
/**
* Get the color of the light object as a "R;G;B" string.
* @returns the color of light object in "R;G;B" format.
*/
getColor(): string {
return this._color[0] + ';' + this._color[1] + ';' + this._color[2];
}
/**
* Set the color of the light object in format "R;G;B" string, with components in the range of [0-255].
*/
setColor(color: string): void {
this._color = gdjs.rgbOrHexToRGBColor(color);
this._renderer.updateColor();
}
/**
* Get the light obstacles manager.
* @returns the light obstacles manager.
*/
getObstaclesManager(): gdjs.LightObstaclesManager {
return this._obstaclesManager;
}
/**
* Returns true if the light shows debug graphics, false otherwise.
* @returns true if debug mode is activated.
*/
getDebugMode(): boolean {
return this._debugMode;
}
/**
* Returns the path of texture resource.
* @returns the path of texture.
*/
getTexture(): string {
return this._texture;
}
}
gdjs.registerObject('Lighting::LightObject', gdjs.LightRuntimeObject);
}