formalizing..?

This commit is contained in:
Tangent 2019-10-22 23:25:46 -07:00
parent 6856581804
commit 78ee6923cc

View File

@ -1,57 +1,70 @@
import graphics from love
import cos, sin, pi from math
export t = 0
tau = 2 * math.pi
w, h = graphics.getDimensions!
tiny = require "tiny" tiny = require "tiny"
MapDisplay = tiny.sortedSystem { IDTracker = tiny.system {
compare: (a, b) => filter: tiny.requireAll "id"
a.orbit.hierarchy < b.orbit.hierarchy onAdd: (entity) =>
filter: tiny.requireAll "orbit" @[entity.id] = entity
draw: => onRemove: (entity) =>
graphics.translate w / 2, h / 2 @[entity.id] = nil
for e in *@entities
entity = e.orbit
-- orbital period
-- real: T = 2pi * sqrt(a^3 / G*M)
-- sim: T = 2pi * speed_parameter
entity.x = entity.radius * cos(t / entity.speed_parameter) + entity.offset
entity.y = entity.radius * sin(t / entity.speed_parameter) + entity.offset
if entity.parent
entity.x += entity.parent.x
entity.y += entity.parent.y
graphics.circle "fill", entity.x, entity.y, 5
} }
world = tiny.world MapDisplay Time = tiny.processingSystem {
filter: tiny.requireAll "time"
process: (entity, dt) =>
entity.time += dt
}
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
}
-- TEMP demo -- TEMP demo
sun = { sun = {
id: 0
x: 0, x: 0,
y: 0, y: 0
orbit: { hierarchy: 0, radius: 0, offset: 0, speed_parameter: 1 }
} }
planet = { planet = {
id: 1
orbit: { orbit: {
hierarchy: 1 hierarchy: 1
radius: 20 radius: 20
parent: sun parent_id: 0
offset: love.math.random! offset: love.math.random!
speed_parameter: 0.5 speed_parameter: 0.5
} }
} }
game = { time: 0 }
world\add planet, sun world = tiny.world MapDisplay, IDTracker, planet, sun, Time, game
love.update = (dt) -> love.update = (dt) ->
t += dt
world\update dt world\update dt
love.draw = -> love.draw = ->
MapDisplay\draw! MapDisplay\draw game.time
love.keypressed = (key) -> love.keypressed = (key) ->
if key == "escape" love.event.quit! if key == "escape" love.event.quit!