Files

144 lines
6.1 KiB
Lua

-- 模拟 cocos 环境,跑一遍 out/gm_combined.lua,尽量把运行时错误提前暴露
local buttons = {}
local scheduled = nil
local scene = nil
local Node = {}
Node.__index = Node
function Node.new(name)
local o = setmetatable({
name = name or "node", children = {}, visible = true, posx = 0, posy = 0,
w = 0, h = 0, parent = nil, handlers = {}, texts = {},
touchEnabled = false, swallow = false,
}, Node)
return o
end
function Node:addChild(c, z) if c then c.parent = self; table.insert(self.children, c) end end
function Node:removeFromParent() if self.parent then for i, v in ipairs(self.parent.children) do if v == self then table.remove(self.parent.children, i) break end end end self.parent = nil end
function Node:getParent() return self.parent end
function Node:setPosition(x, y) if type(x) == "table" then self.posx, self.posy = x.x, x.y else self.posx, self.posy = x, y end end
function Node:getPosition() return { x = self.posx, y = self.posy } end
function Node:setContentSize(w, h) if type(w) == "table" then self.w, self.h = w.width, w.height else self.w, self.h = w, h end end
function Node:getContentSize() return { width = self.w, height = self.h } end
function Node:setAnchorPoint() end
function Node:getAnchorPoint() return { x = 0, y = 0 } end
function Node:setVisible(v) self.visible = v end
function Node:isVisible() return self.visible end
function Node:retain() return self end
function Node:setScale() end
function Node:setColor() end
function Node:setOpacity() end
function Node:setString(s) self.texts[#self.texts + 1] = tostring(s) end
function Node:setTouchEnabled(v) self.touchEnabled = v end
function Node:setSwallowTouches(v) self.swallow = v end
function Node:addTouchEventListener(fn)
self.handlers[#self.handlers + 1] = fn
buttons[#buttons + 1] = self
end
function Node:convertToNodeSpace(p) return p end
function Node:getTouchBeganPosition() return { x = 0, y = 0 } end
function Node:getTouchMovePosition() return { x = 0, y = 0 } end
function Node:getTouchEndPosition() return { x = 0, y = 0 } end
function Node:getChildren() return self.children end
local label = {}
label.createWithSystemFont = function(text, font, size)
local n = Node.new("label:" .. tostring(text))
n.text = text
return n
end
cc = {}
cc.p = function(x, y) return { x = x, y = y } end
cc.size = function(w, h) return { width = w, height = h } end
cc.c3b = function(r, g, b) return { r = r, g = g, b = b } end
cc.c4b = function(r, g, b, a) return { r = r, g = g, b = b, a = a } end
cc.c4f = function(r, g, b, a) return { r = r, g = g, b = b, a = a } end
cc.Node = { create = function() return Node.new("cc.Node") end }
cc.Label = label
cc.LayerColor = { create = function(c) return Node.new("LayerColor") end }
cc.Scene = { create = function() return Node.new("Scene") end }
cc.DrawNode = { create = function()
local n = Node.new("DrawNode")
n.clear = function() end
n.drawSolidRect = function() end
n.drawSolidCircle = function() end
return n
end }
cc.Sprite = { create = function(path)
if not path then return nil end
local n = Node.new("Sprite")
n.w, n.h = 512, 512
return n
end }
cc.Handler = { EVENT_TOUCH_BEGAN = 0, EVENT_TOUCH_MOVED = 1, EVENT_TOUCH_ENDED = 2, EVENT_TOUCH_CANCELLED = 3 }
cc.EventListenerTouchOneByOne = { create = function()
local l = { h = {} }
l.setSwallowTouches = function() end
l.registerScriptHandler = function(self, fn, tag) self.h[tag] = fn end
return l
end }
ccui = {}
ccui.Layout = { create = function() return Node.new("Layout") end }
ccui.LAYOUT_COLOR_SOLID = 1
local dispatcher = { addEventListenerWithSceneGraphPriority = function() end }
local scheduler = { scheduleScriptFunc = function(self, fn, interval, paused) scheduled = fn end }
local fu = {
getWritablePath = function() return "/data/user/0/org.sailor.djddz/files/" end,
isFileExist = function(p) return false end,
writeStringToFile = function(self, data, path) print("[mock] write", path, #data) return true end,
}
scene = Node.new("Scene")
local director = {
getWinSize = function() return { width = 1280, height = 720 } end,
getVisibleSize = function() return { width = 1280, height = 720 } end,
getRunningScene = function() return scene end,
runWithScene = function(self, s) scene = s end,
getScheduler = function() return scheduler end,
getEventDispatcher = function() return dispatcher end,
getTextureCache = function() return { addImage = function() return {} end } end,
}
cc.Director = { getInstance = function() return director end }
cc.FileUtils = { getInstance = function() return setmetatable(fu, { __index = fu }) end }
-- 假游戏数据
gUserGameData = {
gamemode = 0,
gamedata = {
{ xxgoldbean = 1000, tzgoldbean = 2000, zdgoldbean = 3000, xxlevel = 1, xxexp = 0,
xxmaxbeilv = 4, win = 3, loss = 2, dznum = 0 },
{ xxgoldbean = 1100, tzgoldbean = 2100, zdgoldbean = 3100, xxlevel = 2, xxexp = 0,
xxmaxbeilv = 4, win = 1, loss = 1, dznum = 1 },
{ xxgoldbean = 1200, tzgoldbean = 2200, zdgoldbean = 3200, xxlevel = 3, xxexp = 0,
xxmaxbeilv = 4, win = 0, loss = 0, dznum = 2 },
},
}
gDdzCfg = { saveGameInfo = function() print("[mock] saveGameInfo") end }
commonHandler = { notifyEvent = function(self, name) print("[mock] notify", name) end }
local chunk, err = loadfile("out/gm_combined.lua")
if not chunk then print("LOAD FAIL:", err) os.exit(1) end
local ok, rerr = pcall(chunk)
if not ok then print("RUN FAIL:", rerr) os.exit(1) end
print("chunk ok, buttons registered:", #buttons)
-- 跑几次 tick,触发建面板
print("scheduled?", scheduled)
for i = 1, 8 do
local ok3, e3 = pcall(scheduled, 0.05)
if not ok3 then print("TICK FAIL:", e3) end
end
print("after ticks, buttons:", #buttons)
local clicked = 0
for _, b in ipairs(buttons) do
for _, fn in ipairs(b.handlers) do
local ok2, e = pcall(fn, b, 2)
if not ok2 then print("CLICK FAIL:", e) else clicked = clicked + 1 end
end
end
print("clicked handlers:", clicked)
print("gd1 gold:", gUserGameData.gamedata[1].xxgoldbean, "lv:", gUserGameData.gamedata[1].xxlevel)