Skip to content

Commit

Permalink
feat: reactoring cli & add html5 core (#13)
Browse files Browse the repository at this point in the history
* fix: player death invisible asteroids #11
* feat: refactoring CLI
* docs: add commercial intent notice on README.md
* feat: add bundler
* feat: add core html5
* feat: add core ginga_html5
* docs: add pong website
* ci: ignore draft PR
* feat: decorate reset game
* docs: add build to html5 core to README.md
  • Loading branch information
RodrigoDornelles authored Jul 9, 2024
1 parent 9c22c3a commit 1adfd78
Show file tree
Hide file tree
Showing 20 changed files with 622 additions and 125 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: CI
name: ci

on:
workflow_dispatch:
Expand All @@ -13,7 +13,7 @@ on:
jobs:
test:
runs-on: ubuntu-latest

if: github.event_name == 'push' || github.event.pull_request.draft == false
steps:
-
uses: actions/checkout@master
Expand Down
21 changes: 20 additions & 1 deletion .github/workflows/DOCS.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
name: DOCS
name: docs

env:
CLOUDFLARE_PROJECT_DOXYGEN: doxygen-engine
CLOUDFLARE_PROJECT_PONG: pong-engine

on:
workflow_dispatch:
Expand All @@ -16,6 +17,7 @@ on:
jobs:
doxygen:
runs-on: ubuntu-latest
if: github.event_name == 'push' || github.event.pull_request.draft == false
steps:
-
uses: actions/checkout@master
Expand All @@ -29,3 +31,20 @@ jobs:
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
projectName: ${{ env.CLOUDFLARE_PROJECT_DOXYGEN }}
directory: html

pong:
runs-on: ubuntu-latest
if: github.event_name == 'push' || github.event.pull_request.draft == false
steps:
-
uses: actions/checkout@master
-
run: |
docker run --rm -v $(pwd):/app -w /app rodrigodornelles/doxygen:lua ./cli.sh build --core html5 ./examples/pong/game.lua --dist ./html/
-
uses: cloudflare/pages-action@v1
with:
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
projectName: ${{ env.CLOUDFLARE_PROJECT_PONG }}
directory: html
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,28 @@
* **Progressive "Television" Apps** _(PWA but is for Brazilian TVs)_
* **Many testing tools** _(REPL, Unit Tests, [TAS](https://tasvideos.org/WelcomeToTASVideos#WhatIsATas) Tests)_
* **Pure functions**
* **No Tricks**

### Love 2D :heart_decoration: :desktop_computer:

```bash
$ ./cli.sh build --game ./examples/asteroids --core love --run
$ ./cli.sh build ./examples/asteroids/game.lua --core love --run
```

### Ginga :brazil: :tv:

* :octocat: [telemidia/ginga](https://github.com/TeleMidia/ginga)

```bash
$ ./cli.sh build --game ./examples/asteroids --core ginga --run
$ ./cli.sh build ./examples/asteroids/game.lua --core ginga --run
```

### Browser :globe_with_meridians:

Need a web server to work, use live server in your vscode for development and github/cloudflare pages for production.

```bash
$ ./cli.sh build ./examples/asteroids/game.lua --core html5
```

---
This game engine is **open source** and **free** for all uses, focusing on promoting content for our **commercial platform**.
4 changes: 2 additions & 2 deletions cli.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/bin/sh

if lua -v >/dev/null 2>&1; then
lua ./src/cli/init.lua "$@"
lua ./src/cli/main.lua "$@"
elif luajit -v >/dev/null 2>&1; then
./src/cli/init.lua "$@"
./src/cli/main.lua "$@"
else
echo "Lua not found!"
exit 1
Expand Down
2 changes: 1 addition & 1 deletion examples/asteroids/game.lua
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ end
local function asteroid_nest(std, game, x, y, id)
local index = 1
while index < #game.asteroid_size do
if index ~= id then
if index ~= id and game.asteroid_size[index] ~= -1 then
local distance = std.math.dis(x, y, game.asteroid_pos_x[index], game.asteroid_pos_y[index])
if (distance - 3) <= (game.asteroid_size[index] / 2) then
return true
Expand Down
5 changes: 2 additions & 3 deletions examples/pong/game.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ local function loop(std, game)
end

local function draw(std, game)
std.draw.color('black')
std.draw.rect('fill', 0, 0, game.width, game.height)
std.draw.clear('black')
std.draw.color('white')
std.draw.rect('fill', 4, game.player_pos, 8, game.player_size)
std.draw.rect('fill', game.ball_pos_x, game.ball_pos_y, game.ball_size, game.ball_size)
Expand All @@ -52,7 +51,7 @@ local function draw(std, game)
end

local function exit(std, game)
game.highscore = std.math.clamp(game.highscore, game.score, game.highscore)
game.highscore = std.math.max(game.highscore, game.score)
end

local P = {
Expand Down
205 changes: 205 additions & 0 deletions src/cli/fs.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
local function clear(src_path)
if os.execute('rm --version > /dev/null 2> /dev//null') then
os.execute('mkdir -p '..src_path)
os.execute('rm -Rf '..src_path..'/*')
else
src_path = src_path:gsub('/', '\\')
os.execute('mkdir '..src_path)
os.execute('rmdir /s /q '..src_path..'\\*')
end
end

local function move(src_in, dist_out)
local src_file = io.open(src_in, "rb")
local dist_file = io.open(dist_out, "wb")

if src_file and dist_file then
repeat
local buffer = src_file:read(1024)
if buffer then
dist_file:write(buffer)
end
until not buffer
end

if src_file then
src_file:close()
end
if dist_file then
dist_file:close()
end
end

local function moveLua(src_in, dist_path, dist_file)
local deps = {}
local pattern = "local ([%w_%-]+) = require%('src/(.-)'%)"
local src_file = io.open(src_in, "r")
local dist_file_normalized = src_in:gsub('/', '_'):gsub('^src_', '')
local dist_out = dist_path:gsub('/$', '')..'/'..(dist_file or dist_file_normalized)
local dist_file = io.open(dist_out, "w")

if src_file and dist_file then
repeat
local line = src_file:read()
if line then
local line_require = { line:match(pattern) }
if line_require and #line_require > 0 then
local var_name = line_require[1]
local module_path = line_require[2]
deps[#deps + 1] = 'src/'..module_path..'.lua'
dist_file:write('local '..var_name..' = require(\''..module_path:gsub('/', '_')..'\')\n')
else
dist_file:write(line, '\n')
end
end
until not line
end

if src_file then
src_file:close()
end
if dist_file then
dist_file:close()
end

return deps
end

local function build(src_in, dist_path)
local main = true
local deps = {}
local deps_builded = {}

repeat
if src_in:sub(-4) == '.lua' then
local index = 1
local index_deps = #deps
local file_name = main and 'main.lua'
local new_deps = moveLua(src_in, dist_path, file_name)
while index <= #new_deps do
deps[index_deps + index] = new_deps[index]
index = index + 1
end
end

main = false
src_in = nil
local index = 1
while index <= #deps and not src_in do
local dep = deps[index]
if not deps_builded[dep] then
deps_builded[dep] = true
src_in = dep
end
index = index + 1
end
until not src_in
end

--! @short unify files
--! @brief groups code into a single source
--! @param[in] src_path folder with lua includes
--! @param[in] src_file entry file
--! @param[in] dest_file packaged file output
--! @par Input
--! @li @c lib_common_math.lua
--! @code
--! local function sum(a, b)
--! return a + b
--! end
--!
--! local P = {
--! sum = sum
--! }
--!
--! return P
--! @endcode
--!
--! @li @c main.lua
--! @code
--! local os = require('os')
--! local zeebo_math = require('lib_common_math')
--!
--! print(zeebo_math.sum(1, 2))
--! os.exit(0)
--! @endcode
--!
--! @par Output
--! @li @c main.lua
--! @code
--! local os = require('os')
--! local lib_common_math = nil
--!
--! local function main()
--! local zeebo_math = lib_common_math()
--! print(zeebo_math.sum(1, 2))
--! os.exit(0)
--! end
--!
--! lib_common_math = function()
--! local function sum(a, b)
--! return a + b
--! end
--!
--! local P = {
--! sum = sum
--! }
--!
--! return P
--! end
--!
--! main()
--! @endcode
local function bundler(src_path, src_file, dest_file)
local pattern = "local ([%w_%-]+) = require%('(.-)'%)"
local dest_file = io.open(dest_file, 'w')
local main_file = io.open(src_path..src_file, 'r')
local main_content = ''
local main_before = ''
local main_after = ''

repeat
local line = main_file:read()
if line then
local line_require = { line:match(pattern) }

if line_require and #line_require > 0 then
local var_name = line_require[1]
local module_path = line_require[2]
local module_file = io.open(src_path..module_path..'.lua', 'r')
if not module_file then
main_before = main_before..line..'\n'
else
local lib_name = module_path:gsub('/', '_')
local lib_content = module_file:read('*all')
main_before = main_before..'local '..lib_name..' = nil\n'
main_content = main_content..'local '..var_name..' = '..module_path..'()\n'
main_after = main_after..lib_name..' = function()\n'..lib_content..'\nend\n'
module_file:close()
end
else
main_content = main_content..line..'\n'
end
end
until not line

do
main_content = 'local function main()\n'..main_content..'\nend'
main_content = main_before..'\n'..main_content..'\n'..main_after
main_content = main_content..'\nreturn main()\n'
end

dest_file:write(main_content)
dest_file:close()
main_file:close()
end

local P = {
move = move,
moveLua = moveLua,
bundler = bundler,
build = build,
clear = clear
}

return P
64 changes: 0 additions & 64 deletions src/cli/init.lua

This file was deleted.

Loading

0 comments on commit 1adfd78

Please sign in to comment.