diff --git a/ReadMe.md b/ReadMe.md index 184e248..56aa9c2 100644 --- a/ReadMe.md +++ b/ReadMe.md @@ -28,3 +28,14 @@ code, so issues are being tracked *in* the repo - here. 2. ✅ Resource points. The player can hold a button to collect those resources and store them on their ship. 3. ✅ The resources collected by the player can be spent on something, like improving the ship. Speed, capacity, collection speed, etc. 4. Imagine why you are out there collecting resources. Is someone directing you? Are you a freelancer? Is the universe dead? Are you preparing for something? Etc. + +## Documentation + +### Cargo Handling +- `transfer_cargo(source_object, destination_object, cargo_type, cargo_amount)`: Returns actual amount transferred. Source/Destination can be nil to create/destroy resources. +- `check_cargo_amount(source_object, cargo_type, cargo_amount)`: true/false Does that object have that amount of that cargo? + +### Finding Objects +- `get_closest_object(object_list, current_object)`: Requires `position_x` and `position_y` on objects. Returns an object and distance squared to it. +- `get_closest_orbit(object_list, current_object)`: Deprecated. +- `get_closest_orbit2(current_object)`: Uses `parent_object` and `orbital_radius` to find the closest orbit to `current_object`'s orbit. diff --git a/src/cargo_handling.lua b/src/cargo_handling.lua new file mode 100644 index 0000000..191872e --- /dev/null +++ b/src/cargo_handling.lua @@ -0,0 +1,62 @@ +local cargo_handling = {} + +cargo_handling.transfer_cargo = function(source_object, destination_object, cargo_type, cargo_amount) + -- source_object and destination_object can be nil to create/delete resources + -- returns cargo_amount (actual amount transferred) + + if not source_object then + source_object = { cargo_contents = { [cargo_type] = math.huge }, cargo_free_space = 0, } + end + if not destination_object then + destination_object = { cargo_contents = {}, cargo_free_space = math.huge, } + end + + if (not source_object.cargo_contents) or (not source_object.cargo_contents[cargo_type]) then + return 0 + end + if not destination_object.cargo_contents then + destination_object.cargo_contents = {} + destination_object.cargo_free_space = 0 + end + if not destination_object.cargo_contents[cargo_type] then + destination_object.cargo_contents[cargo_type] = 0 + end + + if cargo_amount > source_object.cargo_contents[cargo_type] then + cargo_amount = source_object.cargo_contents[cargo_type] + end + if cargo_amount > destination_object.cargo_free_space then + cargo_amount = destination_object.cargo_free_space + end + + destination_object.cargo_contents[cargo_type] = destination_object.cargo_contents[cargo_type] + cargo_amount + destination_object.cargo_free_space = destination_object.cargo_free_space - cargo_amount + source_object.cargo_contents[cargo_type] = source_object.cargo_contents[cargo_type] - cargo_amount + source_object.cargo_free_space = source_object.cargo_free_space + cargo_amount + + if source_object.cargo_contents[cargo_type] <= 0 then + source_object.cargo_contents[cargo_type] = nil + end + if destination_object.cargo_contents[cargo_type] <= 0 then + destination_object.cargo_contents[cargo_type] = nil + end + + return cargo_amount +end + +cargo_handling.check_cargo_amount = function(source_object, cargo_type, cargo_amount) + -- returns true/false: Does that object have that amount of that cargo? + + if not source_object.cargo_contents then + return false + end + if not source_object.cargo_contents[cargo_type] then + return false + end + if source_object.cargo_contents[cargo_type] >= cargo_amount then + return true + end + return false +end + +return cargo_handling diff --git a/src/finding_objects.lua b/src/finding_objects.lua new file mode 100644 index 0000000..bbbb270 --- /dev/null +++ b/src/finding_objects.lua @@ -0,0 +1,72 @@ +local finding_objects = {} + +finding_objects.get_closest_object = function(object_list, current_object) + -- returns selected_object, distance_squared_to_object (if object_list is empty, returns nil, math.huge) + + local selected_object, distance_squared_to_object = nil, math.huge + for i = 1, #object_list do + local function compare_object() + local comparison_object = object_list[i] + if comparison_object == current_object then + return + end + local comparison_distance_squared = (current_object.position_x - comparison_object.position_x)^2 + (current_object.position_y - comparison_object.position_y)^2 + if comparison_distance_squared < distance_squared_to_object then + selected_object = comparison_object + distance_squared_to_object = comparison_distance_squared + end + end + compare_object() + end + + return selected_object, distance_squared_to_object +end + +-- DEPRECATED +finding_objects.get_closest_orbit = function(object_list, current_object) + -- returns selected_object, distance_to_orbit (if object_list is empty, returns nil, math.huge) + -- object_list must be of objects orbiting the same parent_object or results will be wrong + + local selected_object, distance_to_orbit = nil, math.huge + for i = 1, #object_list do + local function compare_object() + local comparison_object = object_list[i] + if comparison_object == current_object then + return + end + local comparison_distance = math.abs(current_object.orbital_radius - comparison_object.orbital_radius) + if comparison_distance < distance_to_orbit then + selected_object = comparison_object + distance_to_orbit = comparison_distance + end + end + compare_object() + end + + return selected_object, distance_to_orbit +end + +finding_objects.get_closest_orbit2 = function(current_object) + -- returns selected_object, distance_to_orbit (if there are none, returns nil, math.huge) + local object_list = current_object.parent_object.child_objects + + local selected_object, distance_to_orbit = nil, math.huge + for i = 1, #object_list do + local function compare_object() + local comparison_object = object_list[i] + if comparison_object == current_object then + return + end + local comparison_distance = math.abs(current_object.orbital_radius - comparison_object.orbital_radius) + if comparison_distance < distance_to_orbit then + selected_object = comparison_object + distance_to_orbit = comparison_distance + end + end + compare_object() + end + + return selected_object, distance_to_orbit +end + +return finding_objects diff --git a/src/main.lua b/src/main.lua index 6264d75..57e4be9 100644 --- a/src/main.lua +++ b/src/main.lua @@ -4,104 +4,13 @@ love.math.setRandomSeed(os.time()) -- TODO make an object generator that adds all required properties with a default value --- returns selected_object, distance_squared_to_object (if object_list is empty, returns nil, math.huge) -local function get_closest_object(object_list, current_object) - local selected_object, distance_squared_to_object = nil, math.huge - for i = 1, #object_list do - local function compare_object() - local comparison_object = object_list[i] - if comparison_object == current_object then - return - end - local comparison_distance_squared = (current_object.position_x - comparison_object.position_x)^2 + (current_object.position_y - comparison_object.position_y)^2 - if comparison_distance_squared < distance_squared_to_object then - selected_object = comparison_object - distance_squared_to_object = comparison_distance_squared - end - end - compare_object() - end +local finding_objects = require "finding_objects" +local get_closest_object = finding_objects.get_closest_object +local get_closest_orbit = finding_objects.get_closest_orbit - return selected_object, distance_squared_to_object -end - --- returns selected_object, distance_to_orbit (if object_list is empty, returns nil, math.huge) --- object_list must be of objects orbiting the same parent_object or results will be wrong -local function get_closest_orbit(object_list, current_object) - local selected_object, distance_to_orbit = nil, math.huge - for i = 1, #object_list do - local function compare_object() - local comparison_object = object_list[i] - if comparison_object == current_object then - return - end - local comparison_distance = math.abs(current_object.orbital_radius - comparison_object.orbital_radius) - if comparison_distance < distance_to_orbit then - selected_object = comparison_object - distance_to_orbit = comparison_distance - end - end - compare_object() - end - - return selected_object, distance_to_orbit -end - --- source_object and destination_object can be nil to create/delete resources --- returns cargo_amount (actual amount transferred) -local function transfer_cargo(source_object, destination_object, cargo_type, cargo_amount) - if not source_object then - source_object = { cargo_contents = { [cargo_type] = math.huge }, cargo_free_space = 0, } - end - if not destination_object then - destination_object = { cargo_contents = {}, cargo_free_space = math.huge, } - end - - if (not source_object.cargo_contents) or (not source_object.cargo_contents[cargo_type]) then - return 0 - end - if not destination_object.cargo_contents then - destination_object.cargo_contents = {} - destination_object.cargo_free_space = 0 - end - if not destination_object.cargo_contents[cargo_type] then - destination_object.cargo_contents[cargo_type] = 0 - end - - if cargo_amount > source_object.cargo_contents[cargo_type] then - cargo_amount = source_object.cargo_contents[cargo_type] - end - if cargo_amount > destination_object.cargo_free_space then - cargo_amount = destination_object.cargo_free_space - end - - destination_object.cargo_contents[cargo_type] = destination_object.cargo_contents[cargo_type] + cargo_amount - destination_object.cargo_free_space = destination_object.cargo_free_space - cargo_amount - source_object.cargo_contents[cargo_type] = source_object.cargo_contents[cargo_type] - cargo_amount - source_object.cargo_free_space = source_object.cargo_free_space + cargo_amount - - if source_object.cargo_contents[cargo_type] <= 0 then - source_object.cargo_contents[cargo_type] = nil - end - if destination_object.cargo_contents[cargo_type] <= 0 then - destination_object.cargo_contents[cargo_type] = nil - end - - return cargo_amount -end - -local function check_cargo_amount(source_object, cargo_type, cargo_amount) - if not source_object.cargo_contents then - return false - end - if not source_object.cargo_contents[cargo_type] then - return false - end - if source_object.cargo_contents[cargo_type] >= cargo_amount then - return true - end - return false -end +local cargo_handling = require "cargo_handling" +local transfer_cargo = cargo_handling.transfer_cargo +local check_cargo_amount = cargo_handling.check_cargo_amount local ore_types = { "iron ore", "copper ore", "warp fuel", } @@ -161,6 +70,10 @@ local function make_resource_point(x, y) return true end +-- DEBUG hardcoded points to compare against +make_resource_point(15, 0) +make_resource_point(screen_width / 2 - 10, 0) + local function add_resource_points(point_count) for i = 1, point_count do if not make_resource_point() then @@ -169,14 +82,11 @@ local function add_resource_points(point_count) end end add_resource_points(8) --- DEBUG hardcoded points to compare against (except they can still fail to spawn if something else is too close) -make_resource_point(15, 0) -make_resource_point(screen_width / 2 - 10, 0) -- NOTE this object doesn't have "necessary" values because it isn't in resource_points local core_object = { position_x = 0, position_y = 0, orbital_radius = 0, radar_size = 7, rotation_offset = 0, - children = resource_points, -- TEMP all objects are orbitng the core_object + child_objects = resource_points, -- TEMP all objects are orbitng the core_object } local last_message = "" @@ -194,9 +104,9 @@ function love.update(dt) argument = 0 end current_object.position_x, current_object.position_y = parent_x + current_object.orbital_radius * math.cos(argument), parent_y + current_object.orbital_radius * math.sin(argument) - if current_object.children then - for i = 1, #current_object.children do - update_object_position(current_object.children[i], current_object.position_x, current_object.position_y, current_object.radar_size^2) + if current_object.child_objects then + for i = 1, #current_object.child_objects do + update_object_position(current_object.child_objects[i], current_object.position_x, current_object.position_y, current_object.radar_size^2) end end end @@ -309,9 +219,9 @@ function love.draw() draw_object = function(current_object, parent_x, parent_y) love.graphics.circle("fill", current_object.position_x, current_object.position_y, math.max(current_object.radar_size, 0.8)) love.graphics.circle("line", parent_x, parent_y, current_object.orbital_radius) - if current_object.children then - for i = 1, #current_object.children do - draw_object(current_object.children[i], current_object.position_x, current_object.position_y) + if current_object.child_objects then + for i = 1, #current_object.child_objects do + draw_object(current_object.child_objects[i], current_object.position_x, current_object.position_y) end end end