Example #1
0
    def unit_step(self, unit: Unit, comands: dict, dt: float, log: list):
        move = self._validate('move', comands, -1, 1)
        rotate = self._validate('rotate', comands, -1, 1)
        vision = self._validate('vision', comands, -1, 1)
        fire = self._validate('fire', comands, 0, 1)
        if abs(move) > 1e-8:
            p1, p2 = unit.get_move_segment(move, rotate, dt)
            p1, p3 = self.scene.intersected_segment(p1, p2)
            if p3 != p2:
                unit.pos = p1
            else:
                unit.pos = p2
                log.append(
                    f'Unit name={unit.name} moved from {p1} to {unit.pos}')

        if abs(rotate) > 1e-8:
            a0 = unit.alpha
            unit.rotate(rotate, dt)
            a1 = unit.alpha
            log.append(f'Unit name={unit.name} rotated from {a0} to {a1}')

        if abs(vision) > 1e-8:
            t0, r0 = unit.theta, unit.r_vis
            unit.change_vision(vision, dt)
            t1, r1 = unit.theta, unit.r_vis
            log.append(
                f'Unit name={unit.name} change vision theta from {t0} to {t1}')
            log.append(
                f'Unit name={unit.name} change vision radius from {r0} to {r1}'
            )

        if fire > 1e-8:
            round_new = unit.shoot()
            if round_new:
                self.rounds.add(round_new)
                log.append(
                    f'Unit name={unit.name} fired by Round id={round_new.id} with pos={round_new.pos}, vel={round_new.vel}'
                )
        unit.time += dt