diff --git a/examples/animated_sprite.py b/examples/animated_sprite.py new file mode 100644 index 00000000..119e43a3 --- /dev/null +++ b/examples/animated_sprite.py @@ -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) diff --git a/examples/resources/blob_0.png b/examples/resources/blob_0.png new file mode 100644 index 00000000..b268f50f Binary files /dev/null and b/examples/resources/blob_0.png differ diff --git a/examples/resources/blob_1.png b/examples/resources/blob_1.png new file mode 100644 index 00000000..a7b378c6 Binary files /dev/null and b/examples/resources/blob_1.png differ diff --git a/examples/resources/blob_2.png b/examples/resources/blob_2.png new file mode 100644 index 00000000..f561d9cb Binary files /dev/null and b/examples/resources/blob_2.png differ diff --git a/examples/resources/blob_3.png b/examples/resources/blob_3.png new file mode 100644 index 00000000..a7b378c6 Binary files /dev/null and b/examples/resources/blob_3.png differ diff --git a/examples/resources/blob_4.png b/examples/resources/blob_4.png new file mode 100644 index 00000000..b268f50f Binary files /dev/null and b/examples/resources/blob_4.png differ diff --git a/examples/resources/blob_5.png b/examples/resources/blob_5.png new file mode 100644 index 00000000..c51c9b70 Binary files /dev/null and b/examples/resources/blob_5.png differ diff --git a/examples/resources/blob_6.png b/examples/resources/blob_6.png new file mode 100644 index 00000000..b268f50f Binary files /dev/null and b/examples/resources/blob_6.png differ diff --git a/ppb/camera.py b/ppb/camera.py index 10528c6e..6a9d58ee 100644 --- a/ppb/camera.py +++ b/ppb/camera.py @@ -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 diff --git a/tests/test_camera.py b/tests/test_camera.py index 6b29781e..b28bfec7 100644 --- a/tests/test_camera.py +++ b/tests/test_camera.py @@ -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(): @@ -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 \ No newline at end of file + assert cam.frame_left == -2.5