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

temporary values pavnig the way for objects

git-svn-id: http://svn.net-core.org/repos/t-engine4@102 51575b47-30f0-44d4-a5cc-537603b46e54
parent e46fdb22
No related branches found
No related tags found
No related merge requests found
......@@ -17,8 +17,17 @@ function _M:init(t)
self.faction = t.faction or "enemies"
self.changed = true
Entity.init(self, t)
self.compute_vals = {}
end
--- Moves an actor on the map
-- *WARNING*: changing x and y properties manualy is *WRONG* and will blow up in your face. Use this method. Always.
-- @param map the map to move onto
-- @param x coord of the destination
-- @param y coord of the destination
-- @param force if true do not check for the presence of an other entity. *Use wisely*
-- @return true if a move was *ATTEMPTED*. This means the actor will proably want to use energy
function _M:move(map, x, y, force)
if not force and map:checkAllEntities(x, y, "block_move", self) then return true end
......@@ -36,6 +45,8 @@ function _M:move(map, x, y, force)
return true
end
--- Can the actor go there
-- @param terrain_only if true checks only the terrain, otherwise checks all entities
function _M:canMove(x, y, terrain_only)
if terrain_only then
return not game.level.map:checkEntity(x, y, Map.TERRAIN, "block_move")
......@@ -44,6 +55,11 @@ function _M:canMove(x, y, terrain_only)
end
end
--- Teleports randomly to a passable grid
-- @param x the coord of the teleporatation
-- @param y the coord of the teleporatation
-- @param dist the radius of the random effect, if set to 0 it is a precise teleport
-- @return true if the teleport worked
function _M:teleportRandom(x, y, dist)
local poss = {}
......@@ -68,11 +84,13 @@ function _M:deleteFromMap(map)
end
end
--- Do we have enough energy
function _M:enoughEnergy(val)
val = val or game.energy_to_act
return self.energy.value >= val
end
--- Use some energy
function _M:useEnergy(val)
val = val or game.energy_to_act
self.energy.value = self.energy.value - val
......@@ -84,3 +102,54 @@ end
function _M:reactionToward(target)
return Faction:factionReaction(self.faction, target.faction)
end
--- Computes a "temporary" value into a property
-- Example: You cant to give an actor a boost to life_regen, but you do not want it to be permanent<br/>
-- You cannot simply increase life_regen, so you use this method which will increase it AND
-- store the increase. it will return an "increase id" that can be passed to removeTemporaryValue()
-- to remove the effect.
-- @param prop the property to affect
-- @param v the value to add (only numbers supported for now)
-- @param noupdate if true the actual property is not changed and needs to be changed by the caller
-- @return an id that can be passed to removeTemporaryValue() to delete this value
function _M:addTemporaryValue(prop, v, noupdate)
self.compute_vals[prop] = self.compute_vals[prop] or {}
local t = self.compute_vals[prop]
t[#t+1] = v
-- Update the base prop
if not noupdate then
if type(v) == "number" then
-- Simple addition
self[prop] = (self[prop] or 0) + v
-- elseif type(v) == "boolean" then
-- -- False has precedence over true
-- if v == false then
-- self[prop] = false
-- elseif self[prop] ~= false then
-- self[prop] = true
-- end
else
error("unsupported temporary value type: "..type(v))
end
end
return #t
end
--- Removes a temporary value, see addTemporaryValue()
-- @param prop the property to affect
-- @param id the id of the increase to delete
-- @param noupdate if true the actual property is not changed and needs to be changed by the caller
function _M:removeTemporaryValue(prop, id, noupdate)
local oldval = self.compute_vals[prop][id]
self.compute_vals[prop][id] = nil
if not noupdate then
if type(oldval) == "number" then
self[prop] = self[prop] - oldval
-- elseif type(oldval) == "boolean" then
else
error("unsupported temporary value type: "..type(oldval))
end
end
end
......@@ -53,7 +53,7 @@ function _M:incStat(stat, val)
if self.stats[stat] - old ~= 0 then
self:onStatChange(stat, self.stats[stat] - old)
end
return self.stats[stat]
return self.stats[stat] - old
end
--- Gets a stat value
......
......@@ -20,10 +20,10 @@ newEffect{
on_gain = function(self, err) return "#Target# starts to surge mana.", "+Manaflow" end,
on_lose = function(self, err) return "#Target# stops surging mana.", "-Manaflow" end,
activate = function(self, eff)
self.mana_regen = self.mana_regen + eff.power
eff.tmpid = self:addTemporaryValue("mana_regen", eff.power)
end,
deactivate = function(self, eff)
self.mana_regen = self.mana_regen - eff.power
self:removeTemporaryValue("mana_regen", eff.tmpid)
end,
}
......@@ -36,9 +36,9 @@ newEffect{
on_gain = function(self, err) return "#Target# starts to regenerating heath quickly.", "+Regen" end,
on_lose = function(self, err) return "#Target# stops regenerating health quickly.", "-Regen" end,
activate = function(self, eff)
self.life_regen = self.life_regen + eff.power
eff.tmpid = self:addTemporaryValue("life_regen", eff.power)
end,
deactivate = function(self, eff)
self.life_regen = self.life_regen - eff.power
self:removeTemporaryValue("life_regen", eff.tmpid)
end,
}
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