8pack 中文文档教程

发布于 7年前 浏览 36 更新于 3年前

8pack

Utility that injects modular Lua source into a PICO-8 project.

Installation

npm install 8pack -g

Examples

使用最新的 PICO-8 模板从 sample.lua 创建 sample.lua.p8

8pack tests/sample.lua

sample.lua 源代码注入 foo.p8< /em>。 如果已经存在,将覆盖 foo.p8

8pack tests/sample.lua foo.p8  

观察 /tests/sample.lua 的变化并覆盖到 /tests/sample.lua.p8

8pack /tests/sample.lua -w  

/tests/sample.lua 源代码写入/tests/foo.p8 使用 PICO-8 0.1.10c 模板

8pack tests/sample.lua /tests/foo.p8 --template 0.1.10c

Modules

Lua 模块可以通过 require('./libs/lib') 导入并将注入到p8 文件的顶部作为全局变量。 每个模块必须返回一个表、变量或函数。 使用 node.js 风格的路径前缀访问父目录:require('../lib')

main.lua:

local liba = require('./liba');

function _init()
    state = {
        hi = "hello world",
        i = 0,
        someFunct = function()
            return 1
        end,
        someOtherFunct = function()
            return 0
        end
    }
end

function _update60()
    state.i = state.i + rnd(liba.iterator);
end

function _draw()
    cls();
    print(liba.foo() .. ' ' .. state.i, 0, 116, 7);
end

./liba.lua module:< /em>

local lib = {
    foo = function()
        return 'hello world'
    end,
}
lib.iterator = 1

return lib;

每个注入都在 pico8 代码中实例化为一个新函数,因此模块之间不能共享代码。 作为解决方法,8pack 创建了一个 globals 表作为项目中最顶层的变量,可以从每个模块访问它。

./helpers.lua module:

local helpers = {
    setLevel = function(level)
        globals.level = level
    end,
}

return lib;

./game.lua module:

return {
   nextLevel = function() 
     globals.level =  globals.level+1
     return globals.level
   end
} 

JetBrains Watcher Arguments

全局安装 8pack 并将 8pack bin 添加为 watcher,并使用以下参数进行即时注入

$ProjectFileDir$/projectroot.lua $ProjectFileDir$/project.p8

,例如 Lua Windows 10 上 PhpStorm 2017.3 中的观察者(我使用的是官方 Lua 插件) 屏幕截图

ToDos

  • Circular dependancy checks
  • Module order checks
  • Prevent pedundant imports
  • Honor token limits
  • Compress code

8pack

Utility that injects modular Lua source into a PICO-8 project.

Installation

npm install 8pack -g

Examples

Create sample.lua.p8 from sample.lua using newest PICO-8 template

8pack tests/sample.lua

Inject sample.lua source into foo.p8. Will overwrite foo.p8 if already exists.

8pack tests/sample.lua foo.p8  

Watch /tests/sample.lua for changes and overwrite to /tests/sample.lua.p8

8pack /tests/sample.lua -w  

Write /tests/sample.lua source to /tests/foo.p8 using PICO-8 0.1.10c template

8pack tests/sample.lua /tests/foo.p8 --template 0.1.10c

Modules

Lua modules can be imported via require('./libs/lib') and will be injected to the top of the p8 file as global variables. Each module must return a table, variable or a function. Use node.js style path prefixes to access parent directories: require('../lib')

main.lua:

local liba = require('./liba');

function _init()
    state = {
        hi = "hello world",
        i = 0,
        someFunct = function()
            return 1
        end,
        someOtherFunct = function()
            return 0
        end
    }
end

function _update60()
    state.i = state.i + rnd(liba.iterator);
end

function _draw()
    cls();
    print(liba.foo() .. ' ' .. state.i, 0, 116, 7);
end

./liba.lua module:

local lib = {
    foo = function()
        return 'hello world'
    end,
}
lib.iterator = 1

return lib;

Each injection is instantiated as a new function in pico8 code thus no code can be shared between modules. As a workaround 8pack creates a globals table as the topmost variable inside the project which can be accessed from every module.

./helpers.lua module:

local helpers = {
    setLevel = function(level)
        globals.level = level
    end,
}

return lib;

./game.lua module:

return {
   nextLevel = function() 
     globals.level =  globals.level+1
     return globals.level
   end
} 

JetBrains Watcher Arguments

Install 8pack globally and add 8pack bin as watcher with following arguments for on-the-fly injection

$ProjectFileDir$/projectroot.lua $ProjectFileDir$/project.p8

e.g. for Lua watcher in PhpStorm 2017.3 on Windows 10 (I'm using the official Lua plugin) Screenshot

ToDos

  • Circular dependancy checks
  • Module order checks
  • Prevent pedundant imports
  • Honor token limits
  • Compress code
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文