introduce actor trait
This commit is contained in:
parent
11a4fe9826
commit
b832731fa0
1 changed files with 28 additions and 9 deletions
|
|
@ -13,7 +13,6 @@ local function initGame()
|
|||
time = 4,
|
||||
sound = love.sound.newSoundData('resources/bullet.mp3'),
|
||||
},
|
||||
-- 99x75
|
||||
sprite = love.graphics.newImage('resources/ship.png'),
|
||||
},
|
||||
asteroids = {
|
||||
|
|
@ -53,6 +52,9 @@ local function initState()
|
|||
for _, asteroid in ipairs(asteroids) do
|
||||
asteroid.angle = love.math.random() * (2 * math.pi)
|
||||
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
|
||||
|
|
@ -86,6 +88,19 @@ function love.load()
|
|||
reset()
|
||||
end
|
||||
|
||||
-- updates the actor's location by applying the passage of time
|
||||
-- TODO ship
|
||||
-- TODO spin
|
||||
local function update(actor, dt)
|
||||
local dv = actor.speed * dt
|
||||
actor.x = (actor.x + math.cos(actor.angle) * dv) % game.width
|
||||
actor.y = (actor.y + math.sin(actor.angle) * dv) % game.height
|
||||
end
|
||||
|
||||
local function intersects(actor1, actor2)
|
||||
return (actor1.x - actor2.x) ^ 2 + (actor1.y - actor2.y) ^ 2 <= (actor1.radius + actor2.radius) ^ 2
|
||||
end
|
||||
|
||||
function love.update(dt)
|
||||
if state.controls.joystick ~= nil then
|
||||
local thrust = state.controls.joystick:getGamepadAxis('triggerright') * game.controls.ship.maxThrust
|
||||
|
|
@ -108,27 +123,32 @@ function love.update(dt)
|
|||
if bullet.timeLeft <= 0 then
|
||||
table.remove(state.bullets, bulletIndex)
|
||||
else
|
||||
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
|
||||
update(bullet, dt)
|
||||
end
|
||||
for asteroidIndex = #state.asteroids, 1, -1 do
|
||||
local asteroid = state.asteroids[asteroidIndex]
|
||||
if areCirclesIntersecting(bullet.x, bullet.y, game.ship.bullet.radius, asteroid.x, asteroid.y, game.asteroids.stages[asteroid.stage].radius) then
|
||||
if 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)
|
||||
|
|
@ -138,12 +158,9 @@ function love.update(dt)
|
|||
end
|
||||
|
||||
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
|
||||
update(asteroid, dt)
|
||||
|
||||
if areCirclesIntersecting(state.ship.x, state.ship.y, game.ship.radius, asteroid.x, asteroid.y, game.asteroids.stages[asteroid.stage].radius) then
|
||||
if areCirclesIntersecting(state.ship.x, state.ship.y, game.ship.radius, asteroid.x, asteroid.y, asteroid.radius) then
|
||||
reset()
|
||||
break
|
||||
end
|
||||
|
|
@ -164,6 +181,8 @@ function love.gamepadpressed(joystick, button)
|
|||
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,
|
||||
speed = game.ship.bullet.speed,
|
||||
radius = game.ship.bullet.radius,
|
||||
timeLeft = game.ship.bullet.time,
|
||||
sound = love.audio.newSource(game.ship.bullet.sound),
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue