From: <Blu...@us...> - 2010-09-26 22:16:22
|
Revision: 411 http://virtplayground.svn.sourceforge.net/virtplayground/?rev=411&view=rev Author: BlueWolf_ Date: 2010-09-26 22:16:14 +0000 (Sun, 26 Sep 2010) Log Message: ----------- And we have a background which you can walk on! Next step: Sending and receiving user positions Modified Paths: -------------- trunk/client/callback.py trunk/client/playground.py trunk/client/playground_avatar.py trunk/client/timer.py Added Paths: ----------- trunk/client/playground_background.py Modified: trunk/client/callback.py =================================================================== --- trunk/client/callback.py 2010-09-25 21:16:57 UTC (rev 410) +++ trunk/client/callback.py 2010-09-26 22:16:14 UTC (rev 411) @@ -144,6 +144,13 @@ sh['main'].call("filetable", { "action": "update" }) + + elif header == "landscapes": # How the background should look like + sh['landscapes'] = body + + sh['main'].call("landscapes", { + "action": "update" + }) def userlist(self, userlist): if len(userlist) == 1: Modified: trunk/client/playground.py =================================================================== --- trunk/client/playground.py 2010-09-25 21:16:57 UTC (rev 410) +++ trunk/client/playground.py 2010-09-26 22:16:14 UTC (rev 411) @@ -18,6 +18,7 @@ from functions import * import gui from playground_avatar import Avatar +from playground_background import Background POS_UPDATE_DELAY = 1 # Determines when a pos-update should be send. # Every x seconds. @@ -32,7 +33,7 @@ self.status = None self.statuswhere = None - self.backgrounds = {} + self.background = Background(self) self.viewport_float = None self.viewport = None self.direction = None @@ -84,8 +85,6 @@ self.this_user['pos'][:]) sh['timer'].start("playground-walk", self.timer_walk) - #sh['timer'].start("playground-pos-update", - # self.timer_walk, time.time()) def call(self, name, data): @@ -93,12 +92,13 @@ if data['status'] == "login" and self.status == "playground": # Unload data from the playground sh['timer'].stop("playground-move") - for name in self.backgrounds.keys(): - sh['downloader'].remove_usage(name) - self.backgrounds = {} + self.background.reset() self.viewport = None self.viewport_float = None self.direction = None + + for avatar in self.avatars.values(): + avatar.stop() self.avatars = {} self.this_user = None @@ -135,6 +135,9 @@ sh['downloader'].get_file("bg-grass.png", "img", self.download,\ "background", counter) + elif name == "landscapes": + if data['action'] == "update": + self.background.update_landscapes(sh['landscapes']) def update(self, rect = None, doupdate = True): # This will blit and update the screen at rect @@ -145,7 +148,7 @@ rect = Rect(rect) # Blit the background - #self.background.update(self.surf, self.viewport, rect) + self.background.update(self.surf, self.viewport, rect) # Blit the users for avatar in self.avatars.values(): @@ -157,7 +160,7 @@ def download(self, filename, cache, name, *arg): if name == "background": - self.backgrounds[filename] = cache + self.background.add_background(filename, cache) counter = arg[0] counter['counter'] -= 1 @@ -172,6 +175,9 @@ def timer_walk(self, name, starttime = None): if name == "playground-walk": + if self.direction == None: + return True # No direction. We aren't moving + direction, starttime, startpos = self.direction timeline = time.time() - starttime speed = MOVE_SPEED @@ -247,6 +253,33 @@ self.viewport[1] += real_move[1] self.surf.scroll(-real_move[0], -real_move[1]) + + # Blit the 2 sides that now appear + + if real_move[0] < 0: # Left side + rect = Rect(0, 0, -real_move[0], 700) + self.update(rect, doupdate=False) + elif real_move[0] > 0: # Right side + rect = Rect(1000-real_move[0], 0, real_move[0], 700) + self.update(rect, doupdate=False) + + if real_move[1] < 0: # Top + rect = Rect(0, 0, 1000, -real_move[1]) + if real_move[0] < 0 : # And to the left + rect.left = -real_move[0] + rect.width = 1000 + real_move[0] + elif real_move[0] > 0: # And to the right + rect.width = 1000 - real_move[0] + self.update(rect, doupdate=False) + elif real_move[1] > 0: # Bottom + rect = Rect(0, 700-real_move[1], 1000, real_move[1]) + if real_move[0] < 0 : # And to the left + rect.left = -real_move[0] + rect.width = 1000 + real_move[0] + elif real_move[0] > 0: # And to the right + rect.width = 1000 - real_move[0] + self.update(rect, doupdate=False) + sh['update']() if self.viewport_float == desired_pos: return True Modified: trunk/client/playground_avatar.py =================================================================== --- trunk/client/playground_avatar.py 2010-09-25 21:16:57 UTC (rev 410) +++ trunk/client/playground_avatar.py 2010-09-26 22:16:14 UTC (rev 411) @@ -28,30 +28,45 @@ sh['downloader'].get_file("happyblock.png", "img", self.download) + def stop(self): + # Playground is quiting. Stop everything + sh['downloader'].remove_usage("happyblock.png") + def move(self, pos): + # Move the avatar to this place lastpos = self.user['pos'] self.user['pos'] = pos - self.update_parent() + rect = self.get_rect(pos) + rect_old = self.get_rect(lastpos) + rect.union_ip(rect_old) + + self.update_parent(rect) def download(self, filename, cache): + # Callback for the avatar-downloader self.avatar = cache self.update_parent() - - def update_parent(self): - # Update ourself. Send it to our parent - self_rect = Rect( - self.user['pos'][0] - self.playground.viewport[0] - - self.center[0], - self.user['pos'][1] - self.playground.viewport[1] - - self.center[1], + + def get_rect(self, pos): + # Get a rect, pased on the position + return Rect( + pos[0] - self.playground.viewport[0] - self.center[0], + pos[1] - self.playground.viewport[1] - self.center[1], self.size[0], self.size[1]) + + def update_parent(self, rect = None): + if self.playground.viewport == None: + return # Without viewport, we can't update (this can happen due to + # threading + + if rect == None: + rect = self.get_rect(self.user['pos']) - if not Rect(0, 0, 1000, 700).colliderect(self_rect): return - - self.playground.update(self_rect) + if not Rect(0, 0, 1000, 700).colliderect(rect): return + self.playground.update(rect) def update(self, surf, viewport, rect): if not self.avatar: return Added: trunk/client/playground_background.py =================================================================== --- trunk/client/playground_background.py (rev 0) +++ trunk/client/playground_background.py 2010-09-26 22:16:14 UTC (rev 411) @@ -0,0 +1,77 @@ +## This file is part of Virtual Playground +## Copyright (c) 2009 Jos Ratsma + Koen Koning + +## This program is free software; you can redistribute it and/or +## modify it under the terms of the GNU General Public License +## as published by the Free Software Foundation; either +## version 2 of the License, or (at your option) any later version. + +## This program is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. + +## You should have received a copy of the GNU General Public License +## along with this program; if not, write to the Free Software +## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +from functions import * + +class Background(): + def __init__(self, playground): + self.playground = playground + self.backgrounds = {} + self.landscapes = {} + + def add_background(self, filename, surface): + self.backgrounds[filename] = surface + + def update_landscapes(self, landscapes): + self.landscapes = landscapes + + def reset(self): + # Reset everything because the client has been signed off + + # Remove backgrounds + for name in self.backgrounds.keys(): + sh['downloader'].remove_usage(name) + self.backgrounds = {} + + # Remove landscapes + self.landscapes = {} + + def update_parent(self): + self.playground.update(Rect(0, 0, 1000, 700)) + + def update(self, surf, viewport, rect): + # Blit the background + viewport_left = viewport[0] + viewport_top = viewport[1] + viewport_width = 1000 + viewport_height = 700 + + # Calculate the start and end positions of the background + bg_offset = (-viewport_left%250, -viewport_top%250) + x_min = bg_offset[0]-250 + if x_min == -250: x_min = 0 + x_max = x_min + 250 * ((float(viewport_width) / 250)+1) + x_max = int(x_max) + y_min = bg_offset[1]-250 + if y_min == -250: y_min = 0 + y_max = y_min + 250 * ((float(viewport_height) / 250)+1) + y_max = int(y_max) + + # Find the blocks that are within rect + for y in range(y_min, y_max, 250): + for x in range(x_min, x_max, 250): + bg_rect = Rect(x, y, 250, 250) + if bg_rect.colliderect(rect): + + background = self.backgrounds['bg-grass.png'] + bg_area = Rect( + rect.left - bg_rect.left, + rect.top - bg_rect.top, + rect.width, + rect.height) + + surf.blit(background, rect, bg_area) Modified: trunk/client/timer.py =================================================================== --- trunk/client/timer.py 2010-09-25 21:16:57 UTC (rev 410) +++ trunk/client/timer.py 2010-09-26 22:16:14 UTC (rev 411) @@ -53,7 +53,8 @@ # Do stuff for name, info in self.callers.items(): if info[0](name, *info[1]): - del self.callers[name] + try: del self.callers[name] + except: pass # This can happen due to threading. It's not bad # Force one big update sh['main'].updatewaiting = False This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |