fix atan issue
the love2d that I'm using doesn't have the new atan arity lol
This commit is contained in:
parent
9cdf032c39
commit
f5df911707
3 changed files with 40 additions and 23 deletions
|
|
@ -1,5 +1,6 @@
|
||||||
local love = require("love")
|
local love = require("love")
|
||||||
local phys = require("phys")
|
local phys = require("phys")
|
||||||
|
local inspect = require("inspect")
|
||||||
|
|
||||||
local function initGame()
|
local function initGame()
|
||||||
return {
|
return {
|
||||||
|
|
@ -51,7 +52,7 @@ local function initState()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for _, asteroid in ipairs(asteroids) do
|
for _, asteroid in ipairs(asteroids) do
|
||||||
asteroid.angle = love.math.random() * (2 * math.pi)
|
asteroid.angle = love.math.random() * phys.tau
|
||||||
asteroid.stage = #game.asteroids.stages
|
asteroid.stage = #game.asteroids.stages
|
||||||
local stage = game.asteroids.stages[asteroid.stage]
|
local stage = game.asteroids.stages[asteroid.stage]
|
||||||
asteroid.speed = stage.speed
|
asteroid.speed = stage.speed
|
||||||
|
|
@ -65,11 +66,9 @@ local function initState()
|
||||||
ship = {
|
ship = {
|
||||||
x = game.width / 2,
|
x = game.width / 2,
|
||||||
y = game.height / 2,
|
y = game.height / 2,
|
||||||
angle = 3 * math.pi / 2,
|
angle = 0,
|
||||||
speed = {
|
facing = phys.tau * 3 / 4,
|
||||||
x = 0,
|
speed = 0,
|
||||||
y = 0,
|
|
||||||
},
|
|
||||||
radius = game.ship.radius,
|
radius = game.ship.radius,
|
||||||
},
|
},
|
||||||
bullets = {},
|
bullets = {},
|
||||||
|
|
@ -100,14 +99,14 @@ end
|
||||||
function love.update(dt)
|
function love.update(dt)
|
||||||
if state.controls.joystick ~= nil then
|
if state.controls.joystick ~= nil then
|
||||||
local thrust = state.controls.joystick:getGamepadAxis('triggerright') * game.controls.ship.maxThrust
|
local thrust = state.controls.joystick:getGamepadAxis('triggerright') * game.controls.ship.maxThrust
|
||||||
state.ship.speed.x = state.ship.speed.x + math.cos(state.ship.angle) * thrust * dt
|
if thrust > 0 then
|
||||||
state.ship.speed.y = state.ship.speed.y + math.sin(state.ship.angle) * thrust * dt
|
phys.apply(state.ship, { speed = thrust * dt, angle = state.ship.facing })
|
||||||
|
end
|
||||||
|
|
||||||
local spin = state.controls.joystick:getGamepadAxis('leftx') * game.controls.ship.turnRate
|
local spin = state.controls.joystick:getGamepadAxis('leftx') * game.controls.ship.turnRate
|
||||||
state.ship.angle = (state.ship.angle + spin * dt) % (2 * math.pi)
|
state.ship.facing = (state.ship.facing + spin * dt) % phys.tau
|
||||||
end
|
end
|
||||||
state.ship.x = (state.ship.x + state.ship.speed.x * dt) % game.width
|
update(state.ship, dt)
|
||||||
state.ship.y = (state.ship.y + state.ship.speed.y * dt) % game.height
|
|
||||||
|
|
||||||
for bulletIndex = #state.bullets, 1, -1 do
|
for bulletIndex = #state.bullets, 1, -1 do
|
||||||
local bullet = state.bullets[bulletIndex]
|
local bullet = state.bullets[bulletIndex]
|
||||||
|
|
@ -159,7 +158,7 @@ function love.update(dt)
|
||||||
end
|
end
|
||||||
|
|
||||||
if #state.asteroids == 0 then
|
if #state.asteroids == 0 then
|
||||||
reset()
|
--reset()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -169,10 +168,11 @@ function love.gamepadpressed(joystick, button)
|
||||||
end
|
end
|
||||||
if button == "b" then
|
if button == "b" then
|
||||||
-- debounce, perhaps
|
-- debounce, perhaps
|
||||||
|
-- include ship velocity, perhaps
|
||||||
local bullet = {
|
local bullet = {
|
||||||
x = state.ship.x + math.cos(state.ship.angle) * game.ship.radius,
|
x = state.ship.x + math.cos(state.ship.facing) * game.ship.radius,
|
||||||
y = state.ship.y + math.sin(state.ship.angle) * game.ship.radius,
|
y = state.ship.y + math.sin(state.ship.facing) * game.ship.radius,
|
||||||
angle = state.ship.angle,
|
angle = state.ship.facing,
|
||||||
speed = game.ship.bullet.speed,
|
speed = game.ship.bullet.speed,
|
||||||
radius = game.ship.bullet.radius,
|
radius = game.ship.bullet.radius,
|
||||||
timeLeft = game.ship.bullet.time,
|
timeLeft = game.ship.bullet.time,
|
||||||
|
|
@ -180,8 +180,11 @@ function love.gamepadpressed(joystick, button)
|
||||||
}
|
}
|
||||||
table.insert(state.bullets, bullet)
|
table.insert(state.bullets, bullet)
|
||||||
bullet.sound:play()
|
bullet.sound:play()
|
||||||
state.ship.speed.x = state.ship.speed.x + math.cos(state.ship.angle) * game.ship.bullet.recoil
|
--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
|
--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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -195,7 +198,7 @@ function love.draw()
|
||||||
love.graphics.draw(game.ship.sprite,
|
love.graphics.draw(game.ship.sprite,
|
||||||
state.ship.x,
|
state.ship.x,
|
||||||
state.ship.y,
|
state.ship.y,
|
||||||
(state.ship.angle + math.pi / 2) % (math.pi * 2),
|
(state.ship.facing + math.pi / 2) % (math.pi * 2),
|
||||||
scale,
|
scale,
|
||||||
scale,
|
scale,
|
||||||
game.ship.sprite:getWidth() / 2,
|
game.ship.sprite:getWidth() / 2,
|
||||||
|
|
|
||||||
|
|
@ -19,12 +19,10 @@ function phys.update(actor, dt)
|
||||||
end
|
end
|
||||||
|
|
||||||
function phys.rotate(actor, angle)
|
function phys.rotate(actor, angle)
|
||||||
actor.angle = (actor.angle + angle) % phys.tau
|
actor.facing = (actor.facing + angle) % phys.tau
|
||||||
end
|
end
|
||||||
|
|
||||||
function phys.apply(actor, force)
|
function phys.apply(actor, force)
|
||||||
assert(actor.speed >= 0)
|
|
||||||
assert(force.speed >= 0)
|
|
||||||
local ax = math.cos(actor.angle) * actor.speed
|
local ax = math.cos(actor.angle) * actor.speed
|
||||||
local ay = math.sin(actor.angle) * actor.speed
|
local ay = math.sin(actor.angle) * actor.speed
|
||||||
local fx = math.cos(force.angle) * force.speed
|
local fx = math.cos(force.angle) * force.speed
|
||||||
|
|
@ -33,7 +31,7 @@ function phys.apply(actor, force)
|
||||||
local y = ay + fy
|
local y = ay + fy
|
||||||
|
|
||||||
local speed = math.sqrt(x ^ 2 + y ^ 2)
|
local speed = math.sqrt(x ^ 2 + y ^ 2)
|
||||||
local angle = math.atan(y, x) % phys.tau
|
local angle = math.atan2(y, x) % phys.tau
|
||||||
actor.speed = speed
|
actor.speed = speed
|
||||||
actor.angle = angle
|
actor.angle = angle
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,23 @@ function TestApply()
|
||||||
phys.apply(actor, { speed = 1, angle = math.pi / 2 })
|
phys.apply(actor, { speed = 1, angle = math.pi / 2 })
|
||||||
t.assertAlmostEquals(
|
t.assertAlmostEquals(
|
||||||
actor,
|
actor,
|
||||||
{ x = 0, y = 0, speed = math.sqrt(2), angle = math.pi * 3 / 4 }
|
{ 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
|
end
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue