def Move(self,amount): amount = Point(amount.x,amount.y) dir = None if amount.x > 0: dir = Directions.RIGHT elif amount.x < 0: dir = Directions.LEFT elif amount.y > 0: dir = Directions.UP elif amount.y < 0: dir = Directions.DOWN if dir != None and dir != self.dir: self.dir = dir self.quad.SetTextureCoordinates(self.dirs[self.dir]) #check each of our four corners for corner in self.corners: pos = self.pos + corner target_x = pos.x + amount.x if target_x >= self.map.size.x: amount.x = 0 target_x = pos.x elif target_x < 0: amount.x = -pos.x target_x = 0 target_tile_x = self.map.data[int(target_x)][int(pos.y)] if target_tile_x.type in game_view.TileTypes.Impassable: amount.x = 0 elif (int(target_x),int(pos.y)) in self.map.object_cache: obj = self.map.object_cache[int(target_x),int(pos.y)] if obj.Contains(Point(target_x,pos.y)): amount.x = 0 target_y = pos.y + amount.y if target_y >= self.map.size.y: amount.y = 0 target_y = pos.y elif target_y < 0: amount.y = -pos.y target_y = 0 target_tile_y = self.map.data[int(pos.x)][int(target_y)] if target_tile_y.type in game_view.TileTypes.Impassable: amount.y = 0 elif (int(pos.x),int(target_y)) in self.map.object_cache: obj = self.map.object_cache[int(pos.x),int(target_y)] if obj.Contains(Point(pos.x,target_y)): amount.y = 0 self.SetPos(self.pos + amount)
def Move(self,t): if self.last_update == None: self.last_update = globals.time return elapsed = globals.time - self.last_update self.last_update = globals.time if self.attacking: return if self.on_ground() or self.on_ladder(): self.move_speed.x += self.move_direction.x*elapsed*0.03 if self.jumping and not self.jumped: self.move_speed.y += self.jump_amount self.jumped = True self.move_speed.x *= 0.8*(1-(elapsed/1000.0)) if self.ladder and not self.above_ladder(): self.ladder = False if self.ladder or (self.on_ladder() and self.move_direction.y != 0): #no gravity on the ladder self.ladder = True self.move_speed.y = self.move_direction.y else: self.move_speed.y += globals.gravity*elapsed*0.03 amount = Point(self.move_speed.x*elapsed*0.03,self.move_speed.y*elapsed*0.03) self.walked += amount.x dir = None if amount.x > 0: dir = Directions.RIGHT elif amount.x < 0: dir = Directions.LEFT if dir != None and dir != self.dir: self.dir = dir self.dirs[self.dir][self.weapon.type].SetStart(self.walked) if abs(amount.x) < 0.0001: self.still = True amount.x = 0 self.move_speed.x = 0 else: self.still = False self.quad.SetTextureCoordinates(self.dirs[self.dir][self.weapon.type].GetTc(self.still,self.walked)) if self.still: self.ResetWalked() #check each of our four corners for corner in self.corners: pos = self.pos + corner target_x = pos.x + amount.x target_y = pos.y + amount.y if target_x >= self.map.size.x: target_x = self.map.size.x-self.threshold amount.x = target_x - pos.x elif target_x < 0: amount.x = -pos.x target_x = 0 target_tile_x = self.map.data[int(target_x)][int(pos.y)] if target_tile_x.type in game_view.TileTypes.Impassable: if amount.x > 0: amount.x = (int(target_x)-pos.x-self.threshold) else: amount.x = (int(target_x)+1-pos.x+self.threshold) target_x = pos.x + amount.x self.TriggerCollide(None) elif (int(target_x),int(pos.y)) in self.map.object_cache: obj = self.map.object_cache[int(target_x),int(pos.y)] if obj.Contains(Point(target_x,pos.y)): if amount.x > 0: amount.x = (int(target_x)-pos.x-self.threshold) else: amount.x = (int(target_x)+1-pos.x+self.threshold) target_x = pos.x + amount.x self.TriggerCollide(obj) else: for actor in target_tile_x.actors: if actor is self: continue if isinstance(actor,Bullet): actor.TriggerCollide(self) continue if isinstance(actor,Collectable): self.TriggerCollide(actor) continue if target_x >= actor.pos.x and target_x < actor.pos.x + actor.size.x and pos.y >= actor.pos.y and pos.y < actor.pos.y + actor.size.y: self.TriggerCollide(actor) if amount.x > 0: new_amount = (actor.pos.x-pos.x-self.threshold) else: new_amount = (actor.pos.x+actor.size.x-pos.x+self.threshold) if abs(new_amount - amount.x) < 0.2: amount.x = new_amount target_x = pos.x + amount.x break for corner_i,corner in enumerate(self.corners): pos = self.pos + corner target_y = pos.y + amount.y if target_y >= self.map.size.y: target_y = self.map.size.y-self.threshold amount.y = target_y - pos.y elif target_y < 0: amount.y = -pos.y target_y = 0 target_tile_y = self.map.data[int(pos.x)][int(target_y)] if target_tile_y.type in game_view.TileTypes.Impassable or not self.ladder and target_tile_y.type in game_view.TileTypes.LadderTops: if amount.y > 0: amount.y = (int(target_y)-pos.y-self.threshold) else: amount.y = (int(target_y)+1+self.threshold-pos.y) if amount.y < 0 and self.move_speed.y > 0: #hit our head print 'head' self.move_speed.y = 0 target_y = pos.y + amount.y self.TriggerCollide(None) elif (int(pos.x),int(target_y)) in self.map.object_cache: obj = self.map.object_cache[int(pos.x),int(target_y)] if obj.Contains(Point(pos.x,target_y)): if amount.y > 0: amount.y = (int(target_y)-pos.y-self.threshold) else: amount.y = (int(target_y)+1+self.threshold-pos.y) target_y = pos.y + amount.y self.TriggerCollide(obj) else: for actor in target_tile_y.actors: if actor is self: continue if isinstance(actor,Bullet): actor.TriggerCollide(self) continue if isinstance(actor,Collectable): self.TriggerCollide(actor) continue if target_y >= actor.pos.y and target_y < actor.pos.y + actor.size.y and pos.x >= actor.pos.x and pos.x < actor.pos.x + actor.size.x: if amount.y > 0: new_amount = (actor.pos.y-pos.y-self.threshold) else: new_amount = (actor.pos.y+actor.size.y-pos.y+self.threshold) if abs(new_amount) <= 0.0001 and abs(amount.y) > 0.0001: self.move_speed.y = 0 if (abs(new_amount) < 0.2): amount.y = new_amount target_y = pos.y + amount.y self.TriggerCollide(actor) break #self.move_speed.y = amount.y if amount.y == 0: self.move_speed.y = 0 self.SetPos(self.pos + amount)
def Position(self,pos,scale,colour = None,ignore_height = False): """Draw the text at the given location and size. Maybe colour too""" #set up the position for the characters. Note that we do everything here in size relative #to our text box (so (0,0) is bottom_left, (1,1) is top_right. self.pos = pos self.absolute.bottom_left = self.GetAbsoluteInParent(pos) self.scale = scale self.lowest_y = 0 row_height = (float(self.text_manager.font_height*self.scale*drawing.texture.global_scale)/self.absolute.size.y) #Do this without any kerning or padding for now, and see what it looks like cursor = Point(self.margin.x,-self.viewpos + 1 - row_height-self.margin.y) letter_sizes = [Point(float(quad.width *self.scale*drawing.texture.global_scale)/self.absolute.size.x, float(quad.height*self.scale*drawing.texture.global_scale)/self.absolute.size.y) for quad in self.quads] #for (i,(quad,letter_size)) in enumerate(zip(self.quads,letter_sizes)): i = 0 while i < len(self.quads): if i in self.newlines: i += 1 cursor.x = self.margin.x cursor.y -= row_height*1.2 continue quad,letter_size = self.quads[i],letter_sizes[i] if cursor.x + letter_size.x > (1-self.margin.x)*1.001: #This would take us over a line. If we're in the middle of a word, we need to go back to the start of the #word and start the new line there restart = False if quad.letter in ' \t': #It's whitespace, so ok to start a new line, but do it after the whitespace try: while self.quads[i].letter in ' \t': i += 1 except IndexError: break restart = True else: #look for the start of the word while i >= 0 and self.quads[i].letter not in ' \t': i -= 1 if i <= 0: #This single word is too big for the line. Shit, er, lets just bail break #skip the space i += 1 restart = True cursor.x = self.margin.x cursor.y -= row_height*1.2 if restart: continue if cursor.x == self.margin.x and self.alignment == drawing.texture.TextAlignments.CENTRE: #If we're at the start of a row, and we're trying to centre the text, then check to see how full this row is #and if it's not full, offset so that it becomes centred width = 0 for size in letter_sizes[i:]: width += size.x if width > 1-self.margin.x: width -= size.x break if width > 0: cursor.x += float(1-(self.margin.x*2)-width)/2 target_bl = cursor target_tr = target_bl + letter_size if target_bl.y < self.lowest_y: self.lowest_y = target_bl.y if target_bl.y < 0 and not ignore_height: #We've gone too far, no more room to write! break absolute_bl = self.GetAbsolute(target_bl) absolute_tr = self.GetAbsolute(target_tr) self.SetLetterVertices(i,absolute_bl, absolute_tr, drawing.texture.TextTypes.LEVELS[self.text_type]) if colour: quad.SetColour(colour) cursor.x += letter_size.x i += 1 #For the quads that we're not using right now, set them to display nothing for quad in self.quads[i:]: quad.SetVertices(Point(0,0),Point(0,0),-10) height = max([q.height for q in self.quads]) super(TextBox,self).UpdatePosition()
def Move(self): if self.last_update is None: self.last_update = globals.time return elapsed = globals.time - self.last_update self.last_update = globals.time self.stance = Stances.STANDING if self.on_ground: if self.move_direction.y > 0: #sort of a jump self.move_speed.y += self.move_direction.y*0.5 self.move_direction.y = 0 self.crouch = False elif self.move_direction.y < 0: #crouching self.stance = Stances.CROUCH if self.stance != Stances.CROUCH: self.move_speed.x += self.move_direction.x*elapsed*0.03 else: self.move_speed.x += self.move_direction.x*elapsed*0.03*0.6 #Apply friction self.move_speed.x *= math.pow(0.7,(elapsed/20.0)) if self.gravity: self.move_speed.y += globals.gravity*elapsed*0.03 amount = Point(self.move_speed.x*elapsed*0.03,self.move_speed.y*elapsed*0.03) self.walked += amount.x if abs(amount.x) < 0.01: self.still = True self.walked = 0 amount.x = 0 self.move_speed.x = 0 if self.in_bow and globals.time < self.in_bow: new_animation = self.bowing[self.dir] else: self.in_bow = False new_animation = self.standing[self.dir][self.stance] else: self.still = False new_animation = self.walking[self.dir][self.stance] if self.transition_requested or self.end_frame is None: self.transition_requested = False #We can set the frame directly since we're not transitioning if new_animation is not self.current_animation: #we want to quickly transition to the first frame of the new animation new_animation.start = globals.time self.set_key_frame(new_animation.get_frame(globals.time,0),100) else: self.set_key_frame(self.current_animation.get_frame(globals.time,self.walked*24.0),0) self.current_animation = new_animation if self.punching: elapsed = globals.time - self.punching.start if elapsed > self.punching.damping: self.punching = False else: damping = 1.0 if elapsed > self.punching.duration: damping = (elapsed - self.punching.duration)/self.punching.damping_duration damping = 1.0 - damping #transition smoothly back to where it should be self.set_key_frame(self.punching.get_frame(globals.time - self.punching.start),0,damping=damping) target = self.pos + amount if target.y < 0: amount.y = -self.pos.y self.move_speed.y = 0 if target.x < 0: amount.x = -self.pos.x if target.x + self.size.x > globals.game_view.absolute.size.x: amount.x = globals.game_view.absolute.size.x - self.size.x - self.pos.x target = self.pos + amount if abs(target.y) < 0.1: self.on_ground = True else: self.on_ground = False self.set_pos(target)