当前位置: 首页 > 工具软件 > Screeps > 使用案例 >

screeps实战笔记(更新中)

厉坚
2023-12-01

main.js

const data = require('data').creeps;
const harvester = require('role.harvester')
const upgrader = require('role.upgrader')

module.exports.loop = async function async () {
    for (let i in data) {
        // 遍历每个creeps
        let value = data[i]
        let creep = Game.creeps[value.name]

        if (creep && creep.memory.role == 'harvester') {
            harvester.run(creep)
        } else if (creep && creep.memory.role == 'upgrader') {
            upgrader.run(creep)
        } else if (!creep) {
            await Game.spawns['Spawn1'].spawnCreep(value.body, value.name, {memory: { role: value.role }});
        }
    }
}

data.js

let data = {
  creeps: [
       { name: 'Harvester1', body: [WORK,CARRY,MOVE,MOVE,MOVE], role: 'harvester' },
       { name: 'Harvester2', body: [WORK,CARRY,MOVE,MOVE,MOVE], role: 'harvester' },
       { name: 'Builder1', body: [WORK,CARRY,MOVE,MOVE,MOVE], role: 'builder' },
  ]
};
module.exports = data;

role.harvester.js

var harvester = {
  run: function(creep) {
      if (creep.store.getFreeCapacity() > 0) {
          let source = creep.room.find(FIND_SOURCES)
          if (creep.harvest(source[0]) == ERR_NOT_IN_RANGE) {
              creep.moveTo(source[0])
          }
      } else {
          if (creep.transfer(Game.spawns['Spawn1'], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
              creep.moveTo(Game.spawns['Spawn1'])
          }
      } 
  }
}

module.exports = harvester;

role.upgrader.js

var harvester = {
  run: function(creep) {
      if (creep.store.getFreeCapacity() > 0) {
          let source = creep.room.find(FIND_SOURCES)
          if (creep.harvest(source[0]) == ERR_NOT_IN_RANGE) {
              creep.moveTo(source[0])
          }
      } else {
          if (creep.upgradeController(creep.room.controller) == ERR_NOT_IN_RANGE) {
              creep.moveTo(creep.upgradeController(creep.room.controller))
          }
      } 
  }
}

module.exports = harvester;
 类似资料: