- commit
- 3e541e7818edd786a8da8662b38b4b405664bc32
- parent
- 2580569aa34cfc1a6bbd4f5caa7a94885b33121c
- Author
- Tobias Bengfort <tobias.bengfort@gmx.net>
- Date
- 2014-11-02 08:30
Create map manager for managing multiple maps TODO: move from one map to another one
Diffstat
| M | laneya/map.py | 75 | ++++++++++++++++++++++++++++++++++++++++++------------------- |
| M | laneya/server.py | 9 | +++++---- |
2 files changed, 57 insertions, 27 deletions
diff --git a/laneya/map.py b/laneya/map.py
@@ -1,32 +1,23 @@ 1 1 import random 2 2 3 34 -1 class Map(object):5 -1 """A singel map containing sprites.6 -17 -1 The map object takes care of managing all sprites within one map. As only8 -1 very few actions involve multiple maps (e.g. moving from one map to9 -1 another) this covers a lot of the game logic.-1 4 class MapManager(object): -1 5 """Manager that takes care of generating and storing all maps. 10 611 -1 The passed server is used to send updates to the clients.12 -113 -1 Map objects expose an API for the server (e.g. :py:meth:`step`) and another14 -1 one for sprites (e.g. :py:meth:`move_sprite`).-1 7 All maps have the same width/height and identified by X, Y and Z -1 8 coordinates. At any combination of coordinates, there can be only one map. 15 9 16 10 """ 17 11 def __init__(self, server, width=60, height=40): 18 12 self.server = server 19 13 self.width = width 20 14 self.height = height21 -1 self.sprites = {}22 -1 self.movable_layer = [23 -1 [None for i in xrange(height)] for i in xrange(width)]24 -1 self.floor_layer = [25 -1 [None for i in xrange(height)] for i in xrange(width)]26 -1 self.generate()27 -1 self.ghost = Ghost('example', self, 15, 15)-1 15 self.store = {} -1 16 -1 17 def generate(self, X, Y, Z): -1 18 """Generate a new map.""" 28 1929 -1 def generate(self):-1 20 _map = Map(self.server, self.width, self.height) 30 21 rooms = [] 31 22 32 23 # make sure user and ghost are inside of a room @@ -71,9 +62,9 @@ class Map(object): 71 62 for x in range(self.width): 72 63 for y in range(self.height): 73 64 if any((in_room(x, y, room) for room in rooms)):74 -1 self.floor_layer[x][y] = 'floor'-1 65 _map.floor_layer[x][y] = 'floor' 75 66 else:76 -1 self.floor_layer[x][y] = 'wall'-1 67 _map.floor_layer[x][y] = 'wall' 77 68 78 69 # carve paths 79 70 for i, room in enumerate(rooms): @@ -91,10 +82,48 @@ class Map(object): 91 82 y_max = max(y_center, last_y_center) + 1 92 83 93 84 for x in range(x_min, x_max):94 -1 self.floor_layer[x][last_y_center] = 'floor'-1 85 _map.floor_layer[x][last_y_center] = 'floor' 95 86 96 87 for y in range(y_min, y_max):97 -1 self.floor_layer[x_center][y] = 'floor'-1 88 _map.floor_layer[x_center][y] = 'floor' -1 89 -1 90 return _map -1 91 -1 92 def get(self, X, Y, Z): -1 93 """Get a map. If it does not exist yet, generate one.""" -1 94 -1 95 key = '%i:%i:%i' % (X, Y, Z) -1 96 -1 97 if key not in self.store: -1 98 _map = self.generate(X, Y, Z) -1 99 self.store[key] = _map -1 100 -1 101 return self.store[key] -1 102 -1 103 -1 104 class Map(object): -1 105 """A singel map containing sprites. -1 106 -1 107 The map object takes care of managing all sprites within one map. As only -1 108 very few actions involve multiple maps (e.g. moving from one map to -1 109 another) this covers a lot of the game logic. -1 110 -1 111 The passed server is used to send updates to the clients. -1 112 -1 113 Map objects expose an API for the server (e.g. :py:meth:`step`) and another -1 114 one for sprites (e.g. :py:meth:`move_sprite`). -1 115 -1 116 """ -1 117 def __init__(self, server, width, height): -1 118 self.server = server -1 119 self.width = width -1 120 self.height = height -1 121 self.sprites = {} -1 122 self.movable_layer = [ -1 123 [None for i in xrange(height)] for i in xrange(width)] -1 124 self.floor_layer = [ -1 125 [None for i in xrange(height)] for i in xrange(width)] -1 126 self.ghost = Ghost('example', self, 15, 15) 98 127 99 128 def step(self): 100 129 """Update this map and all of its sprites. @@ -203,4 +232,4 @@ class Ghost(MovingSprite): 203 232 super(Ghost, self).step() 204 233 205 234206 -1 __all__ = ['Map', 'Sprite', 'MovingSprite', 'User']-1 235 __all__ = ['MapManager', 'Map', 'Sprite', 'MovingSprite', 'User']
diff --git a/laneya/server.py b/laneya/server.py
@@ -6,18 +6,19 @@ from twisted.internet import reactor 6 6 from twisted.internet import task 7 7 8 8 import protocol9 -1 from map import Map, User-1 9 from map import MapManager, User 10 10 11 11 12 12 class Server(protocol.ServerProtocolFactory): 13 13 def __init__(self): 14 14 protocol.ServerProtocolFactory.__init__(self) 15 15 self.users = {}16 -1 self.map = Map(self) # TODO: more than one map-1 16 self.map_manager = MapManager(self, 60, 40) 17 17 18 18 def requestReceived(self, user, action, **kwargs): # TODO 19 19 if user not in self.users:20 -1 self.users[user] = User(user, self.map, 10, 10)-1 20 initial_map = self.map_manager.get(0, 0, 0) -1 21 self.users[user] = User(user, initial_map, 10, 10) 21 22 print("login %s" % user) 22 23 23 24 if action == 'move': @@ -27,7 +28,7 @@ class Server(protocol.ServerProtocolFactory): 27 28 del self.users[user] 28 29 print("logout %s" % user) 29 30 elif action == 'get_map':30 -1 return self.map.encode()-1 31 return self.users[user].map.encode() 31 32 else: 32 33 raise protocol.InvalidError 33 34