|
3 | 3 | import matplotlib.pyplot as plt |
4 | 4 | import matplotlib.cm as cm |
5 | 5 | import numpy as np |
| 6 | +import itertools |
| 7 | + |
| 8 | + |
| 9 | +def view_game_state(game_map, view_mock=False, grid=True): |
| 10 | + """ |
| 11 | + Method shows current game state including static, active and mock points. |
| 12 | + Active points should have cur_pos, Mock objects should have a list of |
| 13 | + points, static is a 2D np array. |
| 14 | +
|
| 15 | + Note: Mock objects have low opacity (alpha=0.3) |
| 16 | +
|
| 17 | + Args: |
| 18 | + game_map (Map object) |
| 19 | + view_mock (Boolean): mock objects are shown if true |
| 20 | + """ |
| 21 | + # show active units |
| 22 | + unit_points = [game_unit.cur_pos for game_unit in game_map.active] |
| 23 | + x, y = get_x_and_y_from_itr(unit_points) |
| 24 | + colors = np.arange(len(unit_points)) |
| 25 | + plt.scatter(x, y, c=colors) |
| 26 | + |
| 27 | + # show mock units if specified |
| 28 | + if view_mock: |
| 29 | + mock_objects = [game_mock.points for game_mock in game_map.mock] |
| 30 | + |
| 31 | + # each mock object is colored different but points for each object |
| 32 | + # are colored same |
| 33 | + colors = [len(obj) for obj in mock_objects] |
| 34 | + colors = [[i]*value for i, value in enumerate(colors)] |
| 35 | + colors = list(itertools.chain.from_iterable(colors)) |
| 36 | + colors = np.array(colors) |
| 37 | + |
| 38 | + mock_objects = itertools.chain.from_iterable(mock_objects) |
| 39 | + x, y = get_x_and_y_from_itr(mock_objects) |
| 40 | + plt.scatter(x, y, c=colors, alpha=0.3) |
| 41 | + |
| 42 | + # show map with static passable and impassable terrain |
| 43 | + map_data = game_map.static |
| 44 | + m, n = map_data.shape |
| 45 | + plt.imshow(map_data) |
| 46 | + plt.xticks(np.arange(0.5, n, 1.0), []) |
| 47 | + plt.yticks(np.arange(-0.5, m, 1.0), []) |
| 48 | + plt.connect('button_press_event', mouse_move) |
| 49 | + plt.grid(grid) |
| 50 | + plt.show() |
| 51 | + return |
6 | 52 |
|
7 | 53 |
|
8 | 54 | def view_map(map_data, stores=None, paths=None, points=None, grid=True): |
|
0 commit comments