]>
git.r.bdr.sh - rbdr/sumo/blob - lib/factories/sumo.js
1 import { Bodies
, Constraint
} from 'matter-js';
2 import { Entity
} from '@serpentity/serpentity';
4 import AngleComponent
from '../components/angle';
5 import BodyComponent
from '../components/body';
6 import CoupledEntitiesComponent
from '../components/coupled_entities';
7 import PositionComponent
from '@serpentity/components.position';
8 import PixiContainerComponent
from '../components/pixi_container';
9 import PixiFactory
from '../factories/pixi';
12 kNoEntityError: 'Entity Not Found: This method requires entityA and entityB to be set in the config.',
13 kEntityHasNoBodyError: 'Entity Has No Body: This method requires entities have a BodyComponent.'
17 * Factory object that contains many methods to create prefab entities.
25 * Creates a sumo entity and adds it to the engine. Can override
26 * position in the config object
28 * @function createSumo
29 * @memberof SumoFactory
30 * @param {external:Serpentity} [engine] the serpentity engine to attach
31 * to. If not sent, it will not be attached.
32 * @param {object} [config] the config to override the entity, accepts
33 * the key `position` as an object with an x and y property.
34 * @return {external:Serpentity.Entity} the created entity
36 createSumo(engine
, config
= {}) {
38 const entity
= new Entity();
42 entity
.addComponent(new PositionComponent(config
.position
));
43 const position
= entity
.getComponent(PositionComponent
);
45 entity
.addComponent(new AngleComponent(config
.angle
));
46 const angle
= entity
.getComponent(AngleComponent
);
52 const container
= config
.container
|| {
53 container: PixiFactory
.createSumo({ radius
})
55 container
.container
.position
.x
= position
.x
;
56 container
.container
.position
.y
= position
.y
;
57 container
.container
.rotation
= angle
.angle
;
58 entity
.addComponent(new PixiContainerComponent(container
));
62 const frictionAir
= 0.00001;
63 const friction
= 0.00001;
64 const restitution
= 0.9;
67 const body
= Bodies
.circle(position
.x
, position
.y
, radius
, {
74 entity
.addComponent(new BodyComponent({ body
}));
77 engine
.addEntity(entity
);
84 * Creates a rubber band entity and adds it to the engine.
86 * @function createRubberBand
87 * @memberof SumoFactory
88 * @param {external:Serpentity} [engine] the serpentity engine to attach
89 * to. If not sent, it will not be attached.
90 * @param {object} [config] the config to override the entity, it
91 * must include entityA and entityB which will be tied by it. If they
92 * are not sent or don't have a physics body, this will throw.
93 * @return {external:Serpentity.Entity} the created entity
95 createRubberBand(engine
, config
= {}) {
97 const entity
= new Entity();
99 if (!config
.entityA
|| !config
.entityB
) {
100 throw new Error(internals
.kNoEntityError
);
103 if (!config
.entityA
.hasComponent(BodyComponent
) || !config
.entityB
.hasComponent(BodyComponent
)) {
104 throw new Error(internals
.kEntityHasNoBodyError
);
109 const container
= config
.container
|| {
110 container: PixiFactory
.createEmptyGraphic()
112 entity
.addComponent(new PixiContainerComponent(container
));
116 const bodyA
= config
.entityA
.getComponent(BodyComponent
).body
;
117 const bodyB
= config
.entityB
.getComponent(BodyComponent
).body
;
118 const damping
= 0.01;
120 const stiffness
= 0.0001;
121 const angularStiffness
= 0.0001;
123 const body
= Constraint
.create({
131 entity
.addComponent(new BodyComponent({ body
}));
133 entity
.addComponent(new CoupledEntitiesComponent({
134 coupledEntities: [config
.entityA
, config
.entityB
]
138 engine
.addEntity(entity
);