lovexp/asteroids/main.lua
Donald Ball f5df911707 fix atan issue
the love2d that I'm using doesn't have the new atan arity lol
2025-11-17 20:21:42 -05:00

219 lines
6.8 KiB
Lua

local love = require("love")
local phys = require("phys")
local inspect = require("inspect")
local function initGame()
return {
width = 800,
height = 600,
ship = {
radius = 30,
bullet = {
radius = 5,
recoil = -10,
speed = 500,
time = 4,
sound = love.sound.newSoundData('resources/bullet.mp3'),
},
sprite = love.graphics.newImage('resources/ship.png'),
},
asteroids = {
stages = {
{ speed = 120, radius = 15 },
{ speed = 70, radius = 30 },
{ speed = 50, radius = 50 },
{ speed = 20, radius = 80 },
},
},
controls = {
ship = {
turnRate = 10,
maxThrust = 200,
},
},
}
end
local game = initGame()
local function initState()
local asteroids = {
{
x = 100,
y = 100,
},
{
x = game.width - 100,
y = 100,
},
{
x = game.width / 2,
y = game.height - 100,
}
}
for _, asteroid in ipairs(asteroids) do
asteroid.angle = love.math.random() * phys.tau
asteroid.stage = #game.asteroids.stages
local stage = game.asteroids.stages[asteroid.stage]
asteroid.speed = stage.speed
asteroid.radius = stage.radius
end
local joystick = nil
if love.joystick.getJoystickCount() == 1 then
joystick = love.joystick.getJoysticks()[1]
end
return {
ship = {
x = game.width / 2,
y = game.height / 2,
angle = 0,
facing = phys.tau * 3 / 4,
speed = 0,
radius = game.ship.radius,
},
bullets = {},
asteroids = asteroids,
controls = {
joystick = joystick,
}
}
end
local state = {}
local function reset()
state = initState()
end
function love.load()
reset()
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)
if state.controls.joystick ~= nil then
local thrust = state.controls.joystick:getGamepadAxis('triggerright') * game.controls.ship.maxThrust
if thrust > 0 then
phys.apply(state.ship, { speed = thrust * dt, angle = state.ship.facing })
end
local spin = state.controls.joystick:getGamepadAxis('leftx') * game.controls.ship.turnRate
state.ship.facing = (state.ship.facing + spin * dt) % phys.tau
end
update(state.ship, dt)
for bulletIndex = #state.bullets, 1, -1 do
local bullet = state.bullets[bulletIndex]
bullet.timeLeft = bullet.timeLeft - dt
if bullet.timeLeft <= 0 then
table.remove(state.bullets, bulletIndex)
else
update(bullet, dt)
end
for asteroidIndex = #state.asteroids, 1, -1 do
local asteroid = state.asteroids[asteroidIndex]
if phys.intersects(bullet, asteroid) then
table.remove(state.bullets, bulletIndex)
if asteroid.stage > 1 then
local angle1 = love.math.random() * (2 * math.pi)
local angle2 = (angle1 - math.pi) % (2 * math.pi)
local radius = game.asteroids.stages[asteroid.stage - 1].radius
local speed = game.asteroids.stages[asteroid.stage - 1].speed
table.insert(state.asteroids, {
x = asteroid.x,
y = asteroid.y,
angle = angle1,
stage = asteroid.stage - 1,
radius = radius,
speed = speed,
})
table.insert(state.asteroids, {
x = asteroid.x,
y = asteroid.y,
angle = angle2,
stage = asteroid.stage - 1,
radius = radius,
speed = speed,
})
end
table.remove(state.asteroids, asteroidIndex)
break
end
end
end
for _, asteroid in ipairs(state.asteroids) do
update(asteroid, dt)
if phys.intersects(state.ship, asteroid) then
reset()
break
end
end
if #state.asteroids == 0 then
--reset()
end
end
function love.gamepadpressed(joystick, button)
if joystick ~= state.controls.joystick then
return
end
if button == "b" then
-- debounce, perhaps
-- include ship velocity, perhaps
local bullet = {
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,
speed = game.ship.bullet.speed,
radius = game.ship.bullet.radius,
timeLeft = game.ship.bullet.time,
sound = love.audio.newSource(game.ship.bullet.sound),
}
table.insert(state.bullets, bullet)
bullet.sound:play()
--state.ship.speed.x = state.ship.speed.x + math.cos(state.ship.angle) * game.ship.bullet.recoil
--state.ship.speed.y = state.ship.speed.y + math.sin(state.ship.angle) * game.ship.bullet.recoil
end
if button == "x" then
print(inspect(state.ship))
end
end
function love.draw()
for y = -1, 1 do
for x = -1, 1 do
love.graphics.origin()
love.graphics.translate(x * game.width, y * game.height)
love.graphics.setColor(1, 1, 1)
local scale = 2 * game.ship.radius / math.max(game.ship.sprite:getWidth(), game.ship.sprite:getHeight())
love.graphics.draw(game.ship.sprite,
state.ship.x,
state.ship.y,
(state.ship.facing + math.pi / 2) % (math.pi * 2),
scale,
scale,
game.ship.sprite:getWidth() / 2,
game.ship.sprite:getHeight() / 2
)
for _, bullet in ipairs(state.bullets) do
love.graphics.setColor(0, 1, 0)
love.graphics.circle('fill', bullet.x, bullet.y, game.ship.bullet.radius)
end
for _, asteroid in ipairs(state.asteroids) do
love.graphics.setColor(1, 1, 0)
love.graphics.circle('fill', asteroid.x, asteroid.y, game.asteroids.stages[asteroid.stage].radius)
end
end
end
end