def __init__(self): super().__init__(collider=jank.colliders.Rect(width=50, height=50, friction=0.1, radius=25), space=jank.get_app().physics_space) jank.get_app().push_handlers(self)
def end(self): """Since this is a matrix, you will need to reverse the translate after rendering otherwise it will multiply the current offset every draw update pushing it further and further away. """ window = jank.get_app().window zoom = self.zoom if self.auto_adjust: config = jank.get_app().config zoom *= min( window.width/config.default_size[0], window.height/config.default_size[1] ) x = -window.width//2/zoom + self.x y = -window.height//2/zoom + self.y jank.pyglet.gl.glScalef( 1 / zoom, 1 / zoom, 1 ) jank.pyglet.gl.glTranslatef( x * zoom, y * zoom, 0 )
def delete(self, recursive: bool = True): parent = self.parent self.parent = None while len(self.children) > 0: if recursive: self.children[0].delete(True) else: self.children[0].parent = parent jank.get_app().remove_handlers(self) self.run_cleanup()
def __init__(self, x: float = 0, y: float = 0, parent: t.Optional["UIBase"] = None): self._x = x self._y = y self.children = [] self.parent = parent jank.get_app().push_handlers(self) self.update()
def on_update(self, dt): if self.controlling: key_handler = jank.get_app().key_handler self.controls = { "up": key_handler[key.W], "down": key_handler[key.S], "left": key_handler[key.A], "right": key_handler[key.D] } self.v = jank.Vec2d.zero() if self.controls["up"]: self.v.y += PLAYER_SPEED if self.controls["down"]: self.v.y -= PLAYER_SPEED if self.controls["left"]: self.v.x -= PLAYER_SPEED if self.controls["right"]: self.v.x += PLAYER_SPEED if self.v.length > PLAYER_SPEED: scale = PLAYER_SPEED / self.v.length self.v = self.v * scale
def create_sprite(self): image = jank.resource.image("resources/player.png") sprite = jank.Sprite( image, 0, 0, batch=jank.get_app().world_batch, subpixel=True ) self.renderer = jank.renderer.SpriteRenderer.create_from_sprite(sprite) self.label = jank.pyglet.text.Label( text=self.player_id, font_size=8, anchor_x="center", batch=jank.get_app().world_batch, group=jank.get_app().world_layers["name_tags"] )
def bounding_box(self) -> jank.BoundingBox: window = jank.get_app().window zoom = self.zoom if self.auto_adjust: config = jank.get_app().config zoom *= min( window.width/config.default_size[0], window.height/config.default_size[1] ) x = -window.width//2/zoom + self.x y = -window.height//2/zoom + self.y width = window.width/zoom height = window.height/zoom return jank.BoundingBox( x, y, width+x, height+y )
def __init__(self, player_id: str): self.controlling = False self.controls = { "up": False, "down": False, "left": False, "right": False } self.player_id = player_id self.label = None super().__init__( collider=jank.colliders.Rect( width=16, height=16 ) ) self.space = jank.get_app().physics_space jank.get_app().push_handlers(self)
def real_y(self) -> float: real_y = self.y if self.parent is not None: real_y += self.parent.real_y parent = self.parent else: parent = jank.get_app().window real_y -= self.height * self.anchor_y real_y += parent.height * self.parent_anchor_y return real_y
def real_x(self) -> float: real_x = self.x if self.parent is not None: real_x += self.parent.real_x parent = self.parent else: parent = jank.get_app().window real_x -= self.width * self.anchor_x real_x += parent.width * self.parent_anchor_x return real_x
def begin(self): """Set the current camera offset so you can draw your scene.""" window = jank.get_app().window zoom = self.zoom if self.auto_adjust: config = jank.get_app().config zoom *= min( window.width/config.default_size[0], window.height/config.default_size[1] ) x = -window.width//2/zoom + self.x y = -window.height//2/zoom + self.y jank.pyglet.gl.glTranslatef( -x * zoom, -y * zoom, 0 ) jank.pyglet.gl.glScalef( zoom, zoom, 1 )
def delete(self): self.space = None jank.get_app().remove_handlers(self) if self.renderer is not None: self.renderer.remove_handlers(self) self.renderer.delete()
def set_active(self): jank.get_app()._active_camera = self
def on_update(self, dt: float): if self.controlling: self.controls = { "left": jank.get_app().key_handler[jank.key.A], "right": jank.get_app().key_handler[jank.key.D] }