示例#1
0
    def test_new_collisions_involving_same_object_are_handled(self):
        """New collisions involving the same object are handled."""
        a = Rectangle(x=1, y=3, width=2, height=2)
        b = Rectangle(x=2, y=2, width=2, height=2)
        c = Rectangle(x=3, y=1, width=2, height=2)

        self.cache.add_collision(a, b)
        self.cache.update(1)

        # New collision between a and b occurred
        self.assertFalse(self.cache.get_removed_collisions())

        new_collisions = self.cache.get_new_collisions()
        self.assertEqual(1, len(new_collisions))
        self.assertCountEqual((a, b), list(new_collisions)[0])

        self.cache.add_collision(a, b)
        self.cache.add_collision(b, c)
        self.cache.update(1)

        # New collision between b and c occurred
        self.assertFalse(self.cache.get_removed_collisions())

        new_collisions = self.cache.get_new_collisions()
        self.assertEqual(1, len(new_collisions))
        self.assertCountEqual((b, c), list(new_collisions)[0])
    def test_repeated_collisions_are_new_after_removal(self):
        """A collision is considered new after its removal."""
        a = Rectangle(x=1, y=3, width=2, height=2)
        b = Rectangle(x=2, y=2, width=2, height=2)

        self.cache.add_collision(a, b, (1, 1))
        self.cache.update(1)

        # Object is moved to no longer collide
        a.coordinates.set((4, 4))

        self.cache.add_collision(a, b, (0, 0))  # No resolution was necessary
        self.cache.update(1)

        # Object is moved to collide again
        a.coordinates.set((1, 3))

        self.cache.add_collision(a, b, (1, 1))
        self.cache.update(1)

        self.assertFalse(self.cache.get_removed_collisions())

        new_collisions = self.cache.get_new_collisions()
        self.assertEqual(1, len(new_collisions))
        self.assertCountEqual((a, b), list(new_collisions)[0])
    def test_repeated_collisions_are_not_new_or_removed(self):
        """Collisions are not new when the same collision occurs again."""
        a = Rectangle(x=1, y=3, width=2, height=2)
        b = Rectangle(x=2, y=2, width=2, height=2)

        self.cache.add_collision(a, b, (1, 1))
        self.cache.update(1)

        self.cache.add_collision(a, b, (1, 1))
        self.cache.update(1)

        self.assertFalse(self.cache.get_removed_collisions())
        self.assertFalse(self.cache.get_new_collisions())
    def test_new_collisions_are_new_and_not_removed(self):
        """New collisions are considered new and not removed."""
        a = Rectangle(x=1, y=3, width=2, height=2)
        b = Rectangle(x=2, y=2, width=2, height=2)

        self.cache.add_collision(a, b, (1, 1))
        self.cache.update(1)

        self.assertFalse(self.cache.get_removed_collisions())

        new_collisions = self.cache.get_new_collisions()
        self.assertEqual(1, len(new_collisions))
        self.assertCountEqual((a, b), list(new_collisions)[0])
    def test_new_collisions_are_not_registered_twice(self):
        """New collisions are not registered twice between both objects."""
        a = Rectangle(x=1, y=3, width=2, height=2)
        b = Rectangle(x=2, y=2, width=2, height=2)

        self.cache.add_collision(a, b, (1, 1))
        self.cache.add_collision(b, a, (1, 1))
        self.cache.update(1)

        self.assertFalse(self.cache.get_removed_collisions())

        new_collisions = self.cache.get_new_collisions()
        self.assertEqual(1, len(new_collisions))
        self.assertCountEqual((a, b), list(new_collisions)[0])
    def test_collision_is_not_removed_without_velocity_y(self):
        """Collisions are not removed if movement occured without velocity."""
        a = Rectangle(x=1, y=3, width=2, height=2)
        b = Rectangle(x=2, y=2, width=2, height=2)

        self.cache.add_collision(a, b, (1, 1))
        self.cache.update(1)

        # Object moved to no longer collide on the y axis
        a.y = 2

        self.cache.add_collision(a, b, (1, 0))  # No y velocity
        self.cache.update(1)

        self.assertFalse(self.cache.get_removed_collisions())
        self.assertFalse(self.cache.get_new_collisions())
