def relative_side(self, entity): """Return what side of an entity we are on, to be used with collision""" # THIS IS VERY BROKEN PLZ FIX wall = hasattr(entity, 'side') sides = [ 'top', 'right', 'bottom', 'left' ] # 45:'top right', 135:'bottom right', 225:'bottom left', 315:'top left'} sloc = self.location eloc = entity.location rect = entity.get_col_rect() vec = Vector2.from_points(sloc, eloc) if not wall: final = [i * 90 for i in range(4)] first = [i * 45 for i in range(8)] rsides = [rect.midtop, rect.midright, rect.midbottom, rect.midleft] #rsides = [Vector2(x, y) for x, y in rsides] #side_deg = [atan2(v.y, v.x) for v in rsides] side_deg = [ round((heading_to_degrees( Vector2.from_points(entity.location, Vector2( x, y)).get_normalized()) + 360) % 360) for x, y in rsides ] deg = 360 - round((heading_to_degrees(vec.get_normalized())) % 360) if deg <= 45: deg = 0 sdeg = closest(round(deg), side_deg) num = side_deg.index(sdeg) return sides[num] elif wall: return entity.side return None
def process(self, time_passed): """Process brain and move according to time passed if speed > 0 and not at destination""" if self.doprocess: self.brain.think() if self.speed > 0 and self.location != self.destination: #vec_to_dest = self.destination - self.location #distance_to_dest = vec_to_dest.get_length() vec_to_dest = Vector2.from_points(self.location, self.destination) distance_to_dest = self.location.get_distance_to( self.destination) heading = vec_to_dest.get_normalized() # prevent going back and forward really fast once it make it close to destination travel_distance = min(distance_to_dest, (time_passed * self.speed)) self.location += heading * round(travel_distance)
def bounceoff(self, sprite, must_be_visible=False): do = True if must_be_visible: do = sprite.visible and self.visible if self.collision(sprite) and do: test = Vector2.from_points(self.xy(), self._lastpos) test.normalize() if list(test) == list(self.heading): x2, y2 = self.xy() x3, y3 = self._lastpos x1, y1 = x2, y3 a = sqrt(((x1 - x2)**2) + ((y1 - y2)**2)) b = sqrt(((x1 - x3)**2) + ((y1 - y3)**2)) c = sqrt(((x2 - x3)**2) + ((y2 - y3)**2)) try: x = acos(((b**2) - (a**2) - (c**2)) / (2 * a * c)) except ZeroDivisionError: pass else: y = radians(180 - degrees(x)) self.heading = Vector2(cos(y), sin(y)) self.speed *= sprite.bouncinesss
def bounce(entity, wall): """When an entity detects collision, if you also tell it to "bounce", it will bounce off that wall""" # VERY MUCH BROKEN PLZ FIX vec = Vector2.from_points(entity.location, entity.destination) heading = vec.get_normalized() tmp = heading.copy() heading = -heading deg = heading_to_degrees(heading) if wall.addname in ('top', 'bottom'): deg += 90 deg = 180 - deg if wall.addname in ('top', 'bottom'): deg -= 90 if deg < 0: deg += 360 heading = degrees_to_heading(deg) entity.destination = heading * (vec.get_magnitude() + 100) #heading = Vector2.from_points(entity.location, entity.destination).get_normalized() entity.location = heading * 50
def point_at(self, point): surf, xy = self.getdisplay() destination = Vector2(dest) - Vector2(surf.get_size()) / 2 heading = Vector2.from_points(self.xy(), point) self.setheading(heading)