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

Add rotatability to sprites. #214

Merged
merged 5 commits into from
Apr 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
36 changes: 29 additions & 7 deletions ppb/sprites.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,34 @@ def _attribute_gate(self, attribute, bad_sides):
raise AttributeError(message)


class BaseSprite(EventMixin):
class Rotatable:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rotatable sounds weird to me, but I'm a silly non-native speaker. shrugs

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Native speaker, sounds weird to me, too, but lots of mixins are named based on their capabilities.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-able is approximately the right convention, but it leads to lots of weird words.

I would also accept CanBe*? Also weird.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So like CanBeRotated?

Copy link
Contributor

@nbraud nbraud Mar 30, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or just CanRotate? In any case, I don't think Rotatable is a big issue, I was just very confused whether there was a typo, or it was just weird, or I was just bad at English.

"""
A simple rotation mixin. Can be included with sprites.
"""
_rotation = 0
# This is necessary to make facing do the thing while also being adjustable.
basis = Vector(0, -1)
# Considered making basis private, the only reason to do so is to
# discourage people from relying on it as data.

@property
def facing(self):
return Vector(*self.basis).rotate(self.rotation).normalize()

@property
def rotation(self):
return self._rotation

@rotation.setter
def rotation(self, value):
self._rotation = value % 360
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can still be negative; should this use the same convention as ppb-vector and have -180 < _rotation <= 180?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the case of this interface, dunno if it matters. The tests have some negative rotation and it gives the equivalent positive rotation, which is a reasonable result, but if both you and @astronouth7303 feel like I should port that over here, I will.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pathunstrom I don't think it's much of an issue either way, I just wanted to check that was intentional.


def rotate(self, degrees):
"""Rotate the sprite by a given angle (in degrees)."""
self.rotation += degrees


class BaseSprite(EventMixin, Rotatable):
"""
The base Sprite class. All sprites should inherit from this (directly or
indirectly).
Expand All @@ -155,7 +182,6 @@ class BaseSprite(EventMixin):
image = None
resource_path = None
position: Vector = Vector(0, 0)
facing: Vector = Vector(0, -1)
size: Union[int, float] = 1

def __init__(self, **kwargs):
Expand All @@ -164,15 +190,14 @@ def __init__(self, **kwargs):
# Make these instance properties with fresh instances
# Don't use Vector.convert() because we need copying
self.position = Vector(*self.position)
self.facing = Vector(*self.facing)

# Initialize things
for k, v in kwargs.items():
# Abbreviations
if k == 'pos':
k = 'position'
# Castings
if k in ('position', 'facing'):
if k == 'position':
v = Vector(*v) # Vector.convert() when that ships.
setattr(self, k, v)

Expand Down Expand Up @@ -226,9 +251,6 @@ def bottom(self, value):
def _offset_value(self):
return self.size / 2

def rotate(self, degrees: Number):
self.facing.rotate(degrees)

def __image__(self):
if self.image is None:
self.image = f"{type(self).__name__.lower()}.png"
Expand Down
10 changes: 7 additions & 3 deletions ppb/systems/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ def prepare_resource(self, game_object):
self.register_renderable(game_object)

source_image = self.resources[game_object.image]
final_image = self.resize_image(source_image, game_object.size)
# TODO: Rotate Image to facing.
return final_image
resized_image = self.resize_image(source_image, game_object.size)
rotated_image = self.rotate_image(resized_image, game_object.rotation)
return rotated_image

def prepare_rectangle(self, resource, game_object, camera):
rect = resource.get_rect()
Expand Down Expand Up @@ -122,6 +122,10 @@ def resize_image(self, image, game_unit_size):
self.resized_images[key] = resized_image
return resized_image

def rotate_image(self, image, rotation):
"""Rotates image counter-clockwise {rotation} degrees."""
return pygame.transform.rotate(image, -rotation)

def target_resolution(self, width, height, game_unit_size):
values = [width, height]
short_side_index = width > height
Expand Down
43 changes: 43 additions & 0 deletions tests/test_sprites.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from unittest import TestCase

from ppb import BaseSprite, Vector
from ppb.sprites import Rotatable


class TestBaseSprite(TestCase):
Expand Down Expand Up @@ -291,3 +292,45 @@ class TestSprite(BaseSprite):
size = 1.1

assert TestSprite().left < -0.5


def test_rotatable_instatiation():
rotatable = Rotatable()
assert rotatable.rotation == 0


def test_rotatable_subclass():

class TestRotatable(Rotatable):
_rotation = 180
basis = Vector(0, 1)

rotatable = TestRotatable()
assert rotatable.rotation == 180
assert rotatable.facing == Vector(0, -1)


def test_rotatable_rotation_setter():
rotatable = Rotatable()

rotatable.rotation = 405
assert rotatable.rotation == 45


def test_rotatable_rotate():
rotatable = Rotatable()

assert rotatable.rotation == 0
rotatable.rotate(180)
assert rotatable.rotation == 180
rotatable.rotate(200)
assert rotatable.rotation == 20
rotatable.rotate(-300)
assert rotatable.rotation == 80


def test_rotatable_base_sprite():
test_sprite = BaseSprite()

test_sprite.rotate(1)
assert test_sprite.rotation == 1