339
345
-- determine drawing bounds
340
346
-- we draw to fill the entire app windoow
342
local startX, startY = self:pixelToMap(-x, -y)
343
local endX, endY = self:pixelToMap(the.app.width - x, the.app.height - y)
345
-- queue each sprite drawing operation
349
for drawY = startY, endY do
350
for drawX = startX, endX do
351
local sprite = self.sprites[self.map[drawX][drawY]]
353
if sprite and sprite.visible then
354
if not toDraw[sprite] then
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()
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
360
--print('building sprite batch')
362
self.prevStartX, self.prevStartY = startX, startY
363
self.prevEndX, self.prevEndY = endX, endY
365
for k, batch in pairs(self.spriteBatches) do
370
-- queue each sprite drawing operation
374
for drawY = startY, endY do
375
for drawX = startX, endX do
376
local sprite = self.sprites[self.map[drawX][drawY]]
378
if sprite and sprite.visible then
379
if not toDraw[sprite] then
383
table.insert(toDraw[sprite], { (drawX - 1) * self.spriteWidth,
384
(drawY - 1) * self.spriteHeight })
358
table.insert(toDraw[sprite], { x + (drawX - 1) * self.spriteWidth,
359
y + (drawY - 1) * self.spriteHeight })
389
-- draw each sprite in turn
391
for sprite, list in pairs(toDraw) do
392
for _, coords in pairs(list) do
393
sprite:draw(coords[1], coords[2], self)
364
-- draw each sprite in turn
366
for sprite, list in pairs(toDraw) do
367
for _, coords in pairs(list) do
368
sprite:draw(coords[1], coords[2])
398
for _, batch in pairs(self.spriteBatches) do
400
love.graphics.draw(batch, x, y)
373
404
-- makes sure all sprites receive startFrame messages
375
406
startFrame = function (self, elapsed)
376
for _, spr in pairs(self.sprites) do
407
for _, spr in self:members() do
377
408
spr:startFrame(elapsed)
393
424
-- makes sure all sprites receive endFrame messages
395
426
endFrame = function (self, elapsed)
396
for _, spr in pairs(self.sprites) do
427
for _, spr in self:members() do
397
428
spr:endFrame(elapsed)
400
431
Sprite.endFrame(self, elapsed)
434
-- mimic Group's members() method
436
members = function (self)
437
return ipairs(self.sprites)
403
440
__tostring = function (self)
404
local result = 'Map (x: ' .. self.x .. ', y: ' .. self.y ..
405
', w: ' .. self.width .. ', h: ' .. self.height .. ', '
441
local result = 'Map (x: ' .. tostring(self.x) .. ', y: ' .. tostring(self.y) ..
442
', w: ' .. tostring(self.width) .. ', h: ' .. tostring(self.height) .. ', '
407
444
if self.active then
408
445
result = result .. 'active, '