getting ready for doubles

This commit is contained in:
Paul Liverman 2015-11-23 20:16:59 -08:00
parent e2881ed10a
commit 5da68831a9
5 changed files with 92 additions and 33 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 161 KiB

35
src/Double.lua Normal file
View File

@ -0,0 +1,35 @@
local class = require "lib.middleclass"
local atan2 = math.atan2
local Double = class("Double")
Double.static.generated = 0
Double.static.count = 0
function Double:initialize(p1, p2)
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
end
function Double:update(dt)
--TODO check if destroy or do something special
self.x = self.x + (self.vx * dt)
self.y = self.y + (self.vy * dt)
end
function Double:draw()
lg.setColor(255, 55, 55, 250)
lg.point(self.x + lg.getWidth()/2, self.y + lg.getHeight()/2)
end
return Double

View File

@ -1,50 +1,64 @@
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 lg = love.graphics
local Particle = class("Particle")
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 = {}
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.maxParticles = 10000
Particle.static.generated = 0
Particle.static.count = 0
function Particle:initialize(radius, expansionRate)
--NOTE radius is used as a box, not an actual radius...
--TODO FIX THIS IMMEDIATEITGJE
if Particle.static.count >= Particle.static.maxParticles then return nil end -- cancel over-generation of Particles
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??
-- 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)
self.vy = random(-expansionRate, expansionRate)
self.type = floor(random(radius)) -- number of types should increase over time
if self.type > #lifetimes then
for i = #lifetimes, self.type do
lifetimes[i] = lifetimeRandomizer:random(maxLifetime)
if self.type > #Particle.static.lifetimes then
for i = #Particle.static.lifetimes, self.type do
Particle.static.lifetimes[i] = lifetimeRandomizer:random(Particle.static.maxLifetime)
end
end
self.lifetime = lifetimes[self.type]
self.lifetime = Particle.static.lifetimes[self.type]
--[[ debug
for k,v in pairs(lifetimes) do
print(k,v)
end
print(self.lifetime)
]]
Particle.static.generated = Particle.static.generated + 1
Particle.static.count = Particle.static.count + 1
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)
--lg.circle("fill", self.x + lg.getWidth()/2, self.y + lg.getHeight()/2, 1)
end
return Particle

View File

@ -1,14 +1,18 @@
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 Universe = class("Universe")
function Universe:initialize(expansionRate)
self.expansionRate = expansionRate or 1
self.particles = {}
self.radius = 0 --2
self.doubles = {}
self.radius = 0
self.time = {
nextGeneration = random()
}
@ -19,20 +23,18 @@ function Universe:update(dt)
self.time.nextGeneration = self.time.nextGeneration - (self.expansionRate * dt)
while self.time.nextGeneration <= 0 do
--TODO make amount spawned modified by radius ?
local particle = Particle(self.radius, self.expansionRate)
self.particles[particle] = particle
for i = 1, floor(self.radius) do
local particle = Particle(self.radius, self.expansionRate)
self.particles[particle] = particle
end
self.time.nextGeneration = self.time.nextGeneration + random()
end
for key, particle in pairs(self.particles) do
--print(key, particle) --debug
particle.lifetime = particle.lifetime - dt
if particle.lifetime <= 0 then
self.particles[key] = nil
else
particle:update(dt)
end
if 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
end
end
@ -40,8 +42,15 @@ function Universe:draw()
for _, particle in pairs(self.particles) do
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)
end
return Universe

View File

@ -1,5 +1,6 @@
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"