]> git.r.bdr.sh - rbdr/sumo/blobdiff - lib/factories/sumo.js
Add Naive Game Rules (#11)
[rbdr/sumo] / lib / factories / sumo.js
index 18e9d72dfafacfd37455658b5cddc40f0db2bf14..009ba8cbbad903be63b37934743d69af4d36da05 100644 (file)
@@ -14,8 +14,11 @@ import GrabAreaComponent from '../components/grab_area';
 import GrabbableComponent from '../components/grabbable';
 import GrabComponent from '../components/grab';
 import MaxVelocityComponent from '../components/max_velocity';
 import GrabbableComponent from '../components/grabbable';
 import GrabComponent from '../components/grab';
 import MaxVelocityComponent from '../components/max_velocity';
+import PointsColliderComponent from '../components/points_collider';
+import PointsComponent from '../components/points';
 import PositionComponent from '@serpentity/components.position';
 import PixiContainerComponent from '../components/pixi_container';
 import PositionComponent from '@serpentity/components.position';
 import PixiContainerComponent from '../components/pixi_container';
+import WinnerComponent from '../components/winner';
 
 import PixiFactory from '../factories/pixi';
 import Config from '../config';
 
 import PixiFactory from '../factories/pixi';
 import Config from '../config';
@@ -620,14 +623,17 @@ export default {
     const friction = 0;
     const frictionStatic = 0;
     const restitution = 1;
     const friction = 0;
     const frictionStatic = 0;
     const restitution = 1;
+    const label = config.label || 'Invisible Block';
+    const isSensor = !!(config.isSensor);
 
     const body = Bodies.rectangle(position.x / Config.meterSize,
       position.y / Config.meterSize,
       config.width / Config.meterSize,
       config.height / Config.meterSize,
       {
 
     const body = Bodies.rectangle(position.x / Config.meterSize,
       position.y / Config.meterSize,
       config.width / Config.meterSize,
       config.height / Config.meterSize,
       {
+        isSensor,
         isStatic: true,
         isStatic: true,
-        label: 'Invisible Block',
+        label,
         friction,
         restitution,
         frictionStatic
         friction,
         restitution,
         frictionStatic
@@ -638,6 +644,61 @@ export default {
       engine.addEntity(entity);
     }
 
       engine.addEntity(entity);
     }
 
+    return entity;
+  },
+
+  /**
+   * Creates an invisible block that accumulates points if certain
+   * entity collids with it
+   *
+   * @function createPointsCollider
+   * @memberof SumoFactory
+   * @param {external:Serpentity} [engine] the serpentity engine to attach
+   * to. If not sent, it will not be attached.
+   * @param {object} [config] the config to override the entity, accepts
+   * the key `position` as an object with an x and y property.
+   * @return {external:Serpentity.Entity} the created entity
+   */
+  createPointsCollider(engine, config = {}) {
+
+    const entity = this.createInvisibleBlock(null, Object.assign({
+      isSensor: true,
+      label: 'Points Detector'
+    }, config));
+
+    // Points Collider
+
+    entity.addComponent(new PointsColliderComponent(config));
+
+    if (engine) {
+      engine.addEntity(entity);
+    }
+
+    return entity;
+  },
+
+  /**
+   * Creates an entity representing the game state
+   *
+   * @function createGameState
+   * @memberof SumoFactory
+   * @param {external:Serpentity} [engine] the serpentity engine to attach
+   * to. If not sent, it will not be attached.
+   * @param {object} [config] the config to override the entity, accepts
+   * the key `position` as an object with an x and y property.
+   * @return {external:Serpentity.Entity} the created entity
+   */
+  createGameState(engine, config = {}) {
+
+    const entity = new Entity();
+
+    entity.addComponent(new PointsComponent(config));
+    entity.addComponent(new WinnerComponent(config));
+
+    if (engine) {
+      engine.addEntity(entity);
+    }
+
     return entity;
   }
 };
     return entity;
   }
 };