Skip to content
Snippets Groups Projects
Commit dc29acb6 authored by dg's avatar dg
Browse files

more dialog methods

more command line params
levelup dialgo can setup stats


git-svn-id: http://svn.net-core.org/repos/t-engine4@63 51575b47-30f0-44d4-a5cc-537603b46e54
parent 4d5d82fc
No related branches found
No related tags found
No related merge requests found
......@@ -13,6 +13,7 @@ function _M:simplePopup(title, text, fct)
local w, h = font:size(text)
local d = new(title, w + 8, h + 25, nil, nil, nil, font)
d:keyCommands{__DEFAULT=function() game:unregisterDialog(d) if fct then fct() end end}
d:mouseZones{{x=0, y=0, w=game.w, h=game.h, fct=function(b) if b ~= "none" then game:unregisterDialog(d) if fct then fct() end end end, norestrict=true}}
d.drawDialog = function(self, s)
s:drawColorStringCentered(self.font, text, 2, 2, self.iw - 2, self.ih - 2)
end
......@@ -27,6 +28,7 @@ function _M:init(title, w, h, x, y, alpha, font)
self.display_y = y or (game.h - self.h) / 2
self.font = font
if not font then self.font = core.display.newFont("/data/font/Vera.ttf", 12) end
self.font_h = self.font:lineSkip()
self.surface = core.display.newSurface(w, h)
self.iw, self.ih = w - 2 * 5, h - 8 - 16 - 3
self.internal_surface = core.display.newSurface(self.iw, self.ih)
......@@ -74,9 +76,50 @@ function _M:keyCommands(t)
game.key:setCurrent()
end
function _M:mouseZones(t)
-- Offset the x and y with the window position and window title
if not t.norestrict then
for i, z in ipairs(t) do
z.x = z.x + self.display_x + 5
z.y = z.y + self.display_y + 20 + 3
end
end
self.old_mouse = game.mouse
game.mouse = engine.Mouse.new()
game.mouse:registerZones(t)
game.mouse:setCurrent()
end
function _M:unload()
if self.old_key then
game.key = self.old_key
game.key:setCurrent()
end
if self.old_mouse then
game.mouse = self.old_mouse
game.mouse:setCurrent()
end
end
function _M:drawWBorder(s, x, y, w)
for i = x, x + w do
s:merge(tiles:get(nil, 0,0,0, 0,0,0, "border_8.png"), i, y)
end
end
function _M:drawHBorder(s, x, y, h)
for i = y, y + h do
s:merge(tiles:get(nil, 0,0,0, 0,0,0, "border_4.png"), x, i)
end
end
function _M:drawSelectionList(s, x, y, hskip, list, sel)
for i, v in ipairs(list) do
v = tostring(v)
if sel == i then
s:drawColorString(self.font, v, x, y + (i-1) * hskip, 0, 255, 255)
else
s:drawColorString(self.font, v, x, y + (i-1) * hskip)
end
end
end
......@@ -32,15 +32,15 @@ function _M:init(t)
next_uid = next_uid + 1
end
-- If we are cloned we need a new uid
--- If we are cloned we need a new uid
function _M:cloned()
self.uid = next_uid
__uids[self.uid] = self
next_uid = next_uid + 1
end
-- If we are loaded we need a new uid
_M.loadNoDelay = true
--- If we are loaded we need a new uid
function _M:loaded()
local ouid = self.uid
self.uid = next_uid
......@@ -48,6 +48,14 @@ function _M:loaded()
next_uid = next_uid + 1
end
--- Change the entity's uid
-- <strong>*WARNING*</strong>: ONLY DO THIS IF YOU KNOW WHAT YOU ARE DOING!. YOU DO NOT !
function _M:changeUid(newuid)
__uids[self.uid] = nil
self.uid = newuid
__uids[self.uid] = self
end
--- Check for an entity's property
-- If not a function it returns it directly, otherwise it calls the function
-- with the extra parameters
......
......@@ -15,7 +15,7 @@ end
function _M:receiveMouse(button, x, y)
for i, m in ipairs(self.areas) do
if (not m.mode or m.mode.button) and (x >= m.x1 and x < m.x2 and y >= m.y1 and y < m.y2) then
m.fct(button, x, y)
m.fct(button, x, y, nil, nil, x-m.x1, y-m.y1)
end
end
end
......@@ -23,7 +23,7 @@ end
function _M:receiveMouseMotion(button, x, y, xrel, yrel)
for i, m in ipairs(self.areas) do
if (not m.mode or m.mode.move) and (x >= m.x1 and x < m.x2 and y >= m.y1 and y < m.y2) then
m.fct(button, x, y, xrel, yrel)
m.fct(button, x, y, xrel, yrel, x-m.x1, y-m.y1)
end
end
end
......@@ -39,6 +39,12 @@ function _M:registerZone(x, y, w, h, fct, mode)
table.insert(self.areas, 1, {x1=x,y1=y,x2=x+w,y2=y+h, fct=fct, mode})
end
function _M:registerZones(t)
for i, z in ipairs(t) do
self:registerZone(z.x, z.y, z.w, z.h, z.fct, z.mode)
end
end
function _M:unregisterZone(fct)
for i, m in ipairs(self.areas) do
if m.fct == fct then table.remove(self.areas, i) break end
......
......@@ -153,3 +153,8 @@ function _M:loadLevel(zone, level)
fs.umount(path)
return loadedLevel
end
--- Checks for existence
function _M:check()
return fs.exists(self.save_dir.."game.teag")
end
......@@ -7,12 +7,13 @@ _M.stats_def = {}
--- Defines stats
-- Static!
function _M:defineStat(name, short_name, default_value, min, max)
function _M:defineStat(name, short_name, default_value, min, max, desc)
assert(name, "no stat name")
assert(short_name, "no stat short_name")
table.insert(self.stats_def, {
name = name,
short_name = short_name,
description = desc,
def = default_value or 10,
min = min or 1,
max = max or 100,
......@@ -42,10 +43,18 @@ function _M:init(t)
end
end
---
-- Module authors should rewrite it to handle combat, dialog, ...
-- @param target the actor attacking us
--- Increases a stat
-- @param stat the stat id to change
-- @param val the increment to add/substract
function _M:incStat(stat, val)
self.stats[stat] = max(min(val, _M.stats_def[stat].max), _M.stats_def[stat].min)
self.stats[stat] = math.max(math.min(self.stats[stat] + val, _M.stats_def[stat].max), _M.stats_def[stat].min)
return self.stats[stat]
end
--- Gets a stat value
-- Not that the engine also auto-defines stat specific methods on the form: self:getShortname().
-- If you stat short name is STR then it becomes getStr()
-- @param stat the stat id
function _M:getStat(stat)
return self.stats[stat]
end
......@@ -187,3 +187,10 @@ dir_to_coord = {
[8] = { 0,-1},
[9] = { 1,-1},
}
util = {}
function util.boundWrap(i, min, max)
if i < min then i = max
elseif i > max then i = min end
return i
end
......@@ -39,12 +39,12 @@ function _M:run()
-- Damage types
DamageType:loadDefinition("/data/damage_types.lua")
-- Actor stats
ActorStats:defineStat("Strength", "str", 10, 1, 100)
ActorStats:defineStat("Dexterity", "dex", 10, 1, 100)
ActorStats:defineStat("Magic", "mag", 10, 1, 100)
ActorStats:defineStat("Willpower", "wil", 10, 1, 100)
ActorStats:defineStat("Cunning", "cun", 10, 1, 100)
ActorStats:defineStat("Constitution", "con", 10, 1, 100)
ActorStats:defineStat("Strength", "str", 10, 1, 100, "Strength defines your character's ability to apply physical force. It increases your melee damage, damage with heavy weapons, your chance to resist physical effects, and carrying capacity.")
ActorStats:defineStat("Dexterity", "dex", 10, 1, 100, "Dexterity defines your character's ability to be agile and alert. It increases your chance to hit, your ability to avoid attacks and your damage with light weapons.")
ActorStats:defineStat("Magic", "mag", 10, 1, 100, "Magic defines your character's ability to manipulate the magic of the world. It increases your spell power, the effect of spells and other magic items.")
ActorStats:defineStat("Willpower", "wil", 10, 1, 100, "Willpower defines your character's ability to concentrate. It increases your mana and stamina capacity, and your chance to resist mental attacks.")
ActorStats:defineStat("Cunning", "cun", 10, 1, 100, "Cunning defines your character's ability to learn and think. It allows you to learn many wordly abilities, increases your mental resistance and armor penetration.")
ActorStats:defineStat("Constitution", "con", 10, 1, 100, "Constitution defines your character's ability to withstand and resist damage. It increases your maximun life and physical resistance.")
-- Abilities
ActorAbilities:loadDefinition("/data/abilities.lua")
......@@ -390,7 +390,7 @@ end
--- Ask if we realy want to close, if so, save the game first
function _M:onQuit()
-- HACK for quick test
os.exit()
-- os.exit()
if not self.quit_dialog then
self.quit_dialog = QuitDialog.new()
self:registerDialog(self.quit_dialog)
......
......@@ -38,7 +38,7 @@ newAbility{
local t = {type="ball", range=15, radius=math.min(6, 3 + self:getMag(6))}
local x, y = self:getTarget(t)
if not x or not y then return nil end
self:project(t, x, y, DamageType.FIRE, 8 + self:getMag(70))
self:project(t, x, y, DamageType.FIRE, 28 + self:getMag(70))
return true
end,
require = { stat = { mag=16 }, },
......
......@@ -5,17 +5,62 @@ module(..., package.seeall, class.inherit(engine.Dialog))
function _M:init(actor)
self.actor = actor
engine.Dialog.init(self, "Levelup: "..actor.name, game.w, game.h)
self.actor_dup = actor:clone()
engine.Dialog.init(self, "Levelup: "..actor.name, 600, 400)
-- self.
-- self.statstpl = self:loadDisplayTemplate()
self.statsel = 1
self:keyCommands{
_ESCAPE = function()
game:unregisterDialog(self)
end,
_UP = function() self.statsel = util.boundWrap(self.statsel - 1, 1, 6) end,
_DOWN = function() self.statsel = util.boundWrap(self.statsel + 1, 1, 6) end,
_LEFT = function() self:incStat(-1) end,
_RIGHT = function() self:incStat(1) end,
_ESCAPE = function() game:unregisterDialog(self) end,
}
self:mouseZones{
{ x=2, y=25, w=130, h=self.font_h*6, fct=function(button, x, y, xrel, yrel, tx, ty)
self.statsel = 1 + math.floor(ty / self.font_h)
if button == "left" then self:incStat(1)
elseif button == "right" then self:incStat(-1)
end
end },
}
end
function _M:incStat(v)
if v == 1 then
if self.actor.unused_stats == 0 then
self:simplePopup("Not enough stat points", "You have no stat poins left!")
return
end
else
if self.actor_dup:getStat(self.statsel) == self.actor:getStat(self.statsel) then
self:simplePopup("Impossible", "You cannot take out more points!")
return
end
end
self.actor:incStat(self.statsel, v)
self.actor.unused_stats = self.actor.unused_stats - v
end
function _M:drawDialog(s, w, h)
s:drawColorStringCentered(self.font, "Stats points left: "..self.actor.unused_stats, 2, 2, self.iw - 2, self.ih - 2)
-- Description part
self:drawHBorder(s, self.w / 2, 2, self.h - 4)
local lines = self.actor.stats_def[self.statsel].description:splitLines(self.w / 2 - 10, self.font)
for i = 1, #lines do
s:drawColorString(self.font, lines[i], self.w / 2 + 5, 2 + i * self.font:lineSkip())
end
-- Stats
s:drawColorString(self.font, "Stats points left: #00FF00#"..self.actor.unused_stats, 2, 2)
self:drawWBorder(s, 2, 20, 200)
self:drawSelectionList(s, 2, 25, self.font_h, {
"Strength", "Dexterity", "Magic", "Willpower", "Cunning", "Constitution"
}, self.statsel)
self:drawSelectionList(s, 100, 25, self.font_h, {
self.actor:getStr(), self.actor:getDex(), self.actor:getMag(), self.actor:getWil(), self.actor:getCun(), self.actor:getCon(),
}, self.statsel)
end
......@@ -32,11 +32,21 @@ end
--- Skip to a module directly ?
function _M:commandLineArgs(args)
local req_mod = nil
local req_save = nil
local req_new = false
for i, arg in ipairs(args) do
if arg:find("^%-M") then
-- Force module loading
req_mod = arg:sub(3)
end
if arg:find("^%-u") then
-- Force save loading
req_save = arg:sub(3)
end
if arg:find("^%-n") then
-- Force save loading
req_new = true
end
end
if req_mod then
......@@ -46,10 +56,14 @@ function _M:commandLineArgs(args)
_G.game = M.new()
-- Delete the corresponding savefile if any
if req_save then _G.game:setPlayerName(req_save) end
local save = engine.Savefile.new(_G.game.save_name)
save:delete()
if save:check() and not req_new then
_G.game = save:loadGame()
else
save:delete()
end
save:close()
_G.game:run()
else
print("Error: module "..req_mod.." not found!")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment