lovexp/asteroids/actors.lua

159 lines
4.6 KiB
Lua
Raw Normal View History

2025-11-20 21:26:44 -05:00
local love = require("love")
2025-11-20 20:58:15 -05:00
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)
2025-11-20 21:07:12 -05:00
local stageIndex = asteroid.stage or #game.asteroids.stages
local stage = game.asteroids.stages[stageIndex]
2025-11-20 20:58:15 -05:00
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
2025-11-20 21:07:12 -05:00
instance.stage = stageIndex
2025-11-20 20:58:15 -05:00
setmetatable(instance, asteroids_mt)
return instance
end
2025-11-20 21:26:44 -05:00
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(0, 1, 0)
love.graphics.circle('fill', self.x, self.y, self.radius)
love.graphics.pop()
end
2025-11-20 21:37:04 -05:00
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,
2025-11-20 21:42:14 -05:00
throttle = 0,
2025-11-20 21:37:04 -05:00
}
for k, v in pairs(game.ship) do
instance[k] = v
end
for k, v in pairs(ship) do
instance[k] = v
end
2025-11-20 22:07:03 -05:00
instance.engineSound = love.audio.newSource(instance.engineSound)
instance.engineSound:setVolume(0)
instance.engineSound:setLooping(true)
2025-11-20 22:53:33 -05:00
instance.rocketPlume = love.graphics.newParticleSystem(instance.rocketPlume)
2025-11-21 22:32:03 -05:00
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
)
2025-11-20 22:53:33 -05:00
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)
2025-11-21 22:32:03 -05:00
instance.rocketPlume:setEmissionArea('normal', 1.5, 1.5)
2025-11-20 22:53:33 -05:00
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)
2025-11-20 21:37:04 -05:00
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
)
2025-11-20 23:28:48 -05:00
self.rocketPlume:setEmissionRate(1000 * self.throttle)
2025-11-20 22:53:33 -05:00
love.graphics.draw(self.rocketPlume,
0,
0,
(self.facing + phys.tau * 3 / 4) % phys.tau,
scale,
scale,
0,
2025-11-21 22:32:03 -05:00
height * 0.6
2025-11-20 22:53:33 -05:00
)
2025-11-20 21:37:04 -05:00
love.graphics.pop()
end
2025-11-20 20:58:15 -05:00
return {
asteroids = asteroids,
2025-11-20 21:26:44 -05:00
bullets = bullets,
2025-11-20 21:37:04 -05:00
ships = ships,
2025-11-20 20:58:15 -05:00
}
end
return actors