chore(cbs): refactor entirely the code to be more gamecore-like

This commit is contained in:
Kazhnuz 2019-08-14 16:26:23 +02:00
parent becd4aace5
commit 3a51758483
18 changed files with 778 additions and 673 deletions

View file

@ -0,0 +1,33 @@
local Parent = require("scenes.battlesystem.actors.parent")
local Battler = Parent:extend()
function Battler:new(world, x, y, z)
Battler.super.new(self, world, x, y, z)
self.isBattler = true
self.speed = 3
self.isActive = false
self.debugActiveTimer = 0
end
function Battler:setActive()
core.debug:print("cbs/actor","actor " .. self.id .. " is active")
self.isActive = true
self.debugActiveTimer = 0
end
function Battler:update(dt)
if (self.isActive) then
self.debugActiveTimer = self.debugActiveTimer + dt
if self.debugActiveTimer >= 0.5 then
self.world:switchActiveBattler()
self.isActive = false
end
end
end
function Battler:validateAction()
end
return Battler

View file

@ -1,15 +1,15 @@
local Actor = require("scenes.battlesystem.entities.actor") local Battler = require("scenes.battlesystem.actors.battler")
local Ennemy = Actor:extend() local Ennemy = Battler:extend()
function Ennemy:new(controller, x, y) function Ennemy:new(world, x, y)
Ennemy.super.new(self, controller, x, y, 0) Ennemy.super.new(self, world, x, y, 0)
self.isEnnemy = true self.isEnnemy = true
self.actionPerTurn = 2 self.actionPerTurn = 2
end end
function Ennemy:draw() function Ennemy:draw()
x, y = self.controller.battlearena:gridToPixel(self.x, self.y, true) x, y = self.maputils.gridToPixel(self.x, self.y, true)
love.graphics.setColor(1, 0, 0, 1) love.graphics.setColor(1, 0, 0, 1)
love.graphics.rectangle("fill", x - 8, y - 32, 16, 32) love.graphics.rectangle("fill", x - 8, y - 32, 16, 32)
love.graphics.setColor(1, 1, 1, 1) love.graphics.setColor(1, 1, 1, 1)

View file

@ -1,8 +1,8 @@
local Actor = require("scenes.battlesystem.entities.actor") local Battler = require("scenes.battlesystem.actors.battler")
local Character = Actor:extend() local Hero = Battler:extend()
function Character:new(controller, x, y, charid, charnumber) function Hero:new(world, x, y, charid, charnumber)
Character.super.new(self, controller, x, y, 0) Hero.super.new(self, world, x, y, 0)
self.isHero = true self.isHero = true
self.turnAction = nil self.turnAction = nil
self.startx, self.starty = self.x, self.y self.startx, self.starty = self.x, self.y
@ -23,40 +23,40 @@ function Character:new(controller, x, y, charid, charnumber)
self.charnumber = charnumber or 1 self.charnumber = charnumber or 1
end end
function Character:setAnimation(animation) function Hero:setAnimation(animation)
if (self.animation ~= animation) then if (self.animation ~= animation) then
self.animation = animation self.animation = animation
self.assets.sprites[self.charid]:changeAnimation(animation, true) self.assets.sprites[self.charid]:changeAnimation(animation, true)
end end
end end
function Character:draw() function Hero:draw()
x, y = self.controller.battlearena:gridToPixel(self.x, self.y, true) x, y = self.maputils.gridToPixel(self.x, self.y, true)
--love.graphics.rectangle("fill", x - 8, y - 32, 16, 32) --love.graphics.rectangle("fill", x - 8, y - 32, 16, 32)
self:drawSprite() self:drawSprite()
end end
function Character:drawIcon(x, y) function Hero:drawIcon(x, y)
local iconID = 1 local iconID = 1
self.assets.tileset["charicons"]:drawTile(iconID, x, y) self.assets.tileset["charicons"]:drawTile(iconID, x, y)
end end
function Character:setActive() function Hero:setActive()
local gridsize = game.characters.list[self.charid].base_stats.move local gridsize = game.characters.list[self.charid].base_stats.move
if (gridsize == nil) then if (gridsize == nil) then
gridsize = 3 gridsize = 3
core.debug:warning("cbs/character", "move value is nil") core.debug:warning("cbs/character", "move value is nil")
end end
self.controller.cursor:setGrid("square", self.x, self.y, gridsize, self) self.world.cursor:setGrid(self.x, self.y, "square", gridsize, 1, self)
self.startx, self.starty = self.x, self.y self.startx, self.starty = self.x, self.y
self.controller.cursor:set(self.startx, self.starty) self.world.cursor:set(self.startx, self.starty)
self.currentAction = "selectDirection" self.currentAction = "selectDirection"
self.directionPrevious = self.direction self.directionPrevious = self.direction
end end
function Character:update(dt) function Hero:update(dt)
self.keys = self.controller.sources[1].keys self.keys = self.scene:getKeys(1)
if (self.currentAction == "moving") then if (self.currentAction == "moving") then
self.xprevious = self.x self.xprevious = self.x
self.yprevious = self.y self.yprevious = self.y
@ -77,13 +77,13 @@ function Character:update(dt)
self.y = self.dy self.y = self.dy
self:setAnimation("idle") self:setAnimation("idle")
self.currentAction = "selectAttack" self.currentAction = "selectAttack"
self.controller.menu:set( self ) self.scene.menu:set( self )
end end
elseif (self.currentAction == "selectAttack") then elseif (self.currentAction == "selectAttack") then
if (self.keys["B"].isPressed) then if (self.keys["B"].isPressed) then
--self.currentAction = "selectDirection" --self.currentAction = "selectDirection"
--self.controller.cursor:set(self.x, self.y) --self.world.cursor:set(self.x, self.y)
end end
elseif (self.currentAction == "selectDirection") then elseif (self.currentAction == "selectDirection") then
self.xprevious = self.x self.xprevious = self.x
@ -111,47 +111,47 @@ function Character:update(dt)
end end
end end
function Character:validateAction() function Hero:validateAction()
if (self.currentAction == "selectDirection") then if (self.currentAction == "selectDirection") then
self:setAnimation("walk") self:setAnimation("walk")
self.currentAction = "moving" self.currentAction = "moving"
self.dx, self.dy = self.controller.cursor.x, self.controller.cursor.y self.dx, self.dy = self.world.cursor.x, self.world.cursor.y
self.controller.cursor:unset( ) self.world.cursor:unset( )
end end
end end
function Character:getStats() function Hero:getStats()
return game.characters.list[self.charid].stats return game.characters.list[self.charid].stats
end end
function Character:getSignal(action_type, id) function Hero:getSignal(action_type, id)
--print(action_type .. " " .. id) --print(action_type .. " " .. id)
if (action_type == "back") then if (action_type == "back") then
self.currentAction = "selectDirection" self.currentAction = "selectDirection"
self.controller.cursor:set(self.x, self.y) self.world.cursor:set(self.x, self.y)
self:setAnimation("walk") self:setAnimation("walk")
elseif (action_type == "defend") then elseif (action_type == "defend") then
self.turnAction = "defend" self.turnAction = "defend"
self.controller.actormanager:switchActiveActor( ) self.world:switchActiveBattler( )
else else
self.controller.actormanager:switchActiveActor( ) self.world:switchActiveBattler( )
end end
end end
function Character:getBackSignal() function Hero:getBackSignal()
self.currentAction = "selectDirection" self.currentAction = "selectDirection"
self.controller.cursor:set(self.x, self.y) self.world.cursor:set(self.x, self.y)
self:setAnimation("walk") self:setAnimation("walk")
end end
function Character:drawHUD() function Hero:drawHUD()
local HUDBASE = -8 local HUDBASE = -8
local HUDSEP = 152 local HUDSEP = 152
local x = HUDBASE + (self.charnumber-1)*HUDSEP local x = HUDBASE + (self.charnumber-1)*HUDSEP
local y = 32 local y = 32
self.controller.assets.images["statusbar"]:draw(x, y) self.assets.images["statusbar"]:draw(x, y)
end end
return Character return Hero

View file

