Difference between revisions of "LifeOnLove"

Line 8: Line 8:
 
[[File:lifeonlove.png]]
 
[[File:lifeonlove.png]]
  
'''Very first application on lua and love2d) I found it a little bit metaphoric and maybe it can be handy for someone...'''
+
'''I found it a little bit metaphoric and maybe it can be handy for someone...'''
  
  

Revision as of 14:51, 21 April 2018




lifeonlove.png

I found it a little bit metaphoric and maybe it can be handy for someone...


main.lua

 1 gameObjects = require("gameObjects")
 2 grids = {}
 3 step = 0
 4 time = 0;
 5 
 6 function love.load(arg)
 7   if arg[#arg] == "-debug" then require("mobdebug").start() end
 8   grids[0] = gameObjects.Grid:new(50,35)
 9   grids[1] = gameObjects.Grid:new(50,35)
10   grids[0]:init("gun_s.txt")
11   outGrid = 0
12   inGrid = 0
13   grid_switch()
14 end
15 
16 function love.update(dt)
17   time = time + dt
18   if (time > 0.1) then
19     time = 0
20     grid_switch()
21   end
22 end
23 
24 function love.draw()
25   love.graphics.setColor(0.085,1.0,0,0.1)
26   love.graphics.rectangle("line", 265, 50, 250, 50)
27   love.graphics.rectangle("line", 270, 55, 240, 40)
28   love.graphics.setColor(0.085,1.0,0,1.0)
29   love.graphics.print("LIFE", 375, 70);
30   grids[outGrid]:draw(150, 200, 10)
31 end
32 
33 function grid_switch()
34   inGrid = step % 2
35   outGrid = (step + 1) % 2
36   step = step + 1
37   grids[outGrid]:update_from(grids[inGrid])
38 end

gameObjects.lua

  1 local gameObjects = {}
  2 
  3 -- Meta class
  4 gameObjects.Grid = {w = 0, h = 0}
  5 
  6 -- Derived class method new
  7 function gameObjects.Grid:new (w,h)
  8    local obj = {}
  9    setmetatable(obj, gameObjects.Grid)
 10    gameObjects.Grid.__index = gameObjects.Grid
 11    obj.w = w or 0
 12    obj.h = h or 0
 13    obj.cells = {}
 14    for i=1,w do
 15     obj.cells[i] = {}
 16    end
 17    for i = 1,w do
 18     for j = 1,h do
 19       obj.cells[i][j] = false
 20     end
 21    end
 22    return obj
 23 end
 24 
 25 function gameObjects.Grid:init(filename)
 26   local file = io.open(filename,"r")
 27   local line = file:read()
 28   local j = 1
 29   while line ~= nil and line ~= "" do
 30     for i=1,#line do
 31       local c = line:sub(i,i)
 32       if (c=='1') then
 33         self.cells[i][j] = true
 34       else
 35         self.cells[i][j] = false
 36       end
 37     end
 38     j = j + 1
 39     line = file:read()
 40   end
 41   io.close(file)
 42 end
 43 
 44 function gameObjects.Grid:update_from(grid)
 45   for i=1,self.w do
 46     for j=1,self.h do
 47       self.cells[i][j] = grid:check_cell(i,j)
 48     end
 49   end
 50 end
 51 
 52 function gameObjects.Grid:check_cell(x,y)
 53   local count = 0
 54   local positions = {{x-1,y},{x+1,y},{x,y-1},{x,y+1},{x-1,y+1},{x+1,y+1},{x-1, y-1},{x+1,y-1}}
 55   for i=1,8 do
 56     local ok = false
 57     local pos = positions[i]
 58     ok = self:is_legal(pos)
 59     if (ok) then
 60       ok = self.cells[pos[1]][pos[2]]
 61     end
 62     if (ok) then
 63       count = count + 1
 64     end
 65   end
 66   if (count > 3) then
 67     return false;
 68   end
 69   if (count < 2) then
 70     return false
 71   end
 72   if (count == 2) then
 73     return self.cells[x][y]
 74   end
 75   return true
 76 end
 77 
 78 function gameObjects.Grid:is_legal(pos)
 79   if (pos[1] <= 1 or pos[1] > self.w) then
 80     return false
 81   end
 82   if (pos[2] <= 1 or pos[2] > self.h) then
 83     return false
 84   end
 85   return true
 86 end
 87 
 88 function gameObjects.Grid:draw(x,y, cellsize)
 89   local fillType = "fill"
 90   for i=1,self.w do
 91     for j=1,self.h do
 92       if (self.cells[i][j] == true) then
 93         love.graphics.setColor(0.085,1.0,0,1.0)
 94         fillType = "fill"
 95       else
 96         love.graphics.setColor(0.085,1.0,0,0.1)
 97         fillType = "line"
 98       end
 99 
100       love.graphics.rectangle(fillType, x+(i-1)*cellsize, y+(j-1)*cellsize, cellsize,cellsize)
101     end
102   end
103   love.graphics.setColor(0.085,1.0,0,1.0)
104   love.graphics.rectangle("line", x-10, y-10, self.w * cellsize + 20, self.h * cellsize + 20)
105 end
106 
107 return gameObjects

gun_s.txt

 1 00000000000000000000000000000000000000
 2 00000000000000000000000001000000000000
 3 00000000000000000000000101000000000000
 4 00000000000001100000011000000000000110
 5 00000000000010001000011000000000000110
 6 01100000000100000100011000000000000000
 7 01100000000100010110000101000000000000
 8 00000000000100000100000001000000000000
 9 00000000000010001000000000000000000000
10 00000000000001100000000000000000000000
11 00000000000000000000000000000000000000