|
| 1 | + |
| 2 | +import game |
| 3 | +import config |
| 4 | +import pygame |
| 5 | +import threading |
| 6 | +import pypboy.data |
| 7 | + |
| 8 | +class Map(game.Entity): |
| 9 | + |
| 10 | + _mapper = None |
| 11 | + _transposed = None |
| 12 | + _size = 0 |
| 13 | + _fetching = None |
| 14 | + _map_surface = None |
| 15 | + _loading_size = 0 |
| 16 | + _render_rect = None |
| 17 | + |
| 18 | + def __init__(self, width, render_rect=None, *args, **kwargs): |
| 19 | + self._mapper = pypboy.data.Maps() |
| 20 | + self._size = width |
| 21 | + self._map_surface = pygame.Surface((width, width)) |
| 22 | + self._render_rect = render_rect |
| 23 | + super(Map, self).__init__((width, width), *args, **kwargs) |
| 24 | + basicFont = pygame.font.SysFont(None, 14) |
| 25 | + text = basicFont.render("Loading map...", True, (95, 255, 177), (0, 0, 0)) |
| 26 | + self.image.blit(text, (10, 10)) |
| 27 | + |
| 28 | + def fetch_map(self, position, radius): |
| 29 | + #(-5.9234923, 54.5899493) |
| 30 | + self._fetching = threading.Thread(target=self._internal_fetch_map, args=(position, radius)) |
| 31 | + self._fetching.start() |
| 32 | + |
| 33 | + def _internal_fetch_map(self, position, radius): |
| 34 | + self._mapper.fetch_by_coordinate(position, radius) |
| 35 | + self.redraw_map() |
| 36 | + |
| 37 | + def update(self, *args, **kwargs): |
| 38 | + super(Map, self).update(*args, **kwargs) |
| 39 | + |
| 40 | + def move_map(self, x, y): |
| 41 | + self._render_rect.move_ip(x, y) |
| 42 | + |
| 43 | + def redraw_map(self, coef=1): |
| 44 | + self._map_surface.fill((0, 0, 0)) |
| 45 | + for way in self._mapper.transpose_ways((self._size / coef, self._size / coef), (self._size / 2, self._size / 2)): |
| 46 | + pygame.draw.lines( |
| 47 | + self._map_surface, |
| 48 | + (85, 251, 167), |
| 49 | + False, |
| 50 | + way, |
| 51 | + 2 |
| 52 | + ) |
| 53 | + for tag in self._mapper.transpose_tags((self._size / coef, self._size / coef), (self._size / 2, self._size / 2)): |
| 54 | + if tag[3] in config.AMENITIES: |
| 55 | + image = config.AMENITIES[tag[3]] |
| 56 | + else: |
| 57 | + print "Unknown amenity: %s" % tag[3] |
| 58 | + image = config.MAP_ICONS['misc'] |
| 59 | + pygame.transform.scale(image, (10, 10)) |
| 60 | + self._map_surface.blit(image, (tag[1], tag[2])) |
| 61 | + basicFont = pygame.font.SysFont(None, 12) |
| 62 | + text = basicFont.render(tag[0], True, (95, 255, 177), (0, 0, 0)) |
| 63 | + self._map_surface.blit(text, (tag[1] + 17, tag[2] + 4)) |
| 64 | + |
| 65 | + self.image.blit(self._map_surface, (0, 0), area=self._render_rect) |
| 66 | + |
| 67 | + |
| 68 | +class MapSquare(game.Entity): |
| 69 | + _mapper = None |
| 70 | + _size = 0 |
| 71 | + _fetching = None |
| 72 | + _map_surface = None |
| 73 | + map_position = (0, 0) |
| 74 | + |
| 75 | + def __init__(self, size, map_position, parent, *args, **kwargs): |
| 76 | + self._mapper = pypboy.data.Maps() |
| 77 | + self._size = size |
| 78 | + self.parent = parent |
| 79 | + self._map_surface = pygame.Surface((size * 2, size * 2)) |
| 80 | + self.map_position = map_position |
| 81 | + self.tags = {} |
| 82 | + super(MapSquare, self).__init__((size, size), *args, **kwargs) |
| 83 | + |
| 84 | + def fetch_map(self): |
| 85 | + self._fetching = threading.Thread(target=self._internal_fetch_map) |
| 86 | + self._fetching.start() |
| 87 | + |
| 88 | + def _internal_fetch_map(self): |
| 89 | + self._mapper.fetch_grid(self.map_position) |
| 90 | + self.redraw_map() |
| 91 | + self.parent.redraw_map() |
| 92 | + |
| 93 | + def redraw_map(self, coef=1): |
| 94 | + self._map_surface.fill((0, 0, 0)) |
| 95 | + for way in self._mapper.transpose_ways((self._size, self._size), (self._size / 2, self._size / 2)): |
| 96 | + pygame.draw.lines( |
| 97 | + self._map_surface, |
| 98 | + (85, 251, 167), |
| 99 | + False, |
| 100 | + way, |
| 101 | + 1 |
| 102 | + ) |
| 103 | + for tag in self._mapper.transpose_tags((self._size, self._size), (self._size / 2, self._size / 2)): |
| 104 | + self.tags[tag[0]] = (tag[1] + self.position[0], tag[2] + self.position[1], tag[3]) |
| 105 | + self.image.fill((0, 0, 0)) |
| 106 | + self.image.blit(self._map_surface, (-self._size / 2, -self._size / 2)) |
| 107 | + |
| 108 | + |
| 109 | +class MapGrid(game.Entity): |
| 110 | + |
| 111 | + _grid = None |
| 112 | + _delta = 0.002 |
| 113 | + _starting_position = (0, 0) |
| 114 | + |
| 115 | + def __init__(self, starting_position, dimensions, *args, **kwargs): |
| 116 | + self._grid = [] |
| 117 | + self._starting_position = starting_position |
| 118 | + self.dimensions = dimensions |
| 119 | + self._tag_surface = pygame.Surface(dimensions) |
| 120 | + super(MapGrid, self).__init__(dimensions, *args, **kwargs) |
| 121 | + self.tags = {} |
| 122 | + self.fetch_outwards() |
| 123 | + |
| 124 | + def test_fetch(self): |
| 125 | + for x in range(10): |
| 126 | + for y in range(5): |
| 127 | + square = MapSquare( |
| 128 | + 100, |
| 129 | + ( |
| 130 | + self._starting_position[0] + (self._delta * x), |
| 131 | + self._starting_position[1] - (self._delta * y) |
| 132 | + ) |
| 133 | + ) |
| 134 | + square.fetch_map() |
| 135 | + square.position = (100 * x, 100 * y) |
| 136 | + self._grid.append(square) |
| 137 | + |
| 138 | + def fetch_outwards(self): |
| 139 | + for x in range(-4, 4): |
| 140 | + for y in range(-2, 2): |
| 141 | + square = MapSquare( |
| 142 | + 86, |
| 143 | + ( |
| 144 | + self._starting_position[0] + (self._delta * x), |
| 145 | + self._starting_position[1] - (self._delta * y) |
| 146 | + ), |
| 147 | + self |
| 148 | + ) |
| 149 | + square.fetch_map() |
| 150 | + square.position = ((86 * x) + (self.dimensions[0] / 2) - 43, (86 * y) + (self.dimensions[1] / 2) - 43) |
| 151 | + self._grid.append(square) |
| 152 | + |
| 153 | + |
| 154 | + def draw_tags(self): |
| 155 | + self.tags = {} |
| 156 | + for square in self._grid: |
| 157 | + self.tags.update(square.tags) |
| 158 | + self._tag_surface.fill((0, 0, 0)) |
| 159 | + for name in self.tags: |
| 160 | + if self.tags[name][2] in config.AMENITIES: |
| 161 | + image = config.AMENITIES[self.tags[name][2]] |
| 162 | + else: |
| 163 | + print "Unknown amenity: %s" % self.tags[name][2] |
| 164 | + image = config.MAP_ICONS['misc'] |
| 165 | + pygame.transform.scale(image, (10, 10)) |
| 166 | + self.image.blit(image, (self.tags[name][0], self.tags[name][1])) |
| 167 | + # try: |
| 168 | + basicFont = pygame.font.SysFont(None, 12) |
| 169 | + text = basicFont.render(name, True, (95, 255, 177), (0, 0, 0)) |
| 170 | + # text_width = text.get_size()[0] |
| 171 | + # pygame.draw.rect( |
| 172 | + # self, |
| 173 | + # (0, 0, 0), |
| 174 | + # (self.tags[name][0], self.tags[name][1], text_width + 4, 15), |
| 175 | + # 0 |
| 176 | + # ) |
| 177 | + self.image.blit(text, (self.tags[name][0] + 17, self.tags[name][1] + 4)) |
| 178 | + # pygame.draw.rect( |
| 179 | + # self, |
| 180 | + # (95, 255, 177), |
| 181 | + # (self.tags[name][0], self.tags[name][1], text_width + 4, 15), |
| 182 | + # 1 |
| 183 | + # ) |
| 184 | + # except Exception, e: |
| 185 | + # print(e) |
| 186 | + # pass |
| 187 | + |
| 188 | + def redraw_map(self, *args, **kwargs): |
| 189 | + self.image.fill((0, 0, 0)) |
| 190 | + for square in self._grid: |
| 191 | + self.image.blit(square._map_surface, square.position) |
| 192 | + self.draw_tags() |
0 commit comments