Compare commits
No commits in common. "bceb46a612fee9fc8c23c4209e7fbd176e5e0084" and "6c1338e3c2e67924dc72a8f4ccdbac1bf52d4d92" have entirely different histories.
bceb46a612
...
6c1338e3c2
1
asteroids/.gitignore
vendored
|
|
@ -1 +0,0 @@
|
||||||
tmp
|
|
||||||
|
|
@ -1,158 +0,0 @@
|
||||||
local love = require("love")
|
|
||||||
local phys = require("phys")
|
|
||||||
local inspect = require("inspect")
|
|
||||||
|
|
||||||
local actors = {}
|
|
||||||
|
|
||||||
function actors.buildActors(game)
|
|
||||||
local asteroids = {}
|
|
||||||
local asteroids_mt = { __index = asteroids }
|
|
||||||
|
|
||||||
function asteroids.new(asteroid)
|
|
||||||
local stageIndex = asteroid.stage or #game.asteroids.stages
|
|
||||||
local stage = game.asteroids.stages[stageIndex]
|
|
||||||
local instance = {
|
|
||||||
angle = math.random() * phys.tau,
|
|
||||||
speed = stage.speed,
|
|
||||||
facing = math.random() * phys.tau,
|
|
||||||
spin = (math.random() - 0.5),
|
|
||||||
radius = stage.radius,
|
|
||||||
sprite = stage.sprite,
|
|
||||||
}
|
|
||||||
for k, v in pairs(asteroid) do
|
|
||||||
instance[k] = v
|
|
||||||
end
|
|
||||||
instance.stage = stageIndex
|
|
||||||
setmetatable(instance, asteroids_mt)
|
|
||||||
return instance
|
|
||||||
end
|
|
||||||
|
|
||||||
function asteroids:draw()
|
|
||||||
local width = self.sprite:getWidth()
|
|
||||||
local height = self.sprite:getHeight()
|
|
||||||
love.graphics.push()
|
|
||||||
love.graphics.setColor(1, 1, 1)
|
|
||||||
local scale = 2 * self.radius / math.max(width, height)
|
|
||||||
love.graphics.draw(self.sprite,
|
|
||||||
self.x,
|
|
||||||
self.y,
|
|
||||||
self.facing,
|
|
||||||
scale,
|
|
||||||
scale,
|
|
||||||
width / 2,
|
|
||||||
height / 2
|
|
||||||
)
|
|
||||||
love.graphics.pop()
|
|
||||||
end
|
|
||||||
|
|
||||||
local bullets = {}
|
|
||||||
local bullets_mt = { __index = bullets }
|
|
||||||
|
|
||||||
function bullets.new(bullet)
|
|
||||||
local instance = {
|
|
||||||
spin = 0,
|
|
||||||
}
|
|
||||||
for k, v in pairs(game.ship.bullet) do
|
|
||||||
instance[k] = v
|
|
||||||
end
|
|
||||||
for k, v in pairs(bullet) do
|
|
||||||
instance[k] = v
|
|
||||||
end
|
|
||||||
instance.sound = love.audio.newSource(game.ship.bullet.sound)
|
|
||||||
instance.timeLeft = instance.time
|
|
||||||
setmetatable(instance, bullets_mt)
|
|
||||||
return instance
|
|
||||||
end
|
|
||||||
|
|
||||||
function bullets:draw()
|
|
||||||
love.graphics.push()
|
|
||||||
love.graphics.setColor(1, 0.16, 1)
|
|
||||||
love.graphics.circle('fill', self.x, self.y, self.radius)
|
|
||||||
love.graphics.pop()
|
|
||||||
end
|
|
||||||
|
|
||||||
local ships = {}
|
|
||||||
local ships_mt = { __index = ships }
|
|
||||||
|
|
||||||
function ships.new(ship)
|
|
||||||
local instance = {
|
|
||||||
x = game.width / 2,
|
|
||||||
y = game.height / 2,
|
|
||||||
angle = 0,
|
|
||||||
speed = 0,
|
|
||||||
spin = 0,
|
|
||||||
facing = phys.tau * 3 / 4,
|
|
||||||
radius = game.ship.radius,
|
|
||||||
throttle = 0,
|
|
||||||
}
|
|
||||||
for k, v in pairs(game.ship) do
|
|
||||||
instance[k] = v
|
|
||||||
end
|
|
||||||
for k, v in pairs(ship) do
|
|
||||||
instance[k] = v
|
|
||||||
end
|
|
||||||
instance.engineSound = love.audio.newSource(instance.engineSound)
|
|
||||||
instance.engineSound:setVolume(0)
|
|
||||||
instance.engineSound:setLooping(true)
|
|
||||||
instance.rocketPlume = love.graphics.newParticleSystem(instance.rocketPlume)
|
|
||||||
instance.rocketPlume:setColors(
|
|
||||||
0.37, 0.65, 1.00, 0.8,
|
|
||||||
0.45, 0.65, 0.9, 0.8,
|
|
||||||
0.5, 0.6, 0.85, 0.8
|
|
||||||
)
|
|
||||||
instance.rocketPlume:setSizes(1.0, 0.9, 0.1)
|
|
||||||
instance.rocketPlume:setEmissionRate(1000)
|
|
||||||
-- TODO probably governed most by thrust
|
|
||||||
instance.rocketPlume:setParticleLifetime(0.1, 0.25)
|
|
||||||
instance.rocketPlume:setEmissionArea('normal', 1.5, 1.5)
|
|
||||||
instance.rocketPlume:setSpeed(150, 300)
|
|
||||||
instance.rocketPlume:setRadialAcceleration(40, 80)
|
|
||||||
instance.rocketPlume:setLinearDamping(0, 0)
|
|
||||||
instance.rocketPlume:setDirection(phys.tau * 3 / 4)
|
|
||||||
instance.rocketPlume:setSpread(0)
|
|
||||||
instance.rocketPlume:setSizeVariation(0)
|
|
||||||
instance.rocketPlume:setSpin(0.5, 1)
|
|
||||||
instance.rocketPlume:setTangentialAcceleration(0, 0)
|
|
||||||
|
|
||||||
setmetatable(instance, ships_mt)
|
|
||||||
return instance
|
|
||||||
end
|
|
||||||
|
|
||||||
function ships:draw()
|
|
||||||
love.graphics.push()
|
|
||||||
love.graphics.translate(self.x, self.y)
|
|
||||||
love.graphics.setColor(1, 1, 1)
|
|
||||||
local width = self.sprite:getWidth()
|
|
||||||
local height = self.sprite:getHeight()
|
|
||||||
|
|
||||||
local scale = 2 * game.ship.radius / math.max(width, height)
|
|
||||||
love.graphics.draw(game.ship.sprite,
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
(self.facing + phys.tau / 4) % phys.tau,
|
|
||||||
scale,
|
|
||||||
scale,
|
|
||||||
width / 2,
|
|
||||||
height / 2
|
|
||||||
)
|
|
||||||
self.rocketPlume:setEmissionRate(1000 * self.throttle)
|
|
||||||
love.graphics.draw(self.rocketPlume,
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
(self.facing + phys.tau * 3 / 4) % phys.tau,
|
|
||||||
scale,
|
|
||||||
scale,
|
|
||||||
0,
|
|
||||||
height * 0.6
|
|
||||||
)
|
|
||||||
love.graphics.pop()
|
|
||||||
end
|
|
||||||
|
|
||||||
return {
|
|
||||||
asteroids = asteroids,
|
|
||||||
bullets = bullets,
|
|
||||||
ships = ships,
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
return actors
|
|
||||||
|
|
@ -1,371 +0,0 @@
|
||||||
local _tl_compat; if (tonumber((_VERSION or ''):match('[%d.]*$')) or 0) < 5.3 then local p, m = pcall(require, 'compat53.module'); if p then _tl_compat = m end end; local math = _tl_compat and _tl_compat.math or math; local string = _tl_compat and _tl_compat.string or string; local table = _tl_compat and _tl_compat.table or table; local type = type
|
|
||||||
local inspect = { Options = {} }
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
inspect._VERSION = 'inspect.lua 3.1.0'
|
|
||||||
inspect._URL = 'http://github.com/kikito/inspect.lua'
|
|
||||||
inspect._DESCRIPTION = 'human-readable representations of tables'
|
|
||||||
inspect._LICENSE = [[
|
|
||||||
MIT LICENSE
|
|
||||||
|
|
||||||
Copyright (c) 2022 Enrique García Cota
|
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a
|
|
||||||
copy of this software and associated documentation files (the
|
|
||||||
"Software"), to deal in the Software without restriction, including
|
|
||||||
without limitation the rights to use, copy, modify, merge, publish,
|
|
||||||
distribute, sublicense, and/or sell copies of the Software, and to
|
|
||||||
permit persons to whom the Software is furnished to do so, subject to
|
|
||||||
the following conditions:
|
|
||||||
|
|
||||||
The above copyright notice and this permission notice shall be included
|
|
||||||
in all copies or substantial portions of the Software.
|
|
||||||
|
|
||||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
||||||
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
||||||
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
||||||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
||||||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
||||||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
||||||
]]
|
|
||||||
inspect.KEY = setmetatable({}, { __tostring = function() return 'inspect.KEY' end })
|
|
||||||
inspect.METATABLE = setmetatable({}, { __tostring = function() return 'inspect.METATABLE' end })
|
|
||||||
|
|
||||||
local tostring = tostring
|
|
||||||
local rep = string.rep
|
|
||||||
local match = string.match
|
|
||||||
local char = string.char
|
|
||||||
local gsub = string.gsub
|
|
||||||
local fmt = string.format
|
|
||||||
|
|
||||||
local _rawget
|
|
||||||
if rawget then
|
|
||||||
_rawget = rawget
|
|
||||||
else
|
|
||||||
_rawget = function(t, k) return t[k] end
|
|
||||||
end
|
|
||||||
|
|
||||||
local function rawpairs(t)
|
|
||||||
return next, t, nil
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
local function smartQuote(str)
|
|
||||||
if match(str, '"') and not match(str, "'") then
|
|
||||||
return "'" .. str .. "'"
|
|
||||||
end
|
|
||||||
return '"' .. gsub(str, '"', '\\"') .. '"'
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
local shortControlCharEscapes = {
|
|
||||||
["\a"] = "\\a", ["\b"] = "\\b", ["\f"] = "\\f", ["\n"] = "\\n",
|
|
||||||
["\r"] = "\\r", ["\t"] = "\\t", ["\v"] = "\\v", ["\127"] = "\\127",
|
|
||||||
}
|
|
||||||
local longControlCharEscapes = { ["\127"] = "\127" }
|
|
||||||
for i = 0, 31 do
|
|
||||||
local ch = char(i)
|
|
||||||
if not shortControlCharEscapes[ch] then
|
|
||||||
shortControlCharEscapes[ch] = "\\" .. i
|
|
||||||
longControlCharEscapes[ch] = fmt("\\%03d", i)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local function escape(str)
|
|
||||||
return (gsub(gsub(gsub(str, "\\", "\\\\"),
|
|
||||||
"(%c)%f[0-9]", longControlCharEscapes),
|
|
||||||
"%c", shortControlCharEscapes))
|
|
||||||
end
|
|
||||||
|
|
||||||
local luaKeywords = {
|
|
||||||
['and'] = true,
|
|
||||||
['break'] = true,
|
|
||||||
['do'] = true,
|
|
||||||
['else'] = true,
|
|
||||||
['elseif'] = true,
|
|
||||||
['end'] = true,
|
|
||||||
['false'] = true,
|
|
||||||
['for'] = true,
|
|
||||||
['function'] = true,
|
|
||||||
['goto'] = true,
|
|
||||||
['if'] = true,
|
|
||||||
['in'] = true,
|
|
||||||
['local'] = true,
|
|
||||||
['nil'] = true,
|
|
||||||
['not'] = true,
|
|
||||||
['or'] = true,
|
|
||||||
['repeat'] = true,
|
|
||||||
['return'] = true,
|
|
||||||
['then'] = true,
|
|
||||||
['true'] = true,
|
|
||||||
['until'] = true,
|
|
||||||
['while'] = true,
|
|
||||||
}
|
|
||||||
|
|
||||||
local function isIdentifier(str)
|
|
||||||
return type(str) == "string" and
|
|
||||||
not not str:match("^[_%a][_%a%d]*$") and
|
|
||||||
not luaKeywords[str]
|
|
||||||
end
|
|
||||||
|
|
||||||
local flr = math.floor
|
|
||||||
local function isSequenceKey(k, sequenceLength)
|
|
||||||
return type(k) == "number" and
|
|
||||||
flr(k) == k and
|
|
||||||
1 <= (k) and
|
|
||||||
k <= sequenceLength
|
|
||||||
end
|
|
||||||
|
|
||||||
local defaultTypeOrders = {
|
|
||||||
['number'] = 1, ['boolean'] = 2, ['string'] = 3, ['table'] = 4,
|
|
||||||
['function'] = 5, ['userdata'] = 6, ['thread'] = 7,
|
|
||||||
}
|
|
||||||
|
|
||||||
local function sortKeys(a, b)
|
|
||||||
local ta, tb = type(a), type(b)
|
|
||||||
|
|
||||||
|
|
||||||
if ta == tb and (ta == 'string' or ta == 'number') then
|
|
||||||
return (a) < (b)
|
|
||||||
end
|
|
||||||
|
|
||||||
local dta = defaultTypeOrders[ta] or 100
|
|
||||||
local dtb = defaultTypeOrders[tb] or 100
|
|
||||||
|
|
||||||
|
|
||||||
return dta == dtb and ta < tb or dta < dtb
|
|
||||||
end
|
|
||||||
|
|
||||||
local function getKeys(t)
|
|
||||||
|
|
||||||
local seqLen = 1
|
|
||||||
while _rawget(t, seqLen) ~= nil do
|
|
||||||
seqLen = seqLen + 1
|
|
||||||
end
|
|
||||||
seqLen = seqLen - 1
|
|
||||||
|
|
||||||
local keys, keysLen = {}, 0
|
|
||||||
for k in rawpairs(t) do
|
|
||||||
if not isSequenceKey(k, seqLen) then
|
|
||||||
keysLen = keysLen + 1
|
|
||||||
keys[keysLen] = k
|
|
||||||
end
|
|
||||||
end
|
|
||||||
table.sort(keys, sortKeys)
|
|
||||||
return keys, keysLen, seqLen
|
|
||||||
end
|
|
||||||
|
|
||||||
local function countCycles(x, cycles)
|
|
||||||
if type(x) == "table" then
|
|
||||||
if cycles[x] then
|
|
||||||
cycles[x] = cycles[x] + 1
|
|
||||||
else
|
|
||||||
cycles[x] = 1
|
|
||||||
for k, v in rawpairs(x) do
|
|
||||||
countCycles(k, cycles)
|
|
||||||
countCycles(v, cycles)
|
|
||||||
end
|
|
||||||
countCycles(getmetatable(x), cycles)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local function makePath(path, a, b)
|
|
||||||
local newPath = {}
|
|
||||||
local len = #path
|
|
||||||
for i = 1, len do newPath[i] = path[i] end
|
|
||||||
|
|
||||||
newPath[len + 1] = a
|
|
||||||
newPath[len + 2] = b
|
|
||||||
|
|
||||||
return newPath
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
local function processRecursive(process,
|
|
||||||
item,
|
|
||||||
path,
|
|
||||||
visited)
|
|
||||||
if item == nil then return nil end
|
|
||||||
if visited[item] then return visited[item] end
|
|
||||||
|
|
||||||
local processed = process(item, path)
|
|
||||||
if type(processed) == "table" then
|
|
||||||
local processedCopy = {}
|
|
||||||
visited[item] = processedCopy
|
|
||||||
local processedKey
|
|
||||||
|
|
||||||
for k, v in rawpairs(processed) do
|
|
||||||
processedKey = processRecursive(process, k, makePath(path, k, inspect.KEY), visited)
|
|
||||||
if processedKey ~= nil then
|
|
||||||
processedCopy[processedKey] = processRecursive(process, v, makePath(path, processedKey), visited)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local mt = processRecursive(process, getmetatable(processed), makePath(path, inspect.METATABLE), visited)
|
|
||||||
if type(mt) ~= 'table' then mt = nil end
|
|
||||||
setmetatable(processedCopy, mt)
|
|
||||||
processed = processedCopy
|
|
||||||
end
|
|
||||||
return processed
|
|
||||||
end
|
|
||||||
|
|
||||||
local function puts(buf, str)
|
|
||||||
buf.n = buf.n + 1
|
|
||||||
buf[buf.n] = str
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
local Inspector = {}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
local Inspector_mt = { __index = Inspector }
|
|
||||||
|
|
||||||
local function tabify(inspector)
|
|
||||||
puts(inspector.buf, inspector.newline .. rep(inspector.indent, inspector.level))
|
|
||||||
end
|
|
||||||
|
|
||||||
function Inspector:getId(v)
|
|
||||||
local id = self.ids[v]
|
|
||||||
local ids = self.ids
|
|
||||||
if not id then
|
|
||||||
local tv = type(v)
|
|
||||||
id = (ids[tv] or 0) + 1
|
|
||||||
ids[v], ids[tv] = id, id
|
|
||||||
end
|
|
||||||
return tostring(id)
|
|
||||||
end
|
|
||||||
|
|
||||||
function Inspector:putValue(v)
|
|
||||||
local buf = self.buf
|
|
||||||
local tv = type(v)
|
|
||||||
if tv == 'string' then
|
|
||||||
puts(buf, smartQuote(escape(v)))
|
|
||||||
elseif tv == 'number' or tv == 'boolean' or tv == 'nil' or
|
|
||||||
tv == 'cdata' or tv == 'ctype' then
|
|
||||||
puts(buf, tostring(v))
|
|
||||||
elseif tv == 'table' and not self.ids[v] then
|
|
||||||
local t = v
|
|
||||||
|
|
||||||
if t == inspect.KEY or t == inspect.METATABLE then
|
|
||||||
puts(buf, tostring(t))
|
|
||||||
elseif self.level >= self.depth then
|
|
||||||
puts(buf, '{...}')
|
|
||||||
else
|
|
||||||
if self.cycles[t] > 1 then puts(buf, fmt('<%d>', self:getId(t))) end
|
|
||||||
|
|
||||||
local keys, keysLen, seqLen = getKeys(t)
|
|
||||||
|
|
||||||
puts(buf, '{')
|
|
||||||
self.level = self.level + 1
|
|
||||||
|
|
||||||
for i = 1, seqLen + keysLen do
|
|
||||||
if i > 1 then puts(buf, ',') end
|
|
||||||
if i <= seqLen then
|
|
||||||
puts(buf, ' ')
|
|
||||||
self:putValue(t[i])
|
|
||||||
else
|
|
||||||
local k = keys[i - seqLen]
|
|
||||||
tabify(self)
|
|
||||||
if isIdentifier(k) then
|
|
||||||
puts(buf, k)
|
|
||||||
else
|
|
||||||
puts(buf, "[")
|
|
||||||
self:putValue(k)
|
|
||||||
puts(buf, "]")
|
|
||||||
end
|
|
||||||
puts(buf, ' = ')
|
|
||||||
self:putValue(t[k])
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local mt = getmetatable(t)
|
|
||||||
if type(mt) == 'table' then
|
|
||||||
if seqLen + keysLen > 0 then puts(buf, ',') end
|
|
||||||
tabify(self)
|
|
||||||
puts(buf, '<metatable> = ')
|
|
||||||
self:putValue(mt)
|
|
||||||
end
|
|
||||||
|
|
||||||
self.level = self.level - 1
|
|
||||||
|
|
||||||
if keysLen > 0 or type(mt) == 'table' then
|
|
||||||
tabify(self)
|
|
||||||
elseif seqLen > 0 then
|
|
||||||
puts(buf, ' ')
|
|
||||||
end
|
|
||||||
|
|
||||||
puts(buf, '}')
|
|
||||||
end
|
|
||||||
|
|
||||||
else
|
|
||||||
puts(buf, fmt('<%s %d>', tv, self:getId(v)))
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function inspect.inspect(root, options)
|
|
||||||
options = options or {}
|
|
||||||
|
|
||||||
local depth = options.depth or (math.huge)
|
|
||||||
local newline = options.newline or '\n'
|
|
||||||
local indent = options.indent or ' '
|
|
||||||
local process = options.process
|
|
||||||
|
|
||||||
if process then
|
|
||||||
root = processRecursive(process, root, {}, {})
|
|
||||||
end
|
|
||||||
|
|
||||||
local cycles = {}
|
|
||||||
countCycles(root, cycles)
|
|
||||||
|
|
||||||
local inspector = setmetatable({
|
|
||||||
buf = { n = 0 },
|
|
||||||
ids = {},
|
|
||||||
cycles = cycles,
|
|
||||||
depth = depth,
|
|
||||||
level = 0,
|
|
||||||
newline = newline,
|
|
||||||
indent = indent,
|
|
||||||
}, Inspector_mt)
|
|
||||||
|
|
||||||
inspector:putValue(root)
|
|
||||||
|
|
||||||
return table.concat(inspector.buf)
|
|
||||||
end
|
|
||||||
|
|
||||||
setmetatable(inspect, {
|
|
||||||
__call = function(_, root, options)
|
|
||||||
return inspect.inspect(root, options)
|
|
||||||
end,
|
|
||||||
})
|
|
||||||
|
|
||||||
return inspect
|
|
||||||
|
|
@ -1,7 +1,4 @@
|
||||||
local love = require("love")
|
local love = require("love")
|
||||||
local phys = require("phys")
|
|
||||||
local inspect = require("inspect")
|
|
||||||
local actors = require("actors")
|
|
||||||
|
|
||||||
local function initGame()
|
local function initGame()
|
||||||
return {
|
return {
|
||||||
|
|
@ -10,39 +7,32 @@ local function initGame()
|
||||||
ship = {
|
ship = {
|
||||||
radius = 30,
|
radius = 30,
|
||||||
bullet = {
|
bullet = {
|
||||||
|
timerLimit = 0.5,
|
||||||
radius = 5,
|
radius = 5,
|
||||||
recoil = 10,
|
|
||||||
speed = 500,
|
speed = 500,
|
||||||
time = 4,
|
time = 4,
|
||||||
sound = love.sound.newSoundData('resources/bullet.mp3'),
|
|
||||||
},
|
},
|
||||||
sprite = love.graphics.newImage('resources/playerShip1_blue.png'),
|
|
||||||
maxThrust = 200,
|
|
||||||
engineSound = love.sound.newSoundData('resources/enginehum3.ogg'),
|
|
||||||
rocketPlume = love.graphics.newImage('resources/sparkle_cloud.png'),
|
|
||||||
},
|
},
|
||||||
asteroids = {
|
asteroids = {
|
||||||
stages = {
|
stages = {
|
||||||
{ speed = 120, radius = 15, sprite = love.graphics.newImage('resources/meteorBrown_tiny1.png') },
|
{ speed = 120, radius = 15 },
|
||||||
{ speed = 70, radius = 30, sprite = love.graphics.newImage('resources/meteorBrown_small1.png') },
|
{ speed = 70, radius = 30 },
|
||||||
{ speed = 50, radius = 50, sprite = love.graphics.newImage('resources/meteorBrown_med1.png') },
|
{ speed = 50, radius = 50 },
|
||||||
{ speed = 20, radius = 80, sprite = love.graphics.newImage('resources/meteorBrown_big1.png') },
|
{ speed = 20, radius = 80 },
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
controls = {
|
controls = {
|
||||||
ship = {
|
ship = {
|
||||||
turnRate = 10,
|
turnRate = 10,
|
||||||
|
maxThrust = 200,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
-- The constant properties of the game.
|
|
||||||
local game = initGame()
|
local game = initGame()
|
||||||
|
|
||||||
local function initState()
|
local function initState()
|
||||||
---@diagnostic disable-next-line: redefined-local
|
|
||||||
local actors = actors.buildActors(game)
|
|
||||||
local asteroids = {
|
local asteroids = {
|
||||||
{
|
{
|
||||||
x = 100,
|
x = 100,
|
||||||
|
|
@ -57,17 +47,26 @@ local function initState()
|
||||||
y = game.height - 100,
|
y = game.height - 100,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for i, template in ipairs(asteroids) do
|
for _, asteroid in ipairs(asteroids) do
|
||||||
asteroids[i] = actors.asteroids.new(template)
|
asteroid.angle = love.math.random() * (2 * math.pi)
|
||||||
|
asteroid.stage = #game.asteroids.stages
|
||||||
end
|
end
|
||||||
local joystick = nil
|
local joystick = nil
|
||||||
if love.joystick.getJoystickCount() == 1 then
|
if love.joystick.getJoystickCount() == 1 then
|
||||||
joystick = love.joystick.getJoysticks()[1]
|
joystick = love.joystick.getJoysticks()[1]
|
||||||
end
|
end
|
||||||
return {
|
return {
|
||||||
debug = true,
|
ship = {
|
||||||
actors = actors,
|
x = game.width / 2,
|
||||||
ship = actors.ships.new({}),
|
y = game.height / 2,
|
||||||
|
angle = 0,
|
||||||
|
speed = {
|
||||||
|
x = 0,
|
||||||
|
y = 0,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
-- the time after which the ship can fire another bullet
|
||||||
|
bulletTimer = game.ship.bullet.timerLimit,
|
||||||
bullets = {},
|
bullets = {},
|
||||||
asteroids = asteroids,
|
asteroids = asteroids,
|
||||||
controls = {
|
controls = {
|
||||||
|
|
@ -76,16 +75,9 @@ local function initState()
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
-- The varying properties of a game being played.
|
|
||||||
local state = {}
|
local state = {}
|
||||||
|
|
||||||
local function reset()
|
local function reset()
|
||||||
if state.ship then
|
|
||||||
state.ship.engineSound:stop()
|
|
||||||
for _, bullet in ipairs(state.bullets) do
|
|
||||||
bullet.sound:stop()
|
|
||||||
end
|
|
||||||
end
|
|
||||||
state = initState()
|
state = initState()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -93,37 +85,26 @@ function love.load()
|
||||||
reset()
|
reset()
|
||||||
end
|
end
|
||||||
|
|
||||||
-- updates the actor's location by applying the passage of time within the game world
|
|
||||||
local function update(actor, dt)
|
|
||||||
phys.update(actor, dt)
|
|
||||||
actor.x = actor.x % game.width
|
|
||||||
actor.y = actor.y % game.height
|
|
||||||
end
|
|
||||||
|
|
||||||
function love.update(dt)
|
function love.update(dt)
|
||||||
state.ship.rocketPlume:update(dt)
|
if love.keyboard.isDown('right') then
|
||||||
if state.controls.joystick ~= nil then
|
state.ship.angle = state.ship.angle + game.controls.ship.turnRate * dt
|
||||||
state.ship.throttle = state.controls.joystick:getGamepadAxis('triggerright')
|
end
|
||||||
if state.ship.throttle > 0 then
|
if love.keyboard.isDown('left') then
|
||||||
local speed = state.ship.throttle * state.ship.maxThrust * dt
|
state.ship.angle = state.ship.angle - game.controls.ship.turnRate * dt
|
||||||
phys.apply(state.ship, { speed = speed, angle = state.ship.facing })
|
end
|
||||||
if not state.ship.engineSound:isPlaying() then
|
state.ship.angle = state.ship.angle % (2 * math.pi)
|
||||||
state.ship.engineSound:play()
|
|
||||||
end
|
if state.controls.joystick ~= nil then
|
||||||
end
|
local thrust = state.controls.joystick:getGamepadAxis('triggerright') * game.controls.ship.maxThrust
|
||||||
state.ship.engineSound:setVolume(state.ship.throttle * 0.5)
|
state.ship.speed.x = state.ship.speed.x + math.cos(state.ship.angle) * thrust * dt
|
||||||
-- TODO which is better, keeping the sound playing looping at 0 or checking the
|
state.ship.speed.y = state.ship.speed.y + math.sin(state.ship.angle) * thrust * dt
|
||||||
-- state each update?
|
end
|
||||||
if state.ship.throttle == 0 then
|
state.ship.x = (state.ship.x + state.ship.speed.x * dt) % game.width
|
||||||
if state.ship.engineSound:isPlaying() then
|
state.ship.y = (state.ship.y + state.ship.speed.y * dt) % game.height
|
||||||
state.ship.engineSound:stop()
|
|
||||||
end
|
local function areCirclesIntersecting(aX, aY, aRadius, bX, bY, bRadius)
|
||||||
end
|
return (aX - bX) ^ 2 + (aY - bY) ^ 2 <= (aRadius + bRadius) ^ 2
|
||||||
|
|
||||||
local spin = state.controls.joystick:getGamepadAxis('leftx') * game.controls.ship.turnRate
|
|
||||||
state.ship.facing = (state.ship.facing + spin * dt)
|
|
||||||
end
|
end
|
||||||
update(state.ship, dt)
|
|
||||||
|
|
||||||
for bulletIndex = #state.bullets, 1, -1 do
|
for bulletIndex = #state.bullets, 1, -1 do
|
||||||
local bullet = state.bullets[bulletIndex]
|
local bullet = state.bullets[bulletIndex]
|
||||||
|
|
@ -131,27 +112,28 @@ function love.update(dt)
|
||||||
if bullet.timeLeft <= 0 then
|
if bullet.timeLeft <= 0 then
|
||||||
table.remove(state.bullets, bulletIndex)
|
table.remove(state.bullets, bulletIndex)
|
||||||
else
|
else
|
||||||
update(bullet, dt)
|
bullet.x = (bullet.x + math.cos(bullet.angle) * game.ship.bullet.speed * dt) % game.width
|
||||||
|
bullet.y = (bullet.y + math.sin(bullet.angle) * game.ship.bullet.speed * dt) % game.height
|
||||||
end
|
end
|
||||||
for asteroidIndex = #state.asteroids, 1, -1 do
|
for asteroidIndex = #state.asteroids, 1, -1 do
|
||||||
local asteroid = state.asteroids[asteroidIndex]
|
local asteroid = state.asteroids[asteroidIndex]
|
||||||
if phys.intersects(bullet, asteroid) then
|
if areCirclesIntersecting(bullet.x, bullet.y, game.ship.bullet.radius, asteroid.x, asteroid.y, game.asteroids.stages[asteroid.stage].radius) then
|
||||||
table.remove(state.bullets, bulletIndex)
|
table.remove(state.bullets, bulletIndex)
|
||||||
if asteroid.stage > 1 then
|
if asteroid.stage > 1 then
|
||||||
local angle1 = love.math.random() * phys.tau
|
local angle1 = love.math.random() * (2 * math.pi)
|
||||||
local angle2 = angle1 - phys.tau / 2
|
local angle2 = (angle1 - math.pi) % (2 * math.pi)
|
||||||
table.insert(state.asteroids, state.actors.asteroids.new({
|
table.insert(state.asteroids, {
|
||||||
x = asteroid.x,
|
x = asteroid.x,
|
||||||
y = asteroid.y,
|
y = asteroid.y,
|
||||||
angle = angle1,
|
angle = angle1,
|
||||||
stage = asteroid.stage - 1,
|
stage = asteroid.stage - 1,
|
||||||
}))
|
})
|
||||||
table.insert(state.asteroids, state.actors.asteroids.new({
|
table.insert(state.asteroids, {
|
||||||
x = asteroid.x,
|
x = asteroid.x,
|
||||||
y = asteroid.y,
|
y = asteroid.y,
|
||||||
angle = angle2,
|
angle = angle2,
|
||||||
stage = asteroid.stage - 1,
|
stage = asteroid.stage - 1,
|
||||||
}))
|
})
|
||||||
end
|
end
|
||||||
table.remove(state.asteroids, asteroidIndex)
|
table.remove(state.asteroids, asteroidIndex)
|
||||||
break
|
break
|
||||||
|
|
@ -159,10 +141,26 @@ function love.update(dt)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
for _, asteroid in ipairs(state.asteroids) do
|
state.bulletTimer = state.bulletTimer + dt
|
||||||
update(asteroid, dt)
|
if love.keyboard.isDown('s') then
|
||||||
|
if state.bulletTimer >= game.ship.bullet.timerLimit then
|
||||||
|
state.bulletTimer = 0
|
||||||
|
table.insert(state.bullets, {
|
||||||
|
x = state.ship.x + math.cos(state.ship.angle) * game.ship.radius,
|
||||||
|
y = state.ship.y + math.sin(state.ship.angle) * game.ship.radius,
|
||||||
|
angle = state.ship.angle,
|
||||||
|
timeLeft = game.ship.bullet.time,
|
||||||
|
})
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
if phys.intersects(state.ship, asteroid) then
|
for _, asteroid in ipairs(state.asteroids) do
|
||||||
|
asteroid.x = (asteroid.x + math.cos(asteroid.angle) * game.asteroids.stages[asteroid.stage].speed * dt) %
|
||||||
|
game.width
|
||||||
|
asteroid.y = (asteroid.y + math.sin(asteroid.angle) * game.asteroids.stages[asteroid.stage].speed * dt) %
|
||||||
|
game.height
|
||||||
|
|
||||||
|
if areCirclesIntersecting(state.ship.x, state.ship.y, game.ship.radius, asteroid.x, asteroid.y, game.asteroids.stages[asteroid.stage].radius) then
|
||||||
reset()
|
reset()
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
|
|
@ -173,47 +171,30 @@ function love.update(dt)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function love.gamepadpressed(joystick, button)
|
|
||||||
if joystick ~= state.controls.joystick then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
if button == "b" then
|
|
||||||
-- debounce, perhaps
|
|
||||||
local bullet = state.actors.bullets.new({
|
|
||||||
x = state.ship.x + math.cos(state.ship.facing) * game.ship.radius,
|
|
||||||
y = state.ship.y + math.sin(state.ship.facing) * game.ship.radius,
|
|
||||||
angle = state.ship.facing,
|
|
||||||
facing = state.ship.facing,
|
|
||||||
})
|
|
||||||
phys.apply(bullet, state.ship)
|
|
||||||
table.insert(state.bullets, bullet)
|
|
||||||
bullet.sound:play()
|
|
||||||
|
|
||||||
phys.apply(state.ship, { speed = game.ship.bullet.recoil, angle = state.ship.facing + phys.tau / 2 })
|
|
||||||
end
|
|
||||||
if button == "x" then
|
|
||||||
print(inspect(state.ship))
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function love.draw()
|
function love.draw()
|
||||||
if state.debug then
|
|
||||||
love.graphics.setColor(1, 1, 1)
|
|
||||||
love.graphics.line(game.width / 2, 0, game.width / 2, game.height)
|
|
||||||
love.graphics.line(0, game.height / 2, game.width, game.height / 2)
|
|
||||||
end
|
|
||||||
for y = -1, 1 do
|
for y = -1, 1 do
|
||||||
for x = -1, 1 do
|
for x = -1, 1 do
|
||||||
love.graphics.origin()
|
love.graphics.origin()
|
||||||
love.graphics.translate(x * game.width, y * game.height)
|
love.graphics.translate(x * game.width, y * game.height)
|
||||||
state.ship:draw()
|
love.graphics.setColor(0, 0, 1)
|
||||||
|
love.graphics.circle('fill', state.ship.x, state.ship.y, game.ship.radius)
|
||||||
|
local shipCircleDistance = game.ship.radius * 2 / 3
|
||||||
|
love.graphics.setColor(0, 1, 1)
|
||||||
|
love.graphics.circle(
|
||||||
|
'fill',
|
||||||
|
state.ship.x + math.cos(state.ship.angle) * shipCircleDistance,
|
||||||
|
state.ship.y + math.sin(state.ship.angle) * shipCircleDistance,
|
||||||
|
game.ship.radius / 6
|
||||||
|
)
|
||||||
|
|
||||||
for _, bullet in ipairs(state.bullets) do
|
for _, bullet in ipairs(state.bullets) do
|
||||||
bullet:draw()
|
love.graphics.setColor(0, 1, 0)
|
||||||
|
love.graphics.circle('fill', bullet.x, bullet.y, game.ship.bullet.radius)
|
||||||
end
|
end
|
||||||
|
|
||||||
for _, asteroid in ipairs(state.asteroids) do
|
for _, asteroid in ipairs(state.asteroids) do
|
||||||
asteroid:draw()
|
love.graphics.setColor(1, 1, 0)
|
||||||
|
love.graphics.circle('fill', asteroid.x, asteroid.y, game.asteroids.stages[asteroid.stage].radius)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -1,36 +0,0 @@
|
||||||
local inspect = require('inspect')
|
|
||||||
|
|
||||||
local phys = {
|
|
||||||
tau = 2 * math.pi
|
|
||||||
}
|
|
||||||
|
|
||||||
-- returns true if the two actors intersect
|
|
||||||
function phys.intersects(actor1, actor2)
|
|
||||||
return (actor1.x - actor2.x) ^ 2 + (actor1.y - actor2.y) ^ 2 <= (actor1.radius + actor2.radius) ^ 2
|
|
||||||
end
|
|
||||||
|
|
||||||
-- updates the actor's location by applying the passage of time
|
|
||||||
function phys.update(actor, dt)
|
|
||||||
assert(actor.speed >= 0)
|
|
||||||
local dv = actor.speed * dt
|
|
||||||
actor.x = actor.x + math.cos(actor.angle) * dv
|
|
||||||
actor.y = actor.y + math.sin(actor.angle) * dv
|
|
||||||
local dr = actor.spin * dt
|
|
||||||
actor.facing = (actor.facing + dr) % phys.tau
|
|
||||||
end
|
|
||||||
|
|
||||||
function phys.apply(actor, force)
|
|
||||||
local ax = math.cos(actor.angle) * actor.speed
|
|
||||||
local ay = math.sin(actor.angle) * actor.speed
|
|
||||||
local fx = math.cos(force.angle) * force.speed
|
|
||||||
local fy = math.sin(force.angle) * force.speed
|
|
||||||
local x = ax + fx
|
|
||||||
local y = ay + fy
|
|
||||||
|
|
||||||
local speed = math.sqrt(x ^ 2 + y ^ 2)
|
|
||||||
local angle = math.atan2(y, x)
|
|
||||||
actor.speed = speed
|
|
||||||
actor.angle = angle
|
|
||||||
end
|
|
||||||
|
|
||||||
return phys
|
|
||||||
|
Before Width: | Height: | Size: 1.8 KiB |
|
Before Width: | Height: | Size: 2.3 KiB |
|
Before Width: | Height: | Size: 1.7 KiB |
|
Before Width: | Height: | Size: 1.9 KiB |
|
Before Width: | Height: | Size: 982 B |
|
Before Width: | Height: | Size: 881 B |
|
Before Width: | Height: | Size: 692 B |
|
Before Width: | Height: | Size: 667 B |
|
Before Width: | Height: | Size: 461 B |
|
Before Width: | Height: | Size: 371 B |
|
Before Width: | Height: | Size: 2.6 KiB |
|
Before Width: | Height: | Size: 739 B |
|
|
@ -1,49 +0,0 @@
|
||||||
t = require('luaunit')
|
|
||||||
local phys = require('phys')
|
|
||||||
|
|
||||||
function TestUpdate()
|
|
||||||
local actor = { x = 0, y = 0, speed = 1, angle = math.pi }
|
|
||||||
phys.update(actor, 0.1)
|
|
||||||
t.assertAlmostEquals(
|
|
||||||
actor,
|
|
||||||
{ x = -0.1, y = 0, speed = 1, angle = math.pi })
|
|
||||||
end
|
|
||||||
|
|
||||||
function TestApply()
|
|
||||||
local actor = { x = 0, y = 0, speed = 1, angle = math.pi }
|
|
||||||
|
|
||||||
phys.apply(actor, { speed = 1, angle = 0 })
|
|
||||||
t.assertAlmostEquals(
|
|
||||||
{ x = actor.x, y = actor.y, speed = actor.speed },
|
|
||||||
{ x = 0, y = 0, speed = 0 })
|
|
||||||
|
|
||||||
phys.apply(actor, { speed = 1, angle = math.pi })
|
|
||||||
t.assertAlmostEquals(
|
|
||||||
actor,
|
|
||||||
{ x = 0, y = 0, speed = 1, angle = math.pi }
|
|
||||||
)
|
|
||||||
|
|
||||||
phys.apply(actor, { speed = 1, angle = math.pi / 2 })
|
|
||||||
t.assertAlmostEquals(
|
|
||||||
actor,
|
|
||||||
{ x = 0, y = 0, speed = math.sqrt(2), angle = math.pi * 3 / 4 })
|
|
||||||
|
|
||||||
actor = { x = 0, y = 0, speed = 0, angle = 0 }
|
|
||||||
phys.apply(actor, { speed = 1, angle = math.pi / 2 })
|
|
||||||
t.assertAlmostEquals(
|
|
||||||
actor,
|
|
||||||
{ x = 0, y = 0, speed = 1, angle = math.pi / 2 })
|
|
||||||
|
|
||||||
phys.apply(actor, { speed = 1, angle = math.pi / 2 })
|
|
||||||
t.assertAlmostEquals(
|
|
||||||
actor,
|
|
||||||
{ x = 0, y = 0, speed = 2, angle = math.pi / 2 })
|
|
||||||
|
|
||||||
phys.apply(actor, { speed = 1, angle = math.pi })
|
|
||||||
t.assertAlmostEquals(
|
|
||||||
actor,
|
|
||||||
{ x = 0, y = 0, speed = 0, angle = 0 }
|
|
||||||
)
|
|
||||||
end
|
|
||||||
|
|
||||||
os.exit(t.LuaUnit.run())
|
|
||||||