Search code examples
javascriptscreeps

How to extend the creep class in screeps


I am having a hard time trying to figure out how to extend the creep class to add my own functions in the new javascript mmo game, Screeps -> www.screeps.com


Solution

  • I dunno how to do that, but I created a wrapper class like this one:

    You created a function for calling memory, and try to use it 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
    }
    

    So 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 deal with myCreeps, locally stored.