From: David Sharpe Date: Thu, 3 Mar 2016 21:08:58 +0000 (+0200) Subject: Merge X-Git-Url: https://git.ctpug.org.za/?a=commitdiff_plain;h=597d0be4e0942ce456e4ba8850decec1af7d0b4b;p=koperkapel.git Merge --- 597d0be4e0942ce456e4ba8850decec1af7d0b4b diff --cc koperkapel/generators/maps.py index 51aae65,9426929..4f917c6 --- a/koperkapel/generators/maps.py +++ b/koperkapel/generators/maps.py @@@ -6,76 -8,14 +8,87 @@@ import o i = random.randint(0,100) ++<<<<<<< HEAD +class Room: + def __init__(self, region): + """ + """ + self.regions = [region] + self.max_connections = 1 + self.passages = [] + self.tunnels = [] + + def is_linked(self): + """ + Check if the room is linked to another room + :return: Whether the room has any links or not + """ + return len(self.passages) + len(self.tunnels) > 0 + + def add_passage(self, room_region, new_region): + """ Add a passage link between rooms + :param room_region: coordinate of region from which we are linking + :param new_region: coordinate of region to which we are linking + :return: + """ + pass + + def add_region(self, region): + """ + Add a new region into an existing room + :param region: region coordinates to be added to room + :return: + """ + self.regions.append(region) + + def connect_rooms(self, other_rooms): + """ Find the nearest rooms to this room + :param other_rooms: list of Rooms objects that we are searching + :return: + """ + distance = [] + other_tile = [] + this_tile = [] + target_rooms = [] + for local_region in self.regions: + for room in other_rooms: + if self == room: + continue + for new_region in room.regions: + distance.append( + math.sqrt((local_region[0] - new_region[0]) ** 2 + + (local_region[1] - new_region[1]) ** 2)) + other_tile.append(new_region) + this_tile.append(local_region) + target_rooms.append(room) + + sorted_indices = [i[0] for i in sorted(enumerate(distance), + key=lambda x:x[0])] + for index in sorted_indices: + if len(self.passages) + len(self.tunnels) >= self.max_connections: + break + if not target_rooms[index].is_linked(): + self.link_passage(this_tile[index], other_tile[index]) + target_rooms[index].link_passage( + other_tile[index], this_tile[index]) + + def link_passage(self, local_tile, foriegn_tile): + """ Link a passage between two rooms + :param local_tile: tile in this room to which we wish to link + :param foriegn_tile: tile in another room to which we wish to link + :return: + """ + self.passages.append([local_tile, foriegn_tile]) ++======= + ATTRIBUTE_MAP = { + '#': {'base': 'cwall', + 'behaviour': [], + }, + ' ': {'base': 'floor', + 'behaviour': ['walk', 'fly'], + }, + } ++>>>>>>> 0b2149fff63b346192148b7eb334e00ad3b9283d class LevelGenerator: @@@ -214,9 -136,25 +227,29 @@@ file.write(''.join(l)) file.write('\n') file.close() ++<<<<<<< HEAD + for l in self.region_map: ++======= + self._to_json() + for l in self.regions: ++>>>>>>> 0b2149fff63b346192148b7eb334e00ad3b9283d print(l) + def _to_json(self): + level = {} + level['tiles'] = [] + for l in self.map: + row = [] + for t in l: + row.append(ATTRIBUTE_MAP[t]) + level['tiles'].append(row) + name = os.path.join(os.path.dirname(__file__), '..', 'levels', 'map.json') + # FIXME: Do a lot better here + # Crude hack so the level is written into the levels folder + f = open(name, 'w') + json.dump(level, f) + f.close() + if __name__ == '__main__': while True: