sonic-radiance/sonic-radiance.love/scenes/battlesystem/controller/battlearena.lua

232 lines
5.7 KiB
Lua

local BattleArena = Object:extend()
local EmptyArena = {
{00,00,00,00,00,00,03,03,03,00,00,00},
{00,00,00,00,00,00,03,03,03,00,00,00},
{00,00,00,00,00,03,03,03,00,00,00,00},
{00,00,00,00,00,03,03,03,00,00,00,00},
{00,00,00,00,00,03,03,03,00,00,00,00},
{00,00,00,00,00,00,03,03,03,00,00,00},
{00,00,00,00,00,00,03,03,03,00,00,00}
}
local _GROUND_X, _GROUND_Y
_GROUND_X = -8
_GROUND_Y = 90
function BattleArena:new(controller)
self.controller = controller
self.assets = self.controller.assets
self:initArena()
self.entities = {}
self.globalID = 0
end
function BattleArena:registerEntity(entity)
table.insert(self.entities, entity)
self.globalID = self.globalID + 1
end
function BattleArena:initArena(battlefile)
self.datas = {}
self.datas.background = "city"
self.datas.tiles = 1
self.datas.borders = 1
self.datas.terrains = EmptyArena
local backpath = "assets/backgrounds/parallax/" .. self.datas.background
self.assets:addImage("back1", backpath .. "-back.png")
self.assets:addImage("back2", backpath .. "-fore.png")
self.assets:addImage("cliff", backpath .. "-cliff.png")
end
function BattleArena: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 = _GROUND_X + ((x-1) * 31) + ((y-1) * 10)
pixely = _GROUND_Y + ((y-1) * 20)
return math.floor(pixelx), math.floor(pixely)
end
function BattleArena:getTerrain(x, y)
if self.datas.terrains[y] ~= nil then
return self.datas.terrains[y][x]
else
return nil
end
end
function BattleArena:isInGrid(x, y)
--return ((y >= 1) and (y <= 7) and (x >= 1) and (x <= 12))
return ( self:getTerrain(x, y) ~= nil )
end
function BattleArena:caseIsEmpty(x, y)
local isEmpty = true
for i,v in ipairs(self.entities) do
if (v.x == x) and (v.y == y) then
isEmpty = false
else
isEmpty = true
end
end
return isEmpty
end
function BattleArena:getObjectInCase(x, y)
for i,v in ipairs(self.entities) do
if (v.x == x) and (v.y == y) then
print("one entity found in case " .. x .. ";" .. y)
return v
end
end
print("no entity found in case " .. x .. ";" .. y)
return nil
end
function BattleArena:update(dt)
for i,v in ipairs(self.entities) do
v:update(dt)
end
end
function BattleArena:draw()
self:drawBackgrounds()
self:drawBorder()
self:drawTerrains()
self:drawShadows()
end
function BattleArena:drawEntities()
for i,v in ipairs(self.entities) do
v:draw()
end
end
function BattleArena:drawShadows()
for i,v in ipairs(self.entities) do
v:drawShadow()
end
end
function BattleArena:drawBackgrounds()
local w, _ = core.screen:getDimensions()
local w2, h2 = self.assets.images["back1"]:getDimensions()
local imax = math.ceil(w / w2) + 1
for i=1, imax do
self.assets.images["back1"]:draw((i-1)*w2, 0, 0, 1, 1)
end
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, _GROUND_Y-h2, 0, 1, 1)
end
end
function BattleArena:drawBorder()
local border = self.datas.borders + 1
for i=1, 7 do
self.assets.tileset["borders"]:drawTile(border, (i-1)*80, _GROUND_Y-10 , 0, 1, 1)
self.assets.tileset["borders"]:drawTile(border, (i-1)*80, _GROUND_Y+20*7, 0, 1, 1)
end
end
function BattleArena:drawTerrains()
local vl, vhd, vd
vl = 1
vhd = .7
vd = .5
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 = self:gridToPixel(j, i, false)
if (terrain ~= nil) then
local isActive = self.controller.cursor.isActive
if ((isActive == false) or (self.controller.cursor:gridIsActive(j, i))) then
love.graphics.setColor(vl, vl, vl, 1)
else
love.graphics.setColor(vhd, vhd, vhd, 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 BattleArena: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)
end
end
function BattleArena:showMask(ox, oy, shape, size, direction)
for i=1,12 do
for j=1,7 do
if self:isInMask(i, j, ox, oy, shape, size, direction) then
local x, y = self:gridToPixel(i, j)
self.assets.images["emptytile"]:draw(x, y)
end
end
end
end
function BattleArena:isInMask(x, y, ox, oy, shape, size, direction)
local direction = direction or 1
local shape = shape or "point"
if shape == "point" then
return ((x == ox) and (y == oy))
elseif shape == "square" then
local x1 = ox - math.floor(size/2)
local x2 = ox + math.ceil(size/2)
local y1 = oy - math.floor(size/2)
local y2 = oy + math.ceil(size/2)
return ((x >= x1) and (x <= x2) and (y >= y1) and (y <= y2))
elseif shape == "circle" then
local lenght = utils.math.pointDistance(x, y, ox, oy)
return (lenght <= size)
elseif shape == "fullheight" then
local x2 = ox + (size*direction)
return ((x >= ox) and (x <= x2))
elseif shape == "fullwidth" then
local y2 = oy + (size*direction)
return ((y >= oy) and (y <= y2))
elseif shape == "line" then
local x2 = ox + (size*direction)
return ((y == oy) and (x >= ox) and (x <= x2))
elseif shape == "column" then
local y2 = oy + (size*direction)
return ((x == ox) and (y >= oy) and (y <= y2))
elseif shape == "everything" then
return true
end
end
return BattleArena