Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

document everything, add some properties, remove some energy params

  • Loading branch information...
commit 052f914039219d3d07979c556151869d27fa4539 1 parent 234d9e3
@substack authored
Showing with 60 additions and 4 deletions.
  1. +5 −4 index.js
  2. +55 −0 readme.markdown
View
9 index.js
@@ -7,9 +7,9 @@ module.exports = function (energy, opts) {
};
function Relay (energy, opts) {
- [ 'defend', 'attack', 'throughput', 'radius', 'storage', 'generate' ]
- .forEach(function (key) { if (!energy[key]) energy[key] = 0 })
- ;
+ [ 'defend', 'attack', 'storage' ].forEach(function (key) {
+ if (!energy[key]) energy[key] = 0;
+ });
this.energy = energy;
this.delay = opts.delay || 1000;
}
@@ -18,7 +18,8 @@ inherits(Relay, EventEmitter);
Relay.prototype.attack = function (defender) {
var attack = new EventEmitter;
- var attacker = this;
+ attack.defender = defender;
+ var attacker = attack.attacker = this;
var iv = setInterval(function () {
var sum = attacker.energy.attack + defender.energy.defend;
View
55 readme.markdown
@@ -50,6 +50,61 @@ tree -9, monster 0
tree was defeated
```
+# methods
+
+``` js
+var relay = require('energy-relay')
+```
+
+## var unit = relay(energy, opts={})
+
+Create a game `unit` with an `energy` levels object. The energy keys are:
+
+* defend
+* attack
+* storage
+
+Specify `opts.delay` to set the amount of time between attack event.
+
+## var attack = unit.attack(defender)
+
+Attack the `defender` relay with `unit`, returning an attack object.
+
+## attack.cancel()
+
+Cancel an active `attack`.
+
+# events
+
+## attack.on('delta', function (deltas) {})
+
+For the `attack` in progress, emit a `'delta'` with `'attack'` and `'defend'`
+keys every time the energy levels for the attacker or defender change.
+
+## attack.on('success', function () {})
+
+When an `attack` was successful, the `'success'` event fires.
+
+## attack.on('failure', function () {})
+
+When an `attack` was unsuccessful because the attacker ran out of attack energy,
+the `'failure'` event is emitted.
+
+## attack.on('end', function () {})
+
+When an attack is over for whatever reason, the `'end'` event fires.
+
+## unit.on('damage', function (damage, attacker) {})
+
+When a `unit` gets attacked, a `'damage'` event is emitted with the amount of
+damage as an integer and the `attacker` unit responsible for the damage.
+
+## unit.on('cost', function (cost) {})
+
+When a `unit` spends energy on attacks or transfers, the `'cost'` event gets
+emitted with the `'cost.type'` of transfer (`'attack'` or `'transfer'`) and the
+`cost.value` amount of energy.
+
# install
With [npm](https://npmjs.org) do:
Please sign in to comment.
Something went wrong with that request. Please try again.