Skip to content

Commit

Permalink
Merge branch 'master' into immutable-vectors
Browse files Browse the repository at this point in the history
  • Loading branch information
AstraLuma authored May 5, 2019
2 parents 4060e2c + 7498600 commit 8c28091
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 10 deletions.
26 changes: 26 additions & 0 deletions examples/animated_sprite.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import math

import ppb
from ppb.features.animation import Animation
import ppb.events as events


class Blob(ppb.BaseSprite):
image = Animation("./resources/blob_{0..6}.png", 10)
target = ppb.Vector(0, 0)
speed = 1

def on_mouse_motion(self, event: events.MouseMotion, signal):
self.target = event.position

def on_update(self, event: events.Update, signal):
intent_vector = self.target - self.position
self.position += intent_vector.scale(self.speed * event.time_delta)
self.rotation = math.degrees(math.atan2(intent_vector.y, intent_vector.x)) + 90


def setup(scene):
scene.add(Blob())


ppb.run(setup)
Binary file added examples/resources/blob_0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/resources/blob_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/resources/blob_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/resources/blob_3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/resources/blob_4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/resources/blob_5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/resources/blob_6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 11 additions & 3 deletions ppb/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,18 @@ def in_frame(self, sprite: BaseSprite) -> bool:
self.frame_bottom >= sprite.top
)

def translate_to_frame(self, point:Vector) -> Vector:
def translate_to_frame(self, point: Vector) -> Vector:
"""
Converts a vector from pixel-based window to in-game coordinate space
"""
offset = (point - self.viewport_offset) * (1/self.pixel_ratio)
return self.position + offset
loc = self.position + offset
return loc.update(y=-loc.y)

def translate_to_viewport(self, point:Vector) -> Vector:
def translate_to_viewport(self, point: Vector) -> Vector:
"""
Converts a vector from in-game to pixel-based window coordinate space
"""
point = point.update(y=-point.y)
offset = (point - self.position) * self.pixel_ratio
return self.viewport_offset + offset
14 changes: 7 additions & 7 deletions tests/test_camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,20 @@ def test_camera_translate_to_frame():
cam = Camera(viewport=(0, 0, 800, 600), pixel_ratio=80)
assert cam.position == Vector(0, 0)
assert cam.translate_to_frame(Vector(400, 300)) == Vector(0, 0)
assert cam.translate_to_frame(Vector(560, 220)) == Vector(2, -1)
assert cam.translate_to_frame(Vector(560, 220)) == Vector(2, 1)
cam.position = Vector(5, 5)
assert cam.translate_to_frame(Vector(400, 300)) == Vector(5, 5)
assert cam.translate_to_frame(Vector(560, 220)) == Vector(7, 4)
assert cam.translate_to_frame(Vector(400, 300)) == Vector(5, -5)
assert cam.translate_to_frame(Vector(560, 220)) == Vector(7, -4)


def test_camera_translate_to_viewport():
cam = Camera(viewport=(0, 0, 800, 600), pixel_ratio=80)
assert cam.position == Vector(0, 0)
assert cam.translate_to_viewport(Vector(0, 0)) == Vector(400, 300)
assert cam.translate_to_viewport(Vector(2, -1)) == Vector(560, 220)
assert cam.translate_to_viewport(Vector(2, 1)) == Vector(560, 220)
cam.position = Vector(5, 5)
assert cam.translate_to_viewport(Vector(5, 5)) == Vector(400, 300)
assert cam.translate_to_viewport(Vector(7, 4)) == Vector(560, 220)
assert cam.translate_to_viewport(Vector(5, -5)) == Vector(400, 300)
assert cam.translate_to_viewport(Vector(7, -4)) == Vector(560, 220)


def test_sprite_in_viewport():
Expand All @@ -70,4 +70,4 @@ def test_viewport_change_affects_frame_height():
cam = Camera(viewport=(0, 0, 800, 600), pixel_ratio=80)
assert cam.frame_left == -5
cam.viewport_width = 400
assert cam.frame_left == -2.5
assert cam.frame_left == -2.5

0 comments on commit 8c28091

Please sign in to comment.