Permalink
Browse files

reached actual feature parity with ruby-mmo (i.e. added rats)

  • Loading branch information...
1 parent 9e1f218 commit 82301d6c87440c8f44ac94e4e49b880bd0e2bb29 @troygoode committed May 15, 2012
Showing with 110 additions and 30 deletions.
  1. +1 −0 actions/attack.js
  2. +7 −14 app.js
  3. +40 −0 monster.js
  4. +21 −0 monsters/rat.js
  5. +23 −13 player.js
  6. +4 −1 players/player1.js
  7. +6 −1 players/player2.js
  8. +8 −1 world.js
View
@@ -26,6 +26,7 @@ module.exports = function(world){
// if the target died, reward each attacker
if(!target.alive){
+ console.log('! %s has been killed.', target.name);
var xp = target.maxHealth / attack.attackers.length;
attack.attackers.forEach(function(attacker){
if(attacker.alive)
View
21 app.js
@@ -1,15 +1,16 @@
var _ = require('underscore')
, world = require('./world')
, actions = require('./actions')(world)
- , ROUNDS = 50
- , DEBUG = false;
+ , ROUNDS = 50;
var proxyWorld = function(){
return {
players: world.players.map(function(p){
return {
- name: p.name
- , isAlive: p.health > 0
+ type: p.type
+ , name: p.name
+ , alive: p.health > 0
+ , level: p.level ? p.level : 0
};
})
};
@@ -20,21 +21,19 @@ for(var round = 0; round < ROUNDS; round++){
var players = world.players.filter(function(p){ return p.alive; });
if(players.length === 1){
console.log('WINNER: %s', players[0].name);
- console.log('========================================');
- console.log(world);
break;
}
// shuffle the order of players
players = _.shuffle(players);
// begin the round
- console.log('ROUND %d', round);
+ console.log('ROUND %d', round + 1);
actions.beginRound();
// give each player their turn
players.forEach(function(player){
- console.log('----------------------------------------\nPlayer: %s', player.name);
+ console.log('----------------------------------------\n%s: %s (%d/%d)', player.type, player.name, player.health, player.maxHealth);
player.tick(proxyWorld(), function(action, target){
actions.execute(player, action, target);
});
@@ -43,10 +42,4 @@ for(var round = 0; round < ROUNDS; round++){
// end the round
actions.endRound();
console.log('========================================');
-
- // !!! debug
- if(DEBUG){
- console.log(world);
- console.log('========================================');
- }
}
View
@@ -0,0 +1,40 @@
+var Monster = module.exports = function(){
+ this.type = 'monster';
+ this.alive = true;
+};
+
+Monster.prototype.tick = function(world, cb){
+ //noop
+};
+
+Monster.prototype.attack = function(target){
+ if(!this.alive || !target)
+ return;
+
+ var damage = this.strength - target.defense/2;
+ if(damage < 1)
+ damage = 1;
+ target.takeDamage(damage);
+};
+
+Monster.prototype.takeDamage = function(damage){
+ if(!this.alive || damage < 0)
+ return;
+
+ this.health -= damage > this.health
+ ? this.health
+ : damage;
+ if(this.health <= 0)
+ this.alive = false;
+};
+
+Monster.prototype.rest = function(){
+ if(!this.alive)
+ return;
+
+ this.health += 10;
+};
+
+Monster.prototype.gainXp = function(xp){
+ //noop
+};
View
@@ -0,0 +1,21 @@
+var Monster = require('../monster');
+
+var Rat = module.exports = function(name){
+ this.type = 'monster-rat';
+ this.name = name;
+ this.health = this.maxHealth = 10;
+ this.strength = 2;
+ this.defense = 2;
+};
+
+Rat.prototype = new Monster();
+Rat.prototype.constructor = Rat;
+
+Rat.prototype.tick = function(world, cb){
+ var self = this;
+ var targets = world.players.filter(function(p){
+ return p.alive && p.name != self.name;
+ });
+ var randomIndex = Math.floor(Math.random() * targets.length);
+ cb('attack', targets[randomIndex].name);
+};
View
@@ -4,6 +4,7 @@ var LEVEL_THRESHOLDS = [50,100,200,500,1000,1500,2500,4000]
, DEFENSE_INDEX = [20,22,24,26,28,30,32,34];
var Player = module.exports = function(name, script){
+ this.type = 'player';
this.name = name;
this.script = script;
this.health = this.maxHealth = HEALTH_INDEX[0];
@@ -15,12 +16,17 @@ var Player = module.exports = function(name, script){
};
Player.prototype.attack = function(target){
+ if(!this.alive || !target)
+ return;
+
var damage = this.strength - target.defense/2;
+ if(damage < 1)
+ damage = 1;
target.takeDamage(damage);
};
Player.prototype.takeDamage = function(damage){
- if(!this.alive)
+ if(!this.alive || damage < 0)
return;
this.health -= damage > this.health
@@ -31,21 +37,25 @@ Player.prototype.takeDamage = function(damage){
};
Player.prototype.rest = function(){
+ if(!this.alive)
+ return;
+
this.health += 10;
};
Player.prototype.gainXp = function(xp){
- this.xp += xp;
- if(this.xp >= LEVEL_THRESHOLDS[this.level])
- this.levelUp();
-};
+ if(!this.alive || xp < 0)
+ return;
-Player.prototype.levelUp = function(){
- this.level++;
- if(STRENGTH_INDEX[this.level])
- this.strength = STRENGTH_INDEX[this.level];
- if(DEFENSE_INDEX[this.level])
- this.defense = DEFENSE_INDEX[this.level];
- if(HEALTH_INDEX[this.level])
- this.maxHealth = HEALTH_INDEX[this.level];
+ this.xp += xp;
+ if(this.xp >= LEVEL_THRESHOLDS[this.level]){
+ console.log('^ %s levelled up!', this.name);
+ this.level++;
+ if(STRENGTH_INDEX[this.level])
+ this.strength = STRENGTH_INDEX[this.level];
+ if(DEFENSE_INDEX[this.level])
+ this.defense = DEFENSE_INDEX[this.level];
+ if(HEALTH_INDEX[this.level])
+ this.maxHealth = HEALTH_INDEX[this.level];
+ }
};
View
@@ -1,3 +1,6 @@
module.exports = function(world, cb){
- cb('attack', 'player2');
+ // targets random enemies (including self or dead enemies)
+ var randomIndex = Math.floor(Math.random() * world.players.length);
+ var target = world.players[randomIndex];
+ cb('attack', target.name);
};
View
@@ -1,3 +1,8 @@
module.exports = function(world, cb){
- cb('attack', 'player1');
+ var self = this;
+ var targets = world.players.filter(function(p){
+ return p.alive && p.name != self.name;
+ });
+ var randomIndex = Math.floor(Math.random() * targets.length);
+ cb('attack', targets[randomIndex].name);
};
View
@@ -1,4 +1,5 @@
-var sandbox = require('./sandbox');
+var sandbox = require('./sandbox')
+ , Rat = require('./monsters/rat');
var world = module.exports = {
players: require('./players')
@@ -7,3 +8,9 @@ var world = module.exports = {
world.players.forEach(function(player){
player.tick = player.script.runInNewContext(sandbox());
});
+
+// create 2 rats for every 1 player
+var ratCount = world.players.length * 2;
+for(var i = 0; i < ratCount; i++)
+ world.players.push(new Rat('Rat' + i));
+

0 comments on commit 82301d6

Please sign in to comment.