Wiz Programming Tutorials

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...

from wyvern.lib import Kernel, Timed
from wyvern.lib.classes import DynamicObject
from wyvern.lib.properties import Applyable

class hello_timer(DynamicObject, Timed, Applyable):

def initialize(self):
self.super__initialize()

self.setImage('objects/hourglass')
self.setProperty('short', 'hello timer')
self.setProperty('desc', 'Apply to start or stop the timer.')

self.timer = None
self.agent = None

def apply(self, agent):
if self.timer:
self.stopTimer(agent)
else:
self.startTimer(agent)

def startTimer(self, agent):
self.timer = Kernel.setRepeatingTimer(1000, self)
self.agent = agent

def stopTimer(self, agent):
self.agent = None
Kernel.killTimer(self.timer)
self.timer = None

def timerExpired(self):
self.agent.message('Hello, World')pre>

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:

from wyvern.lib import *
from wyvern.lib.classes import Terrain
from wyvern.kernel.combat import Combat
from wyvern.lib.properties import WalkNotify

class Lava (Terrain, WalkNotify, Timed, Terrain.NoFlyweight):

def initialize(self):
self.super__initialize()

self.setImage('terrain/lava')

# terrain priority (i.e. layer) and borders
self.setProperty('priority', 20)
self.addProperty('borders')

self.setProperty('short', 'lava')
self.setIntProperty('wc-fire', 10)

# combat stuff - show messages
self.addProperty('show-defender-msg')
self.setProperty('killed-by', 'lava')

# variables
self.monster = None
self.timer = None

def steppedOn(self, monster):
self.monster = monster
self.damageMonster(monster)
self.timer = Kernel.setTimer(self.getDelay(), self)


def timerExpired(self):
if not self.monster: return

map = self.getMap()
map2 = self.monster.getMap()

# if the monster's not in our map anymore, stop timer
if map != map2:
self.shutdown()
return

# if the monster's not in our square anymore, stop timer
locs = self.monster.getLocations()
if not locs.contains(self.getReferenceLoc()):
self.shutdown()
return

# damage monster and set another one-shot timer
self.damageMonster(self.monster)
Kernel.setTimer(self.getDelay(), self)

# our own method for doing damage
def damageMonster(self, mon):
if not (mon.hasProperty('flying') or mon.hasProperty('fire-walk')):
Combat.damageMonster(self, mon)

# inherited method: wizard disposed the object
def destroy(self):
self.super__destroy()
self.shutdown()

def getDelay(self):
return 1000

# our own method for nuking our timer
def shutdown(self):
self.monster = None
Kernel.killTimer(self.timer)
self.timer = None<pre>

<< Previous Chapter Next Chapter >>