My first map Prototype -- Updated with object layer

Well if you count the 4 times i refactored it it is the 5th

http://www.youtube.com/watch?v=NV9pYrH4qF8

The map is parsed from a data file. One layer for the map and another layer for the objects.
the map scrolls, I have a touch and drag scroll working in another version but need to program the touch events in a better way.

The tiles are animated with animation class with a variable framerate per object and a dynamc list of frame uv cordinates
and A-star pathfinding working.

Time for another rewrite :slight_smile:
hmm…maybe combat can be hacked in before that.

As always any comments are welcome.

That’s pretty nice work @Thwapp. Would you consider sharing the code? I’d love to know how you did it.

Looks great! I’d also be interested in peeking at your code. I have a map based game and would like to implement drag scrolling.

Ric_Esrey. Well it is part of a game I am working so I can’t give you the code but I can point you in the right direction.

get a grid of tiles drawing from and nested list grid[r][c]
see:
http://twolivesleft.com/Codea/Talk/discussion/1578/i-need-help-making-a-faster-way-to-draw-this

Now granted I rewrote the whole tile thing after I understood the code and
you will need solid knowledge of the mesh class with setRect and setRecTex. Do not attempt to do it with sprites.

and the Astar is from Reefwing exellent tuts.
http://codeatuts.blogspot.com.au/2012/09/15.html

I will see if I have an older version I post. Or if you have a specific question I can see if I can answer it. I am still a noob learning LUA and Codea. But I have a goal and having a great time with Codea

Drag scrolling is nothing more than getting the distance from the start drag touch storing it and adding the difference to the blocks when you drag (poll toush.state.MOVING). So, myMesh.setRect(id,x+dragOffsetX,y+dragOffsetY,tileWidthX,tileWidthY)
add this to every blocki displayed
I will look for a simple test I did earlier when I get home after work

You can use deltaX, deltaY to tell you how far the finger has moved since the last redraw, and translate to move the drawing origin 0,0, which means you don’t need to add anything to any of the block coordinates, like so


function touched(touch)
    x,y=x+touch.deltaX,y+touch.deltaY
end

function draw()
    translate(x,y)
    myMesh:draw()
end

I will have to look in to that., but I will deffinately try it. I think I did a translate on the individual blocks and not the mesh and it was soo slow. More likely, a mistake was made with translate. I will try to after BBQing dinnerm but shoehorning in combat might come tonight. I do not want to tackle registering tile uv animations with the player class and animation class tonight. After 2 months of learning Codea LUA that z needs to go BOOM! baby Boom

Here is some very old code, but it has all the basics. Just a warning it is not commented.
My current code is nothing like this, but you gotta start somewhere
forgot you need image for a texture. tiles 66x66 and 660W and 660H

edit I removed all the redundant loops for updating the block one at a time…Sometimes you can’t see the forrest because of the trees. Now I need to look into translate and related functions

I was killing the gpu for no reson thanks Ignatz


-- create world

function setup()
    world = worldCreate()
    ellipseMode(RADIUS)
    playerPos  = vec2(HEIGHT/2,WIDTH/2)
    maxMapDrag = vec2(250,250)
    startDragX = 0 
    startDragY = 0
    prevOffsetX = 0
    prevOffsetY = 0
    offsetX = 0
    offsetY = 0
    tX = 0
    tY = 0
    x=0
    y=0
    parameter.watch("setTween")
    parameter.watch("tweenObject")
    parameter.watch("startDragX")
    parameter.watch("startDragY")
    parameter.watch("endDragX")
    parameter.watch("endDragY")
    parameter.watch("prevOffsetX")
    parameter.watch("prevOffsetY")
    parameter.watch("offsetX")
    parameter.watch("offsetY")
end

function draw()
    background(0, 0, 0, 255)  
    translate(x,y)
    world:draw()
end

function touched(touch)
    x =x+touch.deltaX
    y = y +touch.deltaY
    --world:touched(touch)
end

--# worldCreate


worldCreate = class()

