2014-12-10 2 views
5

저는 잠시 동안 Screeps를 다뤄 보았습니다. 어젯밤 크리프 메인 클래스에서 Miner와 Transporter라는 두 클래스를 파생시켜 클래스 계층 구조로 일부 동작을 결정했습니다. 그러나 때마다 나는 Screeps 개체에서 상속을 구현하는 방법은 무엇입니까?

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); 
    } 
}; 

답변

6

...

Miner.prototype = Creep.prototype; 

... 둘 다 프로토 타입이 실제로 같은 객체 것을 JS 말한다. 따라서 Miner.prototype에 대한 모든 업데이트는 Creep.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, ... } 
+0

코드를 Creeps 코드 패널에 복사하고 붙여 넣은 것으로, 예상했던 것과 똑같이 작동합니다. 철저하고 완전한 답변 주셔서 감사합니다! – Toolmaker

관련 문제