refactor
This commit is contained in:
parent
d7223b6d60
commit
9882dc75e1
48
main.js
48
main.js
|
@ -2,14 +2,23 @@ var roleHarvester = require('role.harvester');
|
||||||
var roleUpgrader = require('role.upgrader');
|
var roleUpgrader = require('role.upgrader');
|
||||||
var roleBuilder = require('role.builder');
|
var roleBuilder = require('role.builder');
|
||||||
|
|
||||||
var _defaultBodyParts = [WORK, CARRY, MOVE];
|
var creepsConfig = {
|
||||||
var _roleNames = ["harvester", "builder", "upgrader"];
|
harvester: {
|
||||||
var _minimums = {
|
bodyParts: [WORK, CARRY, MOVE],
|
||||||
harvester: 4,
|
minimumCreeps: 4,
|
||||||
builder: 2,
|
run: roleHarvester.run
|
||||||
upgrader: 2
|
},
|
||||||
};
|
builder:{
|
||||||
|
bodyParts: [WORK, CARRY, MOVE],
|
||||||
|
minimumCreeps: 2,
|
||||||
|
run: roleBuilder.run
|
||||||
|
},
|
||||||
|
upgrader: {
|
||||||
|
bodyParts: [WORK, CARRY, MOVE],
|
||||||
|
minimumCreeps: 2,
|
||||||
|
run: roleUpgrader.run
|
||||||
|
}
|
||||||
|
}
|
||||||
module.exports.loop = function(){
|
module.exports.loop = function(){
|
||||||
// Cleanup Creeps
|
// Cleanup Creeps
|
||||||
for (var name in Memory.creeps){
|
for (var name in Memory.creeps){
|
||||||
|
@ -18,31 +27,20 @@ module.exports.loop = function(){
|
||||||
console.log("Clearning non-existing creep memory: ", name);
|
console.log("Clearning non-existing creep memory: ", name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Refactor some similar behavior for all roles
|
// Refactor some similar behavior for all roles
|
||||||
|
|
||||||
_roleNames.forEach(function(roleName){
|
for (var roleName in creepsConfig){
|
||||||
var creepsOfKind = _.filter(Game.creeps, (creep) => creep.memory.role == roleName);
|
var creepsOfKind = _.filter(Game.creeps, (creep) => creep.memory.role == roleName);
|
||||||
if (creepsOfKind.length < _minimums[roleName]){
|
if (creepsOfKind.length < creepsConfig[roleName].minimumCreeps){
|
||||||
var newName = Game.spawns.Spawn1.createCreep(_defaultBodyParts, undefined, {role: roleName});
|
var newName = Game.spawns.Spawn1.createCreep(creepsConfig[roleName].bodyParts, undefined, {role: roleName});
|
||||||
console.log("Spawning new ", roleName, ": ", newName);
|
console.log("Spawning new ", roleName, ": ", newName);
|
||||||
}
|
}
|
||||||
});
|
};
|
||||||
|
|
||||||
// Run stuff for each creep
|
// Run stuff for each creep
|
||||||
for (var name in Game.creeps){
|
for (var name in Game.creeps){
|
||||||
var creep = Game.creeps[name];
|
var creep = Game.creeps[name];
|
||||||
if (creep.memory.role == 'harvester'){
|
var role = creep.memory.role;
|
||||||
roleHarvester.run(creep);
|
creepsConfig[role].run(creep);
|
||||||
}
|
|
||||||
|
|
||||||
if (creep.memory.role == 'upgrader'){
|
|
||||||
roleUpgrader.run(creep);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (creep.memory.role == 'builder'){
|
|
||||||
roleBuilder.run(creep);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user