From e5773e7d45d723e312fe8bc3501f98f6668c3863 Mon Sep 17 00:00:00 2001 From: Tangent Date: Wed, 23 Oct 2019 00:15:24 -0700 Subject: [PATCH] breaking up into files --- src/main.moon | 86 +++++++++++++------------------------ src/systems/IDTracker.moon | 11 +++++ src/systems/MapDisplay.moon | 33 ++++++++++++++ src/systems/Time.moon | 9 ++++ 4 files changed, 84 insertions(+), 55 deletions(-) create mode 100644 src/systems/IDTracker.moon create mode 100644 src/systems/MapDisplay.moon create mode 100644 src/systems/Time.moon diff --git a/src/main.moon b/src/main.moon index c18f21e..7e4dc6b 100644 --- a/src/main.moon +++ b/src/main.moon @@ -1,70 +1,46 @@ tiny = require "tiny" -IDTracker = tiny.system { - filter: tiny.requireAll "id" - onAdd: (entity) => - @[entity.id] = entity - onRemove: (entity) => - @[entity.id] = nil -} +systems = {} +for name in *love.filesystem.getDirectoryItems "systems" + if ".lua" == name\sub -4 + name = name\sub 1, -5 + systems[#systems + 1] = require "systems/#{name}" + systems[name] = systems[#systems] -Time = tiny.processingSystem { - filter: tiny.requireAll "time" - process: (entity, dt) => - entity.time += dt +game = { + time: 0 + next_id: 0 } +makeEntity = (tab) -> + tab.id = game.next_id + game.next_id += 1 + return tab -import graphics from love -import cos, sin from math -w, h = graphics.getDimensions! -MapDisplay = tiny.sortedSystem { - compare: (a, b) => - if a.orbit and b.orbit - a.orbit.hierarchy <= b.orbit.hierarchy - else - true - filter: tiny.requireAny "orbit", tiny.requireAll("x", "y") - draw: (t) => - graphics.translate w / 2, h / 2 - for entity in *@entities - if orbit = entity.orbit - orbit = entity.orbit - -- orbital period - -- real: T = 2pi * sqrt(a^3 / G*M) - -- sim: T = 2pi * speed_parameter - entity.x = orbit.radius * cos(t / orbit.speed_parameter) + orbit.offset - entity.y = orbit.radius * sin(t / orbit.speed_parameter) + orbit.offset - if orbit.parent_id - parent = IDTracker[orbit.parent_id] - entity.x += parent.x - entity.y += parent.y - graphics.circle "fill", entity.x, entity.y, 5 -- TEMP radius -} +world = tiny.world game, unpack systems --- TEMP demo -sun = { - id: 0 - x: 0, - y: 0 -} -planet = { - id: 1 - orbit: { - hierarchy: 1 - radius: 20 - parent_id: 0 - offset: love.math.random! - speed_parameter: 0.5 +system = -> + sun = makeEntity { + x: 0, + y: 0 } -} -game = { time: 0 } -world = tiny.world MapDisplay, IDTracker, planet, sun, Time, game + planet = makeEntity { + orbit: { + hierarchy: 1 + radius: 20 + parent_id: sun.id + offset: love.math.random! + speed_parameter: 0.5 + } + } + world\add sun, planet + +system! love.update = (dt) -> world\update dt love.draw = -> - MapDisplay\draw game.time + systems.MapDisplay\draw game.time love.keypressed = (key) -> if key == "escape" love.event.quit! diff --git a/src/systems/IDTracker.moon b/src/systems/IDTracker.moon new file mode 100644 index 0000000..62f6187 --- /dev/null +++ b/src/systems/IDTracker.moon @@ -0,0 +1,11 @@ +tiny = require "tiny" + +IDTracker = tiny.system { + filter: tiny.requireAll "id" + onAdd: (entity) => + @[entity.id] = entity + onRemove: (entity) => + @[entity.id] = nil +} + +return IDTracker diff --git a/src/systems/MapDisplay.moon b/src/systems/MapDisplay.moon new file mode 100644 index 0000000..fbbb080 --- /dev/null +++ b/src/systems/MapDisplay.moon @@ -0,0 +1,33 @@ +import graphics from love +import cos, sin from math + +w, h = graphics.getDimensions! + +tiny = require "tiny" +IDTracker = require "systems/IDTracker" + +MapDisplay = tiny.sortedSystem { + compare: (a, b) => + if a.orbit and b.orbit + a.orbit.hierarchy <= b.orbit.hierarchy + else + true + filter: tiny.requireAny "orbit", tiny.requireAll("x", "y") + draw: (t) => + graphics.translate w / 2, h / 2 + for entity in *@entities + if orbit = entity.orbit + orbit = entity.orbit + -- orbital period + -- real: T = 2pi * sqrt(a^3 / G*M) + -- sim: T = 2pi * speed_parameter + entity.x = orbit.radius * cos(t / orbit.speed_parameter) + orbit.offset + entity.y = orbit.radius * sin(t / orbit.speed_parameter) + orbit.offset + if orbit.parent_id + parent = IDTracker[orbit.parent_id] + entity.x += parent.x + entity.y += parent.y + graphics.circle "fill", entity.x, entity.y, 5 -- TEMP radius +} + +return MapDisplay diff --git a/src/systems/Time.moon b/src/systems/Time.moon new file mode 100644 index 0000000..7abf5af --- /dev/null +++ b/src/systems/Time.moon @@ -0,0 +1,9 @@ +tiny = require "tiny" + +Time = tiny.processingSystem { + filter: tiny.requireAll "time" + process: (entity, dt) => + entity.time += dt +} + +return Time