gpt4 book ai didi

javascript - 如何在 Screeps 对象中实现继承?

转载 作者:数据小太阳 更新时间:2023-10-29 06:11:23 24 4
gpt4 key购买 nike

我已经研究 Screeps 一段时间了,昨晚我决定通过从 Creep 主类派生两个类 Miner 和 Transporter,将我的一些行为纳入类层次结构。但是,每当我做

console.log(_.functions(minerInstance));

我得到的函数列表和我做的时候完全一样

console.log(_.functions(transporterInstance));

有人可以告诉我我是否做错了什么,或者我是否真的遇到了我的代码运行环境的限制?这是我的代码:

////////////////////////////
// Creep.js
var Creep = function(creep, room) {
this.creep = creep;
this.room = room;
this.name = creep.name;
this.id = creep.id;
};

module.exports = Creep;

Creep.prototype = {
tick: function() {
console.log("Base class implementation of tick(), should never happen.");
},

getRole: function() {
return this.creep.memory.role;
}
};

////////////////////////////
// Miner.js
var Creep = require("Creep");

var Miner = function(creep, room) {
this.base = Creep;
this.base(creep, room);
//Creep.call(this, creep, room);
};

module.exports = Miner;

Miner.prototype = Creep.prototype;

Miner.prototype.tick = function() {
var creep = this.creep;

if (creep.memory.activity === undefined || creep.memory.activity === "") {
var target = creep.pos.findNearest(Game.SOURCES_ACTIVE);
this.mine(creep, target);
}

var act = creep.memory.activity;
if (act == "mine") {
var target = this.getTarget(creep);
if (target !== undefined) {
if (creep.energy < creep.energyCapacity) {
creep.moveTo(target);
creep.harvest(target);
} else {
console.log("Write dump to truck code");
/*var trucks = find.transporterInRange(creep, 1);
if (trucks.length) {
creep.moveTo(trucks[0]);
var amount = trucks[0].energyCapacity - trucks[0].energy;
creep.transferEnergy(trucks[0], amount);
}*/
}
}
}
};

Miner.prototype.mine = function(creep, target) {
creep.memory.target = target.id;
creep.memory.activity = "mine";
};

Miner.prototype.getTarget = function(creep) {
return Game.getObjectById(creep.memory.target);
};

////////////////////////////
// Transporter.js
var Creep = require("Creep");

var Transporter = function(creep, room) {
Creep.call(this, creep, room);
};

module.exports = Transporter;

Transporter.prototype = Creep.prototype;

Transporter.prototype.tick = function() {
var creep = this.creep;
if (creep.energy < creep.energyCapacity) {
var miner = this.room.findByRole(creep, "miner");
console.log(miner);
if (miner !== null) {
//console.log(miner[0].name);
//creep.moveTo(miner);

} else
console.log("no miners found");
} else {
console.log("moving to drop");
//var drop = find.nearestEnergyDropOff(creep);
//creep.moveTo(drop);
//creep.transferEnergy(drop);
}
};

最佳答案

用这条线...

Miner.prototype = Creep.prototype;

...你告诉 JS 这两个原型(prototype)实际上是同一个对象。因此,Miner.prototype 的任何更新也会影响 Creep.prototype

一种可能的方法是在建立原型(prototype)之间的链接时使用Object.create。这是一个简化的例子:

function Foo(a) {
this.a = a;
}

Foo.prototype.tick = function() { console.log('Foo ticks'); };
Foo.prototype.tock = function() { console.log('Foo tocks'); };

function Bar(a, b) {
this.base = Foo;
this.base(a);
this.b = b;
}

Bar.prototype = Object.create(Foo.prototype);
// as you inherit all the properties, you'll have to reassign a constructor
Bar.prototype.constructor = Bar;
Bar.prototype.tick = function() { console.log('Bar ticks'); };

var f = new Foo(1);
f.tick(); // Foo ticks
f.tock(); // Foo tocks
console.log(f); // Foo { a=1, ... }

var b = new Bar(1, 2);
b.tick(); // Bar ticks
b.tock(); // Foo tocks
console.log(b); // Bar { a=1, b=2, ... }

关于javascript - 如何在 Screeps 对象中实现继承?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27400822/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com