示例#7
0
    def test_collision_is_removed_when_no_longer_colliding(self):
        """Collisions are removed if the collision wasn't added again."""
        a = Rectangle(x=1, y=3, width=2, height=2)
        b = Rectangle(x=2, y=2, width=2, height=2)

        self.cache.add_collision(a, b)
        self.cache.update(1)

        # The collision is not added because the objects no longer collide
        self.cache.update(1)

        self.assertFalse(self.cache.get_new_collisions())

        removed_collisions = self.cache.get_removed_collisions()
        self.assertEqual(1, len(removed_collisions))
        self.assertCountEqual((a, b), list(removed_collisions)[0])
    def test_collision_is_removed_after_noncolliding_movement(self):
        """Collisions are removed after objects move outside the collision."""
        a = Rectangle(x=1, y=3, width=2, height=2)
        b = Rectangle(x=2, y=2, width=2, height=2)

        self.cache.add_collision(a, b, (1, 1))
        self.cache.update(1)

        # Object is moved to no longer collide
        a.coordinates.set((4, 4))

        self.cache.add_collision(a, b, (0, 0))  # No resolution was necessary
        self.cache.update(1)

        self.assertFalse(self.cache.get_new_collisions())

        removed_collisions = self.cache.get_removed_collisions()
        self.assertEqual(1, len(removed_collisions))
        self.assertCountEqual((a, b), list(removed_collisions)[0])
示例#9
0
    def test_repeated_collisions_are_new_after_removal(self):
        """A collision is considered new after its removal."""
        a = Rectangle(x=1, y=3, width=2, height=2)
        b = Rectangle(x=2, y=2, width=2, height=2)

        self.cache.add_collision(a, b)
        self.cache.update(1)

        # No collision added because they no longer collide
        self.cache.update(1)

        self.cache.add_collision(a, b)
        self.cache.update(1)

        self.assertFalse(self.cache.get_removed_collisions())

        new_collisions = self.cache.get_new_collisions()
        self.assertEqual(1, len(new_collisions))
        self.assertCountEqual((a, b), list(new_collisions)[0])
    def test_removed_collisions_are_not_registered_twice(self):
        """Removed collisions are not registered twice between both objects."""
        a = Rectangle(x=1, y=3, width=2, height=2)
        b = Rectangle(x=2, y=2, width=2, height=2)

        self.cache.add_collision(a, b, (1, 1))
        self.cache.update(1)

        # Object is moved to no longer collide
        a.coordinates.set((4, 4))

        self.cache.add_collision(a, b, (0, 0))  # No resolution was necessary
        self.cache.add_collision(b, a, (0, 0))
        self.cache.update(1)

        self.assertFalse(self.cache.get_new_collisions())

        removed_collisions = self.cache.get_removed_collisions()
        self.assertEqual(1, len(removed_collisions))
        self.assertCountEqual((a, b), list(removed_collisions)[0])
示例#11
0
    def test_removed_collisions_involving_same_object_are_handled(self):
        """Removed collisions involving the same object are handled."""
        a = Rectangle(x=1, y=3, width=2, height=2)
        b = Rectangle(x=2, y=2, width=2, height=2)
        c = Rectangle(x=3, y=1, width=2, height=2)

        self.cache.add_collision(a, b)
        self.cache.update(1)

        self.cache.add_collision(a, b)
        self.cache.add_collision(b, c)
        self.cache.update(1)

        # Collision between a and b did not reoccur
        self.cache.add_collision(b, c)
        self.cache.update(1)

        # Collision between a and b was removed
        removed_collisions = self.cache.get_removed_collisions()
        self.assertEqual(1, len(removed_collisions))
        self.assertCountEqual((a, b), list(removed_collisions)[0])

        # No new collision between a and b was erroneously added
        new_collisions = self.cache.get_new_collisions()
        self.assertNotIn((a, b), new_collisions)
        self.assertNotIn((b, a), new_collisions)

        # Collision between b and c did not reoccur
        self.cache.update(1)

        # Collision between b and c was removed
        self.assertFalse(self.cache.get_new_collisions())

        removed_collisions = self.cache.get_removed_collisions()
        self.assertEqual(1, len(removed_collisions))
        self.assertCountEqual((b, c), list(removed_collisions)[0])
示例#12
0
    def __init__(self, width, height):
        """Creates a new camera with the given dimensions.

        Args:
            width (int): The width of the camera.
            height (int): The height of the camera.
        """
        super(Camera, self).__init__(0, 0, width, height)

        # Scale of the visible scene
        self.scale = 1

        # Object tracking
        self.follow = None
        self.follow_easing = None
        self.follow_lead = Point2d(0, 0)
        self._follow_deadzone = Rectangle(0, 0, 0, 0)
        self._apply_deadzone = [False, False]  # Internal for smooth following

        # Boundary to restrict the camera to
        self._x_boundary = None
        self._y_boundary = None