Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
piXelicidio committed Apr 1, 2018
2 parents e950a25 + 43c2e5a commit e977857
Show file tree
Hide file tree
Showing 15 changed files with 129 additions and 22 deletions.
11 changes: 6 additions & 5 deletions code/camview.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@ local cam = {}

cam.translation = {x=0, y=0}
cam.zoomOrigin = {x = 0, y = 0}
cam.scale ={x = 1, y = 1}
cam.scale ={x = 1, y = 1} -- this is the scale of world
cam.contentScale = 1 -- this is the scale of the app due to screen change in resolution

function cam.screenToWorld(x, y)
return
( (x - cam.translation.x ) /cam.scale.x),
( (y - cam.translation.y ) /cam.scale.y)
( (x - cam.translation.x ) /cam.scale.x / cam.contentScale ),
( (y - cam.translation.y ) /cam.scale.y / cam.contentScale )
end

function cam.screenToGrid(x, y)
return
math.floor( ( (x - cam.translation.x ) /cam.scale.x ) / cfg.mapGridSize ),
math.floor( ( (y - cam.translation.y ) /cam.scale.y ) / cfg.mapGridSize )
math.floor( ( (x - cam.translation.x ) /cam.scale.x / cam.contentScale ) / cfg.mapGridSize ),
math.floor( ( (y - cam.translation.y ) /cam.scale.y / cam.contentScale ) / cfg.mapGridSize )
end

-- zoom in out the world
Expand Down
87 changes: 83 additions & 4 deletions code/cell.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,26 @@ TCell.cavesStorage = {}
local imgCave = {}
local imgFood = {}
local imgGrass = {}
local imgPortals = {}

local grass = {} --singleton class instance

--- cell base classs
--- cell base abstract classs
function TCell.newCell()
local cell={}
--public properties
cell.type = 'obstacle'
cell.pass = false
cell.color = cfg.colorObstacle
cell.posi = {0,0} --world position

function cell.affectAnt( ant )
--subclasses should implement this
end

function cell.draw(x, y)
apiG.draw(cell.img, x, y, 0, cfg.imgScale, cfg.imgScale)
end

return cell
end
Expand All @@ -31,12 +40,28 @@ function TCell.init()
imgFood[0] = apiG.newImage('images//food01.png')
imgGrass = apiG.newImage('images//grass01.png')

imgPortals.blue = {
apiG.newImage('images/portalBlue_00.png'),
apiG.newImage('images/portalBlue_01.png'),
apiG.newImage('images/portalBlue_02.png')
}

imgPortals.orange = {
apiG.newImage('images/portalOrange_00.png'),
apiG.newImage('images/portalOrange_01.png'),
apiG.newImage('images/portalOrange_02.png')
}


-- singletons
grass = TCell.newCell()
grass.type = 'grass'
grass.pass = true
grass.img = imgGrass
grass.friction = 0.8
function grass.affectAnt( ant )
ant.friction = grass.friction
end
end

--- child class food
Expand Down Expand Up @@ -91,12 +116,66 @@ end

--- child class grass - singleton
function TCell.newGrass()
function grass.affectAnt( ant )
ant.friction = grass.friction
end
return grass
end

--- Portals O.o
--
local lastPortal = nil
function TCell.newPortal()
local portal = TCell.newCell()
portal.type = 'portal'
portal.gridPos = {0,0} -- need to set this after creation by caller
portal.needUpdate = true --need to update the animation
-- every time we create a Portal it alternate colors, started with blue the first one,
if lastPortal==nil then
portal.color = "blue"
portal.img = imgPortals.blue[1]
portal.imgs = imgPortals.blue
elseif lastPortal.color == "blue" then
portal.color = "orange"
portal.img = imgPortals.orange[1]
portal.imgs = imgPortals.orange
-- when the current color is Orange, it will link with the last portal that was Blue.
portal.link = lastPortal
lastPortal.link = portal
else
portal.color = "blue"
portal.img = imgPortals.blue[1]
portal.imgs = imgPortals.blue
end

--handle ant
function portal.affectAnt( ant )
local teleport = false
if ant.teleportedOnFrame then
--check when
if (cfg.simFrameNumber - ant.teleportedOnFrame) > 30 then
--teleport
teleport = true
end
else teleport = true end
if teleport and portal.link then
ant.position[1] = portal.link.posi[1] + cfg.mapGridSize / 2
ant.position[2] = portal.link.posi[2] + cfg.mapGridSize / 2
ant.resetPositionMemory( ant.position )
ant.teleportedOnFrame = cfg.simFrameNumber
end
end

function portal.draw(x, y)
apiG.draw(portal.imgs[ (math.floor(cfg.simFrameNumber/4) % 3) + 1 ], x, y, 0, cfg.imgScale, cfg.imgScale)
if portal.link then
local mid = cfg.mapGridSize /2
apiG.setColor(255, 255, 255, 50)
apiG.line(portal.posi[1] + mid, portal.posi[2] + mid, portal.link.posi[1] + mid, portal.link.posi[2] + mid )
end
end

lastPortal = portal
return portal
end


return TCell

