Page 1 of 1

help with level building

Posted: Mon Oct 06, 2014 7:29 pm
by smokingbunny
hello all,

been a bit busy of late, planning to get away from the UK but want to get some code on. and one thing i have been on and off trying to get going is a certain type of level building , thats built through 1s and 0s.
the type of level building is:

Code: Select all

thisIsALevel = {
   {1,1,1,1,1,1,1,1},
   {1,0,0,0,0,0,0,1},
   {1,0,0,1,1,0,0,1},
   {1,0,0,0,0,0,0,1},
   {1,1,1,1,1,1,1,1}
}
but i have been trying to incorporate collision to the image that is used, namely the '1' in all of this, but am running up empty, and cant figure out how. im using bump, even tried to not use it, but still running up empty.
ive searched in the forum, but im gathering my wording is wrong, since i cant remember what the name of the code above is called. i would have figured someone would of asked this question

thanks

Re: help with level building

Posted: Mon Oct 06, 2014 8:29 pm
by artofwork
The code is called a matrix but also can be called a grid, level building is known as layers.

If you have an issue with your code either A place it in its entirety in a code tag in your post or B upload a love file

Re: help with level building

Posted: Mon Oct 06, 2014 8:31 pm
by smokingbunny
aye, i was trying to find what i had done before, but its missing. which is odd. but ill drum up something to show

thanks

Re: help with level building

Posted: Mon Oct 06, 2014 8:45 pm
by smokingbunny
found an old file, which built up to what i made before going closer to collision for blocks that are 1s, but doesnt have the collision as it was before i started to put it in.
ill look further, but my computer has been really giving me problems lately and have missing a lot now. a new one is needed ;)

Code: Select all

function mapsLoad()
	mapImage = love.graphics.newImage('assets/image/level/platform01.png')
	level01 = {
		{1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1},
		{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
		{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
		{1,1,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
		{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
		{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
		{1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
		{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
		{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
		{1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
		{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
		{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
		{1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
		{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
		{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
		{1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1},
	}
end

function mapsDraw()
	for y=1, #map do
        for x=1, #map[y] do
      -- Check space and render suitable tile.
            if map[y][x] == 1 then
                love.graphics.setColor(255,255,255,255)
                love.graphics.draw(mapImage,x*32,y*32)
            end
            if map[y][x] == 0 then
                love.graphics.setColor(255,255,255,170)
                love.graphics.rectangle('fill',x*32,y*32,32,32)
            end
        end
    end
end
thanks

Re: help with level building

Posted: Mon Oct 06, 2014 8:59 pm
by artofwork
This is a basic tutorial about creating maps and character movement

Code: Select all

http://www.love2d.org/wiki/Tutorial:Gridlocked_Player
But you can expand upon that idea by generating layers using this same system

Code: Select all

map = { -- z
		{ -- y -1
			{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, -- x
			{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, -- background layer
			{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, -- 1 is background
			{ 1, 1, 1, 1, 1, 1, 1, 1, 1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0, 0, 0, 0, 0, 0, 0, 0, 0, 2 },
			{ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 },
		},
		
			{ -- y -3
			{ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255 }, -- x
			{ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255 }, -- empty top layer for jumping
			{ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255 }, -- 255 no collision
			{ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255 }, -- 255 is path and top order empty space
			{ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255 },
			{ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255 },
			{ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255 },
			{ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255 },
			{ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255 },
			{ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255 },
			{ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255 },
			{ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255 },
			{ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255 },
		}

    }
I'll upload a love file to show you how this done

Re: help with level building

Posted: Mon Oct 06, 2014 10:03 pm
by artofwork
So I made this awhile back, I removed somethings from the original code, but i wanted to show you how to load a multi-layered map.

Although the sprite is smaller then the tiles, its the only sprite sheet I had made at the time :)
All and all it still works with the map :p
Test_Layered_Map.PNG

Re: help with level building

Posted: Tue Oct 07, 2014 11:55 am
by smokingbunny
oooh. thank you very much for that, ill have a nice dig into it.

i did have a little bit of a 'spark' moment when figuring some things out, still using physics, but just stuck on the placement, or even the coding of where the physics go in the grid. if that makes sense :s
here is what i have got to, its a new file i have been working on. this is making a 2d platformer, using physics as the world, also the grid/matrix type of drawing
here is where im up to at this moment in time. i have commented where it is i need to fix, with the original, none physics block.

so in short, im trying the get physics blocks to draw on the grid.

Code: Select all

level01 = {}
function mapLoad()
	ground = {}
	ground.body = love.physics.newBody(world,64,64)
	ground.shape = love.physics.newRectangleShape(64,64)
	ground.fixture = love.physics.newFixture(ground.body,ground.shape)

	level01 = {
		{1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1},
		{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
		{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
		{1,1,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
		{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
		{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
		{1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
		{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
		{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
		{1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
		{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
		{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
		{1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
		{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
		{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
		{1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1},
	}
end

function mapDraw()
	for y=1, #map do
        for x=1, #map[y] do
            if map[y][x] == 1 then
                love.graphics.setColor(0,0,0,255)
                --THIS IS THE ORIGINAL DRAWING
--                love.graphics.rectangle('fill',x*32,y*32,32,32)

				--THIS IS THE PART WHERE I CANT GET IT TO DRAW IN THE GRID WITH PHYSICS
                love.graphics.polygon('fill',ground.body:getWorldPoints(ground.shape:getPoints()))
            end
            if map[y][x] == 0 then
                love.graphics.setColor(255,255,255,170)
                love.graphics.rectangle('fill',x*32,y*32,32,32)
            end
        end
    end
end
thanks again for your help