diff --git a/assets/minesweeper/CMakeLists.txt b/assets/minesweeper/CMakeLists.txt index 96e0af1..c98c82d 100644 --- a/assets/minesweeper/CMakeLists.txt +++ b/assets/minesweeper/CMakeLists.txt @@ -3,4 +3,6 @@ # This software is released under the MIT License. # https://opensource.org/licenses/MIT -add_asset(TILESET ui.png type=PALETTIZED tileWidth=8 tileHeight=8) \ No newline at end of file +add_asset(TILESET ui.png type=PALETTIZED tileWidth=8 tileHeight=8) +add_asset(TILESET cell.png type=PALETTIZED tileWidth=8 tileHeight=8) +add_asset(TILESET grid.png type=PALETTIZED tileWidth=8 tileHeight=8) \ No newline at end of file diff --git a/assets/palette/paletteMinesweeper.png b/assets/palette/paletteMinesweeper.png index 26bb086..7193630 100644 Binary files a/assets/palette/paletteMinesweeper.png and b/assets/palette/paletteMinesweeper.png differ diff --git a/assets/scene/minesweeper.lua b/assets/scene/minesweeper.lua index 198ff35..4be4ba9 100644 --- a/assets/scene/minesweeper.lua +++ b/assets/scene/minesweeper.lua @@ -9,11 +9,85 @@ module('text') module('tileset') module('texture') +CELL_STATE_DEFAULT = 0 +CELL_STATE_HOVER = 1 +CELL_STATE_DOWN = 2 +CELL_STATE_DISABLED = 3 + screenSetBackground(colorBlack()) camera = cameraCreate(CAMERA_PROJECTION_TYPE_ORTHOGRAPHIC) -tileset = tilesetGetByName("ui") -ui = textureLoad(tileset.texture) +tilesetUi = tilesetGetByName("ui") +textureUi = textureLoad(tilesetUi.texture) + +tilesetGrid = tilesetGetByName("grid") +textureGrid = textureLoad(tilesetGrid.texture) +gridPiece = tilesetPositionGetUV(tilesetGrid, 0, 0) +gridSlice = tilesetPositionGetUV(tilesetGrid, 11, 3) + +tilesetCell = tilesetGetByName("cell") +textureCell = textureLoad(tilesetCell.texture) +cellSliceHover = tilesetPositionGetUV(tilesetCell, 3, 4) +cellSliceDefault = tilesetPositionGetUV(tilesetCell, 3, 5) +cellSliceDown = tilesetPositionGetUV(tilesetCell, 3, 6) +cellSliceDisabled = tilesetPositionGetUV(tilesetCell, 3, 7) + +width = 10 +height = 14 + +i = 0 +cells = {} +for y = 1, height do + for x = 1, width do + cells[i] = CELL_STATE_DEFAULT + i = i + 1 + end +end + +function cellDraw(x, y, type) + if type == CELL_STATE_HOVER then + slice = cellSliceHover + elseif type == CELL_STATE_DOWN then + slice = cellSliceDown + elseif type == CELL_STATE_DISABLED then + slice = cellSliceDisabled + else + slice = cellSliceDefault + end + + spriteBatchPush(textureCell, + x, y, + x + tilesetCell.tileWidth, y + tilesetCell.tileHeight, + colorWhite(), + slice.u0, slice.v0, + slice.u1, slice.v1 + ) +end + +function backgroundDraw() + local t = (TIME.time * 3) % 1 + local offXStart = gridPiece.u1 * t + local offYStart = gridPiece.v1 * t + local offXEnd = offXStart + gridPiece.u1 + local offYEnd = offYStart + gridPiece.v1 + + -- Tile background + local cols = math.ceil(screenGetWidth() / tilesetGrid.tileWidth) + local rows = math.ceil(screenGetHeight() / tilesetGrid.tileHeight) + + for y = 0, rows do + for x = 0, cols do + spriteBatchPush( + textureGrid, + x * tilesetGrid.tileWidth, y * tilesetGrid.tileHeight, + (x + 1) * tilesetGrid.tileWidth, (y + 1) * tilesetGrid.tileHeight, + colorWhite(), + gridSlice.u0 + offXStart, gridSlice.v0 + offYStart, + gridSlice.u1 + offXEnd, gridSlice.v1 + offYEnd + ) + end + end +end function sceneDispose() end @@ -27,17 +101,20 @@ function sceneRender() camera.bottom = screenGetHeight() camera.right = screenGetWidth() - slice = tilesetTileGetUV(tileset, 0) - spriteBatchPush(ui, - 0, 0, - ui.width * slice.u1, ui.height * slice.v1, - colorRainbow(), - slice.u0, slice.v0, - slice.u1, slice.v1 - ) - spriteBatchFlush() + backgroundDraw() - -- textDraw(0, 0, "Hello World", colorRainbow()) + offsetX = 32 + offsetY = 32 + for y = 0, height - 1 do + for x = 0, width - 1 do + cellDraw( + x * tilesetCell.tileWidth + offsetX, + y * tilesetCell.tileHeight + offsetY, + cells[i] + ) + end + end + spriteBatchFlush() cameraPopMatrix() end \ No newline at end of file diff --git a/src/script/module/display/modulecolor.c b/src/script/module/display/modulecolor.c index 9063a27..8fe4f0f 100644 --- a/src/script/module/display/modulecolor.c +++ b/src/script/module/display/modulecolor.c @@ -27,9 +27,8 @@ void moduleColor(scriptcontext_t *context) { lua_pushstring(context->luaState, "__tostring"); lua_pushcfunction(context->luaState, moduleColorToString); lua_settable(context->luaState, -3); - - lua_pop(context->luaState, 1); } + lua_pop(context->luaState, 1); lua_register(context->luaState, "color", moduleColorFuncColor); lua_register(context->luaState, "colorRainbow", moduleColorRainbow); diff --git a/src/script/module/display/moduletileset.c b/src/script/module/display/moduletileset.c index 9e7e732..5cd8542 100644 --- a/src/script/module/display/moduletileset.c +++ b/src/script/module/display/moduletileset.c @@ -46,6 +46,21 @@ int moduleTilesetIndex(lua_State *l) { } else if(stringCompare(key, "texture") == 0) { lua_pushstring(l, ts->image); return 1; + } else if(stringCompare(key, "tileWidth") == 0) { + lua_pushnumber(l, ts->tileWidth); + return 1; + } else if(stringCompare(key, "tileHeight") == 0) { + lua_pushnumber(l, ts->tileHeight); + return 1; + } else if(stringCompare(key, "tileCount") == 0) { + lua_pushnumber(l, ts->tileCount); + return 1; + } else if(stringCompare(key, "columns") == 0) { + lua_pushnumber(l, ts->columns); + return 1; + } else if(stringCompare(key, "rows") == 0) { + lua_pushnumber(l, ts->rows); + return 1; } lua_pushnil(l); diff --git a/src/script/module/time/moduletime.c b/src/script/module/time/moduletime.c index a813a73..9d666fa 100644 --- a/src/script/module/time/moduletime.c +++ b/src/script/module/time/moduletime.c @@ -13,21 +13,20 @@ void moduleTime(scriptcontext_t *ctx) { // Script structure if(luaL_newmetatable(ctx->luaState, "time_mt")) { - // Metatable is new, set __index - lua_pushstring(ctx->luaState, "__index"); lua_pushcfunction(ctx->luaState, moduleTimeIndex); - lua_settable(ctx->luaState, -3); + lua_setfield(ctx->luaState, -2, "__index"); } + lua_pop(ctx->luaState, 1); - // Global time struct - lua_pushlightuserdata(ctx->luaState, &TIME); + dusktime_t **ud = (dusktime_t**)lua_newuserdatauv( + ctx->luaState, sizeof(dusktime_t*), 0 + ); + *ud = &TIME; luaL_setmetatable(ctx->luaState, "time_mt"); lua_setglobal(ctx->luaState, "TIME"); } int moduleTimeIndex(lua_State *L) { - assertNotNull(L, "Lua state cannot be NULL."); - const char_t *key = lua_tostring(L, 2); assertStrLenMin(key, 1, "Key cannot be empty."); diff --git a/tools/asset/process/image.py b/tools/asset/process/image.py index 570527a..0daa917 100644 --- a/tools/asset/process/image.py +++ b/tools/asset/process/image.py @@ -45,6 +45,7 @@ def processPalettizedImage(asset): elif color[3] == 0 and palColor[3] == 0: break else: + print('Pallete {} does not contain color #{}'.format(p['paletteName'], '{:02x}{:02x}{:02x}{:02x}'.format(color[0], color[1], color[2], color[3]))) hasAllColors = False break if hasAllColors: