I've been working on creating a setup that takes maps and tilesets defined in Tiled, and portrays them as navigable maps in a 3/4 perspective in Ren'py. The player character is portrayed as a sprite on the map, and is controlled by the keyboard, with the camera following the player's sprite around (eventually to be expanded with the ability to follow other things for cutscenes and such). The camera tries to keep the player centered, except when it is bounded by the edges of the map. Essentially, think of how old Pokemon or RPGMaker games are set up.
However, I've been having some issues getting the viewport to function properly. First, the code in question:
The label by which I transition to a map. Start_coord and map must be set before calling the call_map label:
Code: Select all
label call_map:
$ player_sprite = RPGPlayerSprite()
$ player_sprite.set_location(start_coord.x, start_coord.y)
$ map.load_movement_map()
$ camera = Camera(map)
call screen rpg_map(map)
Code: Select all
screen rpg_map(map):
viewport id "rpg_map_view":
xadjustment camera.x
yadjustment camera.y
frame:
background None
for layer_key in sorted(map.layers):
$ map_layer = map.layers[layer_key]
frame:
background None
pos map_layer.offset
$ tile_y_coord = 0
for tile_row in map_layer.data:
$ tile_x_coord = 0
for tile in tile_row:
$ tile_img = tile.image
add tile_img pos (tile_x_coord, tile_y_coord)
$ tile_x_coord += map.tile_size.x
$ tile_y_coord += map.tile_size.y
add player_sprite
Code: Select all
class Camera:
def __init__(self, set_map):
self.map = set_map
self.x = renpy.display.behavior.Adjustment(value=0, range=self.map.image_size.x)
self.y = renpy.display.behavior.Adjustment(value=0, range=self.map.image_size.y)
def center_on_location(self, player_x, player_y):
max_x = map.image_size.x - config.screen_width + 8
centered_x = player_x - (config.screen_width / 2) + (map.tile_size.x / 2)
final_x = max(min(max_x, centered_x), 0)
self.x.change(final_x)
max_y = (map.image_size.y - config.screen_height) + 8
centered_y = player_y - (config.screen_height / 2) + (map.tile_size.y / 2)
final_y = max(min(max_y, centered_y), 0)
self.y.change(final_y)
Obviously, I'm doing something wrong here, but I can't for the life of me figure out what.