-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEntity.js
144 lines (127 loc) · 3.83 KB
/
Entity.js
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
import { Renderer } from "./Renderer.js";
import { Transform } from "./Transform.js";
export {Entity};
/**
* Class representing a game object.
*
* Fields:
* - name : String
* - transform : Transform
* - renderer : Renderer
*
* Methods:
* - {@link update} : Expects to run every frame. Should be overridden by classes extending this.
* - {@link render} : Updates the renderer's transform an updates the corresponding pixi sprite.
* - {@link broadcast} : Broadcasts an event to the entity. Should be overridden by classes extending this.
*/
class Entity
{
_Name;
_Transform;
_Renderer;
game;
uuid;
/**
* Construct a new abstract Entity. Considering as a base, Entity only contains renderer and transform information,
* you should probably only do this to render images that have no implicit behaviour.
*
* @param {String} name Name of the entity.
* @param {Transform} transform Entity position, scale, and rotation.
* @param {Renderer} renderer Entity renderer
* @param {Game} game The Game this is attached to (if any)
*/
constructor(name = "", transform = new Transform(), renderer = new Renderer(), game = undefined)
{
if (!(transform instanceof Transform))
throw new Error("Type mismatch: transform parameter is not of type Transform (was "+transform.constructor.name+" instead).");
if (!(renderer instanceof Renderer))
throw new Error("Type mismatch: renderer parameter is not of type Renderer (was "+renderer.constructor.name+" instead).");
this.transform = transform;
this.renderer = renderer;
this.game = game;
this.name = name;
this.uuid = crypto.randomUUID();
this.game.registerEntity(this);
}
get name() {
return this._Name;
}
set name(n) {
this._Name = n;
}
get transform() {
return this._Transform;
}
set transform(t)
{
this._Transform = t;
}
get renderer() {
return this._Renderer;
}
set renderer(r) {
this._Renderer = r;
}
getID()
{
return this.uuid;
}
update(delta) {}
/**
* Updates the renderer's transform member to match this entities transform member,
* then renders the renderer.
* @param {number} delta The time passed since the last render call.
*/
render(delta)
{
this.renderer.transform = this.transform;
this.renderer.update(delta);
}
broadcast(event) {}
/**
* Unregisters this entity from its respective game's registry, disposes its renderer,
* and deletes itself.
* @returns True if the renderer is properly disposed, false otherwise.
*/
destroy()
{
if (this.game == undefined)
return false;
if (this.renderer.dispose())
{
this.game.unregisterEntity(this);
return true;
}
else
{
return false;
}
}
/**
* Simple shorthand for `this.constructor.name`
* @returns The Type of this Entity.
*/
getType()
{
return this.constructor.name;
}
/**
* Serializes the entity into a JSON object string
* @returns the JSON string representation
*/
serialize()
{
return '{ "type":"Entity", "name": "' + this.name + '", "transform": ' + this.transform.serialize() + ', "renderer": '
+ this.renderer.serialize() + '}';
}
/**
* Deserializes the JSON object into an entity
* @param {*} obj the JSON object
* @param {*} game the current game instance
* @returns the new entity
*/
static deserialize(obj, game)
{
return new Entity(obj.name, Transform.deserialize(obj.transform), Renderer.deserialize(obj.renderer, game.stage), game);
}
}