Skip to content
Snippets Groups Projects
PlayerRest.lua 3.52 KiB
Newer Older
DarkGod's avatar
DarkGod committed
-- Copyright (C) 2009 - 2019 Nicolas Casalini
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program.  If not, see <http://www.gnu.org/licenses/>.
--
-- Nicolas Casalini "DarkGod"
-- darkgod@te4.org

require "engine.class"
dg's avatar
dg committed
local Dialog = require "engine.ui.Dialog"
-- @classmod engine.generator.interface.PlayerRest
module(..., package.seeall, class.make)

--- Initializes resting
function _M:restInit(turns, what, past, on_end, on_very_end)
	what = what or _t"resting"
	past = past or _t"rested"
	self.resting = {
		rest_turns = turns,
		past = past,
		on_end = on_end,
		on_very_end = on_very_end,
		dialog = Dialog:simplePopup(what:capitalize().."...", ("You are %s, press Enter to stop."):tformat(what), function()
			self:restStop()
		end),
	}
	self.resting.dialog.__showup = nil
	game.log(what:capitalize().." starts...")
	local ret, msg = self:restCheck()
	if ret and self.resting and self.resting.rest_turns and self.resting.cnt > self.resting.rest_turns then ret = false msg = nil end
	if not ret then
		self:restStop(msg)
	else
		self:useEnergy()
		self.resting.cnt = self.resting.cnt + 1
	end
--- We started resting
-- Rewrite this method to do whatever you need
function _M:onRestStart()
end

--- We stopped resting
-- Rewrite this method to do whatever you need
function _M:onRestStop()
end

Hachem_Muche's avatar
Hachem_Muche committed
--- The Player rests a turn
-- For a turn based game, you want to call this in your player's act() method:
-- @usage if not self:restStep() then game.paused = true end
Hachem_Muche's avatar
Hachem_Muche committed
-- @return[1] true if we can continue to rest (This uses energy)
-- @return[2] false if we can't continue
function _M:restStep()
	if not self.resting then return false end

	local ret, msg = self:restCheck()
	if ret and self.resting and self.resting.rest_turns and self.resting.cnt > self.resting.rest_turns then ret = false msg = nil end
	if not ret then
		self:restStop(msg)
		return false
	else
		self:useEnergy()
		self.resting.cnt = self.resting.cnt + 1
		return true
	end
end

--- Can we continue resting?  
dg's avatar
dg committed
-- Rewrite this method to check for mana, life, whatever. By default we always return false so resting will never work
-- @return[1] true if we can continue to rest
-- @return[2] false if we can't continue
function _M:restCheck()
	return false, "player:restCheck() method not defined"
end

--- Stops resting
function _M:restStop(msg)
	if not self.resting then return false end

	game:unregisterDialog(self.resting.dialog)

	if msg then
		game.log(self.resting.past:capitalize().." for %d turns (stop reason: %s).", self.resting.cnt, msg)
	else
		game.log(self.resting.past:capitalize().." for %d turns.", self.resting.cnt)
	end

	local finish = self.resting.cnt and self.resting.rest_turns and self.resting.cnt > self.resting.rest_turns
	local on_very_end = self.resting.on_very_end
	if self.resting.on_end then self.resting.on_end(self.resting.cnt, self.resting.rest_turns) end
	if on_very_end then on_very_end(finish) end