@ -0,0 +1,8 @@
local entities = {}
local baseURI = "scenes.battlesystem.actors."
entities.Hero = require(baseURI .. "hero")
entities.Ennemy = require(baseURI .. "ennemy")
return entities

View file

@ -0,0 +1,70 @@
local Parent = Object:extend() -- On créer la classe des entitées, c'est la classe de base
local maputils = require "scenes.battlesystem.utils"
function Parent:new(world, x, y, z)
self.depth = 0
self.x = x
self.y = y
self.z = z or 0
self.direction = 1
--self.id = self.world.creationID
self.world = world
self.assets = self.world.assets
self.scene = self.world.scene
self.map = self.world.map
self.maputils = maputils
self.isHero = false
self.isActor = false
self.isEnnemy = false
self:register()
end
function Parent:register()
self.world:registerActor(self)
end
function Parent:setSprite(name, ox, oy, active)
self.sprite = {}
self.sprite.name = name
self.sprite.ox = ox or 0
self.sprite.oy = oy or 0
self.sprite.active = active or false
end
function Parent:drawSprite(tx, ty)
utils.graphics.resetColor()
local x, y = self.maputils.gridToPixel(self.x, self.y, true)
local tx = tx or 0
local ty = ty or 0
if (self.sprite.active) then
self.assets.sprites[self.sprite.name]:drawAnimation(x + tx, y + ty, 0, self.direction, 1, self.sprite.ox, self.sprite.oy)
end
end
function Parent:update(dt)
-- lol
end
function Parent:draw()
end
function Parent:drawShadow()
local x, y = self.maputils.gridToPixel(self.x, self.y, true)
self.assets.images["actorsShadow"]:draw(x, y, 0, 1, 1, 12, 5)
end
function Parent:drawHUD()
end
return Parent

View file

@ -1,140 +0,0 @@
local ActorManager = Object:extend()
local entities = require "scenes.battlesystem.entities"
local POSITIONS = {
{x = 3, y = 4},
{x = 2, y = 2},
{x = 2, y = 6},
}
function ActorManager:new(controller)
self.controller = controller
self.turns = {}
self.turns.current = 1
self.turns.number = 1
self.turns.isFinished = true
self.turns.changeActor = true
self.actorlist = {}
self.actionlist = {}
self.cursor = self.turns.current
for i, v in ipairs(game.characters.team) do
self:addCharacter(POSITIONS[i].x, POSITIONS[i].y, v, i)
end
self:addEnnemy(10, 3, "motobug")
self:addEnnemy(10, 5, "motobug")
end
function ActorManager:generateActionList()
self.actionlist = {}
for i,v in ipairs(self.actorlist) do
for i=1, v.actionPerTurn do
local action = {}
action.actor = v
action.number = i
table.insert(self.actionlist, action)
end
end
end
function ActorManager:sendSignalToCurrentEntity()
self.actionlist[self.turns.current].actor:validateAction()
end
function ActorManager:addCharacter(x, y, id)
local char = entities.Character(self.controller, x, y, id)
table.insert(self.actorlist, char)
end
function ActorManager:addEnnemy(x, y, id)
local enn = entities.Ennemy(self.controller, x, y, id)
table.insert(self.actorlist, enn)
end
function ActorManager:switchActiveActor()
if (self.turns.isFinished) or (self.turns.current >= #self.actionlist) then
self.turns.current = 1
self.turns.isFinished = false
self.turns.number = self.turns.number + 1
self:recalculateTurns()
else
self.turns.current = self.turns.current + 1
end
self.actionlist[self.turns.current].actor:setActive()
self.turns.changeActor = false
end
local function sortActors(a, b)
local astats = a.actor:getStats()
local bstats = b.actor:getStats()
local aspeed = astats.speed / 1.5 * a.number
local bspeed = bstats.speed / 1.5 * b.number
if (aspeed == bspeed) then
if (a.actor.isHero == b.actor.isHero) then
return (a.actor.id > b.actor.id)
else
return a.actor.isHero
end
else
return (aspeed > bspeed)
end
end
function ActorManager:recalculateTurns()
self:generateActionList()
table.sort(self.actionlist, sortActors)
end
function ActorManager:update(dt)
if (self.turns.changeActor) then
self:switchActiveActor( )
end
local cursorSpeed = 16
if (self.turns.current == 1) then
cursorSpeed = 16 * 4
end
if math.abs(self.cursor - self.turns.current) > (cursorSpeed * dt) then
self.cursor = self.cursor - utils.math.sign(self.cursor - self.turns.current) * cursorSpeed * dt
else
self.cursor = self.turns.current
end
end
function ActorManager:countEnnemy()
local count = 0
for i,v in ipairs(self.entities) do
if (v.isEnnemy) then
count = count + 1
end
end
return count
end
function ActorManager:draw()
for i,v in ipairs(self.actionlist) do
v.actor:drawIcon(4 + (i-1)*(20), 6)
end
self.controller.assets.images["menucursor"]:draw(self.cursor * 20 - 6, 26, math.rad(-90), 1, 1, 4, 8)
for i,v in ipairs(self.actorlist) do
v:drawHUD()
end
end
return ActorManager

View file

@ -1,232 +0,0 @@
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
core.debug:print("cbs/battlearena", "one entity found in case <" .. x .. ";" .. y .. ">")
return v
end
end
--core.debug:print("cbs/battlearena", "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

View file

@ -1,144 +0,0 @@
local Cursor = Object:extend()
local EmptyGrid = {
{00,00,00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00,00,00}
}
function Cursor:new(controller)
self.controller = controller
self.x = 1
self.y = 1
self.isActive = false
self.tx = 1
self.ty = 1
self.grid = EmptyGrid
end
function Cursor:set(x, y)
self.x = math.max(math.min(x, 12), 1)
self.y = math.max(math.min(y, 07), 1)
self.tx = self.x
self.ty = self.y
self.isActive = true
end
function Cursor:setGrid(type, x, y, value, whitelistedEntity)
self.grid = {
{00,00,00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00,00,00}
}
if type == "square" then
local x = x - value
local y = y - value
local value = (value * 2) + 1
for i=1, value do
for j=1, value do
local dx, dy
dx = x + i - 1
dy = y + j - 1
if (dy >= 1) and (dy <= 7) and (dx >= 1) and (dx <= 12) then
if ((self.controller.battlearena:getObjectInCase(dx, dy) == nil) or
(self.controller.battlearena:getObjectInCase(dx, dy) == whitelistedEntity) and (whitelistedEntity ~= nil)) and
(self.controller.battlearena:getTerrain(dx, dy) ~= 3) then
self.grid[dy][dx] = 1
end
end
end
end
end
end
function Cursor:gridIsActive(x, y)
if self.grid[y] ~= nil then
return (self.grid[y][x] == 1)
else
return false
end
end
function Cursor:resetGrid()
self.grid = EmptyGrid
end
function Cursor:unset()
self.isActive = false
end
function Cursor:update(dt)
if (self.isActive) then
local keys = self.controller.sources[1].keys
if (keys["up"].isPressed) then
dy = math.max(self.y - 1, 1)
if (self.grid[dy][self.x] == 1) then
self.y = dy
end
end
if (keys["down"].isPressed) then
dy = math.min(self.y + 1, 7)
if (self.grid[dy][self.x] == 1) then
self.y = dy
end
end
if (keys["left"].isPressed) then
dx = math.max(self.x - 1, 1)
if (self.grid[self.y][dx] == 1) then
self.x = dx
end
end
if (keys["right"].isPressed) then
dx = math.min(self.x + 1, 12)
if (self.grid[self.y][dx] == 1) then
self.x = dx
end
end
if (keys["A"].isPressed) then
self.controller.actormanager:sendSignalToCurrentEntity()
end
self.tx = (self.tx) + ((self.x) - (self.tx)) * dt*30
self.ty = (self.ty) + ((self.y) - (self.ty)) * dt*30
-- test
--game
end
end
function Cursor:drawBottom()
if (self.isActive) then
local x, y, frame
x, y = self.controller.battlearena:gridToPixel(self.tx, self.ty, true)
self.controller.assets.sprites["cursorground"]:drawAnimation(x, y, 0, 1, 1, 14, 6)
end
end
function Cursor:drawTop()
if (self.isActive) then
local x, y
x, y = self.controller.battlearena:gridToPixel(self.tx, self.ty, true)
self.controller.assets.images["cursorpeak"]:draw(x, y - 24, 0, 1, 1, 7, 26)
end
end
return Cursor

View file

@ -0,0 +1,142 @@
local Cursor = Object:extend()
local maputils = require "scenes.battlesystem.utils"
function Cursor:new(world)
self.world = world
self.scene = world.scene
self.assets = world.assets
self.x = 1
self.y = 1
self.isActive = false
self.tx = 1
self.ty = 1
self.grid = maputils.newEmptyMap()
end
function Cursor:set(x, y)
self.x = math.max(math.min(x, 12), 1)
self.y = math.max(math.min(y, 07), 1)
self.tx = self.x
self.ty = self.y
self.isActive = true
end
function Cursor:setGrid(ox, oy, shape, size, direction, whitelistedEntity)
self.grid = maputils.newEmptyMap()
for y, line in ipairs(self.grid) do
for x, case in ipairs(line) do
if not maputils.isInMask(x, y, ox, oy, shape, size, direction) then
self.grid[y][x] = 0
else
if (self:testPoint(x, y, whitelistedEntity)) then
self.grid[y][x] = 1
else
self.grid[y][x] = 0
end
end
end
end
end
function Cursor:testPoint(x, y, whitelistedActor)
if ((self.world:getActorInCase(x, y) == nil) or
(self.world:getActorInCase(x, y) == whitelistedActor) and (whitelistedActor ~= nil)) and
(self.world.map:getTerrain(x, y) ~= 3) then
return true
else
return false
end
end
function Cursor:gridIsActive(x, y)
if self.grid[y] ~= nil then
return (self.grid[y][x] == 1)
else
return false
end
end
function Cursor:getGrid()
if (self.isActive) then
return self.grid
else
return maputils.newFullMap()
end
end
function Cursor:resetGrid()
self.grid = EmptyGrid
end
function Cursor:unset()
self.isActive = false
end
function Cursor:update(dt)
if (self.isActive) then
local keys = self.scene:getKeys(1)
if (keys["up"].isPressed) then
dy = math.max(self.y - 1, 1)
if (self.grid[dy][self.x] == 1) then
self.y = dy
end
end
if (keys["down"].isPressed) then
dy = math.min(self.y + 1, 7)
if (self.grid[dy][self.x] == 1) then
self.y = dy
end
end
if (keys["left"].isPressed) then
dx = math.max(self.x - 1, 1)
if (self.grid[self.y][dx] == 1) then
self.x = dx
end
end
if (keys["right"].isPressed) then
dx = math.min(self.x + 1, 12)
if (self.grid[self.y][dx] == 1) then
self.x = dx
end
end
if (keys["A"].isPressed) then
self.world:sendSignalToCurrentBattler()
end
self.tx = (self.tx) + ((self.x) - (self.tx)) * dt*30
self.ty = (self.ty) + ((self.y) - (self.ty)) * dt*30
-- test
--game
end
end
function Cursor:drawBottom()
if (self.isActive) then
local x, y, frame
x, y = maputils.gridToPixel(self.tx, self.ty, true)
self.assets.sprites["cursorground"]:drawAnimation(x, y, 0, 1, 1, 14, 6)
end
end
function Cursor:drawTop()
if (self.isActive) then
local x, y
x, y = maputils.gridToPixel(self.tx, self.ty, true)
self.assets.images["cursorpeak"]:draw(x, y - 24, 0, 1, 1, 7, 26)
end
end
return Cursor

View file

@ -1,34 +0,0 @@
local Entity = require("scenes.battlesystem.entities.base")
local Actor = Entity:extend()
function Actor:new(controller, x, y, z)
Actor.super.new(self, controller, x, y, z)
self.isActor = true
self.speed = 3
self.isActive = false
self.debugActiveTimer = 0
end
function Actor:setActive()
core.debug:print("cbs/actor","actor " .. self.id .. " is active")
self.isActive = true
self.debugActiveTimer = 0
end
function Actor:update(dt)
if (self.isActive) then
self.debugActiveTimer = self.debugActiveTimer + dt
if self.debugActiveTimer >= 0.5 then
self.controller.actormanager:switchActiveActor()
--self.controller.actormanager.turns.changeActor = false
self.isActive = false
end
end
end
function Actor:validateAction()
end
return Actor

View file

@ -1,65 +0,0 @@
local BaseEntity = Object:extend() -- On créer la classe des entitées, c'est la classe de base
function BaseEntity:new(controller, x, y, z)
self.depth = 0
self.x = x
self.y = y
self.z = z or 0
self.direction = 1
--self.id = self.world.creationID
self.controller = controller
self.assets = self.controller.assets
self.isHero = false
self.isActor = false
self.isEnnemy = false
self:register()
end
function BaseEntity:register()
self.controller.battlearena:registerEntity(self)
self.id = self.controller.battlearena.globalID
end
function BaseEntity:setSprite(name, ox, oy, active)
self.sprite = {}
self.sprite.name = name
self.sprite.ox = ox or 0
self.sprite.oy = oy or 0
self.sprite.active = active or false
end
function BaseEntity:drawSprite(tx, ty)
utils.graphics.resetColor()
local x, y = self.controller.battlearena:gridToPixel(self.x, self.y, true)
local tx = tx or 0
local ty = ty or 0
if (self.sprite.active) then
self.assets.sprites[self.sprite.name]:drawAnimation(x + tx, y + ty, 0, self.direction, 1, self.sprite.ox, self.sprite.oy)
end
end
function BaseEntity:update(dt)
-- lol
end
function BaseEntity:draw()
end
function BaseEntity:drawShadow()
local x, y = self.controller.battlearena:gridToPixel(self.x, self.y, true)
self.controller.assets.images["actorsShadow"]:draw(x, y, 0, 1, 1, 12, 5)
end
function BaseEntity:drawHUD()
end
return BaseEntity

View file

@ -1,8 +0,0 @@
local entities = {}
local baseURI = "scenes.battlesystem.entities."
entities.Character = require(baseURI .. "character")
entities.Ennemy = require(baseURI .. "ennemy")
return entities

View file

@ -2,11 +2,8 @@ local Scene = require "core.modules.scenes"
local BattleSystem = Scene:extend() local BattleSystem = Scene:extend()
local BattleArena = require "scenes.battlesystem.controller.battlearena" local World = require "scenes.battlesystem.world"
local ActorManager = require "scenes.battlesystem.controller.actors" local MenuSystem = require "scenes.battlesystem.menu"
local HUD = require "scenes.battlesystem.controller.hud"
local Cursor = require "scenes.battlesystem.controller.cursor"
local MenuSystem = require "scenes.battlesystem.controller.menu"
local gui = require "game.modules.gui" local gui = require "game.modules.gui"
@ -24,27 +21,18 @@ end
function BattleSystem:initManagers() function BattleSystem:initManagers()
self.datas = {} self.datas = {}
self.battlearena = BattleArena(self) self.world = World(self)
self.actormanager = ActorManager(self)
self.cursor = Cursor(self)
self.menu = MenuSystem(self) self.menu = MenuSystem(self)
end end
function BattleSystem:update(dt) function BattleSystem:update(dt)
self.battlearena:update(dt) self.world:update(dt)
self.cursor:update(dt)
self.actormanager:update(dt)
end end
function BattleSystem:draw() function BattleSystem:draw()
self.battlearena:draw() self.world:draw()
self.cursor:drawBottom()
self.battlearena:drawEntities()
self.cursor:drawTop()
love.graphics.draw(self.frame, 424, 20, 0, -1, -1) love.graphics.draw(self.frame, 424, 20, 0, -1, -1)
self.actormanager:draw()
end end
function BattleSystem:exit() function BattleSystem:exit()

View file

@ -0,0 +1,134 @@
local Map = Object:extend()
local maputils = require "scenes.battlesystem.utils"
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.terrains = terrain or maputils.newEmptyMap()
local zones = require "datas.gamedata.maps.shoot.zones"
local datas = zones[self.datas.type]
self.datas.background = datas.background
self.datas.tiles = datas.tiles
self.datas.borders = datas.borders
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
-- GET FUNCTIONS
-- Get information from the map
function Map:getTerrain(x, y)
if self.datas.terrains[y] ~= nil then
return self.datas.terrains[y][x]
else
return nil
end
end
function Map:isInGrid(x, y)
return ( self:getTerrain(x, y) ~= nil )
end
-- DRAW FUNCTIONS
-- Draw the battle map
function Map:draw(activeGrid, effectGrid)
self:drawBackgrounds()
self:drawBorders()
self:drawTerrains(activeGrid)
if (effectGrid ~= nil) then
self:drawEffectGrid(effectGrid)
end
end
function Map: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, maputils.CONST.STARTY-h2, 0, 1, 1)
end
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)
end
end
function Map:drawEffectGrid(effectGrid)
for i=1,12 do
for j=1,7 do
if (effectGrid[i][j] == true) then
local x, y = maputils.gridToPixel(i, j)
self.assets.images["emptytile"]:draw(x, y)
end
end
end
end
return Map

