Jump to content




Grid based tiling?


6 replies to this topic

#1 LeDark Lua

  • Members
  • 369 posts
  • LocationLeLua

Posted 18 August 2015 - 10:47 AM

Yup i know its possible, but how to make rectangle grids in which the tiles could be placed. These grids are self aware when they are colliding with other tile and or out of the screen bounds.

I cant think of any good ideas/formulas to make that grid based tiling so I don't have code.

The for ex: 4x4 tile basing is easy or any of those. But 4x6, 1x4, 5x2, ... I cant think of what to do.

#2 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 18 August 2015 - 02:05 PM

Not exactly sure what you're asking, here... are you wanting to create a tiled pattern, filling as much space in the grid as possible, with randomly sized shapes? Or are you simply wanting to allow placement where ever, so long as no tiles overlap? More details on your final goal may be helpful here - when are the tiles being placed, do they ever move, etc...

#3 LeDark Lua

  • Members
  • 369 posts
  • LocationLeLua

Posted 18 August 2015 - 02:22 PM

tiles cant move. and placement is where ever in X axis.

#4 COOLGAMETUBE

  • Members
  • 49 posts
  • LocationGermany

Posted 18 August 2015 - 02:28 PM

View PostLeDark Lua, on 18 August 2015 - 02:22 PM, said:

tiles cant move. and placement is where ever in X axis.

Shoud they move but can't do it

Or shoudn't they move as Information?

#5 Exerro

  • Members
  • 801 posts

Posted 18 August 2015 - 02:35 PM

Grid based tiling is easy as long as each tile is the same width/height.

local displayWidth, displayHeight = term.getSize()
local tileWidth = 5 -- the width of a tile
local tileHeight = 3 -- the height of a tile
local xMargin = 1 -- the horizontal offset
local yMargin = 1 -- the vertical offset
local xPadding = 1 -- the padding between tiles horizontally
local yPadding = 1 -- the padding between tiles vertically

local columns = math.floor( ( displayWidth - 2 * xMargin + xPadding ) / ( tileWidth + xPadding ) )
local rows = math.floor( ( displayHeight - 2 * yMargin + yPadding ) / ( tileHeight + yPadding ) )

for n = 1, columns do
    for m = 1, rows do
        local tileX = xMargin + ( n - 1 ) * ( tileWidth + xPadding )
        local tileY = yMargin + ( m - 1 ) * ( tileHeight + yPadding )
    end
end

That should do it.

If you're having multiple sized tiles, however, I have absolutely no idea. It depends on how you want them to stack, the degree of different sizing (i.e. rounded to 2 pixels or literally anything?), and whether the order of tiles matters.

#6 LeDark Lua

  • Members
  • 369 posts
  • LocationLeLua

Posted 18 August 2015 - 03:08 PM

term.width, term.heigth = term.getSize()
local tileRows = {currTile=1;}
for i=1,19 do
tileRows[i]={maxWidth=term.width;filledWidth=1;y=i;tiles={};combinedWidth=1;};
end
local icons={}
local event={}

function createIcon( name )
local newT = {}
newT.x=1
newT.y=1
newT.name=name
newT.width=#name
function newT:draw()
term.setCursorPos(self.x, self.y)
term.write(self.name)
end
icons[#icons+1]=newT
return newT
end

function updateIcons()
for i=1, #icons do
icons[i]:draw()
end
end

function autoPlaceTiles()
for i=1, #icons do
if tileRows[tileRows.currTile].maxWidth < tileRows[tileRows.currTile].filledWidth+(icons[i].width+1) then
tileRows.currTile=tileRows.currTile+1
end
if tileRows[tileRows.currTile].maxWidth > tileRows[tileRows.currTile].filledWidth then
icons[i].x=tileRows[tileRows.currTile].filledWidth
icons[i].y=tileRows[tileRows.currTile].y
tileRows[tileRows.currTile].combinedWidth = tileRows[tileRows.currTile].combinedWidth + icons[i].width
tileRows[tileRows.currTile].tiles[i]=icons[i]
tileRows[tileRows.currTile].filledWidth=tileRows[tileRows.currTile].filledWidth+(icons[i].width+1)
end
end
end

function repositionTiles()
for i=1, tileRows.currTile do
--Repositioning with combinedWidth/2 and positionig like that
end
end

for i=1, 95 do
createIcon( "|Test: "..i.."|" )
end

autoPlaceTiles()

repositionTiles()

while true do
term.clear()
updateIcons()
event = {os.pullEvent()}
updateIcons()
end

this is what I have right now. It works fine but I need to make so it positions itself in the middle if there is still room left.

Edited by LeDark Lua, 18 August 2015 - 03:11 PM.


#7 LeDark Lua

  • Members
  • 369 posts
  • LocationLeLua

Posted 18 August 2015 - 04:55 PM

Ok this works jus fine for me, tell me if you agree:
function autoPlaceTiles()
for i=1, #term.icons do
if tileRows[tileRows.currTile].maxWidth < tileRows[tileRows.currTile].filledWidth+term.icons[i].width-2 then
tileRows.currTile=tileRows.currTile+1
end
if tileRows[tileRows.currTile].maxWidth > tileRows[tileRows.currTile].filledWidth-2 then
tileRows[tileRows.currTile].tiles[#tileRows[tileRows.currTile].tiles + 1]=term.icons[i]
term.icons[i].x=tileRows[tileRows.currTile].filledWidth
term.icons[i].y=tileRows[tileRows.currTile].y
term.icons[i].oldY=tileRows[tileRows.currTile].y
tileRows[tileRows.currTile].combinedWidth = tileRows[tileRows.currTile].combinedWidth + term.icons[i].width
tileRows[tileRows.currTile].filledWidth=tileRows[tileRows.currTile].filledWidth+(term.icons[i].width+1)
end
end
end

function repositionTiles()
for i=1, tileRows.currTile do
local addX = term.width - tileRows[i].combinedWidth-8
local tiles = tileRows[i].tiles
for j=1, #tiles do
tiles[j].x = tiles[j].x + math.floor(addX/2)
end
end
end






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users