From: <ch...@us...> - 2009-02-26 21:01:14
|
Revision: 282 http://virtplayground.svn.sourceforge.net/virtplayground/?rev=282&view=rev Author: chozone Date: 2009-02-26 21:01:04 +0000 (Thu, 26 Feb 2009) Log Message: ----------- New avatar type: multidir. Also added error catcher for non-existing playground modules. Modified Paths: -------------- branches/VP-Grounds/brains.py branches/VP-Grounds/modules/playground.py Added Paths: ----------- branches/VP-Grounds/modules/playground/avatar-multidir.py Modified: branches/VP-Grounds/brains.py =================================================================== --- branches/VP-Grounds/brains.py 2009-02-25 19:17:16 UTC (rev 281) +++ branches/VP-Grounds/brains.py 2009-02-26 21:01:04 UTC (rev 282) @@ -91,7 +91,7 @@ #Is our timer NOT running? if self.walkTimer == None or self.walkTimer.isRunning == False: #Start it! - self.walkTimer = self.sh['Interface'].Timer(0.05, self.doWalk) + self.walkTimer = self.sh['Interface'].Timer(0.07, self.doWalk) #Is our update timer NOT on? if self.walkUpdateTimer == None or self.walkUpdateTimer.isRunning == False: Added: branches/VP-Grounds/modules/playground/avatar-multidir.py =================================================================== --- branches/VP-Grounds/modules/playground/avatar-multidir.py (rev 0) +++ branches/VP-Grounds/modules/playground/avatar-multidir.py 2009-02-26 21:01:04 UTC (rev 282) @@ -0,0 +1,99 @@ +from functions import * + +class Object(): + def __init__(self, data, share): + self.sh = share + + self.currentDir=-1 #-1=none, 0=d, 1=l, 2=u, 3=r + + self.imageName=data['Avatar'][1] + self.image=None + self.fullImage=None + + self.rect = Rect(data['Pos'][1], data['Pos'][2], 0, 0) + self.zorder = self.rect.top + self.rect.height + self.blitRect = Rect(0,0,0,0) #Internal rect, inside fullImage + + def show(self): + """ + Start the whole process of: + checking whether avatar is downloaded, [download it], return avatar, and finally: blit ourselves. + This has to be a seperate def. If you put this right into __init__, we will make playground blit + ourself, before the initialisation is ready. And so, if that's not ready, we're not in the objects + list of the playground, and we will not be blitted. + """ + self.sh['Downloader'].getImage(self.imageName, self.imageUpdate) + + def imageUpdate(self, image, *arg): + """ + The downloader will call this when he's finished. + Will load in the image, and make playground blit our rect. + """ + + self.rect.size = image[1].size + self.rect.w = self.rect.w/4 + + self.blitRect.size=self.rect.size + + self.fullImage = image[0] + + self.setDir(0) + + self.zorder = self.rect.top + self.rect.height + + self.sh['Playground'].blit(self.rect) + + def update(self, screen, rect): + if not self.image: return + if self.rect.colliderect(rect): #Check if the rect collide with ours + rect = rect.move(-self.rect[0], -self.rect[1]) #Move rect to our position + myPos = (self.rect[0] + rect[0], self.rect[1] + rect[1]) #Calculate our position from the rect + screen.blit(self.image, myPos, rect) #Blit + + + def move(self, pos): + """ + Call this def to move this avatar to pos x=pos[1], y=pos[2]. + Will update rect, and make playground module update our old+new area + """ + oldRect=self.rect.__copy__() + + #Change avatar direction + if self.rect.x < pos[1]: self.setDir('r') + elif self.rect.x > pos[1]: self.setDir('l') + elif self.rect.y < pos[2]: self.setDir('d') + elif self.rect.y > pos[2]: self.setDir('u') + + self.rect.x=pos[1] + self.rect.y=pos[2] + self.zorder = self.rect.top + self.rect.height + + updateRect=oldRect.union(self.rect) + + self.sh['Playground'].blit(updateRect) + + def setDir(self, dir, blit=False): + """ + Change avatar direction. + `dir` can be either int(0,1,2,3) or str('d','l','u','r'). + """ + + #Change to correct value, or exit + if type(dir)!=int: + if dir=='d': dir=0 + elif dir=='l': dir=1 + elif dir=='u': dir=2 + elif dir=='r': dir=3 + else: return + + if dir < 0 or dir > 3: return #Valid number? + if dir == self.currentDir: return #Not the same as old direction? + + if not self.fullImage: return #Do we have base image? + + #Actually change image + self.blitRect.x = self.blitRect.w * dir + self.image=self.fullImage.subsurface(self.blitRect) + + if blit: + self.sh['Playground'].blit(self.rect) Modified: branches/VP-Grounds/modules/playground.py =================================================================== --- branches/VP-Grounds/modules/playground.py 2009-02-25 19:17:16 UTC (rev 281) +++ branches/VP-Grounds/modules/playground.py 2009-02-26 21:01:04 UTC (rev 282) @@ -123,6 +123,7 @@ else: #Create new one! :D self.openObject('avatar-' + user['Avatar'][0], 'av-' + user['Name'], user) + if not 'avatar-' + user['Avatar'][0] in self.modObjects.keys(): return self.objects['av-%s'%user['Name']].show() #Will start downloading, see modules/playground/avatar-static.py for full explanation else: @@ -189,7 +190,10 @@ """ Create an new object, of type `objname`. """ + + if not objname in self.modObjects.keys(): return print '## Open Object', name, 'as', objname #DEBUG + obj = self.modObjects[objname] obj = obj(data, self.sh) self.objects[name] = obj This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |