How to extend the creep creep class

I find it difficult to find ways to extend the creep class to add my own functions to the new javascript-mmo game, Screeps β†’ www.screeps.com

Has anyone else understood this?

thank

+4
source share
2 answers

I don’t know how to do this, but I created a wrapper class similar to this:

You created a function to call memory and try to use its s property. See below: var _ = require ("lodash");

function MyCreep(creep){
    this.creep = creep;
    this.memoryProp = creep.memory;
}

MyCreep.prototype.memoryFunc = function(){
    return this.creep.memory;
};

MyCreep.prototype.moveTo = function(target){
    this.creep.moveTo(target);
}

MyCreep.prototype.myFunction = function(target){
    //TODO something
}

Therefore, when I need to deal with creep, I do:

var myCreeps = [];
for (var creep in Game.creeps){
    creep.memory.role = "hello memory";
    var myCreep = new MyCreep(Game.creeps[creep]);
    myCreeps.push(myCreep);      ;
    console.log("original creep memory: "+creep.memory.role);
    console.log("my creep memory func: "+myCreep.memoryFunc().role);
    console.log("my creep memory prop: "+myCreep.memoryProp.role);
}

or

var myCreeps = [];
_.forEach(Game.creeps, function(creep){
    var myCreep = new MyCreep(creep);
    myCreeps.push(myCreep);
});

and then process the local myCreeps files.

+3
source

, , Screeps , ...

? / Creep?

.

Creep.prototype.myFunction = function(target){
         // my logic
       }
Hide result

screeps. (Google API screeps "" )

, , "" .

, .

+5

All Articles