Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds sprite_in_view method to replace old sprite_in_viewport. #467

Merged
merged 2 commits into from
May 13, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions ppb/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from ppb_vector import Vector

from ppb.sprites import RectangleShapeMixin
from ppb.sprites import Sprite


class Camera(RectangleShapeMixin):
Expand Down Expand Up @@ -110,6 +111,25 @@ def point_is_visible(self, point: Vector) -> bool:
and self.bottom <= point.y <= self.top
)

def sprite_in_view(self, sprite: Sprite) -> bool:
AstraLuma marked this conversation as resolved.
Show resolved Hide resolved
"""
Determine if a given sprite is in view of the camera.

Does not guarantee that the sprite will be rendered, only that it
exists in the visible space.

:param sprite: The sprite to check
:type: Sprite
:return: Whether the sprite is in the space in view of the camera.
:rtype: bool
"""
width = max(self.right, sprite.right) - min(self.left, sprite.left)
AstraLuma marked this conversation as resolved.
Show resolved Hide resolved
height = max(self.top, sprite.top) - min(self.bottom, sprite.bottom)
max_width = self.width + sprite.width
max_height = self.height + sprite.height
print(f"W: {width}, H: {height}, MW: {max_width}, MH: {max_height}")
return width < max_width and height < max_height

def translate_point_to_screen(self, point: Vector) -> Vector:
"""
Convert a vector from game position to screen position.
Expand Down
40 changes: 19 additions & 21 deletions tests/test_camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,27 +104,25 @@ def test_camera_translate_point_to_game_space(camera, position, point, expected)
assert camera.translate_point_to_game_space(point) == expected


# @pytest.mark.skip(reason="Test for old camera. Will want to restore this functionality in new camera.")
# def test_sprite_in_viewport():
# # Added the expected pixel ratio due to change in default breaking this test.
# # 80 is the legacy value.
# cam = OldCamera(viewport=(0, 0, 800, 600), pixel_ratio=80)
#
# class Thing(Sprite):
# def __init__(self, position=Vector(2, 2)):
# super().__init__()
# self.size = 2
# self.position = position
#
# sprite_in = Thing(Vector(-3, -1))
# sprite_half_in = Thing(Vector(5, -2))
# sprite_out = Thing(Vector(2, 5))
#
# assert not cam.in_frame(sprite_out)
# assert cam.in_frame(sprite_in)
# assert cam.in_frame(sprite_half_in)
#
#
@pytest.mark.parametrize("input_position, expected", [
[Vector(-3, 1), True], # Fully inside the camera's view
[Vector(5, -2), True], # partially inside the camera's view
[Vector(2, 6), False], # well outside the Camera's view.
[Vector(6, 0), False], # Outside with edges touching (horizontal)
[Vector(0, 4.75), False], # Outside with edges touching (vertical)
])
def test_sprite_in_view(camera, input_position, expected):

class Thing(Sprite):
AstraLuma marked this conversation as resolved.
Show resolved Hide resolved
size = 2
position = input_position

test_sprite = Thing()
print(f"Sprite: {test_sprite.position}, {test_sprite.width}, {test_sprite.height}")
print(f"Camera: {camera.position}, {camera.width}, {camera.height}")
assert camera.sprite_in_view(test_sprite) == expected


# @pytest.mark.skip("Old camera test. Will probably want to rewrite this in the future to support new camera.")
# @given(
# vp_width=st.integers(min_value=1),
Expand Down