def shoot(self): """ Eject projectile """ # -- set relative muzzle location muzzle_loc = Vec2(self.radius * 1.5, -self.radius * 0.4) # -- calculate direction of (1.muzzle location), (2.player rotation) rotation = muzzle_loc.angle + self.rotation d_muzzle = Vec2(math.cos(rotation), math.sin(rotation)) d_player = Vec2(math.cos(self.rotation), math.sin(self.rotation)) # -- eject bullet pos = self.position + (d_muzzle * muzzle_loc.length) self.projectiles.add(pos, d_player, self.batch, tag="PlayerBullet")
def on_update(self, dt): """ Center the camera on target """ if not self._track_target: return # -- clamp target to camera bounds sx, sy = self.size / 2 px, py = self._track_target.position position = Vec2( clamp(px, self.bounds.left + sx, self.bounds.right - sx), clamp(py, self.bounds.bottom + sy, self.bounds.top - sy), ) # -- set camera offset (distance from center of camera to target_pos) self.offset = self._size / 2 - position # -- move the camera steadily to reduce offset epsilon = 2.0 dist = self.offset.get_distance(self._position) norm = (self.offset - self._position).normalized() if dist >= epsilon: # XXX NOTE: dist is added to speed to prevent camera from lagging behind self._position += norm * dt * (self.speed + dist) pg.gl.glMatrixMode(pg.gl.GL_MODELVIEW) pg.gl.glLoadIdentity() pg.gl.glTranslatef(*self._position, 0) pg.gl.glScalef(*self._scale, 1)
def _shoot_at(self, target): """ Eject projectile every attack frequency""" self.attack_counter += 1 if self.attack_counter % self.attack_fequency == 0: # -- set relative muzzle location muzzle_loc = Vec2(self.radius * 1.5, -self.radius * 0.4) # -- calculate direction of (1.muzzle location), (2.enemy rotation) rotation = muzzle_loc.angle + self.rotation d_muzzle = Vec2(math.cos(rotation), math.sin(rotation)) d_enemy = Vec2(math.cos(self.rotation), math.sin(self.rotation)) # -- eject bullet pos = self.position + (d_muzzle * muzzle_loc.length) self.projectiles.add(pos, d_enemy, self.batch, tag="EnemyBullet")
def _move_to(self, target, dt): diff = Vec2(target) - self.position dist = self.position.get_dist_sqrd(target) if dist: dx, dy = diff.normalized() self.velocity = (dx * self.speed * dt, dy * self.speed * dt) else: self.velocity = (0, 0)
def _set_offset(self, off): self._offset = off if isinstance(off, Vec2) else Vec2(off)
def _set_scale(self, sc): self._scale = Vec2(sc)
def _set_size(self, sc): self._size = Vec2(sc)
def _set_speed(self, sc): self._speed = Vec2(sc)