def separation(self, list): mean = (0.0, 0.0) for i in list: dist = vector.mag(vector.sub(self.rect.center, i.rect.center)) if dist > 0 and dist < self.__separation_limit: mean = vector.add(mean, vector.divs(vector.sub(self.rect.center, i.rect.center), dist)) return vector.divs(mean, len(list))
def rate(self, list): mean = (0, 0) for i in list: mean = vector.add(mean, i.velocity) mean = vector.divs(mean, len(list)) meandist = vector.mag(mean) if meandist > self.__force_max: mean = vector.muls(vector.divs(mean, meandist), self.__force_max) return mean
def update(self, delta, view): super(Boss, self).update(delta) if self.rect.colliderect(view): if not self.active: self.active = True self.sound_announce.play() if self.idle > 0: self.idle -= delta else: if self.whirl: v = (self.target.centerx - self.rect.centerx, self.target.centery - self.rect.centery) mag = vector.mag(v) if mag < 300 * delta: self.rect.center = self.target.center self.whirl = False self.target = None self.idle = self.__whirlsecs self.charging = False else: v = vector.muls(vector.divs(v, vector.mag(v)), 300 * delta) self.move(v) else: if self.charging: self.move((0, self.__charge_speed * delta)) self.rect.bottom = min(self.rect.bottom, view.bottom) if self.rect.bottom == view.bottom: self.charging = False self.set_animation(self.sprite_swim, 4) elif self.target: v = (self.target.centerx - self.rect.centerx, self.target.centery - self.rect.centery) mag = vector.mag(v) if mag < self.__speed * delta: self.rect.center = self.target.center self.set_animation(self.sprite_swim, 4) self.target = None else: v = vector.muls(vector.divs(v, vector.mag(v)), self.__speed * delta) self.move(v) else: if random.random() < self.__charge_chance: self.charging = True self.set_animation(self.sprite_charge, 4) self.sound_charge.play() else: self.target = pygame.Rect((self.rect.left + random.randint(-self.__range, self.__range), self.rect.top + random.randint(-self.__range, self.__range)), (137, 274)) self.target.left = max(self.target.left, 144) self.target.right = min(self.target.right, 768 - 144) self.target.top = max(self.target.top, view.top) self.target.bottom = min(self.target.bottom, view.top + (864 * 0.75))
def cohesion(self, l): v = (0, 0) for i in l: v = vector.add(v, i.rect.center) v = vector.divs(v, len(l)) return self.steer(v)
def steer(self, v): diff = vector.sub(v, self.rect.center) dist = vector.mag(diff) if dist > 0: diff = vector.divs(diff, dist) damp = 64.0 if dist < damp: diff = vector.muls(diff, self.__velocity_max * (dist / damp)) else: diff = vector.muls(diff, self.__velocity_max) vec = vector.sub(diff, self.velocity) vecdist = vector.mag(vec) if vecdist > self.__force_max: vec = vector.muls(vector.divs(vec, vecdist), self.__force_max) else: vec = (0, 0) return vec
def update(self, delta, view): """Updates Location""" super(Guppy, self).update(delta) # Move based on School if self.whirl: v = (self.target.centerx - self.rect.centerx, self.target.centery - self.rect.centery) mag = vector.mag(v) if mag < 300 * delta: self.rect.center = self.target.center else: v = vector.muls(vector.divs(v, vector.mag(v)), 300 * delta) self.move(v) else: self.rect.center = vector.add(self.rect.center, vector.muls(self.velocity, delta))
def update(self, delta, view): super(Fish, self).update(delta) # Only drift if on screen #if self.rect.colliderect(view): # self.rect.top += self.__drift_speed * delta #if whirlpool hits do not allow fish to target salmon if self.target == None: self.move((0, self.__drift_speed * delta)) else: if not self.whirl: self.move((self.__velocity[0] * delta, self.__velocity[1] * delta )) else: v = (self.target.centerx - self.rect.centerx, self.target.centery - self.rect.centery) mag = vector.mag(v) if mag < 300 * delta: self.rect.center = self.target.center else: v = vector.muls(vector.divs(v, vector.mag(v)), 300 * delta) self.move(v)
def update(self, delta, view): super(Catfish, self).update(delta) if self.rect.colliderect(view): if self.whirl: v = (self.target.centerx - self.rect.centerx, self.target.centery - self.rect.centery) mag = vector.mag(v) if mag < 300 * delta: self.rect.center = self.target.center else: v = vector.muls(vector.divs(v, vector.mag(v)), 300 * delta) self.move(v) else: if self.bound.colliderect(self.target): if self.rect.centerx < self.target.centerx: self.move((self.__chase_speed * delta, 0)) self.rect.centerx = min(self.rect.centerx, self.target.centerx) self.rect.right = min(self.rect.right, self.bound.right) elif self.rect.centerx > self.target.centerx: self.move((-self.__chase_speed * delta, 0)) self.rect.centerx = max(self.rect.centerx, self.target.centerx) self.rect.left = max(self.rect.left, self.bound.left) self.move(vector.muls(self.__velocity, delta))