getting ready for maybe elements, also runs too slowly now

This commit is contained in:
Paul Liverman 2015-11-23 23:09:00 -08:00
parent 5da68831a9
commit 1a1183a854
5 changed files with 86 additions and 13 deletions

View File

@ -1,12 +1,22 @@
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
@ -18,18 +28,34 @@ function Double:initialize(p1, p2)
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)
--TODO check if destroy or do something special
-- 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)
lg.point(self.x + lg.getWidth()/2, self.y + lg.getHeight()/2)
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

13
src/Element.lua Normal file
View File

@ -0,0 +1,13 @@
local class = require "lib.middleclass"
local Element = class("Element")
Double.static.maxCount = 100 --1000
Double.static.generated = 0
Double.static.count = 0
function Element:initialize(double)
-- DO SHIT BASED ON POSITIVE OR NEGATIVE
end
return Element

View File

@ -4,6 +4,7 @@ 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")
@ -12,12 +13,12 @@ local lifetimeRandomizer = love.math.newRandomGenerator(13) -- seeded instead of
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.maxParticles = 10000
Particle.static.maxCount = 1000 --10000
Particle.static.generated = 0
Particle.static.count = 0
function Particle:initialize(radius, expansionRate)
if Particle.static.count >= Particle.static.maxParticles then return nil end -- cancel over-generation of Particles
--if Particle.static.count >= Particle.static.maxCount then return nil end -- cancel over-generation of Particles
-- this is because radius < 1 means that random() can't be used
if not (radius > 1) then
@ -61,4 +62,10 @@ function Particle:draw()
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)
end
return Particle

View File

@ -5,13 +5,16 @@ local lg = love.graphics
local Particle = require "Particle"
local Double = require "Double"
local Element = require "Element"
local Universe = class("Universe")
function Universe:initialize(expansionRate)
function Universe:initialize(expansionRate, maxRadius)
self.expansionRate = expansionRate or 1
self.maxRadius = maxRadius or 500
self.particles = {}
self.doubles = {}
self.elements = {}
self.radius = 0
self.time = {
nextGeneration = random()
@ -19,22 +22,44 @@ function Universe:initialize(expansionRate)
end
function Universe:update(dt)
self.radius = self.radius + (self.expansionRate * dt)
if self.radius <= self.maxRadius then
self.radius = self.radius + (self.expansionRate * dt)
end
self.time.nextGeneration = self.time.nextGeneration - (self.expansionRate * dt)
while self.time.nextGeneration <= 0 do
for i = 1, floor(self.radius) do
local particle = Particle(self.radius, self.expansionRate)
self.particles[particle] = particle
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
self.time.nextGeneration = self.time.nextGeneration + random()
end
for key, particle in pairs(self.particles) do
if particle:update(dt) then self.particles[key] = nil end
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
if double:update(dt) then self.doubles[key] = nil end
local result = double:update(dt)
if result then
self.doubles[key] = nil
self.elements[result] = result
end
end
end
@ -42,6 +67,7 @@ function Universe:draw()
for _, particle in pairs(self.particles) do
particle:draw()
end
for _, double in pairs(self.doubles) do
double:draw()
end
@ -51,6 +77,7 @@ function Universe:draw()
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

View File

@ -5,7 +5,7 @@ local lm = love.math
local Universe = require "Universe"
--local Particle = require "Particle"
local universe = Universe()
local universe = Universe(10, 300)
function love.update(dt)
universe:update(dt)