Skip to content

Commit

Permalink
Clean up character data before starting mission
Browse files Browse the repository at this point in the history
Closes #277.
  • Loading branch information
rm-code committed Sep 9, 2018
1 parent c397827 commit 2d2b8b2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
20 changes: 17 additions & 3 deletions src/base/BaseScreen.lua
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,10 @@ end
-- @tparam number y The parent's absolute coordinates along the y-axis.
-- @treturn UIButton The newly created UIButton.
--
local function createNextMissionButton( x, y )
local function createNextMissionButton( x, y, factionData )
-- The function to call when the button is activated.
local function callback()
SaveHandler.copyPlayerFaction( factionData )
ScreenManager.switch( 'combat' )
end

Expand All @@ -135,6 +136,19 @@ local function createNextMissionButton( x, y )
return UIButton( x, y, rx, ry, w, h, callback, Translator.getText( 'base_next_mission' ))
end

---
-- Clean the character data for the next mission.
-- @tparam table factionData The data to clean.
-- @treturn table The cleaned character data.
--
local function cleanUpFactionData( factionData )
for _, character in ipairs( factionData ) do
character.x, character.y = nil, nil
character.body.hp = character.body.maxHP
character.actionPoints = character.maxActionPoints
end
return factionData
end

-- ------------------------------------------------
-- Constructor
Expand All @@ -143,7 +157,7 @@ end
function BaseScreen:initialize()
self.x, self.y = GridHelper.centerElement( UI_GRID_WIDTH, UI_GRID_HEIGHT )

self.factionData = SaveHandler.pastePlayerFaction()
self.factionData = cleanUpFactionData( SaveHandler.pastePlayerFaction() )

self.outlines = generateOutlines( self.x, self.y )
self.background = UIBackground( self.x, self.y, 0, 0, UI_GRID_WIDTH, UI_GRID_HEIGHT )
Expand All @@ -152,7 +166,7 @@ function BaseScreen:initialize()
self.container = UIContainer()
self.characterList = createCharacterList( self.x, self.y, self.factionData, self.uiBaseCharacterInfo )
self.quitButton = createQuitButton( self.x, self.y )
self.nextMissionButton = createNextMissionButton( self.x, self.y )
self.nextMissionButton = createNextMissionButton( self.x, self.y, self.factionData )

self.container:register( self.characterList )
self.container:register( self.quitButton )
Expand Down
15 changes: 3 additions & 12 deletions src/ui/screens/GameOverScreen.lua
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,9 @@ end
-- Public Methods
-- ------------------------------------------------

function GameOverScreen:initialize( playerFaction, win )
function GameOverScreen:initialize( playerFaction, win )
self.playerFaction = playerFaction
self.win = win
if self.win then
-- TODO Proper implementation
-- Removes saved position to avoid characters spawning at the same
-- position on each combat map.
local serializedData = playerFaction:serialize()
for i = 1, #serializedData do
serializedData[i].x = nil
serializedData[i].y = nil
end
SaveHandler.copyPlayerFaction( serializedData )
end

self.x, self.y = GridHelper.centerElement( UI_GRID_WIDTH, UI_GRID_HEIGHT )

Expand All @@ -94,6 +84,7 @@ end

function GameOverScreen:keypressed()
if self.win then
SaveHandler.copyPlayerFaction( self.playerFaction:serialize() )
ScreenManager.switch( 'base' )
return
end
Expand Down

0 comments on commit 2d2b8b2

Please sign in to comment.