Wiki

Clone wiki

Core / LimgCode

Main

--living images
function setup()
    it = {} -- image table
    ni = 20 -- number images/knights who say ni
    ft = {} -- food table
    nf = 10 -- number food
    scl = 10
    local i
    for i = 1, ni do
        local x = (math.random(WIDTH - (WIDTH/10)) + WIDTH/20) / scl
        local y = (math.random(HEIGHT - (HEIGHT/10)) + HEIGHT/20) / scl
        local h = 4
        local w = 4
        local r = math.random(256)-1
        it[i] = Lim(x,y,w,h,r,i)
        it[i]:rand()  
    end
    for i = 1, nf do
        ft[i] = Food()
    end
    --f = Food()
end

function draw()
    background(0, 0, 0)
    scale(scl)
    noSmooth()
    local i,j,fv
    for i = 1, ni do
        it[i]:draw()
        it[i]:move()
        for j = 1, nf do
            fv = vec2(ft[j].x,ft[j].y)
            if fv:dist(vec2(it[i].x,it[i].y)) < it[i].w + 1 then
                eat(it[i],ft[j])
            end
        end
        --local fv = vec2(f.x,f.y)
        --if fv:dist(vec2(it[i].x,it[i].y)) < 3 then
            --eat(it[i])
        --end
    end
    for i = 1, nf do
        ft[i]:draw()
    end
    --f:draw()
end

function eat(e,f)
    f:rand()
    local d = oldest()
    e:divide(d)
end

function oldest()
    local i
    local o = 0 --oldest
    local h = 0 --highest
    for i = 1, ni do
        if it[i].a > h then
            h = it[i].a
            o = i
        end
    end
    return it[o] 
end

Lim (Living Image) class

#lua
Lim = class()

function Lim:init(x,y,w,h,r,a)
    self.x = x
    self.y = y
    self.w = w
    self.h = h
    self.r = r -- rotation
    self.a = a -- age
    self.im = image(w,h)
end

function Lim:rand()
    local i, j
    for i = 1, self.w do
        for j = 1, self.h do
            local r = math.random(256)-1
            local g = math.random(256)-1
            local b = math.random(256)-1
            local a = math.random(256)-1
            self.im:set(i,j,r,g,b,a)
        end
    end   
end

function Lim:draw()
    pushMatrix()
    translate(self.x,self.y)
    rotate((self.r/255)*360)
    translate(-self.x,-self.y)
    spriteMode(CORNER)
    sprite(self.im,self.x,self.y)
    strokeWidth(1)
    stroke(127, 127, 127, 255)
    noFill()
    rectMode(CORNER)
    rect(self.x-1,self.y-1,self.w+2-(1/scl),self.h+2)
    --rect(self.x-1,self.y-1,self.w+2,self.h+2)
    popMatrix()
end

function Lim:move()
    self.a = self.a + 1
    local i, j, r, g, b, a, v
    for i = 1, self.w do
        for j = 1, self.h do
            r, g, b, a = self.im:get(i,j)
            if b > 127 then          
                v = vec2(a/25,0)
                v = v:rotate(math.rad((self.r/255)*360))
                self.x = self.x + (v.x/100)
                self.y = self.y + (v.y/100)
            else
                self.r = self.r + (a-127)/128
                if self.r > 255 then self.r = self.r - 255 end
            end
        end
    end   
    local bdr = 3
    if self.x < -(bdr) then self.x = (WIDTH/scl) + (bdr) end
    if self.x > (WIDTH/scl) + (bdr) then self.x = 0 - (bdr) end 
    if self.y < -(bdr) then self.y = (HEIGHT/scl) + (bdr)end
    if self.y > (HEIGHT/scl) + (bdr) then self.y = 0 - (bdr) end
end

function Lim:divide(d)
    local gd = 1 --genentic drift
    self.a = 0
    d.a = 0
    d.x = self.x
    d.y = self.y
    d.r = self.r
    local i, j, r, g, b, a
    for i = 1, self.w do
        for j = 1, self.h do
            r,g,b,a = self.im:get(i,j)
            r = math.fmod(r+(math.random((gd*2)+1) - gd -1),256)
            g = math.fmod(g+(math.random((gd*2)+1) - gd -1),256)
            b = math.fmod(b+(math.random((gd*2)+1) - gd -1),256)
            a = math.fmod(a+(math.random((gd*2)+1) - gd -1),256)
            d.im:set(i,j,r,g,b,a)
            r,g,b,a = self.im:get(i,j)
            r = math.fmod(r+(math.random((gd*2)+1) - gd -1),256)
            g = math.fmod(g+(math.random((gd*2)+1) - gd -1),256)
            b = math.fmod(b+(math.random((gd*2)+1) - gd -1),256)
            a = math.fmod(a+(math.random((gd*2)+1) - gd -1),256)
            self.im:set(i,j,r,g,b,a)
        end
    end   
end

Food Class

#lua

Food = class()

function Food:init(x,y)
    self.x = x
    self.y = y
    if self.x == nil or self.y == nil then
        self:rand()
    end
end

function Food:draw()
    pushMatrix()
    strokeWidth(1)
    stroke(0, 255, 0, 255)
    noFill()
    ellipse(self.x,self.y,3,3)
    popMatrix()
end

function Food:rand()
    self.x = (math.random(WIDTH - (WIDTH/10)) + WIDTH/20) / scl
    self.y = (math.random(HEIGHT - (HEIGHT/10)) + HEIGHT/20) / scl
end

Updated