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

211 lines
5.2 KiB
Lua
Raw Normal View History

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: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
self:loadRessources()
end
function BattleArena:loadRessources()
local backname = self.datas.background or "city"
local tileline = self.datas.tiles or 1
local borderline = self.datas.borders or 1
self.textures = {}
self.textures.tiles = love.graphics.newImage("assets/backgrounds/normaltile.png")
self.textures.sptiles = love.graphics.newImage("assets/backgrounds/specialtile.png")
self.textures.back1 = love.graphics.newImage("assets/backgrounds/parallax/" .. backname .. "-back.png")
self.textures.back2 = love.graphics.newImage("assets/backgrounds/parallax/" .. backname .. "-fore.png")
self.textures.borders = love.graphics.newImage("assets/backgrounds/borders.png")
self.textures.shadow = love.graphics.newImage("assets/sprites/shadow.png")
self.quads = {}
self.quads.tiles = {}
self.quads.sptiles = {}
local w, h = self.textures.tiles:getDimensions()
self.quads.tiles[1] = love.graphics.newQuad( 0, tileline*24, 40, 24, w, h)
self.quads.tiles[2] = love.graphics.newQuad(40, tileline*24, 40, 24, w, h)
local w, h = self.textures.sptiles:getDimensions()
local h2 = math.floor(h / 26)
for i=1, h2 do
self.quads.sptiles[i] = love.graphics.newQuad(0, (i-1)*26, 40, 26, w, h)
end
local w, h = self.textures.borders:getDimensions()
self.quads.borders = love.graphics.newQuad(0, borderline*10, 80, 10, w, h)
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()
for i=1, 4 do
if (i == 2) or (i == 4) then
love.graphics.draw(self.textures.back1, (i)*240, 20, 0, -1, 1)
else
love.graphics.draw(self.textures.back1, (i-1)*240, 20, 0, 1, 1)
end
end
for i=1, 3 do
love.graphics.draw(self.textures.back2, (i-1)*240, 20, 0, 1, 1)
end
end
function BattleArena:drawBorder()
for i=1, 7 do
love.graphics.draw(self.textures.borders, self.quads.borders, (i-1)*80, 80, 0, 1, 1)
end
end
function BattleArena:drawTerrains()
local vl, vhd, vd
vl = 1
vhd = .7
vd = .5
for i=1, 9 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 - 4, terrain, k)
else
love.graphics.setColor(vd, vd, vd, 1)
self:drawTile(x, y - 4, 0, k)
end
end
end
love.graphics.setColor(1, 1, 1, 1)
end
function BattleArena:drawTile(x, y, type, variant)
if type == 0 then
love.graphics.draw(self.textures.tiles, self.quads.tiles[variant], x, y)
else
love.graphics.draw(self.textures.sptiles, self.quads.sptiles[type], x, y-2)
end
end
return BattleArena