]>
Commit | Line | Data |
---|---|---|
1 | import { Entity } from '@serpentity/serpentity'; | |
2 | ||
3 | import PositionComponent from '@serpentity/components.position'; | |
4 | import PixiContainerComponent from '../components/pixi_container'; | |
5 | import PixiFactory from '../factories/pixi'; | |
6 | ||
7 | /** | |
8 | * Factory object that contains many methods to create prefab entities. | |
9 | * | |
10 | * @type object | |
11 | * @name SumoFactory | |
12 | */ | |
13 | export default { | |
14 | ||
15 | /** | |
16 | * Creates a sumo entity and adds it to the engine. Can override | |
17 | * position in the config object | |
18 | * | |
19 | * @function createSumo | |
20 | * @memberof SumoFactory | |
21 | * @param {external:Serpentity} [engine] the serpentity engine to attach | |
22 | * to. If not sent, it will not be attached. | |
23 | * @param {object} [config] the config to override the entity, accepts | |
24 | * the key `position` as an object with an x and y property. | |
25 | * @return {external:Serpentity.Entity} the created entity | |
26 | */ | |
27 | createSumo(engine, config = {}) { | |
28 | ||
29 | const entity = new Entity(); | |
30 | ||
31 | entity.addComponent(new PositionComponent(config.position)); | |
32 | ||
33 | const container = config.container || { | |
34 | container: PixiFactory.createSumo() | |
35 | }; | |
36 | ||
37 | // Match the symbol position with the entity position on init | |
38 | // Otherwise it's up to systems | |
39 | const position = entity.getComponent(PositionComponent); | |
40 | container.container.position.x = position.x; | |
41 | container.container.position.y = position.y; | |
42 | ||
43 | entity.addComponent(new PixiContainerComponent(container)); | |
44 | ||
45 | if (engine) { | |
46 | engine.addEntity(entity); | |
47 | } | |
48 | ||
49 | return entity; | |
50 | } | |
51 | }; |