Page 1 of 2

Creating a Tile Map of Classes? [Fixed]

Posted: Thu Jan 12, 2012 1:41 am
by xXOdysseusXx
Hello I'm relatively new at programming and I was attempting to create a tiled game. What I did was create a Wall Tile Class and then I used some of the code in the Tiling tutorials in attempt to initialize, draw, and update the class in accordance to a Tile map... I utterly failed. I dont' get an error, but the titles refuse to show up. Can anybody help out here?

Code: Select all

require("lib/SECS")
require("Circle")
require("WallTile")
gamestate = require ("lib/hump/gamestate")
level1 = gamestate.new() -- I added a gamestate libary that should help
-- Tile Stuff
 tile = {}
	tile[1] = WallTile:new()
   map_w = 20 	
   map_h = 15
   map_x = 0
   map_y = 0
   map_offset_x = 30
   map_offset_y = 30
   tile_w = 48
   tile_h = 48
--Tile Stuff
function love.load(dt)
    gamestate.registerEvents() -- Calling the GameStates
    gamestate.switch(level1) -- Ofcourse this will eventually be the into or menu
end
function love.draw()
love.graphics.setColor(255,255,255)
love.graphics.print( "V 0.0001", 100, 10, 0, 1.5,1.5)
end

function level1:init()
entity = Entity:new()
entity:init(0,0,40,40)
map={ -- My friends, we have a tile map for level1
   { 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, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},
   { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
   { 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
   { 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
   { 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
   { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
   { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
   { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
   { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
   { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
   { 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0},
   { 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0},
   { 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0},
   { 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0},
   { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
   { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
   { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
   { 0, 1, 1, 1, 1, 1, 1, 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},
}

initTiles() -- Initiating the tiles

end
function level1:enter() 
-- Runs every time it enters the game state.
end
function level1:update(dt)
entity:update(dt)
end
function level1:draw()
entity:draw()
drawTiles()
end

function initTiles()
 for y=1, map_h do
      for x=1, map_w do
			if tile[map[y+map_y][x+map_x]] == 1 then -- if it's a wall tile
            tile[map[y+map_y][x+map_x]]:init((x*tile_w)+map_offset_x,(y*tile_h)+map_offset_y ) -- What the fuck just happened?
			end
      end
   end
end

function drawTiles()
 for y=1, map_h do
      for x=1, map_w do
			if tile[map[y+map_y][x+map_x]] == 1 then -- if it's a wall tile
            tile[map[y+map_y][x+map_x]]:draw()
			end
      end
   end
end


If anybody needs some information on what I did or what I was trying to do just ask.

Re: Creating a Tile Map of Classes?

Posted: Thu Jan 12, 2012 1:52 am
by MarekkPie
If you want something to appear on the screen, it has to come from the love.draw() callback function.

Code: Select all

function love.draw()
love.graphics.setColor(255,255,255)
love.graphics.print( "V 0.0001", 100, 10, 0, 1.5,1.5)
end
Right now, this is all you have in there. Try putting level1:draw() inside there.

I should clarify...not the definition of the method(function), but the actual call to it.

Re: Creating a Tile Map of Classes?

Posted: Thu Jan 12, 2012 1:59 am
by xXOdysseusXx
I think I should mention that I'm using a Game State library and that it does work the way I have it. The reason I only have the version in the main love.draw() function is because I want the version to display in all game states. I only have one right now though :). Anyways no cigar there.

Re: Creating a Tile Map of Classes?

Posted: Thu Jan 12, 2012 2:06 am
by MarekkPie
You still need to call that game states draw in love.draw(), at least according to the my understanding of the hump documentation:
http://vrld.github.com/hump/#Gamestate-draw

See the example.

Re: Creating a Tile Map of Classes?

Posted: Thu Jan 12, 2012 2:30 am
by xXOdysseusXx
If you read above the example it will tell you that it's necessary only if not using registerevents(). So I should be good.

Re: Creating a Tile Map of Classes?

Posted: Thu Jan 12, 2012 10:48 am
by T-Bone
If the library claims to be able to draw stuff without you telling it to, it has to achieve this by creating a function called love.draw() itself. So when you do

Code: Select all

function love.draw()
    love.graphics.setColor(255,255,255)
    love.graphics.print( "V 0.0001", 100, 10, 0, 1.5,1.5)
end
Then you probably just overwrite the drawing function from the library. Try replacing with this and see if it helps

Code: Select all

oldDraw = love.draw
function love.draw()
    oldDraw()
    love.graphics.setColor(255,255,255)
    love.graphics.print( "V 0.0001", 100, 10, 0, 1.5,1.5)
end
Remember, the function love.draw() is the ONLY function that can draw something. Ever.

Re: Creating a Tile Map of Classes?

Posted: Thu Jan 12, 2012 11:03 am
by thelinx
Or call the register function in love.load, when your love.draw has already been created.

Re: Creating a Tile Map of Classes?

Posted: Thu Jan 12, 2012 4:28 pm
by vrld
Here is your first problem:

Code: Select all

if tile[map[y+map_y][x+map_x]] == 1 then -- if it's a wall tile
map[y+map_y][x+map_x] can be 0 or 1. But you wrote

Code: Select all

tile = {}
tile[1] = WallTile:new()
in the beginning, so tile[0] = nil and tile[1] = <instance of WallTile>, but it is never 1.

Here's your second problem:

Code: Select all

tile[map[y+map_y][x+map_x]]:init((x*tile_w)+map_offset_x,(y*tile_h)+map_offset_y ) -- What the fuck just happened?
You reinitialize one tile over and over again with different positions. Either create more than one tile instances or put the position into tile:draw() and remove initTiles().

Re: Creating a Tile Map of Classes?

Posted: Thu Jan 12, 2012 9:02 pm
by xXOdysseusXx
Thanks vrld. For the first problem does that mean I should rewrite it as:

Code: Select all

if tile[map[y+map_y][x+map_x]] == WallTile then -- if it's a wall tile
?

Also for the second problem, I want to add physic's oriented collisions to the tiles so that means the just drawing them wouldn't suffice. Thus why I made a WallTile class, so how could I make multiple instances of the class in the positions in the map? I will also then need to call their draw and update function... or is there a totally better way to do this? The game would be oriented like Super Mario is.

Re: Creating a Tile Map of Classes?

Posted: Fri Jan 13, 2012 8:23 am
by Robin
xXOdysseusXx wrote:

Code: Select all

if tile[map[y+map_y][x+map_x]] == WallTile then -- if it's a wall tile
?
I don't think that would work, since it was an instance of WallTile rather than WallTile itself (unless you changed that).

This should work, though:

Code: Select all

if map[y+map_y][x+map_x] == 1 then -- if it's a wall tile