improvement(cbs): precalculate the map
This commit is contained in:
parent
1f3d07c223
commit
d8dafdfe48
1 changed files with 68 additions and 56 deletions
|
@ -7,13 +7,16 @@ local DURATION = 0.66
|
|||
local OPACITY_MIN = 0
|
||||
local OPACITY_MAX = 0.75
|
||||
|
||||
local HEIGHT = 5
|
||||
local BOTTOM_BORDER = 1
|
||||
|
||||
function Map:new(world, type, terrain)
|
||||
self.world = world
|
||||
self.assets = self.world.assets
|
||||
self.scene = self.world.scene
|
||||
|
||||
self.datas = {}
|
||||
self.datas.type = type or "city"
|
||||
self.datas.type = type or "forest"
|
||||
self.datas.terrains = terrain or maputils.newEmptyMap()
|
||||
|
||||
self.tweens = TweenManager(self)
|
||||
|
@ -30,14 +33,21 @@ function Map:new(world, type, terrain)
|
|||
self.assets:addImage("back1", backpath .. "-back.png")
|
||||
self.assets:addImage("back2", backpath .. "-fore.png")
|
||||
self.assets:addImage("cliff", backpath .. "-cliff.png")
|
||||
|
||||
self.texture = {}
|
||||
self.texture.floor = self:generateFloor(tile)
|
||||
end
|
||||
|
||||
-- GET FUNCTIONS
|
||||
-- Get information from the map
|
||||
|
||||
function Map:getTotalHeight()
|
||||
return HEIGHT + BOTTOM_BORDER;
|
||||
end
|
||||
|
||||
function Map:getTerrain(x, y)
|
||||
if self.datas.terrains[y] ~= nil then
|
||||
return self.datas.terrains[y][x]
|
||||
if (y <= HEIGHT) then
|
||||
return 0
|
||||
else
|
||||
return nil
|
||||
end
|
||||
|
@ -47,6 +57,26 @@ function Map:isInGrid(x, y)
|
|||
return ( self:getTerrain(x, y) ~= nil )
|
||||
end
|
||||
|
||||
function Map:getStartY()
|
||||
return 240 - (self:getTotalHeight() * 20) - 10
|
||||
end
|
||||
|
||||
function Map:gridToPixel(x, y, center)
|
||||
local pixelx, pixely
|
||||
local center = center or false
|
||||
local x, y = x, y
|
||||
|
||||
if (center) then
|
||||
x = x + .5
|
||||
y = y + .5
|
||||
end
|
||||
|
||||
pixelx = maputils.CONST.STARTX + ((x-1) * 31) + ((y-1) * 10)
|
||||
pixely = self:getStartY() + ((y-1) * 20)
|
||||
|
||||
return math.floor(pixelx), math.floor(pixely)
|
||||
end
|
||||
|
||||
function Map:update(dt)
|
||||
self.tweens:update(dt)
|
||||
end
|
||||
|
@ -75,17 +105,43 @@ end
|
|||
-- DRAW FUNCTIONS
|
||||
-- Draw the battle map
|
||||
|
||||
function Map:draw(activeGrid, effectGrid)
|
||||
function Map:generateFloor(tile)
|
||||
local canvas = love.graphics.newCanvas(31*16, self:getTotalHeight() * 20)
|
||||
local tile = tile or 1
|
||||
|
||||
love.graphics.setCanvas( canvas )
|
||||
|
||||
for i=1, self:getTotalHeight() do
|
||||
for j=0, 18 do
|
||||
local tiley = (i-1)*20
|
||||
local tilex = (j-2)*31 + (i-1)*10
|
||||
local variant = 1 + ((i + j) % 2)
|
||||
local tiles = self.datas.tiles*2 + variant
|
||||
if (not self:isInGrid(j, i)) then
|
||||
love.graphics.setColor(.66, .66, .66, 1)
|
||||
end
|
||||
self.assets.tileset["normaltiles"]:drawTile(tiles, tilex, tiley)
|
||||
utils.graphics.resetColor()
|
||||
end
|
||||
end
|
||||
|
||||
love.graphics.setCanvas( )
|
||||
|
||||
local imagedata = canvas:newImageData()
|
||||
local texture = love.graphics.newImage( imagedata )
|
||||
imagedata:release()
|
||||
canvas:release()
|
||||
return texture
|
||||
end
|
||||
|
||||
|
||||
function Map:draw()
|
||||
self:drawBackgrounds()
|
||||
self:drawBorders()
|
||||
self:drawTerrains(activeGrid)
|
||||
if (effectGrid ~= nil) then
|
||||
self:drawEffectGrid(effectGrid)
|
||||
end
|
||||
love.graphics.draw(self.texture.floor, maputils.CONST.STARTX, self:getStartY())
|
||||
end
|
||||
|
||||
function Map:drawBackgrounds()
|
||||
|
||||
local w, _ = core.screen:getDimensions()
|
||||
|
||||
local w2, h2 = self.assets.images["back1"]:getDimensions()
|
||||
|
@ -97,7 +153,7 @@ function Map:drawBackgrounds()
|
|||
local w2, h2 = self.assets.images["back2"]:getDimensions()
|
||||
local imax = math.ceil(w / w2) + 1
|
||||
for i=1, imax do
|
||||
self.assets.images["back2"]:draw((i-1)*w2, maputils.CONST.STARTY-h2, 0, 1, 1)
|
||||
self.assets.images["back2"]:draw((i-1)*w2, self:getStartY()-h2, 0, 1, 1)
|
||||
end
|
||||
|
||||
end
|
||||
|
@ -105,51 +161,8 @@ end
|
|||
function Map:drawBorders()
|
||||
local border = self.datas.borders + 1
|
||||
for i=1, 7 do
|
||||
self.assets.tileset["borders"]:drawTile(border, (i-1)*80, maputils.CONST.STARTY-10 , 0, 1, 1)
|
||||
self.assets.tileset["borders"]:drawTile(border, (i-1)*80, maputils.CONST.STARTY+20*7, 0, 1, 1)
|
||||
end
|
||||
end
|
||||
|
||||
function Map:drawTerrains(activeGrid)
|
||||
local vl, vhd, vd = 1, .7, .5
|
||||
local isActive = (activeGrid ~= nil)
|
||||
|
||||
|
||||
for i=1, 7 do
|
||||
for j= -2, 17 do
|
||||
local k = 1 + ((i + j) % 2)
|
||||
|
||||
local terrain = self:getTerrain(j, i)
|
||||
local x, y = maputils.gridToPixel(j, i, false)
|
||||
|
||||
if (terrain ~= nil) then
|
||||
if (isActive) then
|
||||
if (activeGrid[i][j] == 1) then
|
||||
love.graphics.setColor(vl, vl, vl, 1)
|
||||
else
|
||||
love.graphics.setColor(vhd, vhd, vhd, 1)
|
||||
end
|
||||
else
|
||||
love.graphics.setColor(vl, vl, vl, 1)
|
||||
end
|
||||
|
||||
self:drawTile(x, y, terrain, k)
|
||||
else
|
||||
love.graphics.setColor(vd, vd, vd, 1)
|
||||
self:drawTile(x, y, 0, k)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
love.graphics.setColor(1, 1, 1, 1)
|
||||
end
|
||||
|
||||
function Map:drawTile(x, y, type, variant)
|
||||
if type == 0 then
|
||||
local tiles = self.datas.tiles*2 + variant
|
||||
self.assets.tileset["normaltiles"]:drawTile(tiles, x, y)
|
||||
else
|
||||
self.assets.tileset["sptiles"]:drawTile(type, x, y)
|
||||
self.assets.tileset["borders"]:drawTile(border, (i-1)*80, self:getStartY()-10 , 0, 1, 1)
|
||||
self.assets.tileset["borders"]:drawTile(border, (i-1)*80, 240-10, 0, 1, 1)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -166,5 +179,4 @@ function Map:drawEffectGrid(effectGrid)
|
|||
end
|
||||
end
|
||||
|
||||
|
||||
return Map
|
||||
|
|
Loading…
Reference in a new issue