/ld27

To get this branch, use:
bzr branch http://9ix.org/bzr/ld27

« back to all changes in this revision

Viewing changes to zoetrope/sprites/map.lua

  • Committer: Josh C
  • Date: 2013-08-24 23:59:23 UTC
  • Revision ID: josh@9ix.org-20130824235923-xorddky74v31bsfm
first draft map complete!

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
        -- nb. The tile at (0, 0) visually is stored in [1, 1].
28
28
        map = {},
29
29
 
30
 
        spriteBatches = {},
31
 
        prevStartX = nil,
32
 
        prevStartY = nil,
33
 
        prevEndX = nil,
34
 
        prevEndY = nil,
35
 
 
36
30
        -- Method: empty
37
31
        -- Creates an empty map.
38
32
        --
345
339
                -- determine drawing bounds
346
340
                -- we draw to fill the entire app windoow
347
341
                
348
 
                --local startX, startY = self:pixelToMap(-x, -y)
349
 
                --local endX, endY = self:pixelToMap(the.app.width - x, the.app.height - y)
350
 
                local startX, startY = 1, 1
351
 
                local endX, endY = self:getMapSize()
352
 
 
353
 
                -- if using spritebatches, check if anything has changed
354
 
                -- TODO: encapsulate this check?
355
 
                if startX ~= self.prevStartX or
356
 
                        startY ~= self.prevStartY or
357
 
                        endX ~= self.prevEndX or
358
 
                        endY ~= self.prevEndY then
359
 
 
360
 
                        --print('building sprite batch')
361
 
 
362
 
                        self.prevStartX, self.prevStartY = startX, startY
363
 
                        self.prevEndX, self.prevEndY = endX, endY
364
 
 
365
 
                        for k, batch in pairs(self.spriteBatches) do
366
 
                                --batch:bind()
367
 
                                batch:clear()
368
 
                        end
369
 
 
370
 
                        -- queue each sprite drawing operation
371
 
 
372
 
                        local toDraw = {}
373
 
 
374
 
                        for drawY = startY, endY do
375
 
                                for drawX = startX, endX do
376
 
                                        local sprite = self.sprites[self.map[drawX][drawY]]
377
 
 
378
 
                                        if sprite and sprite.visible then
379
 
                                                if not toDraw[sprite] then
380
 
                                                        toDraw[sprite] = {}
381
 
                                                end
382
 
 
383
 
                                                table.insert(toDraw[sprite], { (drawX - 1) * self.spriteWidth,
384
 
                                                                               (drawY - 1) * self.spriteHeight })
 
342
                local startX, startY = self:pixelToMap(-x, -y)
 
343
                local endX, endY = self:pixelToMap(the.app.width - x, the.app.height - y)
 
344
                
 
345
                -- queue each sprite drawing operation
 
346
                
 
347
                local toDraw = {}
 
348
                
 
349
                for drawY = startY, endY do
 
350
                        for drawX = startX, endX do
 
351
                                local sprite = self.sprites[self.map[drawX][drawY]]
 
352
                                
 
353
                                if sprite and sprite.visible then
 
354
                                        if not toDraw[sprite] then
 
355
                                                toDraw[sprite] = {}
385
356
                                        end
386
 
                                end
387
 
                        end
388
 
 
389
 
                        -- draw each sprite in turn
390
 
 
391
 
                        for sprite, list in pairs(toDraw) do
392
 
                                for _, coords in pairs(list) do
393
 
                                        sprite:draw(coords[1], coords[2], self)
 
357
                                        
 
358
                                        table.insert(toDraw[sprite], { x + (drawX - 1) * self.spriteWidth,
 
359
                                                                                                   y + (drawY - 1) * self.spriteHeight })
394
360
                                end
395
361
                        end
396
362
                end
397
 
 
398
 
                for _, batch in pairs(self.spriteBatches) do
399
 
                        --batch:unbind()
400
 
                        love.graphics.draw(batch, x, y)
 
363
                
 
364
                -- draw each sprite in turn
 
365
                
 
366
                for sprite, list in pairs(toDraw) do
 
367
                        for _, coords in pairs(list) do
 
368
                                sprite:draw(coords[1], coords[2])
 
369
                        end
401
370
                end
402
371
        end,
403
372