function worldCreate:init()
    self.player ={x=1,y=2,health=10,attack=1,def=2,moves=10}
    -- you can accept and set parameters here
    self.player.x=1
   -- self.player.y=1--"health" = 10, "ammo" = 10, "moves" = 10]
    print(self.player.x.." "..self.player.y.." "..self.player.health)

    wWidth = (WIDTH/66)*2
    wHeight = (HEIGHT/66)*2
    local grid = {}
    local id = {}
    local bid = {} -- block id
    local worldOffsetY = 0
    local worldOffsetX = 0
    blocks = {}
    img = readImage("Dropbox:TileMap660")
    -- the image is the tileset andthe tileoffset is for moving the wholeworld
    local tileWidth = 66
    local tileWidth = 66
    self.blocks = mesh() -- the rectangle mesh
    self.blocks.texture = img -- the TIlemap
    for i = 1,wWidth do
        grid[i] = {}
        for j = 1, wHeight do
            grid[i][j] = math.random(6) -- random tile
        end
    end

        for r,row in pairs(grid) do
        id[r] = {}
        bid[r] = {}
        for c,col in pairs(row) do
            --print(c)
            -- add the rectangle to the mesh
            id[r][c] = self.blocks:addRect(-33+ r*66,-33 + c*66,66,66) 
     
                tileCordList = {name = "grassTile", x = 1/10, y= 9/10}
                tileCordList[2] = {name = "zombieTile", x = 2/10, y= 9/10}
                tileCordList[2] = {name = "trapTile", x = 3/10, y= 9/10}
                tileCordList[2] = {name = "hiddenTile", x = 4/10, y= 9/10}
                tileCordList[2] = {name = "playerTile", x = 5/10, y= 9/10}
                tileCordList[2] = {name = "goalTile", x = 6/10, y= 9/10}
     
            if(grid[r][c] == 1) then
              self.blocks:setRectTex(id[r][c], 1/10,9/10,66/660,66/660)
                bid[r][c] = 1
                elseif (grid[r][c] == 2) then
                self.blocks:setRectTex(id[r][c],2/10,9/10,66/660,66/660)
                    bid[r][c] = 2
                elseif (grid[r][c] == 3) then
                    self.blocks:setRectTex(id[r][c],3/10,9/10,66/660,66/660)
                    bid[r][c] = 3
                elseif (grid[r][c] == 4) then
                    self.blocks:setRectTex(id[r][c],4/10,9/10,66/660,66/660)
                    bid[r][c] = 4
                elseif (grid[r][c] == 5) then
                    self.blocks:setRectTex(id[r][c],4/10,9/10,66/660,66/660)
                    bid[r][c] = 5
                elseif (grid[r][c] == 6) then
                    self.blocks:setRectTex(id[r][c],4/10,9/10,66/660,66/660)
                    bid[r][c] = 6
                elseif (grid[r][c] == 10) then
                    self.blocks:setRectTex(id[r][c],3/10,5/10,66/660,66/660)
                    bid[r][c] = 10
                    print(" changed ")
            end
        end
    end
    self.grid = grid -- the holder for the world
    --print(world)
    self.id = id -- the id of the block 0 lower left col1 1,2...13  col2 1,2,..13 col3
    self.bid = bid -- the tile type id's texture cordinates
   
end

function worldCreate:draw()
    -- Codea does not automatically call this method
     
       self.blocks:draw()
    end

function worldCreate:touched(touch)
 
   

end

I finally killed two bugs today. One was just on oversight of mine not drawing tileTiles if they were zero, and the other one is a bit more complex. I was having an issue with all my map objects drawing with uv from setRecTex. I have ids from 1 to 100 for the map tiles and the object started at id 1000. I did this so the map could be a variable size up to 100x100 tiles(in theory). The mesh class will let you add rectangles with any id but if they are not sequential then they will not use the setRecTex values. When I changed the id to start after the map id with no numerical gap…poof…it textured correctly instead of showing the whole image. I hope This will help others besides me save hours of frustration.

I’m sure this had to do with a nil value after the id of the rectangles in the mesh, but one would assume the rectangle would not draw if that was the case. The gap in the numbering started there.

A good solid bug squashing day. Next I will program selecting objects and putting back in the astar movement. Then I need to register each player and enemy with a timer to move them around on timed intervals

And now Star Command calls me to play.

Here is the new movie with placeholder graphics I made.

http://www.youtube.com/watch?v=TEFZsrTkv9g

p.s. I also need to stop drawing the game objects if they are not on the displayed tiles.