breaking up into files

This commit is contained in:
Tangent 2019-10-23 00:15:24 -07:00
parent 78ee6923cc
commit e5773e7d45
4 changed files with 84 additions and 55 deletions

View File

@ -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
system = ->
sun = makeEntity {
x: 0,
y: 0
}
planet = {
id: 1
}
planet = makeEntity {
orbit: {
hierarchy: 1
radius: 20
parent_id: 0
parent_id: sun.id
offset: love.math.random!
speed_parameter: 0.5
}
}
game = { time: 0 }
world = tiny.world MapDisplay, IDTracker, planet, sun, Time, game
}
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!

View File

@ -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

View File

@ -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

9
src/systems/Time.moon Normal file
View File

@ -0,0 +1,9 @@
tiny = require "tiny"
Time = tiny.processingSystem {
filter: tiny.requireAll "time"
process: (entity, dt) =>
entity.time += dt
}
return Time