2 changes: 2 additions & 0 deletions code/gui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ ui.radioBtns_cells = {
{caption = 'grass'},
{caption = 'cave'},
{caption = 'food'},
{caption = 'portal'},
{caption = 'remove'},
bWidth = btnDims.w,
bHeight = btnDims.h,
Expand Down Expand Up @@ -83,6 +84,7 @@ function ui.mainUpdate()
ui.suitRadio(ui.radioBtns_cells)

if suit.Checkbox( ui.showPheromones, suit.layout:row() ).hit then cfg.debugPheromones = ui.showPheromones.checked end

end

function ui.draw()
Expand Down
8 changes: 4 additions & 4 deletions code/map.lua
Original file line number Diff line number Diff line change
Expand Up @@ -302,12 +302,12 @@ function map.draw()
for i = map.minXg, map.maxXg do
for j = map.minYg, map.maxYg do
if map.grid[i][j].pass then
apiG.setColor(255,255,255);
apiG.draw(imgGround, i*cfg.mapGridSize, j*cfg.mapGridSize, 0, cfg.imgScale, cfg.imgScale );
apiG.setColor(255,255,255);
local cell = map.grid[i][j].cell
if cell then
--apiG.setColor( cell.color )
apiG.draw(cell.img, i*cfg.mapGridSize, j*cfg.mapGridSize, 0, cfg.imgScale, cfg.imgScale )
cell.draw( i*cfg.mapGridSize, j*cfg.mapGridSize )
else
apiG.draw(imgGround, i*cfg.mapGridSize, j*cfg.mapGridSize, 0, cfg.imgScale, cfg.imgScale );
end
else
-- apiG.setColor( cfg.colorObstacle )
Expand Down
2 changes: 1 addition & 1 deletion code/simconfig.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

local simconfig = {

numAnts = 3600,
numAnts = 6600,
numAntsMobile = 1000,
antMaxSpeed = 1.2,
antComAlgorithm = 1, -- 0 = Nothing; 1 = Pheromones inspiration
Expand Down
31 changes: 28 additions & 3 deletions code/simulation.lua
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,30 @@ function sim.onClick(x, y)
if map.isInsideGrid(xg, yg) then map.grid[xg][yg].pass = false end
end

function sim.removeCell(xg, yg)
if map.isInsideGrid(xg, yg) then
local grid = map.grid[xg][yg]
if grid.cell then
-- be carful with portals, they are linked
if grid.cell.type == 'portal' then
--remove link too
if grid.cell.link then
local linkedCell = map.grid[ grid.cell.link.gridPos[1] ][ grid.cell.link.gridPos[2] ]
linkedCell.pass = true
linkedCell.cell = nil
end
end
grid.cell = nil
end
grid.pass = true
end
end

function sim.setCell( cellType, xworld, yworld)
local xg, yg = map.worldToGrid( xworld, yworld)
local xg, yg = map.worldToGrid( xworld, yworld)
if map.isInsideGrid(xg, yg) then
--always remove existing first:
sim.removeCell(xg, yg)
local grid = map.grid[xg][yg]
if (cellType == 'block') or (cellType == 'obstacle') or (cellType == 'donotpass') then
grid.pass = false
Expand All @@ -147,8 +168,12 @@ function sim.setCell( cellType, xworld, yworld)
grid.pass = true
grid.cell = TCell.newCave()
elseif (cellType == 'ground') or (cellType == "remove") then
grid.pass =true
grid.cell = nil
--sim.removeCell(xg, yg)
elseif (cellType == 'portal') then
grid.pass = true
grid.cell = TCell.newPortal()
grid.cell.gridPos = {xg, yg}
grid.cell.posi = {xg * cfg.mapGridSize, yg * cfg.mapGridSize}
end
end
end
Expand Down
Binary file added images/portalBlue_00.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/portalBlue_01.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/portalBlue_02.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/portalOrange_00.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/portalOrange_01.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/portalOrange_02.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 5 additions & 5 deletions main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ local contentScaling
local function screenSizeUpdated()
contentScaling = apiG.getHeight() / cfg.idealContentHeight
ui.setContentScale( contentScaling, contentScaling )
cam.contentScale = contentScaling
end
screenSizeUpdated()

Expand All @@ -68,7 +69,7 @@ end
function api.update()
ui.numAnts = map.ants.count
ui.mainUpdate()
sim.update()
sim.update()
end

function api.draw()
Expand Down Expand Up @@ -125,7 +126,7 @@ local function dragmoved(x, y, dx, dy)


if api.mouse.isDown(1) and (x > ui.leftPanelWidth) then
if (tool ~= 'cave') and (tool~='pan view') then
if (tool ~= 'cave') and (tool ~= 'portal') and (tool~='pan view') then
sim.setCell(ui.radioBtns_cells.selectedCaption, cam.screenToWorld(x, y) )
end
end
Expand Down Expand Up @@ -158,9 +159,8 @@ function api.mousepressed(x, y, button, istouch)
end

function api.wheelmoved( x, y)
local inc
if y>0 then inc = 0.5 end
if y<0 then inc = -0.5 end
local inc = y * 0.5

cam.zoomOrigin.x, cam.zoomOrigin.y = api.mouse.getX(), api.mouse.getY()
cam.zoom(inc)
end
Expand Down
Binary file added psd/portals.psd
Binary file not shown.
Binary file added psd/portals2.psd
Binary file not shown.

0 comments on commit e977857

Please sign in to comment.