Files
SpaceTrader/treemodel/main.lua
2025-11-28 00:51:46 -07:00

160 lines
4.3 KiB
Lua

local screen_width, screen_height = 960, 540
love.window.setMode(screen_width, screen_height)
local game_time = os.time()
love.math.setRandomSeed(game_time)
game_time = 0 -- TEMP ?
-- local core
-- local generate_system
-- generate_system = function(object)
-- end
-- core = generate_system()
local star = {
x = 0, y = 0, orbital_radius = 0, body_radius = 100^0.5,
children = {
{
orbital_radius = 100, body_radius = 6^0.5,
children = {
{
orbital_radius = 10, body_radius = 1.5^0.5,
}
}
},
{
orbital_radius = 50, body_radius = 3^0.5,
},
{
orbital_radius = 200, body_radius = 12^0.5,
children = {
{
orbital_radius = 25, body_radius = 3^0.5,
children = {
{
orbital_radius = 5, body_radius = 1^0.5,
}
}
}
}
}
}
}
local set_offsets
set_offsets = function(object)
object.rotation_offset = math.pi * 2 * love.math.random()
if object.children then
for i = 1, #object.children do
set_offsets(object.children[i])
end
end
end
set_offsets(star)
-- max size star
star = {
x = 0, y = 0, orbital_radius = 0, body_radius = 100^0.5, rotation_offset = 0,
children = {
{
orbital_radius = 950 / 2, body_radius = 10^0.5, rotation_offset = 0,
},
{
orbital_radius = 530 / 2, body_radius = 10^0.5, rotation_offset = math.pi / 2,
},
{
orbital_radius = 30 / 2, body_radius = 1^0.5, rotation_offset = 0,
}
}
}
-- min size star
star = {
x = 0, y = 0, orbital_radius = 0, body_radius = 10^0.5, rotation_offset = 0,
children = {
{
orbital_radius = 950 / 2, body_radius = 2^0.5, rotation_offset = 0,
},
{
orbital_radius = 530 / 2, body_radius = 2^0.5, rotation_offset = math.pi / 2,
},
{
orbital_radius = 30 / 2, body_radius = 0.1^0.5, rotation_offset = 0,
}
}
}
local generate_bodies
generate_bodies = function(parent)
local object
if parent then
object = {
rotation_offset = math.pi * 2 * love.math.random(),
body_radius = love.math.random() * math.sqrt(parent.body_radius / 2) + parent.body_radius * 0.05,
}
object.orbital_radius = object.body_radius * 1.1 + parent.body_radius * 1.05 + love.math.random() * parent.body_radius^2.5
else
object = {
x = 0, y = 0, orbital_radius = 0, rotation_offset = 0, body_radius = math.sqrt(love.math.random() * 200 + 20),
}
end
object.children = {}
local potential_child_count = math.floor(object.body_radius)
if love.math.random() > 0.5 then return object end
if potential_child_count >= 1 then
for i = 1, potential_child_count do
local child_object = generate_bodies(object)
if child_object then
table.insert(object.children, child_object)
end
end
end
return object
end
star = generate_bodies()
function love.update(dt)
game_time = game_time + dt
local update_object_position
update_object_position = function(object, parent_x, parent_y, parent_mass)
local temporary_speed_up = 1
local argument = game_time / object.orbital_radius^1.337 * temporary_speed_up * parent_mass + object.rotation_offset
if (argument == math.huge) or (argument ~= argument) then
argument = 0
end
object.x, object.y = parent_x + object.orbital_radius * math.cos(argument), parent_y + object.orbital_radius * math.sin(argument)
if object.children then
for i = 1, #object.children do
update_object_position(object.children[i], object.x, object.y, object.body_radius^2)
end
end
end
update_object_position(star, 0, 0, 0)
end
function love.draw()
love.graphics.setColor(1, 1, 1, 1)
love.graphics.translate(screen_width / 2, screen_height / 2)
local draw_object
draw_object = function(object, parent_x, parent_y)
love.graphics.circle("fill", object.x, object.y, math.max(object.body_radius, 0.8))
love.graphics.circle("line", parent_x, parent_y, object.orbital_radius) -- orbital path
if object.children then
for i = 1, #object.children do
draw_object(object.children[i], object.x, object.y)
end
end
end
draw_object(star, 0, 0)
end
function love.keypressed(key)
if key == "escape" then
love.event.quit()
elseif key == "r" then
star = generate_bodies()
end
end