Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
d5ee0f3a51 | ||
|
28cb7c13fc |
@ -1,61 +0,0 @@
|
||||
local class = require "lib.middleclass"
|
||||
local atan2 = math.atan2
|
||||
local sin = math.sin
|
||||
local cos = math.cos
|
||||
local pi = math.pi
|
||||
local lg = love.graphics
|
||||
|
||||
local Element = require "Element"
|
||||
|
||||
local Double = class("Double")
|
||||
|
||||
Double.static.threshold = 0.06
|
||||
Double.static.maxCount = 500 --5000
|
||||
Double.static.generated = 0
|
||||
Double.static.count = 0
|
||||
|
||||
function Double:initialize(p1, p2)
|
||||
--if Double.static.count >= Double.static.maxCount then return nil end -- cancel over-generation
|
||||
|
||||
self.x = (p1.x + p2.x) / 2
|
||||
self.y = (p1.y + p2.y) / 2
|
||||
self.vx = (p1.vx + p2.vx) / 2
|
||||
self.vy = (p1.vy + p2.vy) / 2
|
||||
|
||||
--self.type = ? NO TYPES (or type = p1.type + p2.type)
|
||||
local dx = p2.x - p1.x
|
||||
local dy = p2.y - p1.y
|
||||
self.angle = atan2(dy, dx)
|
||||
|
||||
self.energy = p1.lifetime + p2.lifetime
|
||||
|
||||
Double.static.generated = Double.static.generated + 1
|
||||
Double.static.count = Double.static.count + 1
|
||||
end
|
||||
|
||||
function Double:update(dt)
|
||||
-- if energy too high / too low, make an Element
|
||||
if (self.energy >= 10) or (self.energy <= -20) then
|
||||
return Element(self)
|
||||
end
|
||||
|
||||
self.energy = self.energy - dt/10
|
||||
|
||||
self.x = self.x + (self.vx * dt)
|
||||
self.y = self.y + (self.vy * dt)
|
||||
self.angle = self.angle + (self.energy * dt)
|
||||
end
|
||||
|
||||
function Double:draw()
|
||||
lg.setColor(255, 55, 55, 250)
|
||||
|
||||
local x = self.x + (1 * cos(self.angle))
|
||||
local y = self.y + (1 * sin(self.angle))
|
||||
lg.point(x + lg.getWidth()/2, y + lg.getHeight()/2)
|
||||
|
||||
local x = self.x + (1 * cos(self.angle + pi))
|
||||
local y = self.y + (1 * sin(self.angle + pi))
|
||||
lg.point(x + lg.getWidth()/2, y + lg.getHeight()/2)
|
||||
end
|
||||
|
||||
return Double
|
@ -1,13 +0,0 @@
|
||||
local class = require "lib.middleclass"
|
||||
|
||||
local Element = class("Element")
|
||||
|
||||
Element.static.maxCount = 100 --1000
|
||||
Element.static.generated = 0
|
||||
Element.static.count = 0
|
||||
|
||||
function Element:initialize(double)
|
||||
-- DO SHIT BASED ON POSITIVE OR NEGATIVE
|
||||
end
|
||||
|
||||
return Element
|
@ -1,71 +1,50 @@
|
||||
local class = require "lib.middleclass"
|
||||
local random = math.random --TODO change out as needed later
|
||||
local floor = math.floor
|
||||
local pi2 = math.pi * 2
|
||||
local sin = math.sin
|
||||
local cos = math.cos
|
||||
local sqrt = math.sqrt
|
||||
local lg = love.graphics
|
||||
|
||||
local Particle = class("Particle")
|
||||
|
||||
local lifetimeRandomizer = love.math.newRandomGenerator(13) -- seeded instead of random, all types are set
|
||||
|
||||
Particle.static.maxLifetime = 10 --NOTE can only used fixed "randomization" as long as maxLifetime is constant (because universe is random)
|
||||
Particle.static.lifetimes = {}
|
||||
Particle.static.maxCount = 1000 --10000
|
||||
Particle.static.generated = 0
|
||||
Particle.static.count = 0
|
||||
local lifetimeRandomizer = love.math.newRandomGenerator(13--[[os.time()]])
|
||||
local maxLifetime = 10 --NOTE can only used fixed "randomization" as long as maxLifetime is constant ?
|
||||
local lifetimes = {}
|
||||
|
||||
function Particle:initialize(radius, expansionRate)
|
||||
--if Particle.static.count >= Particle.static.maxCount then return nil end -- cancel over-generation of Particles
|
||||
--NOTE radius is used as a box, not an actual radius...
|
||||
--TODO FIX THIS IMMEDIATEITGJE
|
||||
|
||||
-- this is because radius < 1 means that random() can't be used
|
||||
if not (radius > 1) then
|
||||
radius = 1
|
||||
self.x = 0
|
||||
self.y = 0
|
||||
else
|
||||
local r = random(radius)
|
||||
local d = random() * pi2
|
||||
self.x = r * cos(d)
|
||||
self.y = r * sin(d)
|
||||
end
|
||||
self.vx = random(-expansionRate, expansionRate)
|
||||
if not (radius > 1) then radius = 1 end -- stop randomization errors
|
||||
|
||||
self.x = random(-radius, radius)
|
||||
self.y = random(-radius, radius)
|
||||
self.vx = random(-expansionRate, expansionRate) --TODO make max velocity change based on radius??
|
||||
self.vy = random(-expansionRate, expansionRate)
|
||||
|
||||
self.type = floor(random(radius)) -- number of types should increase over time
|
||||
if self.type > #Particle.static.lifetimes then
|
||||
for i = #Particle.static.lifetimes, self.type do
|
||||
Particle.static.lifetimes[i] = lifetimeRandomizer:random(Particle.static.maxLifetime)
|
||||
if self.type > #lifetimes then
|
||||
for i = #lifetimes, self.type do
|
||||
lifetimes[i] = lifetimeRandomizer:random(maxLifetime)
|
||||
end
|
||||
end
|
||||
self.lifetime = Particle.static.lifetimes[self.type]
|
||||
self.lifetime = lifetimes[self.type]
|
||||
|
||||
Particle.static.generated = Particle.static.generated + 1
|
||||
Particle.static.count = Particle.static.count + 1
|
||||
--[[ debug
|
||||
for k,v in pairs(lifetimes) do
|
||||
print(k,v)
|
||||
end
|
||||
print(self.lifetime)
|
||||
]]
|
||||
end
|
||||
|
||||
function Particle:update(dt)
|
||||
self.lifetime = self.lifetime - dt
|
||||
if self.lifetime <= 0 then
|
||||
Particle.static.count = Particle.static.count - 1
|
||||
return true -- true means we need to be deleted
|
||||
end
|
||||
|
||||
self.x = self.x + (self.vx * dt)
|
||||
self.y = self.y + (self.vy * dt)
|
||||
--TODO shouldn't this update its life and set self nil here??
|
||||
end
|
||||
|
||||
function Particle:draw()
|
||||
lg.setColor(255, 255, 255, 200)
|
||||
lg.point(self.x + lg.getWidth()/2, self.y + lg.getHeight()/2)
|
||||
end
|
||||
|
||||
function Particle:distanceTo(particle)
|
||||
local dx = particle.x - self.x
|
||||
local dy = particle.y - self.y
|
||||
return sqrt(dx*dx + dy*dy)
|
||||
--lg.circle("fill", self.x + lg.getWidth()/2, self.y + lg.getHeight()/2, 1)
|
||||
end
|
||||
|
||||
return Particle
|
||||
|
@ -1,64 +1,37 @@
|
||||
local class = require "lib.middleclass"
|
||||
local random = math.random --TODO change out as needed later
|
||||
local floor = math.floor
|
||||
local lg = love.graphics
|
||||
|
||||
local Particle = require "Particle"
|
||||
local Double = require "Double"
|
||||
local Element = require "Element"
|
||||
|
||||
local Universe = class("Universe")
|
||||
|
||||
function Universe:initialize(expansionRate, maxRadius)
|
||||
function Universe:initialize(expansionRate)
|
||||
self.expansionRate = expansionRate or 1
|
||||
self.maxRadius = maxRadius or 500
|
||||
self.particles = {}
|
||||
self.doubles = {}
|
||||
self.elements = {}
|
||||
self.radius = 0
|
||||
self.radius = 0 --2
|
||||
self.time = {
|
||||
nextGeneration = random()
|
||||
}
|
||||
end
|
||||
|
||||
function Universe:update(dt)
|
||||
if self.radius <= self.maxRadius then
|
||||
self.radius = self.radius + (self.expansionRate * dt)
|
||||
end
|
||||
self.radius = self.radius + (self.expansionRate * dt)
|
||||
|
||||
self.time.nextGeneration = self.time.nextGeneration - (self.expansionRate * dt)
|
||||
if self.time.nextGeneration <= 0 then
|
||||
if Particle.static.count < Particle.static.maxCount then
|
||||
for i = 1, floor(self.radius) do
|
||||
local particle = Particle(self.radius, self.expansionRate)
|
||||
self.particles[particle] = particle
|
||||
end
|
||||
end
|
||||
while self.time.nextGeneration <= 0 do
|
||||
--TODO make amount spawned modified by radius ?
|
||||
local particle = Particle(self.radius, self.expansionRate)
|
||||
self.particles[particle] = particle
|
||||
self.time.nextGeneration = self.time.nextGeneration + random()
|
||||
end
|
||||
|
||||
for key, particle in pairs(self.particles) do
|
||||
if Double.static.count < Double.static.maxCount then
|
||||
for key2, particle2 in pairs(self.particles) do
|
||||
if (particle:distanceTo(particle2) <= Double.static.threshold) and (particle ~= particle2) then
|
||||
-- make a double!
|
||||
local double = Double(particle, particle2)
|
||||
self.doubles[double] = double
|
||||
self.particles[key] = nil
|
||||
self.particles[key2] = nil
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if particle and particle:update(dt) then self.particles[key] = nil end
|
||||
end
|
||||
|
||||
for key, double in pairs(self.doubles) do
|
||||
local result = double:update(dt)
|
||||
if result then
|
||||
self.doubles[key] = nil
|
||||
self.elements[result] = result
|
||||
--print(key, particle) --debug
|
||||
particle.lifetime = particle.lifetime - dt
|
||||
if particle.lifetime <= 0 then
|
||||
self.particles[key] = nil
|
||||
else
|
||||
particle:update(dt)
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -68,16 +41,7 @@ function Universe:draw()
|
||||
particle:draw()
|
||||
end
|
||||
|
||||
for _, double in pairs(self.doubles) do
|
||||
double:draw()
|
||||
end
|
||||
|
||||
lg.setColor(255, 255, 255, 50)
|
||||
lg.circle("line", lg.getWidth()/2, lg.getHeight()/2, self.radius)
|
||||
lg.setColor(255, 255, 255, 255)
|
||||
lg.print("Particles: " .. Particle.static.count .. "/" .. Particle.static.generated, 2, 2)
|
||||
lg.print("Doubles: " .. Double.static.count .. "/" .. Double.static.generated, 2, 14)
|
||||
lg.print("Elements: " .. Element.static.count .. "/" .. Element.static.generated, 2, 26)
|
||||
end
|
||||
|
||||
return Universe
|
||||
|
@ -1,11 +1,10 @@
|
||||
local lg = love.graphics
|
||||
local lm = love.math
|
||||
--NOTE math.random seed has not been set, this means every run should be the same (except for potential hardware differences)
|
||||
|
||||
local Universe = require "Universe"
|
||||
--local Particle = require "Particle"
|
||||
|
||||
local universe = Universe(10, 300)
|
||||
local universe = Universe()
|
||||
|
||||
function love.update(dt)
|
||||
universe:update(dt)
|
||||
|
Loading…
Reference in New Issue
Block a user