Files

145 lines
4.0 KiB
Lua

local screen_width, screen_height = love.graphics.getDimensions()
local function distance(x1, y1, x2, y2)
delta_x = x1 - x2
delta_y = y1 - y2
return math.sqrt(delta_x^2 + delta_y^2)
end
orbital_speed = 0.0625 * 2500 -- I have no idea what the fuck this means
local function new_body(orbital_radius, body_radius)
local body = {
orbital_radius = orbital_radius,
body_radius = body_radius,
rotation_offset = math.pi * 2 * love.math.random(),
color = { love.math.random(), love.math.random(), love.math.random(), 1 },
}
return body
end
local function get_body_position(body, time)
local argument = time * orbital_speed / body.orbital_radius^1.337 + body.rotation_offset
if (argument == math.huge) or (argument ~= argument) then
argument = 0
end
return body.orbital_radius * math.cos(argument), body.orbital_radius * math.sin(argument)
end
local function draw_body(body, time)
local x, y = get_body_position(body, time)
love.graphics.setColor(body.color)
love.graphics.circle("fill", x, y, body.body_radius)
love.graphics.setColor(1, 1, 1, 1) -- TEMP delete this if it works without it
end
local function new_ship()
local ship = {
orbital_radius = 0,
rotation_offset = 0,
x = 30, y = 30,
orbiting = false,
time_offset = 0,
parent = nil,
}
return ship
end
local function get_ship_position(ship, time)
if ship.orbiting then
local x, y
if ship.parent then
x, y = get_body_position(ship.parent, time) -- WARNING this should work the same for all objects or things will suck
else
x, y = 0, 0
end
local argument = (time - ship.time_offset) * orbital_speed / ship.orbital_radius^1.337 + ship.rotation_offset
if (argument == math.huge) or (argument ~= argument) then
argument = 0
end
return x + ship.orbital_radius * math.cos(argument), y + ship.orbital_radius * math.sin(argument)
else
return ship.x, ship.y
end
end
local function draw_ship(ship, time)
local x, y = get_ship_position(ship, time)
love.graphics.rectangle("line", x - 3, y - 3, 6, 6)
end
local time = os.time()
love.math.setRandomSeed(time)
local speed = 1
local bodies = {}
table.insert(bodies, new_body(0, 25))
for i = 1, 1 do -- TEMP only adding 1 secondary body
table.insert(bodies, new_body(love.math.random() * 200 + 100, love.math.random() * 4 + 1))
end
local ship = new_ship()
-- ship was in bodies table; not currently possible here
local camera = { x = 0, y = 0, }
function love.update(dt)
time = time + dt * speed
-- NOTE camera controls 200 * dt
if not ship.orbiting then
if love.keyboard.isDown("w") then
ship.y = ship.y - 100 * dt
end
if love.keyboard.isDown("s") then
ship.y = ship.y + 100 * dt
end
if love.keyboard.isDown("a") then
ship.x = ship.x - 100 * dt
end
if love.keyboard.isDown("d") then
ship.x = ship.x + 100 * dt
end
end
end
local font_size = love.graphics.getFont():getHeight()
function love.draw()
love.graphics.print("wasd: Move. o: Toggle orbit.", 1, screen_height - (font_size + 1))
love.graphics.translate(screen_width / 2 - camera.x, screen_height / 2 - camera.y)
for i = 1, #bodies do
local body = bodies[i]
draw_body(body, time)
end
draw_ship(ship, time)
end
-- TEMP ship is always orbiting body 2
ship.parent = bodies[2]
function love.keypressed(key)
if key == "escape" then
love.event.quit()
-- NOTE ignoring speed control
elseif key == "o" then
if ship.orbiting then
local x, y = get_ship_position(ship, time)
ship.x = x
ship.y = y
ship.orbiting = false
else
-- only works with center (fuck)
-- ship.orbital_radius = math.sqrt(ship.x^2 + ship.y^2)
-- ship.rotation_offset = math.atan2(ship.y, ship.x)
-- ship.time_offset = time
-- ship.orbiting = true
local x, y = get_body_position(ship.parent, time)
ship.orbital_radius = distance(x, ship.x, y, ship.y)
ship.rotation_offset = math.atan2(ship.y - y, ship.x - x)
ship.time_offset = time
ship.orbiting = true
end
end
end