コード例 #1
0
ファイル: blocks.py プロジェクト: mappy13/nihil-ace
    def enemy_ai_update(self):
        # TODO: This is pretty stupid AI. We'll have to work on that later.
        # ALWAYS TURN ON SHIELDS
        if not self.shieldsup:
            [b.on_key_down() for b in self.slave_blocks if isinstance(b, ShieldBlock)]
            self.shieldsup = True

        # ALWAYS FLY TOWARDS PLAYER
        if not SPACE.camera_lock(): # they win and quit moving
            [b.on_key_up() for b in self.slave_blocks]
            return
        target = SPACE.camera_lock # this is naive, but works for now
        target_dir = target()._body.position - self._body.position
        ang = (self._body.angle - target_dir.angle + math.pi / 2) % (math.pi * 2)
        general_direction = abs((ang) % (math.pi * 2)) < math.pi / 8
        ang += self._body.angular_velocity
        if ang > 0 and abs(ang) <= math.pi:
            best_direction = "ccw"
        elif ang > 0 and abs(ang) > math.pi:
            best_direction = "cw"
        elif ang < 0 and abs(ang) <= math.pi:
            best_direction = "cw"
        elif ang < 0 and abs(ang) > math.pi:
            best_direction = "ccw"
        if self._body.angular_velocity > math.pi / 2:
            [b.on_key_up() for b in self.slave_blocks if b.key == key.LEFT]
            [b.on_key_down() for b in self.slave_blocks if b.key == key.RIGHT]
        elif self._body.angular_velocity < -math.pi / 2:
            [b.on_key_up() for b in self.slave_blocks if b.key == key.RIGHT]
            [b.on_key_down() for b in self.slave_blocks if b.key == key.LEFT]
        # TODO: If moving very fast relative to the target, slow down
        elif best_direction == "cw":
            [b.on_key_up() for b in self.slave_blocks if b.key == key.RIGHT]
            [b.on_key_down() for b in self.slave_blocks if b.key == key.LEFT]
        else:
            [b.on_key_up() for b in self.slave_blocks if b.key == key.LEFT]
            [b.on_key_down() for b in self.slave_blocks if b.key == key.RIGHT]

        # SHOOT AT PLAYER IF CLOSE
        ang = self._body.angle - target_dir.angle + math.pi / 2
        if (target_dir.length <= BLOCK_SIZE * 50 and general_direction):
            [b.on_key_down() for b in self.slave_blocks if isinstance(b, BlasterBlock)]
        else:
            [b.on_key_up() for b in self.slave_blocks if isinstance(b, BlasterBlock)]