breaking up into files
This commit is contained in:
parent
78ee6923cc
commit
e5773e7d45
@ -1,70 +1,46 @@
|
|||||||
tiny = require "tiny"
|
tiny = require "tiny"
|
||||||
|
|
||||||
IDTracker = tiny.system {
|
systems = {}
|
||||||
filter: tiny.requireAll "id"
|
for name in *love.filesystem.getDirectoryItems "systems"
|
||||||
onAdd: (entity) =>
|
if ".lua" == name\sub -4
|
||||||
@[entity.id] = entity
|
name = name\sub 1, -5
|
||||||
onRemove: (entity) =>
|
systems[#systems + 1] = require "systems/#{name}"
|
||||||
@[entity.id] = nil
|
systems[name] = systems[#systems]
|
||||||
}
|
|
||||||
|
|
||||||
Time = tiny.processingSystem {
|
game = {
|
||||||
filter: tiny.requireAll "time"
|
time: 0
|
||||||
process: (entity, dt) =>
|
next_id: 0
|
||||||
entity.time += dt
|
|
||||||
}
|
}
|
||||||
|
makeEntity = (tab) ->
|
||||||
|
tab.id = game.next_id
|
||||||
|
game.next_id += 1
|
||||||
|
return tab
|
||||||
|
|
||||||
import graphics from love
|
world = tiny.world game, unpack systems
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
-- TEMP demo
|
system = ->
|
||||||
sun = {
|
sun = makeEntity {
|
||||||
id: 0
|
x: 0,
|
||||||
x: 0,
|
y: 0
|
||||||
y: 0
|
|
||||||
}
|
|
||||||
planet = {
|
|
||||||
id: 1
|
|
||||||
orbit: {
|
|
||||||
hierarchy: 1
|
|
||||||
radius: 20
|
|
||||||
parent_id: 0
|
|
||||||
offset: love.math.random!
|
|
||||||
speed_parameter: 0.5
|
|
||||||
}
|
}
|
||||||
}
|
planet = makeEntity {
|
||||||
game = { time: 0 }
|
orbit: {
|
||||||
world = tiny.world MapDisplay, IDTracker, planet, sun, Time, game
|
hierarchy: 1
|
||||||
|
radius: 20
|
||||||
|
parent_id: sun.id
|
||||||
|
offset: love.math.random!
|
||||||
|
speed_parameter: 0.5
|
||||||
|
}
|
||||||
|
}
|
||||||
|
world\add sun, planet
|
||||||
|
|
||||||
|
system!
|
||||||
|
|
||||||
love.update = (dt) ->
|
love.update = (dt) ->
|
||||||
world\update dt
|
world\update dt
|
||||||
|
|
||||||
love.draw = ->
|
love.draw = ->
|
||||||
MapDisplay\draw game.time
|
systems.MapDisplay\draw game.time
|
||||||
|
|
||||||
love.keypressed = (key) ->
|
love.keypressed = (key) ->
|
||||||
if key == "escape" love.event.quit!
|
if key == "escape" love.event.quit!
|
||||||
|
11
src/systems/IDTracker.moon
Normal file
11
src/systems/IDTracker.moon
Normal 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
|
33
src/systems/MapDisplay.moon
Normal file
33
src/systems/MapDisplay.moon
Normal 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
9
src/systems/Time.moon
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
tiny = require "tiny"
|
||||||
|
|
||||||
|
Time = tiny.processingSystem {
|
||||||
|
filter: tiny.requireAll "time"
|
||||||
|
process: (entity, dt) =>
|
||||||
|
entity.time += dt
|
||||||
|
}
|
||||||
|
|
||||||
|
return Time
|
Loading…
Reference in New Issue
Block a user