diff options
| author | Rubén Beltrán del Río <ben@nsovocal.com> | 2018-04-23 05:13:08 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-04-23 05:13:08 -0500 |
| commit | 7ade6f8d96825386bf2e89dea51f9297cbac8e9c (patch) | |
| tree | 1e0c95625250c6fc41c7c8722ed81f4a3246ce6c /lib/systems | |
| parent | f45bcde17fe0a8849e647ac843106fb51d2e8971 (diff) | |
Add control via keyboard (#6)
* Correct angle documentation
* Correct body component doc
* Add a config module with px 2 meters
* Create component to map input
* Add components for mappable actions
* Add component for elastic manipulation
* Add node to modify physics
* Add controllable node
* Add dasher node
* Add control mapper system
* Add component to limit velocity
* Add node for limiting velocity
* Add systems to move and dash
* Use meters in physics systems
* Correct documentation in render system
* Add elastic manipulation system
* Update factories to use new components
* Update main app to use new systems
* Ignore dist dir
* Also ignore cache
* Ignore personal configuration files
* Add system to reduce velocity
* Add changelog
Diffstat (limited to 'lib/systems')
| -rw-r--r-- | lib/systems/apply_force.js | 97 | ||||
| -rw-r--r-- | lib/systems/control_mapper.js | 118 | ||||
| -rw-r--r-- | lib/systems/dash.js | 116 | ||||
| -rw-r--r-- | lib/systems/elastic.js | 93 | ||||
| -rw-r--r-- | lib/systems/physics_to_attributes.js | 5 | ||||
| -rw-r--r-- | lib/systems/physics_world_control.js | 2 | ||||
| -rw-r--r-- | lib/systems/reduce_velocity.js | 70 | ||||
| -rw-r--r-- | lib/systems/render.js | 10 |
8 files changed, 503 insertions, 8 deletions
diff --git a/lib/systems/apply_force.js b/lib/systems/apply_force.js new file mode 100644 index 0000000..4adc55d --- /dev/null +++ b/lib/systems/apply_force.js @@ -0,0 +1,97 @@ +import { System } from '@serpentity/serpentity'; +import { Body, Vector } from 'matter-js'; + +const internals = { + kForce: 0.0001 +}; + +import PhysicalWithExternalForceNode from '../nodes/physical_with_external_force'; + +/** + * Applies physica from external forces (eg. controls) to the physics body + * + * @extends {external:Serpentity.System} + * @class ApplyForceSystem + * @param {object} config a configuration object to extend. + */ +export default class ApplyForceSystem extends System { + + constructor(config = {}) { + + super(); + + /** + * The node collection of entities that have external force + * + * @property {external:Serpentity.NodeCollection} physicalEntities + * @instance + * @memberof ApplyForceSystem + */ + this.physicalEntities = null; + } + + /** + * Initializes system when added. Requests physics nodes + * + * @function added + * @memberof ApplyForceSystem + * @instance + * @param {external:Serpentity.Engine} engine the serpentity engine to + * which we are getting added + */ + added(engine) { + + this.physicalEntities = engine.getNodes(PhysicalWithExternalForceNode); + } + + /** + * Clears system resources when removed. + * + * @function removed + * @instance + * @memberof ApplyForceSystem + */ + removed() { + + this.physicalEntities = null; + } + + /** + * Runs on every update of the loop. Updates the body based on the force + * component + * + * @function update + * @instance + * @param {Number} currentFrameDuration the duration of the current + * frame + * @memberof ApplyForceSystem + */ + update(currentFrameDuration) { + + for (const physicalEntity of this.physicalEntities) { + const body = physicalEntity.body.body; + const force = physicalEntity.force; + const forceVector = Vector.create(force.x * internals.kForce, force.y * internals.kForce); + + + // Store the last angle and apply force on non-zero forces + + if (force.x || force.y) { + force.lastAngle = Math.atan2(force.y, force.x); + Body.applyForce(body, + { + x: body.position.x, + y: body.position.y + }, + forceVector + ); + } + + // Reset the force + + force.x = 0; + force.y = 0; + } + } +}; + diff --git a/lib/systems/control_mapper.js b/lib/systems/control_mapper.js new file mode 100644 index 0000000..e91ecc6 --- /dev/null +++ b/lib/systems/control_mapper.js @@ -0,0 +1,118 @@ +import { System } from '@serpentity/serpentity'; + +import ControllableNode from '../nodes/controllable'; + +/* global window */ + +const internals = { + keyboardState: { + } +}; + +/** + * Updates control status based on the controller map + * + * @extends {external:Serpentity.System} + * @class ControlMapperSystem + * @param {object} config a configuration object to extend. + */ +export default class ControlMapperSystem extends System { + + constructor(config = {}) { + + super(); + + /** + * The node collection of controllable entities + * + * @property {external:Serpentity.NodeCollection} controllables + * @instance + * @memberof RenderSystem + */ + this.controllables = null; + + this._initializeKeyboard(); + } + + /** + * Initializes system when added. Requests controllable nodes. + * + * @function added + * @memberof RenderSystem + * @instance + * @param {external:Serpentity.Engine} engine the serpentity engine to + * which we are getting added + */ + added(engine) { + + this.controllables = engine.getNodes(ControllableNode); + } + + /** + * Clears system resources when removed. + * + * @function removed + * @instance + * @memberof RenderSystem + */ + removed() { + + this.controllables = null; + } + + /** + * Runs on every update of the loop. Maps the actions given the current state of the inputs + * + * @function update + * @instance + * @param {Number} currentFrameDuration the duration of the current + * frame + * @memberof RenderSystem + */ + update(currentFrameDuration) { + + for (const controllable of this.controllables) { + for (const map of controllable.controlMap.map) { + if (map.source.type === 'keyboard') { + this._setValue(controllable.entity, map.target, !!internals.keyboardState[map.source.index]); + } + } + } + } + + // Listens to keyboard to update internal map + + _initializeKeyboard() { + + window.addEventListener('keydown', (event) => { + + internals.keyboardState[event.keyCode] = true; + }); + + window.addEventListener('keyup', (event) => { + + internals.keyboardState[event.keyCode] = false; + }); + } + + // Sets the value to a target + + _setValue(entity, target, value) { + + const component = entity.getComponent(target.component); + + if (component) { + const keyFragments = target.property.split('.'); + let currentObject = component; + for (const keyFragment of keyFragments.slice(0, keyFragments.length - 1)) { + currentObject = currentObject[keyFragment] = currentObject[keyFragment] || {}; + } + + + const finalValue = !!target.value ? target.value(value) : value; + const finalProperty = keyFragments.pop(); + currentObject[finalProperty] += finalValue; + } + } +}; + diff --git a/lib/systems/dash.js b/lib/systems/dash.js new file mode 100644 index 0000000..d05e9ea --- /dev/null +++ b/lib/systems/dash.js @@ -0,0 +1,116 @@ +import { System } from '@serpentity/serpentity'; + +const internals = { + kForce: 10 +}; + +import DasherNode from '../nodes/dasher'; + +/** + * Applies a dash as a force on an entity. Locks it until the button is released + * and a cooldown period has passed + * + * @extends {external:Serpentity.System} + * @class DashSystem + * @param {object} config a configuration object to extend. + */ +export default class DashSystem extends System { + + constructor(config = {}) { + + super(); + + /** + * The node collection of dashers + * + * @property {external:Serpentity.NodeCollection} dashers + * @instance + * @memberof DashSystem + */ + this.dashers = null; + } + + /** + * Initializes system when added. Requests dasher nodes + * + * @function added + * @memberof DashSystem + * @instance + * @param {external:Serpentity.Engine} engine the serpentity engine to + * which we are getting added + */ + added(engine) { + + this.dashers = engine.getNodes(DasherNode); + } + + /** + * Clears system resources when removed. + * + * @function removed + * @instance + * @memberof DashSystem + */ + removed() { + + this.dashers = null; + } + + /** + * Runs on every update of the loop. Triggers dash and manages cooldown + * + * @function update + * @instance + * @param {Number} currentFrameDuration the duration of the current + * frame + * @memberof DashSystem + */ + update(currentFrameDuration) { + + for (const dasher of this.dashers) { + + const dash = dasher.dash; + + if (dash.dashing && !dash.locked) { + this._dash(dasher); + } + + if (!dash.dashing && dash.locked && dash.currentCooldown >= dash.cooldown) { + this._unlock(dasher); + } + + if (dash.locked) { + dash.currentCooldown += currentFrameDuration; + } + + dash.dashing = 0; + } + } + + // Executes the dash action + + _dash(dasher) { + + const dash = dasher.dash; + const force = dasher.force; + + const angle = force.lastAngle || 0; + dash.locked = true; + dash.currentCooldown = 0; + + const xComponent = internals.kForce * Math.cos(angle); + const yComponent = internals.kForce * Math.sin(angle); + + force.x += xComponent; + force.y += yComponent; + } + + // Executes the unlock action + + _unlock(dasher) { + + dasher.dash.locked = false; + } +}; + + diff --git a/lib/systems/elastic.js b/lib/systems/elastic.js new file mode 100644 index 0000000..b94d371 --- /dev/null +++ b/lib/systems/elastic.js @@ -0,0 +1,93 @@ +import { System } from '@serpentity/serpentity'; + +const internals = { + kTightStiffness: 0.001, + kBaseStiffness: 0.0008, + kLooseStiffness: 0.0000001 +}; + +import ElasticNode from '../nodes/elastic'; + +/** + Changes the stiffness on the node when it's less extended + * + * @extends {external:Serpentity.System} + * @class ElasticSystem + * @param {object} config a configuration object to extend. + */ +export default class ElasticSystem extends System { + + constructor(config = {}) { + + super(); + + /** + * The node collection of entities that have external force + * + * @property {external:Serpentity.NodeCollection} elastics + * @instance + * @memberof ElasticSystem + */ + this.elastics = null; + } + + /** + * Initializes system when added. Requests elastic nodes + * + * @function added + * @memberof ElasticSystem + * @instance + * @param {external:Serpentity.Engine} engine the serpentity engine to + * which we are getting added + */ + added(engine) { + + this.elastics = engine.getNodes(ElasticNode); + } + + /** + * Clears system resources when removed. + * + * @function removed + * @instance + * @memberof ElasticSystem + */ + removed() { + + this.elastics = null; + } + + /** + * Runs on every update of the loop. Checks length of elastic and adjusts + * stiffness + * + * @function update + * @instance + * @param {Number} currentFrameDuration the duration of the current + * frame + * @memberof ElasticSystem + */ + update(currentFrameDuration) { + + for (const elastic of this.elastics) { + const constraint = elastic.body.body; + + const currentDistance = Math.abs( + Math.sqrt( + Math.pow(constraint.bodyA.position.x - constraint.bodyB.position.x, 2) + + Math.pow(constraint.bodyA.position.y - constraint.bodyB.position.y, 2))); + + if (currentDistance <= constraint.length) { + constraint.stiffness = internals.kLooseStiffness; + continue; + } + + if (currentDistance >= 2.6 * constraint.length) { + constraint.stiffness = internals.kTightStiffness; + continue; + } + + constraint.stiffness = internals.kBaseStiffness; + } + } +}; diff --git a/lib/systems/physics_to_attributes.js b/lib/systems/physics_to_attributes.js index a49a16b..e054710 100644 --- a/lib/systems/physics_to_attributes.js +++ b/lib/systems/physics_to_attributes.js @@ -1,6 +1,7 @@ import { System } from '@serpentity/serpentity'; import PhysicalWithAttributesNode from '../nodes/physical_with_attributes'; +import Config from '../config'; /** * Distribuets physics data to the related components @@ -64,8 +65,8 @@ export default class PhysicsToAttributesSystem extends System { update(currentFrameDuration) { for (const physicalEntity of this.physicalEntities) { - physicalEntity.position.x = physicalEntity.body.body.position.x; - physicalEntity.position.y = physicalEntity.body.body.position.y; + physicalEntity.position.x = physicalEntity.body.body.position.x * Config.meterSize; + physicalEntity.position.y = physicalEntity.body.body.position.y * Config.meterSize; physicalEntity.angle.angle = physicalEntity.body.body.angle; } } diff --git a/lib/systems/physics_world_control.js b/lib/systems/physics_world_control.js index daa39c1..a658d5c 100644 --- a/lib/systems/physics_world_control.js +++ b/lib/systems/physics_world_control.js @@ -91,7 +91,7 @@ export default class PhysicsWorldControlSystem extends System { */ update(currentFrameDuration) { - Engine.run(this.engine); + Engine.update(this.engine, currentFrameDuration); } }; diff --git a/lib/systems/reduce_velocity.js b/lib/systems/reduce_velocity.js new file mode 100644 index 0000000..6521014 --- /dev/null +++ b/lib/systems/reduce_velocity.js @@ -0,0 +1,70 @@ +import { System } from '@serpentity/serpentity'; + +import LimitedVelocityNode from '../nodes/limited_velocity'; + +/** + * Reduces velocity if it exceeds threshold + * + * @extends {external:Serpentity.System} + * @class ReduceVelocitySystem + * @param {object} config a configuration object to extend. + */ +export default class ReduceVelocitySystem extends System { + + constructor(config = {}) { + + super(); + + /** + * The node collection of entities that have external force + * + * @property {external:Serpentity.NodeCollection} limitedVelocityEntities + * @instance + * @memberof ReduceVelocitySystem + */ + this.limitedVelocityEntities = null; + } + + /** + * Initializes system when added. Requests limited velocity nodes + * + * @function added + * @memberof ReduceVelocitySystem + * @instance + * @param {external:Serpentity.Engine} engine the serpentity engine to + * which we are getting added + */ + added(engine) { + + this.limitedVelocityEntities = engine.getNodes(LimitedVelocityNode); + } + + /** + * Clears system resources when removed. + * + * @function removed + * @instance + * @memberof ReduceVelocitySystem + */ + removed() { + + this.limitedVelocityEntities = null; + } + + /** + * Runs on every update of the loop. Checks current velocity and adjusts if necessary + * + * @function update + * @instance + * @param {Number} currentFrameDuration the duration of the current + * frame + * @memberof ReduceVelocitySystem + */ + update(currentFrameDuration) { + + for (const limitedVelocityEntity of this.limitedVelocityEntities) { + console.log(limitedVelocityEntity.body.body.velocity, limitedVelocityEntity.maxVelocity.maxVelocity); + } + } +}; + diff --git a/lib/systems/render.js b/lib/systems/render.js index a2e3020..06d0e60 100644 --- a/lib/systems/render.js +++ b/lib/systems/render.js @@ -31,13 +31,13 @@ export default class RenderSystem extends System { /** * The pixi engine we will use to render * - * @property {external:PixiJs.Application} renderables + * @property {external:PixiJs.Application} application * @instance * @memberof RenderSystem */ - this._application = config.application; + this.application = config.application; - if (!this._application) { + if (!this.application) { throw new Error(internals.kNoPixiError); } } @@ -57,11 +57,11 @@ export default class RenderSystem extends System { this.renderables = engine.getNodes(RenderableNode); this.renderables.on('nodeAdded', (event) => { - this._application.stage.addChild(event.node.container.container); + this.application.stage.addChild(event.node.container.container); }); this.renderables.on('nodeRemoved', (event) => { - this._application.stage.removeChild(event.node.container.container); + this.application.stage.removeChild(event.node.container.container); }); } |