| |
- __builtin__.object
-
- MapHandler
- SuperMap
class MapHandler(__builtin__.object) |
|
A MapHandler object is responsible for sensing when triggers have been
tripped by a triggering rect (e.g. the camera), and loading and unloading
its map. |
|
Methods defined here:
- __init__(self, name, map_file)
- Construct a MapHandler object.
name is a tuple representing the 2D vector of this map in the SuperMap.
The position in the SuperMap is relative to the origin map, so negative
values are valid.
map_file is a string containing the path to the map file used by the
MapHandler.load() method.
Attributes:
name : The name argument from the constructor.
map_file : The map_file argument from the constructor.
rect : The bounding rect of this map in world space.
supermap : The parent SuperMap object.
map : The map object containing tiles. Current supported: BasicMap
and TiledMap.
triggers : The list of trigger objects linking this map to
neighboring maps.
timestamp : The time that this handler was last updated.
- __str__(self)
- enter(self)
- Optional. Override this class to perform actions when the map is
promoted to SuperMap.current.
- exit(self)
- Optional. Override this class to perform actions when the map is
demoted from SuperMap.current.
- get_objects(self, map_range)
- Return a dict of tiles in the specified bounds.
supermap_range is a dict of range specifications, such as returned by
SuperMap.get_tile_range_in_rect().
- get_objects_in_rect(self, rect)
- Return a dict of objects that intersect rect.
rect is a pygame.Rect expressed in world coordinates.
- get_tile_range_in_rect(self, rect)
- Return a list of tile ranges, one for each layer, that intersect
rect.
rect is a pygame.Rect expressed in world coordinates.
- load(self)
- Override this class. It must set self.map to a valid BasicMap or
TiledMap object.
This stub method does absolutely nothing to manage the map. To load the
tile map, call the appropriate loader.
Considerations:
If a map is loaded (self.map is not None), this method will not be
called.
If a map was loaded, the unload method does not necessarily have to
completely shut down a map. For example, Tiled maps can provide data
for non-tile objects. Such object may have AI, or otherwise serve
a purpose in the game logic. *IF* the tiles were unloaded but the
objects were not, then you will need to extend this class to handle
that situation since it will be desirable to reload the tiles on
demand but undesirable to create more objects.
- local_to_world(self, xy)
- Convert a 2D vector in local space to a 2D vector in world space.
This conversion is needed to translate maps, which use local space.
- run_triggers(self, triggering_rect)
- Run all triggers in this map versus the triggering_rect.
The triggering rect is typically the camera rect. It is collision-
checked against each trigger's rect to assess if neighboring map needs
to be loaded.
- unload(self)
- Optional. Override this class to perform actions, such as saving the
state of dynamic context, when the map is unloaded from memory.
This stub method does absolutely nothing to manage the map. To unload
the tile map, set self.map=None.
- update(self, dt, *args)
- Optional. Override this class to perform actions when the map is
updated via SuperMap.update().
- world_to_local(self, xy)
- Convert a 2D vector in world space to a 2D vector in local space.
This conversion is needed to translate maps, which use local space.
Data descriptors defined here:
- __dict__
- dictionary for instance variables (if defined)
- __weakref__
- list of weak references to the object (if defined)
|
class SuperMap(__builtin__.object) |
|
SuperMap is a dict of MapHandler objects. The supermap is expressed in world
coordinates. It uses map handlers to trigger map loading and unloading as
the camera moves around the world.
This class implements the required method signature for integration with the
Camera class. Thus, State.map = SuperMap() is valid.
Note that map tools in the toolkit module will not work with the SuperMap
because those tools only know how to work with BasicMap or TiledMap objects
where the map represents the entire world.
The following SuperMap shows the names and relative positions of the maps
in world space. The name is a tuple of int, length 2. In general it makes
sense to have (0,0) be the origin map, but it is not required. A supermap
can be irregular, and even have holes in it.
+-------+-------+-------+
|(-1,-1)| (0,-1)| (1,-1)|
+-------+-------+-------+
|(-1,0) | (0,0) | (1,0) |
+-------+-------+-------+
|(-1,1) | (0,1) | (1,1) |
+-------+-------+-------+
The following code adds three maps in single-row layout, starting with map
(0,0) and proceeding to the right:
class MyMapHandler(MapHandler):
def load(self):
self.map = load_a_map()
supermap = SuperMap()
supermap.add_handler(MyMapHandler((0,0), 'map00.tmx'))
supermap.add_handler(MyMapHandler((1,0), 'map10.tmx'))
supermap.add_handler(MyMapHandler((2,0), 'map20.tmx')) |
|
Methods defined here:
- __init__(self, origin=(0, 0), max_maps=4)
- Construct a SuperMap object.
origin is a tuple representing the 2D vector in the SuperMap which
holds the SuperMap origin. The topleft of this maps rect will be set to
(0,0) and all other maps' world space will be relative to this map.
max_maps is an int representing the maximum number of maps to keep in
memory. Maps above this number will be unloaded via the MapHandler
based on the order in which they were last updated. The minimum
functional value is 1. A value of 0 will never unload maps.
Attributes:
origin : origin from the constructor.
max_maps : max_maps from the constructor.
rect : The world bounding rect.
handlers : A dict containing map handler objects, keyed by the tuple
MapHandler.name.
current : The current MapHandler object which contains the global
camera's position (State.camera.position).
visible_maps : The list of MapHandler objects that are visible
within the global camera's rect (State.camera.rect).
history : The list of loaded maps, ordered on last update.
- add_handler(self, map_handler)
- Add map_handler to this SuperMap.
map_handler is the MapHandler object to add to this SuperMap.
- draw(self)
- get_handler(self, name)
- Return the named MapHandler object.
name is a tuple representing the 2D vector of this map in this SuperMap.
- get_objects(self, supermap_range)
- get_objects_in_rect(self, rect)
- get_tile_range_in_rect(self, rect)
- rect must be in world space.
- set_current(self, map_handler)
- Promote map_handler as the current map handler.
map_handler is a MapHandler object to promote to current.
If the current map handler is a valid MapHandler object, its exit()
method will be called.
Finally, map_handler's map will be loaded (as a failsafe; typically it
is already loaded via a trigger), and its enter() method is called.
- update(self, dt, *args)
- Update this SuperMap.
This method runs each MapHandler's triggers, promotes a MapHandler to
current when appropriate, and calls each MapHandler's update() method.
Data descriptors defined here:
- __dict__
- dictionary for instance variables (if defined)
- __weakref__
- list of weak references to the object (if defined)
| |