Permalink
Browse files

Workaround for when viewport needs to be determined but it hasn't bee…

…n created.
  • Loading branch information...
dfloer committed Nov 10, 2018
1 parent 4f04f30 commit af9433e10e2662610b6991acd36fde4127e528f7
Showing with 12 additions and 3 deletions.
  1. +12 −3 helpers.py
@@ -1,7 +1,8 @@
import hex_math
from hex_math import Point

def get_current_viewport(sprite_width, scroller, safe=True):

def get_current_viewport(layout, sprite_width, scroller, safe=True):
"""
Get the current viewport coordinates. I feel like Cocos2d should handle this, but I can't seem to find it.
Args:
@@ -19,6 +20,14 @@ def get_current_viewport(sprite_width, scroller, safe=True):
window_width = scroller.view_w
window_height = scroller.view_h

# Workaround for when this function gets called before the window has been properly instantiated.
if (x, y) == (0, 0):
x = layout.origin.x
y = layout.origin.y
if (window_width, window_height) == (1, 1):
window_width = layout.origin.x * 2
window_height = layout.origin.y * 2

tl = x - window_width // 2, y + window_height // 2
bl = x - window_width // 2, y - window_height // 2
tr = x + window_width // 2, y + window_height // 2
@@ -42,7 +51,7 @@ def get_current_viewport_hexes(layout, sprite_width, scroller, safe=True):
Returns:
A dictionary of the hegaxons corresponding to the corners of the viewport.
"""
coordinates = get_current_viewport(sprite_width, scroller, safe)
coordinates = get_current_viewport(layout, sprite_width, scroller, safe)
return {k: hex_math.pixel_to_hex(layout, Point(*v)) for k, v in coordinates.items()}


@@ -61,4 +70,4 @@ def find_visible_hexes(sprite_width, layout, scroller, safe=True):
for x in zip(top_line, bottom_line):
visible += hex_math.hex_linedraw(*x)
# Use a set to make sure we don't have any duplicates.
return {x for x in visible}
return {x for x in visible}

0 comments on commit af9433e

Please sign in to comment.