简体   繁体   English

如何在screeps中扩展creep类

[英]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 我正在努力弄清楚如何扩展creep类以在新的javascript mmo游戏中添加我自己的函数,Screeps - > www.screeps.com

Has anyone figured this out yet ? 有人想出来了吗?

Thanks 谢谢

A bit of an old thread, and I am not sure if Screeps has changed since the initial query was posted, but here are my thoughts... 有点旧线程,我不确定自从初始查询发布以来Screeps是否发生了变化,但这是我的想法......

Why have a wrapper class?? 为什么有一个包装类? why not extend the original / game provided Creep class ? 为什么不扩展原始/游戏提供的Creep类?

eg 例如

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

Make sure to check out the screeps inheritance structure.. (Google the screeps API and check out the 'Prototypes' section on the landing page) 请务必查看screeps继承结构..(Google screeps API并查看着陆页上的'Prototypes'部分)

This can save a lot of duplicate code, for example one declaration of an inherited function in the 'Structure' prototype may save a seperate declaration for each individual Structure sub class protocols. 这可以节省大量重复的代码,例如,“Structure”原型中的一个继承函数声明可以为每个单独的Structure子类协议保存单独的声明。

Hope this helps. 希望这可以帮助。

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"); 见下文: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. 然后处理myCreeps,本地存储。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM