Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/game_map.py
diff options
context:
space:
mode:
authorGonzalo Odiard <godiard@gmail.com>2012-02-21 03:41:40 (GMT)
committer Gonzalo Odiard <godiard@gmail.com>2012-02-21 03:41:40 (GMT)
commit2ff9cc16b223b38687a1c0c1c2798ce26aece7cf (patch)
treefe239d6f76d3ceca8067f78409371eeba013c024 /game_map.py
parentb68ae2d0c20fb38158380f3f488e08560832331d (diff)
Move the information of the doors in the walls to another array.
The doors have different properties than the other objects attached to the walls. The doors are present at the two sides of the wall, and the position is fixed. I have moved the doors to a 'doors' array, and the 'objects' array will be loaded with the other objects. I think the code will be simpler in this way. Signed-off-by: Gonzalo Odiard <gonzalo@laptop.org>
Diffstat (limited to 'game_map.py')
-rw-r--r--game_map.py92
1 files changed, 49 insertions, 43 deletions
diff --git a/game_map.py b/game_map.py
index caaf32e..121173d 100644
--- a/game_map.py
+++ b/game_map.py
@@ -57,21 +57,21 @@ class GameMap():
# walls are defined by the cell position x,y and the direction
# N,S,E,W
- 'walls': [{'position': [0, 0, 'S'], 'objects': ['door_1']},
- {'position': [1, 0, 'S'], 'objects': ['door_2']},
- {'position': [3, 0, 'S'], 'objects': ['door_3']},
- {'position': [0, 1, 'E'], 'objects': ['door_4']},
- {'position': [2, 1, 'E'], 'objects': ['door_6']},
- {'position': [0, 2, 'S'], 'objects': ['door_7']},
- {'position': [0, 2, 'E'], 'objects': ['door_8']},
- {'position': [4, 2, 'S'], 'objects': ['door_11']},
- {'position': [0, 3, 'E'], 'objects': ['door_12']},
- {'position': [3, 3, 'E'], 'objects': ['door_13']},
- {'position': [0, 4, 'S'], 'objects': ['door_14']},
- {'position': [0, 4, 'E'], 'objects': ['door_15']},
- {'position': [1, 4, 'S'], 'objects': ['door_16']},
- {'position': [2, 4, 'E'], 'objects': ['door_17']},
- {'position': [3, 4, 'S'], 'objects': ['door_18']}]}
+ 'walls': [{'position': [0, 0, 'S'], 'doors': ['door_1']},
+ {'position': [1, 0, 'S'], 'doors': ['door_2']},
+ {'position': [3, 0, 'S'], 'doors': ['door_3']},
+ {'position': [0, 1, 'E'], 'doors': ['door_4']},
+ {'position': [2, 1, 'E'], 'doors': ['door_6']},
+ {'position': [0, 2, 'S'], 'doors': ['door_7']},
+ {'position': [0, 2, 'E'], 'doors': ['door_8']},
+ {'position': [4, 2, 'S'], 'doors': ['door_11']},
+ {'position': [0, 3, 'E'], 'doors': ['door_12']},
+ {'position': [3, 3, 'E'], 'doors': ['door_13']},
+ {'position': [0, 4, 'S'], 'doors': ['door_14']},
+ {'position': [0, 4, 'E'], 'doors': ['door_15']},
+ {'position': [1, 4, 'S'], 'doors': ['door_16']},
+ {'position': [2, 4, 'E'], 'doors': ['door_17']},
+ {'position': [3, 4, 'S'], 'doors': ['door_18']}]}
def __init__(self, data=None):
if data is None:
@@ -151,18 +151,9 @@ class GameMap():
# Search in walls data
for wall in self.data['walls']:
if wall['position'] == [x, y, direction]:
+ if not 'objects' in wall:
+ wall['objects'] = []
return wall['objects']
- # look for information in the other side of the room too.
- # (only valid for doors)
- if next_room is not None:
- reversed_direction = self.get_reversed_direction(direction)
- x2, y2 = self.get_next_coords(x, y, direction)
- if x2 == -1 and y2 == -1:
- return []
- for wall in self.data['walls']:
- if wall['position'] == [x2, y2, reversed_direction]:
- return wall['objects']
- # Nothing found
return []
def get_wall_color(self, x, y):
@@ -204,18 +195,14 @@ class GameMap():
def cross_door(self, x, y, direction):
""" Return next position if the user go to the left"""
# verify is the door is in the right position/direction
- objects = self.get_wall_info(x, y, direction)
- if objects is None:
+ if not self.have_door(x, y, direction):
return x, y, direction
else:
- if self.have_door(objects):
- new_x, new_y, new_dir = self.go_forward(x, y, direction)
- if self.get_wall_info(new_x, new_y, new_dir) is None:
- new_x, new_y, new_dir = self.go_forward(new_x, new_y,
- new_dir)
- return new_x, new_y, new_dir
- else:
- return x, y, direction
+ new_x, new_y, new_dir = self.go_forward(x, y, direction)
+ if self.get_wall_info(new_x, new_y, new_dir) is None:
+ new_x, new_y, new_dir = self.go_forward(new_x, new_y,
+ new_dir)
+ return new_x, new_y, new_dir
def go_forward(self, x, y, direction):
if direction == 'N':
@@ -227,13 +214,32 @@ class GameMap():
if direction == 'W':
return x - 1, y, direction
- def have_door(self, wall_info):
- _have_door = False
- for wall_object in wall_info:
- if wall_object.startswith('door'):
- _have_door = True
- break
- return _have_door
+ def have_door(self, x, y, direction):
+ """ Return if the wall have a door
+ or None if there are not a wall in this cell and direction"""
+
+ # verify if there are a wall in the requested position
+ actual_room = self.get_room(x, y)
+ next_room = self.get_next_room(x, y, direction)
+ # if the two rooms are the same, there are no wall
+ if next_room is not None and actual_room == next_room:
+ return None
+ # Search in walls data
+ for wall in self.data['walls']:
+ if wall['position'] == [x, y, direction]:
+ return 'doors' in wall and len(wall['doors']) > 0
+ # look for information in the other side of the room too.
+ # (only valid for doors)
+ if next_room is not None:
+ reversed_direction = self.get_reversed_direction(direction)
+ x2, y2 = self.get_next_coords(x, y, direction)
+ if x2 == -1 and y2 == -1:
+ return []
+ for wall in self.data['walls']:
+ if wall['position'] == [x2, y2, reversed_direction]:
+ return 'doors' in wall and len(wall['doors']) > 0
+ # Nothing found
+ return False
# testing