diff options
author | Thorbjørn Lindeijer <thorbjorn@lindeijer.nl> | 2012-03-02 23:17:07 +0100 |
---|---|---|
committer | Thorbjørn Lindeijer <thorbjorn@lindeijer.nl> | 2012-03-03 21:24:45 +0100 |
commit | 84c87cc99be29a694f0ffe83ab7a06ae433bb0cd (patch) | |
tree | 9180b67970e62707ee0fe2fee19cbe2e0b829122 /example | |
parent | f872528771f0b71741fb36ddf70f2ae23f54c1e3 (diff) | |
download | manaserv-84c87cc99be29a694f0ffe83ab7a06ae433bb0cd.tar.gz manaserv-84c87cc99be29a694f0ffe83ab7a06ae433bb0cd.tar.xz manaserv-84c87cc99be29a694f0ffe83ab7a06ae433bb0cd.zip |
Use callbacks for items, monsters and status effects
Previously, global function names were defined in the respective XML
definitions of items, monsters and status effects. This was reasonable when
they all had the same state, but now they're sharing the single global
Lua state.
Now the Lua API provides access to the ItemClass, MonsterClass and
StatusEffect instances, on which callbacks for both standard and custom
events can be explicitly set.
Reviewed-by: Erik Schilling
Diffstat (limited to 'example')
-rw-r--r-- | example/items.xml | 2 | ||||
-rw-r--r-- | example/monsters.xml | 2 | ||||
-rw-r--r-- | example/scripts/items/candy.lua | 8 | ||||
-rw-r--r-- | example/scripts/main.lua | 3 | ||||
-rw-r--r-- | example/scripts/monster/testmonster.lua | 8 | ||||
-rw-r--r-- | example/scripts/status/jump.lua | 4 | ||||
-rw-r--r-- | example/scripts/status/plague.lua | 4 |
7 files changed, 22 insertions, 9 deletions
diff --git a/example/items.xml b/example/items.xml index 730e572..cdbc3e4 100644 --- a/example/items.xml +++ b/example/items.xml @@ -68,7 +68,7 @@ max-per-slot="30" value="15"> <effect trigger="activation"> - <script src="candy.lua" function="use_candy" /> + <scriptevent activate="use" /> <consumes /> </effect> </item> diff --git a/example/monsters.xml b/example/monsters.xml index f036191..962b137 100644 --- a/example/monsters.xml +++ b/example/monsters.xml @@ -78,7 +78,7 @@ exp<TAG>: Tells how much experience point a monster is giving up damage-factor="1" range="32" animation="attack" - script-function="on_maggot_strike" + script-event="strike" /> <script>testmonster.lua</script> <!-- only Proof of Concept--> </monster> diff --git a/example/scripts/items/candy.lua b/example/scripts/items/candy.lua index a9c59fe..f60e687 100644 --- a/example/scripts/items/candy.lua +++ b/example/scripts/items/candy.lua @@ -2,12 +2,14 @@ Example item script. - Makes the player character say "*munch*munch*munch*" when using this item. + Makes the player character say "*munch*munch*munch*" when using a candy. The HP regeneration effect is handled separately based on the heal value in items.xml. --]] -function use_candy(user) +local candy = mana.get_item_class("candy") + +candy:on("use", function(user) mana.being_say(user, "*munch*munch*munch*") -end +end) diff --git a/example/scripts/main.lua b/example/scripts/main.lua index aeaca63..6d8207e 100644 --- a/example/scripts/main.lua +++ b/example/scripts/main.lua @@ -9,3 +9,6 @@ require "scripts/global_events" require "scripts/special_actions" require "scripts/crafting" + +require "scripts/items/candy" +require "scripts/monster/testmonster" diff --git a/example/scripts/monster/testmonster.lua b/example/scripts/monster/testmonster.lua index 2701d24..63b2917 100644 --- a/example/scripts/monster/testmonster.lua +++ b/example/scripts/monster/testmonster.lua @@ -7,14 +7,14 @@ --]] -function update_monster(mob) +local function update(mob) local r = math.random(0, 200); if r == 0 then mana.being_say(mob, "Roar! I am a boss") end end -function on_maggot_strike(mob, victim, hit) +local function strike(mob, victim, hit) if hit > 0 then mana.being_say(mob, "Take this! "..hit.." damage!") mana.being_say(victim, "Oh Noez!") @@ -23,3 +23,7 @@ function on_maggot_strike(mob, victim, hit) mana.being_say(victim, "Whew...") end end + +local maggot = mana.get_monster_class("maggot") +maggot:on_update(update) +maggot:on("strike", strike) diff --git a/example/scripts/status/jump.lua b/example/scripts/status/jump.lua index 10ad928..166c90d 100644 --- a/example/scripts/status/jump.lua +++ b/example/scripts/status/jump.lua @@ -12,7 +12,7 @@ ---------------------------------------------------------------------------------- -function tick_jump(target, ticknumber) +local function tick(target, ticknumber) if (ticknumber % 10 == 0) then mana.being_say(target, "I have the jumping bug!") end @@ -51,3 +51,5 @@ function tick_jump(target, ticknumber) mana.being_apply_status(victim, 2, 6000) mana.being_say(victim, "Now I have the jumping bug") end + +mana.get_status_effect("jumping status"):on_tick(tick) diff --git a/example/scripts/status/plague.lua b/example/scripts/status/plague.lua index 5f33eb8..a43b9d4 100644 --- a/example/scripts/status/plague.lua +++ b/example/scripts/status/plague.lua @@ -12,7 +12,7 @@ -- Software Foundation; either version 2 of the License, or any later version. -- ---------------------------------------------------------------------------------- -function tick_plague(target, ticknumber) +local function tick(target, ticknumber) if (ticknumber % 10 == 0) then mana.being_say(target, "I have the plague! :( = " .. ticknumber) end @@ -26,3 +26,5 @@ function tick_plague(target, ticknumber) i = i + 1 end end + +mana.get_status_effect("plague"):on_tick(tick) |