|
Timers are used for anything that happens at regular intervals,
such as having lava burn your feet.
You can set one-shot timers that go off one time and then stop. You
can set repeating timers that go off every N milliseconds. There's
also a special class,
wyvern.lib.classes.construct.SuspendableTimer , that lets
you set a timer that turns itself off automatically when the map
suspends or unloads.
Example 1 — Hello, World... Hello, World... Hello, World...
<span class='keyword'></span>from<span class='keyword'></span> <span class='keyword'></span>wyvern<span class='keyword'></span>.<span class='keyword'></span>lib<span class='keyword'></span> <span class='keyword'></span>import<span class='keyword'></span> <span class='keyword'></span>Kernel<span class='keyword'></span>, <span class='keyword'></span>Timed<span class='keyword'></span><br><span class='keyword'></span>from<span class='keyword'></span> <span class='keyword'></span>wyvern<span class='keyword'></span>.<span class='keyword'></span>lib<span class='keyword'></span>.<span class='keyword'></span>classes<span class='keyword'></span> <span class='keyword'></span>import<span class='keyword'></span> <span class='keyword'></span>DynamicObject<span class='keyword'></span><br><span class='keyword'></span>from<span class='keyword'></span> <span class='keyword'></span>wyvern<span class='keyword'></span>.<span class='keyword'></span>lib<span class='keyword'></span>.<span class='keyword'></span>properties<span class='keyword'></span> <span class='keyword'></span>import<span class='keyword'></span> <span class='keyword'></span>Applyable<span class='keyword'></span><br><br><span class='keyword'>class</span> <span class='function'>hello_timer</span>(DynamicObject, Timed, Applyable):<br><br> <span class='keyword'>def</span> <span class='function'>initialize</span>(self):<br> <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>super__initialize<span class='keyword'></span>()<br><br> <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>setImage<span class='keyword'></span>('<span class='keyword'></span>objects<span class='keyword'></span>/<span class='keyword'></span>hourglass<span class='keyword'></span>')<br> <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>setProperty<span class='keyword'></span>('<span class='keyword'></span>short<span class='keyword'></span>', '<span class='keyword'></span>hello<span class='keyword'></span> <span class='keyword'></span>timer<span class='keyword'></span>')<br> <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>setProperty<span class='keyword'></span>('<span class='keyword'></span>desc<span class='keyword'></span>', '<span class='keyword'></span>Apply<span class='keyword'></span> <span class='keyword'></span>to<span class='keyword'></span> <span class='keyword'></span>start<span class='keyword'></span> <span class='keyword'></span>or<span class='keyword'></span> <span class='keyword'></span>stop<span class='keyword'></span> <span class='keyword'></span>the<span class='keyword'></span> <span class='keyword'></span>timer<span class='keyword'></span>.')<br><br> <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>timer<span class='keyword'></span> = <span class='keyword'></span>None<span class='keyword'></span><br> <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>agent<span class='keyword'></span> = <span class='keyword'></span>None<span class='keyword'></span><br><br> <span class='keyword'>def</span> <span class='function'>apply</span>(self, agent):<br> <span class='keyword'></span>if<span class='keyword'></span> <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>timer<span class='keyword'></span>:<br> <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>stopTimer<span class='keyword'></span>(<span class='keyword'></span>agent<span class='keyword'></span>)<br> <span class='keyword'></span>else<span class='keyword'></span>:<br> <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>startTimer<span class='keyword'></span>(<span class='keyword'></span>agent<span class='keyword'></span>)<br><br> <span class='keyword'>def</span> <span class='function'>startTimer</span>(self, agent):<br> <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>timer<span class='keyword'></span> = <span class='keyword'></span>Kernel<span class='keyword'></span>.<span class='keyword'></span>setRepeatingTimer<span class='keyword'></span>(<span class='keyword'></span>1000<span class='keyword'></span>, <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>)<br> <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>agent<span class='keyword'></span> = <span class='keyword'></span>agent<span class='keyword'></span><br><br> <span class='keyword'>def</span> <span class='function'>stopTimer</span>(self, agent):<br> <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>agent<span class='keyword'></span> = <span class='keyword'></span>None<span class='keyword'></span><br> <span class='keyword'></span>Kernel<span class='keyword'></span>.<span class='keyword'></span>killTimer<span class='keyword'></span>(<span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>timer<span class='keyword'></span>)<br> <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>timer<span class='keyword'></span> = <span class='keyword'></span>None<span class='keyword'></span><br><br> <span class='keyword'>def</span> <span class='function'>timerExpired</span>(self):<br> <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>agent<span class='keyword'></span>.<span class='keyword'></span>message<span class='keyword'></span>('<span class='keyword'></span>Hello<span class='keyword'></span>, <span class='keyword'></span>World<span class='keyword'></span>')</<span class='keyword'></span>pre<span class='keyword'></span>>
|
Example 2 — Lava
Although the standard Lava class,
wyvern.lib.classes.construct.Lava , is
written in Java, it could just as easily have been done in Jython.
Here's a version of it for Jython:
<span class='keyword'></span>from<span class='keyword'></span> <span class='keyword'></span>wyvern<span class='keyword'></span>.<span class='keyword'></span>lib<span class='keyword'></span> <span class='keyword'></span>import<span class='keyword'></span> *<br><span class='keyword'></span>from<span class='keyword'></span> <span class='keyword'></span>wyvern<span class='keyword'></span>.<span class='keyword'></span>lib<span class='keyword'></span>.<span class='keyword'></span>classes<span class='keyword'></span> <span class='keyword'></span>import<span class='keyword'></span> <span class='keyword'></span>Terrain<span class='keyword'></span><br><span class='keyword'></span>from<span class='keyword'></span> <span class='keyword'></span>wyvern<span class='keyword'></span>.<span class='keyword'></span>kernel<span class='keyword'></span>.<span class='keyword'></span>combat<span class='keyword'></span> <span class='keyword'></span>import<span class='keyword'></span> <span class='keyword'></span>Combat<span class='keyword'></span><br><span class='keyword'></span>from<span class='keyword'></span> <span class='keyword'></span>wyvern<span class='keyword'></span>.<span class='keyword'></span>lib<span class='keyword'></span>.<span class='keyword'></span>properties<span class='keyword'></span> <span class='keyword'></span>import<span class='keyword'></span> <span class='keyword'></span>WalkNotify<span class='keyword'></span><br><br><span class='keyword'></span>class<span class='keyword'></span> <span class='keyword'></span>Lava<span class='keyword'></span> (<span class='keyword'></span>Terrain<span class='keyword'></span>, <span class='keyword'></span>WalkNotify<span class='keyword'></span>, <span class='keyword'></span>Timed<span class='keyword'></span>, <span class='keyword'></span>Terrain<span class='keyword'></span>.<span class='keyword'></span>NoFlyweight<span class='keyword'></span>):<br><br> <span class='keyword'>def</span> <span class='function'>initialize</span>(self):<br> <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>super__initialize<span class='keyword'></span>()<br><br> <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>setImage<span class='keyword'></span>('<span class='keyword'></span>terrain<span class='keyword'></span>/<span class='keyword'></span>lava<span class='keyword'></span>')<br><br><span class='comment'> # terrain priority (i.e. layer) and borders</span><br> <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>setProperty<span class='keyword'></span>('<span class='keyword'></span>priority<span class='keyword'></span>', <span class='keyword'></span>20<span class='keyword'></span>)<br> <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>addProperty<span class='keyword'></span>('<span class='keyword'></span>borders<span class='keyword'></span>')<br><br> <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>setProperty<span class='keyword'></span>('<span class='keyword'></span>short<span class='keyword'></span>', '<span class='keyword'></span>lava<span class='keyword'></span>')<br> <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>setIntProperty<span class='keyword'></span>('<span class='keyword'></span>wc<span class='keyword'></span>-<span class='keyword'></span>fire<span class='keyword'></span>', <span class='keyword'></span>10<span class='keyword'></span>)<br><br><span class='comment'> # combat stuff - show messages</span><br> <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>addProperty<span class='keyword'></span>('<span class='keyword'></span>show<span class='keyword'></span>-<span class='keyword'></span>defender<span class='keyword'></span>-<span class='keyword'></span>msg<span class='keyword'></span>')<br> <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>setProperty<span class='keyword'></span>('<span class='keyword'></span>killed<span class='keyword'></span>-<span class='keyword'></span>by<span class='keyword'></span>', '<span class='keyword'></span>lava<span class='keyword'></span>')<br><br><span class='comment'> # variables</span><br> <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>monster<span class='keyword'></span> = <span class='keyword'></span>None<span class='keyword'></span><br> <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>timer<span class='keyword'></span> = <span class='keyword'></span>None<span class='keyword'></span><br><br> <span class='keyword'>def</span> <span class='function'>steppedOn</span>(self, monster):<br> <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>monster<span class='keyword'></span> = <span class='keyword'></span>monster<span class='keyword'></span><br> <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>damageMonster<span class='keyword'></span>(<span class='keyword'></span>monster<span class='keyword'></span>)<br> <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>timer<span class='keyword'></span> = <span class='keyword'></span>Kernel<span class='keyword'></span>.<span class='keyword'></span>setTimer<span class='keyword'></span>(<span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>getDelay<span class='keyword'></span>(), <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>)<br><br><br> <span class='keyword'>def</span> <span class='function'>timerExpired</span>(self):<br> <span class='keyword'></span>if<span class='keyword'></span> <span class='keyword'></span>not<span class='keyword'></span> <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>monster<span class='keyword'></span>: <span class='keyword'></span>return<span class='keyword'></span><br><br> <span class='keyword'></span>map<span class='keyword'></span> = <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>getMap<span class='keyword'></span>()<br> <span class='keyword'></span>map2<span class='keyword'></span> = <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>monster<span class='keyword'></span>.<span class='keyword'></span>getMap<span class='keyword'></span>()<br><br><span class='comment'> # if the monster's not in our map anymore, stop timer</span><br> <span class='keyword'></span>if<span class='keyword'></span> <span class='keyword'></span>map<span class='keyword'></span> != <span class='keyword'></span>map2<span class='keyword'></span>:<br> <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>shutdown<span class='keyword'></span>()<br> <span class='keyword'></span>return<span class='keyword'></span><br><br><span class='comment'> # if the monster's not in our square anymore, stop timer</span><br> <span class='keyword'></span>locs<span class='keyword'></span> = <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>monster<span class='keyword'></span>.<span class='keyword'></span>getLocations<span class='keyword'></span>()<br> <span class='keyword'></span>if<span class='keyword'></span> <span class='keyword'></span>not<span class='keyword'></span> <span class='keyword'></span>locs<span class='keyword'></span>.<span class='keyword'></span>contains<span class='keyword'></span>(<span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>getReferenceLoc<span class='keyword'></span>()):<br> <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>shutdown<span class='keyword'></span>()<br> <span class='keyword'></span>return<span class='keyword'></span><br><br><span class='comment'> # damage monster and set another one-shot timer</span><br> <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>damageMonster<span class='keyword'></span>(<span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>monster<span class='keyword'></span>)<br> <span class='keyword'></span>Kernel<span class='keyword'></span>.<span class='keyword'></span>setTimer<span class='keyword'></span>(<span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>getDelay<span class='keyword'></span>(), <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>)<br><br><span class='comment'> # our own method for doing damage</span><br> <span class='keyword'>def</span> <span class='function'>damageMonster</span>(self, mon):<br> <span class='keyword'></span>if<span class='keyword'></span> <span class='keyword'></span>not<span class='keyword'></span> (<span class='keyword'></span>mon<span class='keyword'></span>.<span class='keyword'></span>hasProperty<span class='keyword'></span>('<span class='keyword'></span>flying<span class='keyword'></span>') <span class='keyword'></span>or<span class='keyword'></span> <span class='keyword'></span>mon<span class='keyword'></span>.<span class='keyword'></span>hasProperty<span class='keyword'></span>('<span class='keyword'></span>fire<span class='keyword'></span>-<span class='keyword'></span>walk<span class='keyword'></span>')):<br> <span class='keyword'></span>Combat<span class='keyword'></span>.<span class='keyword'></span>damageMonster<span class='keyword'></span>(<span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>, <span class='keyword'></span>mon<span class='keyword'></span>)<br><br><span class='comment'> # inherited method: wizard disposed the object</span><br> <span class='keyword'>def</span> <span class='function'>destroy</span>(self):<br> <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>super__destroy<span class='keyword'></span>()<br> <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>shutdown<span class='keyword'></span>()<br><br> <span class='keyword'>def</span> <span class='function'>getDelay</span>(self):<br> <span class='keyword'></span>return<span class='keyword'></span> <span class='keyword'></span>1000<span class='keyword'></span><br><br><span class='comment'> # our own method for nuking our timer</span><br> <span class='keyword'>def</span> <span class='function'>shutdown</span>(self):<br> <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>monster<span class='keyword'></span> = <span class='keyword'></span>None<span class='keyword'></span><br> <span class='keyword'></span>Kernel<span class='keyword'></span>.<span class='keyword'></span>killTimer<span class='keyword'></span>(<span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>timer<span class='keyword'></span>)<br> <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>timer<span class='keyword'></span> = <span class='keyword'></span>None<span class='keyword'></span><<span class='keyword'></span>pre<span class='keyword'></span>>
|
|