Skip to content

Commit

Permalink
feat: pong like hello world
Browse files Browse the repository at this point in the history
  • Loading branch information
RodrigoDornelles committed Jun 25, 2024
1 parent cac1e65 commit d1ef73f
Show file tree
Hide file tree
Showing 8 changed files with 227 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist/
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
# codename-videogame-engine
A cross-platform embeddable LUA game engine such as ginga, love2d, roblox and its own console like a Wii clone.

```bash
$ lua src/cli.lua && ginga dist/main.ncl
```
22 changes: 22 additions & 0 deletions SUPPORT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Platform Support List
=====================

Tier 1
------

- [ ] [Ginga](https://github.com/TeleMidia/ginga)
- [ ] [Love2D](https://github.com/love2d/love)
- [ ] UnitTest

Tier 2
------

- [ ] Curses
- [ ] Console

Tier 3
------

- [ ] Browser
- [ ] Roblox

65 changes: 65 additions & 0 deletions examples/pong/game.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
local function init(std, game)
game.highscore = game.highscore or 0
game.player_size = std.game.height/8
game.player_pos = std.game.height/2 - game.player_size/2
game.ball_pos_x = std.game.witdh/2
game.ball_pos_y = std.game.height/2
game.ball_spd_x = 5
game.ball_spd_y = 1
game.ball_size = 8
game.score = 0
end

local function loop(std, game)
-- inputs
game.player_dir = std.key.press.down - std.key.press.up
-- moves
game.player_pos = std.math.clamp(game.player_pos + (game.player_dir * 7), 0, std.game.height - game.player_size)
game.ball_pos_x = game.ball_pos_x + game.ball_spd_x
game.ball_pos_y = game.ball_pos_y + game.ball_spd_y
-- colisions
if game.ball_pos_x >= (std.game.witdh - game.ball_size) then
game.ball_spd_x = game.ball_spd_x * -1
end
if game.ball_pos_y <= 0 or game.ball_pos_y >= (std.game.height - game.ball_size) then
game.ball_spd_y = game.ball_spd_y * -1
end
if game.ball_pos_x <= 0 then
if std.math.clamp(game.ball_pos_y, game.player_pos, game.player_pos + game.player_size) == game.ball_pos_y then
game.ball_spd_y = std.math.clamp(game.ball_spd_y + (game.player_pos % 10) - 5, -10, 10)
game.ball_spd_x = game.ball_spd_x * -1.05
game.score = game.score + 1
else
std.game.reset()
end
end
end

local function draw(std, game)
std.draw.color('black')
std.draw.rect('fill', 0, 0, std.game.witdh, std.game.height)
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)
std.draw.font('Tiresias', 32)
std.draw.text(std.game.witdh/4, 16, game.score)
std.draw.text((std.game.witdh/4 * 3), 16, game.highscore)
end

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

local P = {
title='Pong',
description='simple pong',
version='1.0.0',
callbacks={
init=init,
loop=loop,
draw=draw,
exit=exit
}
}

return P;
7 changes: 7 additions & 0 deletions src/cli.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
local os = require('os')

print(os.execute('mkdir -p ./dist/'))
print(os.execute('cp examples/pong/game.lua dist/game.lua'))
print(os.execute('cp src/common/*.lua dist'))
print(os.execute('cp src/engine/ginga/core.lua dist/main.lua'))
print(os.execute('cp src/engine/ginga/main.ncl dist/main.ncl'))
23 changes: 23 additions & 0 deletions src/common/game_math.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
local function clamp(value, min, max)
if value < min then
return min
elseif value > max then
return max
else
return value
end
end

local function abs(value)
if value < 0 then
return -value
end
return value
end

local P = {
clamp=clamp,
abs=abs
}

return P;
90 changes: 90 additions & 0 deletions src/engine/ginga/core.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
local game = require('game')
local math = require('game_math')
local canvas = canvas
local event = event
local game_obj = {}
local std = {draw={},key={press={}},game={}}
local fixture190 = ''
_ENV = nil

local function std_draw_color(color)
canvas:attrColor(color)
end

local function std_draw_rect(a,b,c,d,e)
canvas:drawRect(a,b,c,d,e)
end

local function std_draw_text(a,b,c)
canvas:drawText(a,b,c)
end

local function std_draw_font(a,b,c)
canvas:attrFont(a,b,c)
end

local function std_game_reset()
if game.callbacks.exit then
game.callbacks.exit(std, game_obj)
end
if game.callbacks.init then
game.callbacks.init(std, game_obj)
end
end

local function event_loop(evt)
if evt.class ~= 'key' then return end

-- https://github.com/TeleMidia/ginga/issues/190
if #fixture190 == 0 and evt.key ~= 'ENTER' then
fixture190 = evt.type
end

if fixture190 == evt.type then
if evt.key == 'CURSOR_UP' then
std.key.press.up = 1
end
if evt.key == 'CURSOR_DOWN' then
std.key.press.down = 1
end
else
if evt.key == 'CURSOR_UP' then
std.key.press.up = 0
end
if evt.key == 'CURSOR_DOWN' then
std.key.press.down = 0
end
end
end

local function fixed_loop()
game.callbacks.loop(std, game_obj)
canvas:attrColor(0, 0, 0, 0)
canvas:clear()
game.callbacks.draw(std, game_obj)
canvas:flush()
event.timer(1, fixed_loop)
end

local function setup(evt)
if evt.class ~= 'ncl' or evt.action ~= 'start' then return end
local w, h = canvas:attrSize()
std.math=math
std.draw.color=std_draw_color
std.draw.rect=std_draw_rect
std.draw.text=std_draw_text
std.draw.font=std_draw_font
std.key.press.up=0
std.key.press.down=0
std.key.press.left=0
std.key.press.right=0
std.game.reset=std_game_reset
std.game.witdh=w
std.game.height=h
game.callbacks.init(std, game_obj)
event.register(event_loop)
event.timer(1, fixed_loop)
event.unregister(setup)
end

event.register(setup)
15 changes: 15 additions & 0 deletions src/engine/ginga/main.ncl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version='1.0' encoding='utf-8'?>
<ncl>
<head>
<regionBase>
<region id='luaRegion' width='100%' height='100%'/>
</regionBase>
<descriptorBase>
<descriptor id='luaDesc' region='luaRegion' focusIndex='0'/>
</descriptorBase>
</head>
<body>
<port id='init' component='lua'/>
<media id='lua' descriptor='luaDesc' src='main.lua'/>
</body>
</ncl>

0 comments on commit d1ef73f

Please sign in to comment.