scenes: add battlesystem from previous code version

This commit is contained in:
Kazhnuz 2019-03-10 13:11:26 +01:00
parent 5593a8f804
commit cb92a8fb3b
26 changed files with 1241 additions and 0 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 582 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 457 B

View file

@ -0,0 +1,6 @@
return {
metadata = {
height = 16,
width = 16
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.7 KiB

View file

@ -0,0 +1,23 @@
return {
metadata = {
height = 64,
width = 64,
defaultAnim = "idle"
},
animations = {
["idle"] = {
startAt = 1,
endAt = 6,
loop = 1,
speed = 16,
pauseAtEnd = false,
},
["walk"] = {
startAt = 7,
endAt = 14,
loop = 7,
speed = -1,
pauseAtEnd = false,
},
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

View file

@ -0,0 +1,16 @@
return {
metadata = {
height = 16,
width = 16,
defaultAnim = "default"
},
animations = {
["default"] = {
startAt = 1,
endAt = 1,
loop = 1,
speed = 0,
pauseAtEnd = true,
},
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 407 B

View file

@ -0,0 +1,16 @@
return {
metadata = {
height = 16,
width = 16,
defaultAnim = "default"
},
animations = {
["default"] = {
startAt = 1,
endAt = 8,
loop = 1,
speed = 8,
pauseAtEnd = false,
},
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

View file

@ -0,0 +1,142 @@
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
self.controller.assets:addTileset("charicons", "assets/sprites/characters/charicons")
for i, v in ipairs(game.characters.team) do
self:addCharacter(POSITIONS[i].x, POSITIONS[i].y, v)
end
self:addEnnemy(10, 3, "motobug")
self:addEnnemy(10, 5, "motobug")
--print("entities")
self.cursorTexture = love.graphics.newImage("assets/gui/cursor-menulist.png")
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), 1)
end
love.graphics.draw(self.cursorTexture, self.cursor * 20 - 6, 18, math.rad(-90), 1, 1, 4, 8)
end
return ActorManager

View file

@ -0,0 +1,81 @@
local Background = Object:extend()
function Background:new(controller, levelname)
self.controller = controller
local backname = self.controller.datas.background or "city"
local tileline = self.controller.datas.tiles or 1
local borderline = self.controller.datas.borders or 1
self.textures = {}
self.textures.tiles = love.graphics.newImage("assets/levels/normaltile.png")
self.textures.back1 = love.graphics.newImage("assets/levels/backgrounds/" .. backname .. "-back.png")
self.textures.back2 = love.graphics.newImage("assets/levels/backgrounds/" .. backname .. "-fore.png")
self.textures.borders = love.graphics.newImage("assets/levels/borders.png")
self.quads = {}
self.quads.tiles = {}
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.borders:getDimensions()
self.quads.borders = love.graphics.newQuad(0, borderline*10, 80, 10, w, h)
end
function Background:destroy()
self.back1:release( )
self.back2:release( )
end
function Background:draw()
local x0, x1, x2
local turn = 0
x0 = 0
x1 = x0 / 3 % 240
x2 = x0 / 9 % 480
local sx = 1
for i=1, 4 do
if (i == 2) or (i == 4) then
love.graphics.draw(self.textures.back1, (i)*240 - x2, 20, 0, -1, 1)
else
love.graphics.draw(self.textures.back1, (i-1)*240 - x2, 20, 0, 1, 1)
end
end
for i=1, 7 do
love.graphics.draw(self.textures.borders, self.quads.borders, (i-1)*80, 80, 0, 1, 1)
end
for i=1, 3 do
love.graphics.draw(self.textures.back2, (i-1)*240 - x1, 20, 0, 1, 1)
end
--self:drawBorders()
self:drawGround(-8, 86)
end
function Background:drawGround(x, y)
for i=1, 9 do
for j= -2, 17 do
local k = 1 + ((i + j) % 2)
if (i >= 1) and (i <= 7) and (j >= 1) and (j <= 12) then
love.graphics.setColor(1, 1, 1, 1)
else
love.graphics.setColor(.6,.6,.6, 1)
end
love.graphics.draw(self.textures.tiles, self.quads.tiles[k], x + (j-1)*31 + (i-1)*10 , y + (i-1)*20)
end
end
love.graphics.setColor(1, 1, 1, 1)
end
return Background

View file

@ -0,0 +1,210 @@
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/levels/normaltile.png")
self.textures.sptiles = love.graphics.newImage("assets/levels/specialtile.png")
self.textures.back1 = love.graphics.newImage("assets/levels/backgrounds/" .. backname .. "-back.png")
self.textures.back2 = love.graphics.newImage("assets/levels/backgrounds/" .. backname .. "-fore.png")
self.textures.borders = love.graphics.newImage("assets/levels/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

View file

@ -0,0 +1,165 @@
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.frame = 1
self.tx = 1
self.ty = 1
self.grid = EmptyGrid
self:loadRessources()
end
function Cursor:loadRessources()
self.texture = love.graphics.newImage("assets/gui/cursor.png")
self.texture2 = love.graphics.newImage("assets/gui/cursor2.png")
local w, h = self.texture:getDimensions()
self.sprite = {}
self.sprite[1] = love.graphics.newQuad(0, 0, 28, 11, w, h)
self.sprite[2] = love.graphics.newQuad(28, 0, 28, 11, w, h)
self.sprite[3] = love.graphics.newQuad(56, 0, 28, 11, w, h)
self.sprite[4] = love.graphics.newQuad(28, 0, 28, 11, w, h)
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
self.frame = self.frame + dt*4
if (self.frame >= 4) then
self.frame = 1
end
local keys = game.input.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)
frame = math.floor(self.frame)
love.graphics.draw(self.texture, self.sprite[frame], 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)
love.graphics.draw(self.texture2, x, y - 24, 0, 1, 1, 7, 26)
end
end
return Cursor

View file

@ -0,0 +1,55 @@
local BattleHUD = Object:extend()
function BattleHUD:new(controller)
self.controller = controller
self:loadAssets()
end
function BattleHUD:loadAssets()
self.progressbarImage = love.graphics.newImage("assets/gui/progressbar.png")
self.barBack = love.graphics.newQuad(0, 0, 211, 12, 211, 24)
self.barFore = love.graphics.newQuad(0, 12, 211, 12, 211, 24)
self.hud1 = love.graphics.newImage("assets/gui/boosthud1.png")
self.hud2 = love.graphics.newImage("assets/gui/boosthud2.png")
self.hud7 = love.graphics.newImage("assets/gui/status_bar.png")
self.ring = love.graphics.newImage("assets/gui/ring.png")
self.font = love.graphics.newImageFont("assets/gui/hudnumbers.png", " 0123456789:/", 1)
self.font2 = love.graphics.newImageFont("assets/gui/hudsmallnumbers.png", " 0123456789", 0)
self.time = love.graphics.newImage("assets/gui/hudtime.png")
self.score = love.graphics.newImage("assets/gui/hudscore.png")
self.bonus = love.graphics.newImage("assets/gui/hudbonus.png")
self.controller.assets:addTileset("lifeicon", "assets/sprites/characters/charicons")
end
function BattleHUD:destroy( )
self.progressbarImage:release( )
self.barBack:release( )
self.barFore:release( )
self.hud1:release( )
self.hud2:release( )
end
function BattleHUD:update(dt)
end
function BattleHUD:draw()
love.graphics.setFont( self.font )
self:drawFrame()
--self.controller:resetFont( )
end
function BattleHUD:drawFrame()
love.graphics.draw(self.hud1, 0, 0)
love.graphics.draw(self.hud7, -8, 22)
love.graphics.draw(self.hud7, -8, 22 + 24)
love.graphics.draw(self.hud7, -8 + 126 - 2, 23)
--love.graphics.draw(self.hud2, 0, 200)
end
return BattleHUD

View file

@ -0,0 +1,64 @@
local Controller = Object:extend()
local GUI = require "modules.gui"
local Assets = require "modules.assets"
local BattleArena = require "scenes.battlesystem.controller.battlearena"
local ActorManager = require "scenes.battlesystem.controller.actors"
local HUD = require "scenes.battlesystem.controller.hud"
local Cursor = require "scenes.battlesystem.controller.cursor"
local MenuSystem = require "scenes.battlesystem.controller.menu"
function Controller:new()
self.assets = Assets()
self.gui = GUI()
self:initManagers()
end
function Controller:restart()
end
function Controller:exitLevel()
Gamestate.switch(Scenes.options)
end
function Controller:initManagers()
--self.loader = Loader()
self.datas = {}
self.battlearena = BattleArena(self)
self.actormanager = ActorManager(self)
self.hud = HUD(self)
self.cursor = Cursor(self)
self.menusystem = MenuSystem(self)
end
function Controller:destroy()
self.world:destroy()
self.battlearena = nil
--self.assets:clear()
end
function Controller:update(dt)
-- Ici sera tout ce qui se passe dans une update
self.hud:update(dt)
self.battlearena:update(dt)
self.cursor:update(dt)
self.assets:update(dt)
self.actormanager:update(dt)
self.menusystem:update(dt)
end
function Controller:draw()
self.battlearena:draw()
self.cursor:drawBottom()
self.battlearena:drawEntities()
self.cursor:drawTop()
self.hud:draw()
self.actormanager:draw()
self.menusystem:draw(dt)
end
return Controller

View file

@ -0,0 +1,160 @@
local MenuSystem = Object:extend()
local BaseMenu = {"attack", "techs", "object", "defend", "flee", "back"}
local MENUPOS_X1, MENUPOS_X2, MENUPOS_Y = 32, 32, 110
function MenuSystem:new( controller )
self.controller = controller
self.isActive = false
self.techList = {}
self.menu = {}
self.menu[1] = BaseMenu
self.texture = love.graphics.newImage("assets/gui/attacklist.png")
self.cursorTexture = love.graphics.newImage("assets/gui/cursor-menulist.png")
self.font = love.graphics.newFont("assets/fonts/PixelOperator.ttf", 16)
self.cursor = 1
self.cursorTransition = 1
self.view = 1
end
function MenuSystem:set(currentCharacter)
self.isActive = true
self.cursor = 1
self.cursorTransition = 0
self.activeMenu = 1
self.character = currentCharacter
self.menu[2] = self:getSkillList()
self.menu[3] = {"healitems", "rings", "wisps", "other", "back"}
end
function MenuSystem:getSkillList()
local list = game.characters:getSkillList(self.character.charid)
local skillmenu = {}
for k,v in pairs(list) do
table.insert(skillmenu, k)
end
table.insert(skillmenu, "back")
return skillmenu
end
function MenuSystem:unset( )
self.isActive = false
end
function MenuSystem:update(dt)
if (self.isActive) then
local keys = game.input.keys
if self.cursor < self.view then
self.view = self.cursor
end
if self.cursor > self.view + 5 then
self.view = self.cursor - 5
end
local relativecursor = self.cursor - self.view
local transition = self.cursorTransition - relativecursor
if math.abs(transition) < 0.1 then
self.cursorTransition = relativecursor
else
self.cursorTransition = (self.cursorTransition) + ((relativecursor) - (self.cursorTransition)) * dt*45
end
if (keys["up"].isPressed) then
if (self.cursor == 1) then
self.cursor = #self.menu[self.activeMenu]
else
self.cursor = self.cursor - 1
end
end
if (keys["down"].isPressed) then
if (self.cursor == #self.menu[self.activeMenu]) then
self.cursor = 1
else
self.cursor = self.cursor + 1
end
end
if keys["left"].isPressed then
self.character.direction = -1
end
if keys["right"].isPressed then
self.character.direction = 1
end
if (keys["A"].isPressed) then
if self.activeMenu == 1 then
if (self.cursor == 2 or self.cursor == 3) then
self.activeMenu = self.cursor
self.cursor = 1
else
self:sendSignal(self.activeMenu, self.cursor)
end
else
self:sendSignal(self.activeMenu, self.cursor)
end
end
if (keys["B"].isPressed) then
if self.activeMenu == 1 then
self:sendSignal(1, 6)
else
self.cursor = self.activeMenu
self.activeMenu = 1
end
end
end
end
function MenuSystem:sendSignal(menu, id)
--print(self.menu[menu][id])
self.isActive = false
if (menu == 1) then
self.character:getSignal(BaseMenu[id], 1)
else
self.character:getSignal(BaseMenu[menu], id)
end
end
function MenuSystem:draw()
if (self.isActive) then
local x, y, addition
x = 32
y = 32
addition = 16
local currentMenu = self.menu[self.activeMenu]
-- MENUPOS_X1, MENUPOS_X2, MENUPOS_Y
local MENUPOS_X = MENUPOS_X1
local maxView = math.min(#currentMenu, (self.view + 5))
for i = self.view, maxView do
love.graphics.draw(self.texture, MENUPOS_X - 16, MENUPOS_Y + (i-self.view)*addition + 3)
love.graphics.setFont(self.font)
love.graphics.setColor(0, 0, 0, .8)
love.graphics.print(currentMenu[i], MENUPOS_X + 1, MENUPOS_Y + (i-self.view)*addition + 1)
if (i == self.cursor) then
love.graphics.setColor(1, 1, 1, 1)
else
love.graphics.setColor(1, 1, 1, 1)
end
love.graphics.print(currentMenu[i], MENUPOS_X, MENUPOS_Y + (i-self.view)*addition)
love.graphics.setColor(1, 1, 1, 1)
end
love.graphics.draw(self.cursorTexture, MENUPOS_X - 12, MENUPOS_Y + (self.cursorTransition) * addition + 2 )
end
end
return MenuSystem

View file

@ -0,0 +1,36 @@
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()
print("actor " .. self.id .. " is active")
self.isActive = true
self.debugActiveTimer = 0
print(self.debugActiveTimer)
end
function Actor:update(dt)
if (self.isActive) then
self.debugActiveTimer = self.debugActiveTimer + dt
print(math.floor(self.debugActiveTimer * 60))
if self.debugActiveTimer >= 1 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

@ -0,0 +1,61 @@
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.draw.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)
love.graphics.draw(self.controller.battlearena.textures.shadow, x, y, 0, 1, 1, 12, 5)
end
return BaseEntity

View file

@ -0,0 +1,135 @@
local Actor = require("scenes.battlesystem.entities.actor")
local Character = Actor:extend()
function Character:new(controller, x, y, charid)
Character.super.new(self, controller, x, y, 0)
self.isHero = true
self.turnAction = nil
self.startx, self.starty = self.x, self.y
self.dx, self.dy = self.x, self.y
self.direction = 1
self.directionPrevious = 1
if charid == nil then
err("FATAL ERROR: charid not set")
end
self.charid = charid
self.actionPerTurn = game.characters.list[self.charid].base_stats.turns
self.assets:addSprite(charid, "assets/sprites/characters/" .. charid)
self.assets.sprites[self.charid]:setCustomSpeed(16)
self:setAnimation("idle")
self:setSprite(charid, 32, 48, true)
end
function Character:setAnimation(animation)
if (self.animation ~= animation) then
self.animation = animation
self.assets.sprites[self.charid]:changeAnimation(animation, true)
end
end
function Character:draw()
x, y = self.controller.battlearena:gridToPixel(self.x, self.y, true)
--love.graphics.rectangle("fill", x - 8, y - 32, 16, 32)
self:drawSprite()
end
function Character:drawIcon(x, y)
local iconID = 1
self.assets.tileset["charicons"]:drawTile(iconID, x, y)
end
function Character:setActive()
local gridsize = game.characters.list[self.charid].base_stats.move
self.controller.cursor:setGrid("square", self.x, self.y, gridsize, self)
self.startx, self.starty = self.x, self.y
self.controller.cursor:set(self.startx, self.starty)
self.currentAction = "selectDirection"
self.directionPrevious = self.direction
end
function Character:update(dt)
if (self.currentAction == "moving") then
self.xprevious = self.x
self.yprevious = self.y
self.x = (self.x) + ((self.dx) - (self.x)) * dt*15
self.y = (self.y) + ((self.dy) - (self.y)) * dt*15
local xspeed, yspeed = math.abs(self.x - self.xprevious),
math.abs(self.y - self.yprevious)
local speed = math.sqrt(xspeed*xspeed + yspeed*yspeed) * 32
self.assets.sprites[self.charid]:setCustomSpeed(speed * 60)
local direction = utils.math.sign(self.x - self.xprevious)
if direction ~= 0 then
self.direction = direction
end
if (math.abs(self.x - self.dx) < 0.01) and (math.abs(self.y - self.dy) < 0.01) then
self.x = self.dx
self.y = self.dy
self:setAnimation("idle")
self.currentAction = "selectAttack"
self.controller.menusystem:set( self )
end
elseif (self.currentAction == "selectAttack") then
if (game.input.keys["B"].isPressed) then
--self.currentAction = "selectDirection"
--self.controller.cursor:set(self.x, self.y)
end
elseif (self.currentAction == "selectDirection") then
self.xprevious = self.x
self.yprevious = self.y
self.x = (self.x) + ((self.startx) - (self.x)) * dt*15
self.y = (self.y) + ((self.starty) - (self.y)) * dt*15
local xspeed, yspeed = math.abs(self.x - self.xprevious),
math.abs(self.y - self.yprevious)
local speed = math.sqrt(xspeed*xspeed + yspeed*yspeed) * 32
self.assets.sprites[self.charid]:setCustomSpeed(speed * 60)
local direction = utils.math.sign(self.x - self.xprevious)
if direction ~= 0 then
self.direction = direction
end
if (math.abs(self.x - self.startx) < 0.01) and (math.abs(self.y - self.starty) < 0.01) then
self.x = self.startx
self.y = self.starty
self:setAnimation("idle")
self.direction = self.directionPrevious
end
end
end
function Character:validateAction()
if (self.currentAction == "selectDirection") then
self:setAnimation("walk")
self.currentAction = "moving"
self.dx, self.dy = self.controller.cursor.x, self.controller.cursor.y
self.controller.cursor:unset( )
end
end
function Character:getStats()
return game.characters.list[self.charid].stats
end
function Character:getSignal(action_type, id)
--print(action_type .. " " .. id)
if (action_type == "back") then
self.currentAction = "selectDirection"
self.controller.cursor:set(self.x, self.y)
self:setAnimation("walk")
elseif (action_type == "defend") then
self.turnAction = "defend"
self.controller.actormanager:switchActiveActor( )
else
self.controller.actormanager:switchActiveActor( )
end
end
return Character

View file

@ -0,0 +1,31 @@
local Actor = require("scenes.battlesystem.entities.actor")
local Ennemy = Actor:extend()
function Ennemy:new(controller, x, y)
Ennemy.super.new(self, controller, x, y, 0)
self.isEnnemy = true
self.actionPerTurn = 2
end
function Ennemy:draw()
x, y = self.controller.battlearena:gridToPixel(self.x, self.y, true)
love.graphics.setColor(1, 0, 0, 1)
love.graphics.rectangle("fill", x - 8, y - 32, 16, 32)
love.graphics.setColor(1, 1, 1, 1)
end
function Ennemy:drawIcon(x, y)
love.graphics.setColor(1, 0, 0, 1)
love.graphics.rectangle("fill", x, y, 16, 16)
love.graphics.setColor(1, 1, 1, 1)
end
function Ennemy:getStats()
local stats = {}
stats.speed = 100
return stats
end
return Ennemy

View file

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

View file

@ -0,0 +1,31 @@
local BattleSystem = {}
local Controller = require "scenes.battlesystem.controller"
function BattleSystem:enter()
self.controller = Controller()
--self.background = love.graphics.newImage("assets/background/testbackground.png")
end
function BattleSystem:update(dt)
game:update(dt)
game.input:update(dt)
self.controller:update(dt)
end
function BattleSystem:draw()
--CScreen:apply()
love.graphics.setColor(1, 1, 1, 1)
love.graphics.rectangle("fill", 0, 0, 424, 240)
--love.graphics.draw(self.background, 0, 0)
self.controller:draw()
--CScreen:cease()
end
function BattleSystem:leave()
self.controller:destroy()
self.controller = nil
collectgarbage()
end
return BattleSystem

View file

@ -1,4 +1,5 @@
return { return {
test = require "scenes.test_scene", test = require "scenes.test_scene",
title = require "scenes.titlescreen", title = require "scenes.titlescreen",
cbs = require "scenes.battlesystem",
} }