View file

@ -149,6 +149,7 @@ end
function CharMenuWidget:action() function CharMenuWidget:action()
self.character:getSignal(self.menuname, self.label) self.character:getSignal(self.menuname, self.label)
self.scene:flushKeys()
self.scene.menusystem:reset() self.scene.menusystem:reset()
end end

View file

@ -0,0 +1,104 @@
local maputils = {}
maputils.CONST = {}
maputils.CONST.STARTX = -8
maputils.CONST.STARTY = 90
function maputils.newEmptyMap()
return {
{00,00,00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00,00,00},
}
end
function maputils.newFullMap()
return {
{01,01,01,01,01,01,01,01,01,01,01,01},
{01,01,01,01,01,01,01,01,01,01,01,01},
{01,01,01,01,01,01,01,01,01,01,01,01},
{01,01,01,01,01,01,01,01,01,01,01,01},
{01,01,01,01,01,01,01,01,01,01,01,01},
{01,01,01,01,01,01,01,01,01,01,01,01},
{01,01,01,01,01,01,01,01,01,01,01,01},
}
end
function maputils.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
else
if shape == nil then
shape = "nil"
end
core.debug:warning("maputils", "shape " .. shape .. " doesn't exist")
end
end
function maputils.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 = maputils.CONST.STARTY + ((y-1) * 20)
return math.floor(pixelx), math.floor(pixely)
end
function maputils.sortBattlers(a, b)
local astats = a.actor:getStats()
local bstats = b.actor:getStats()
local aspeed = astats.speed / 1.5 * a.number
local bspeed = bstats.speed / 1.5 * b.number
if (aspeed == bspeed) then
if (a.actor.isHero == b.actor.isHero) then
return (a.actor.id > b.actor.id)
else
return a.actor.isHero
end
else
return (aspeed > bspeed)
end
end
return maputils

View file

@ -0,0 +1,248 @@
local World = Object:extend()
local maputils = require "scenes.battlesystem.utils"
local Map = require "scenes.battlesystem.map"
local Cursor = require "scenes.battlesystem.cursor"
local POSITIONS = {
{x = 3, y = 4},
{x = 2, y = 2},
{x = 2, y = 6},
}
-- INIT FUNCTIONS
-- Initialize the battle world
function World:new(scene, battlefile)
self.scene = scene
self.assets = scene.assets
self.obj = require "scenes.battlesystem.actors"
self.actors = {}
self.globalID = 0
self.turns = {}
self.turns.current = 1
self.turns.number = 1
self.turns.isFinished = true
self.turns.changeBattler = true
self.battlers = {}
self.actionlist = {}
self.BattlerCursor = self.turns.current
self.heroNumber = 0
self.ennNumber = 0
self.map = Map(self, "city")
self.cursor = Cursor(self)
self:initHeroes()
self:initEnnemies()
end
function World:initHeroes(battlefile)
for i, hero in ipairs(game.characters.team) do
self:addHero(POSITIONS[i].x, POSITIONS[i].y, hero, i)
end
end
function World:initEnnemies(battlefile)
self:addEnnemy(10, 3, "motobug")
self:addEnnemy(10, 5, "motobug")
end
function World:registerActor(actor)
self.globalID = self.globalID + 1
table.insert(self.actors, actor)
actor.id = self.globalID
end
-- INFO FUNCTIONS
-- Get info from the world
function World:caseIsEmpty(x, y)
local isEmpty = true
for i, actor in ipairs(self.actors) do
if (actor.x == x) and (actor.y == y) then
isEmpty = false
else
isEmpty = true
end
end
return isEmpty
end
function World:getActorInCase(x, y)
for i, actor in ipairs(self.actors) do
if (actor.x == x) and (actor.y == y) then
core.debug:print("cbs/world", "one actor found in case <" .. x .. ";" .. y .. ">")
return actor
end
end
return nil
end
-- BATTLER FUNCTIONS
-- Handle the actual battle participants
function World:addHero(x, y, id)
self.heroNumber = self.heroNumber + 1
local hero = self.obj.Hero(self, x, y, id, self.heroNumber)
table.insert(self.battlers, hero)
end
function World:addEnnemy(x, y, id)
self.ennNumber = self.ennNumber + 1
local enn = self.obj.Ennemy(self, x, y, id, self.ennNumber)
table.insert(self.battlers, enn)
end
function World:generateActionList()
self.actionlist = {}
for i,v in ipairs(self.battlers) do
for i=1, v.actionPerTurn do
local action = {}
action.actor = v
action.number = i
table.insert(self.actionlist, action)
end
end
end
function World:countHeroes()
local count = 0
for i, battler in ipairs(self.battlers) do
if (battler.isHero) then
count = count + 1
end
end
return count
end
function World:countEnnemies()
local count = 0
for i, battler in ipairs(self.battlers) do
if (battler.isEnnemy) then
count = count + 1
end
end
return count
end
-- UPDATE FUNCTION
-- Update all actors
function World:update(dt)
for i, actor in ipairs(self.actors) do
actor:update(dt)
end
self:updateTurns(dt)
self:moveBattleCursor(dt)
self.cursor:update(dt)
end
function World:moveBattleCursor(dt)
local cursorSpeed = 16
if (self.turns.current == 1) then
cursorSpeed = 16 * 4
end
if math.abs(self.BattlerCursor - self.turns.current) > (cursorSpeed * dt) then
self.BattlerCursor = self.BattlerCursor - utils.math.sign(self.BattlerCursor - self.turns.current) * cursorSpeed * dt
else
self.BattlerCursor = self.turns.current
end
end
-- TURNS FUNCTIONS
-- Handle everything related to the turn system
function World:recalculateTurns()
self:generateActionList()
table.sort(self.actionlist, maputils.sortBattlers)
end
function World:updateTurns(dt)
if (self.turns.changeBattler) then
self:switchActiveBattler( )
end
end
function World:switchActiveBattler()
if (self.turns.isFinished) or (self.turns.current >= #self.actionlist) then
self.turns.current = 1
self.turns.isFinished = false
self.turns.number = self.turns.number + 1
self:recalculateTurns()
else
self.turns.current = self.turns.current + 1
end
self.actionlist[self.turns.current].actor:setActive()
self.turns.changeBattler = false
end
function World:sendSignalToCurrentBattler()
self.actionlist[self.turns.current].actor:validateAction()
end
-- DRAW FUNCTION
-- Draw the world
function World:draw()
local activeGrid = self.cursor:getGrid()
self.map:draw(activeGrid)
self.cursor:drawBottom()
self:drawShadows()
self:drawActors()
self.cursor:drawTop()
self:drawHUD()
end
function World:drawActors()
for i, actor in ipairs(self.actors) do
actor:draw()
end
end
function World:drawShadows()
for i, actor in ipairs(self.actors) do
actor:drawShadow()
end
end
function World:drawHUD()
for i,v in ipairs(self.actionlist) do
v.actor:drawIcon(4 + (i-1)*(20), 6)
end
local cursorx = self.BattlerCursor * 20 - 6
self.assets.images["menucursor"]:draw(cursorx, 26, math.rad(-90), 1, 1, 4, 8)
for i,v in ipairs(self.battlers) do
v:drawHUD()
end
end
return World