From: <Blu...@us...> - 2010-09-25 21:17:04
|
Revision: 410 http://virtplayground.svn.sourceforge.net/virtplayground/?rev=410&view=rev Author: BlueWolf_ Date: 2010-09-25 21:16:57 +0000 (Sat, 25 Sep 2010) Log Message: ----------- Finally made a proper engine for the viewport mover when walking with the avatar. Background temporary removed as it will be placed in a seperated file as well. Beware of weird effects while walking! :D Modified Paths: -------------- trunk/client/playground.py trunk/client/playground_avatar.py Modified: trunk/client/playground.py =================================================================== --- trunk/client/playground.py 2010-09-17 18:10:18 UTC (rev 409) +++ trunk/client/playground.py 2010-09-25 21:16:57 UTC (rev 410) @@ -19,6 +19,11 @@ import gui from playground_avatar import Avatar +POS_UPDATE_DELAY = 1 # Determines when a pos-update should be send. + # Every x seconds. +MOVE_SPEED = 175 # How fast the avatar shoud walk + + class Playground(): def __init__(self): @@ -28,11 +33,11 @@ self.statuswhere = None self.backgrounds = {} + self.viewport_float = None self.viewport = None self.direction = None self.avatars = {} self.this_user = None - self.avatar_offset = None def event(self, ev): @@ -68,6 +73,7 @@ if direction == None and self.direction != None: # Stop the timer. We are not walking + self.direction = None sh['timer'].stop("playground-walk") elif self.direction == None or \ @@ -77,7 +83,9 @@ self.direction = (direction, time.time(), self.this_user['pos'][:]) sh['timer'].start("playground-walk", - self.playground_walk) + self.timer_walk) + #sh['timer'].start("playground-pos-update", + # self.timer_walk, time.time()) def call(self, name, data): @@ -89,10 +97,10 @@ sh['downloader'].remove_usage(name) self.backgrounds = {} self.viewport = None + self.viewport_float = None self.direction = None self.avatars = {} self.this_user = None - self.avatar_offset = None elif data['status'] == "playground" and self.status != "playground": # Load the data for the playground @@ -101,26 +109,18 @@ self.this_user = sh['client'].userlist[sh['client'].cid] for user in sh['client'].userlist.values(): if user['pos'] != None: - self.avatars[user['cid']] = Avatar(user) - - to_center = ( - (self.surf.get_width()/2) - \ - (self.avatars[self.this_user['cid']].size[0]/2), - (self.surf.get_height()/2) - \ - (self.avatars[self.this_user['cid']].size[1]/2), - ) - self.avatar_offset = ( - self.this_user['pos'][0] - to_center[0], - self.this_user['pos'][1] - to_center[1],\ - ) + self.avatars[user['cid']] = Avatar(user, self) - self.viewport = Rect( - self.avatar_offset[0], - self.avatar_offset[1], - 1000, - 700) - - self.playground_render() + avatar = self.avatars[self.this_user['cid']] + self.viewport_float = [ + (self.this_user['pos'][0] - avatar.center[0]) - (1000/2), + (self.this_user['pos'][1] - avatar.center[1]) - (700/2), + ] + self.viewport = [ + int(self.viewport_float[0]), + int(self.viewport_float[1]) + ] + self.update() self.status = data['status'] self.statuswhere = data['where'] @@ -136,56 +136,20 @@ "background", counter) - def playground_render(self, rect = None, doupdate = True): - # Render the playground + def update(self, rect = None, doupdate = True): + # This will blit and update the screen at rect + if rect == None: rect = Rect(0, 0, 1000, 700) else: rect = Rect(rect) # Blit the background - bg_offset = (-self.viewport.left%250, -self.viewport.top%250) - x_min = bg_offset[0]-250 - if x_min == -250: x_min = 0 - x_max = x_min + 250 * ((float(self.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(self.viewport.height) / 250)+1) - y_max = int(y_max) + #self.background.update(self.surf, self.viewport, rect) - # 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) - - self.surf.blit(background, rect, bg_area) - # Blit the users for avatar in self.avatars.values(): - avatar_rect = Rect( - avatar.user['pos'][0] - self.viewport[0], - avatar.user['pos'][1] - self.viewport[1], - avatar.size[0], - avatar.size[1], - ) - - if not rect.colliderect(avatar_rect): continue - - avatar_area = Rect( - rect.left - avatar_rect.left, - rect.top - avatar_rect.top, - rect.width, - rect.height) - avatar.blit(self.surf, avatar_area) + avatar.update(self.surf, self.viewport, rect) if doupdate: sh['update'](rect) @@ -206,35 +170,87 @@ }) - def playground_walk(self, name): - direction, starttime, startpos = self.direction - timeline = time.time() - starttime - speed = 175 - move = timeline*speed + def timer_walk(self, name, starttime = None): + if name == "playground-walk": + direction, starttime, startpos = self.direction + timeline = time.time() - starttime + speed = MOVE_SPEED + move = timeline*speed + + pos = self.this_user['pos'][:] + if direction == 0: + pos[1] = int(startpos[1] - move) + elif direction == 1: + pos[1] = int(startpos[1] - (move/1.7)) + pos[0] = int(startpos[0] + (move/1.7)) + elif direction == 2: + pos[0] = int(startpos[0] + move) + elif direction == 3: + pos[0] = int(startpos[0] + (move/1.7)) + pos[1] = int(startpos[1] + (move/1.7)) + elif direction == 4: + pos[1] = int(startpos[1] + move) + elif direction == 5: + pos[1] = int(startpos[1] + (move/1.7)) + pos[0] = int(startpos[0] - (move/1.7)) + elif direction == 6: + pos[0] = int(startpos[0] - move) + elif direction == 7: + pos[0] = int(startpos[0] - (move/1.7)) + pos[1] = int(startpos[1] - (move/1.7)) + + avatar = self.avatars[self.this_user['cid']] + avatar.move(pos) + sh['timer'].start("playground-viewport-move", + self.timer_walk) - if direction == 0: - self.this_user['pos'][1] = startpos[1] - move - elif direction == 1: - self.this_user['pos'][1] = startpos[1] - (move/1.7) - self.this_user['pos'][0] = startpos[0] + (move/1.7) - elif direction == 2: - self.this_user['pos'][0] = startpos[0] + move - elif direction == 3: - self.this_user['pos'][0] = startpos[0] + (move/1.7) - self.this_user['pos'][1] = startpos[1] + (move/1.7) - elif direction == 4: - self.this_user['pos'][1] = startpos[1] + move - elif direction == 5: - self.this_user['pos'][1] = startpos[1] + (move/1.7) - self.this_user['pos'][0] = startpos[0] - (move/1.7) - elif direction == 6: - self.this_user['pos'][0] = startpos[0] - move - elif direction == 7: - self.this_user['pos'][0] = startpos[0] - (move/1.7) - self.this_user['pos'][1] = startpos[1] - (move/1.7) - self.viewport.topleft = ( - self.this_user['pos'][0]+self.avatar_offset[0], - self.this_user['pos'][1]+self.avatar_offset[1], - ) - self.playground_render() + elif name == "playground-viewport-move": + # Move the viewport in a smooth way + + # First get the current viewport pos and the desired pos + pos = self.viewport_float + avatar = self.avatars[self.this_user['cid']] + desired_pos = [ + (self.this_user['pos'][0] - avatar.center[0]) - (1000/2), + (self.this_user['pos'][1] - avatar.center[1]) - (700/2) + ] + + # Calculate the differences between these two positions. + # Hello Pythagoras! + distance = math.sqrt( + (pos[0] - desired_pos[0])**2 + + (pos[1] - desired_pos[1])**2) + + if distance < 1: + self.viewport_float = desired_pos + else: + # Calculate how much we should move + speed = (distance/100000) + + if speed > 0.9: speed = 0.9 + speed += 0.05 + move = [0,0] + + move[0] = (desired_pos[0] - pos[0]) * speed + move[1] = (desired_pos[1] - pos[1]) * speed + + self.viewport_float[0] += move[0] + self.viewport_float[1] += move[1] + + # Keep in mind that pygame only likes integers, not floats + real_move = [ + int(self.viewport_float[0] - self.viewport[0]), + int(self.viewport_float[1] - self.viewport[1]) + ] + self.viewport[0] += real_move[0] + self.viewport[1] += real_move[1] + + self.surf.scroll(-real_move[0], -real_move[1]) + sh['update']() + + if self.viewport_float == desired_pos: return True + + + elif name == "playground-pos-update": + pass Modified: trunk/client/playground_avatar.py =================================================================== --- trunk/client/playground_avatar.py 2010-09-17 18:10:18 UTC (rev 409) +++ trunk/client/playground_avatar.py 2010-09-25 21:16:57 UTC (rev 410) @@ -18,17 +18,55 @@ from functions import * class Avatar(): - def __init__(self, user): + def __init__(self, user, playground): self.user = user - self.lastpos = user['pos'] + self.playground = playground self.avatar = None self.size = (40,40) + self.center = (20, 40) + sh['downloader'].get_file("happyblock.png", "img", self.download) + def move(self, pos): + lastpos = self.user['pos'] + self.user['pos'] = pos + + self.update_parent() + def download(self, filename, cache): self.avatar = cache - def blit(self, surf, area): - if self.avatar: - surf.blit(self.avatar, (0,0), area) + 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], + self.size[0], + self.size[1]) + + if not Rect(0, 0, 1000, 700).colliderect(self_rect): return + + self.playground.update(self_rect) + + def update(self, surf, viewport, rect): + if not self.avatar: return + + # Check if we collide + self_rect = Rect( + self.user['pos'][0] - viewport[0] - self.center[0], + self.user['pos'][1] - viewport[1] - self.center[1], + self.size[0], + self.size[1]) + + if not rect.colliderect(self_rect): return + + surf.blit(self.avatar, rect, Rect( + rect.left - self_rect.left, + rect.top - self_rect.top, + rect.width, + rect.height)) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |