def load_map(path): color_to_material = _load_color_to_value_mapping( os.path.join(path, 'materials.json'), Material) color_to_object = _load_color_to_value_mapping( os.path.join(path, 'objects.json'), Object) materials, width = _load_map_layer(os.path.join(path, 'materials.png'), color_to_material, default=Material.FLOOR) object_layer, object_width = _load_map_layer(os.path.join( path, 'objects.png'), color_to_object, default=None) square_to_center_offset = Direction(0.5, 0.5) objects = [] def to_position(index): return Position(index % object_width, int(index / object_width)) for index, object in enumerate(object_layer): if object is not None: position = to_position(index) + square_to_center_offset objects.append((object, position)) return Map(materials, objects, width)
def deserialize(Type, v): if Type in [float, int, NoneType, str]: result = v elif issubclass(Type, enum.Enum): result = Type[v] elif Type in [Direction, Position]: x, y = v result = Type(x=deserialize(float, x), y=deserialize(float, y)) elif Type == Map: result = Map( materials=[deserialize(Material, m) for m in v['materials']], objects=[(deserialize(Object, o), deserialize(Position, p)) for o, p in v['objects']], width=deserialize(int, v['width'])) elif Type == Player: result = Player(name=deserialize(str, v['name']), position=deserialize(Position, v['position']), forward=deserialize(Direction, v['forward'])) else: raise Exception("Cannot deserialize object of type {}".format(Type)) assert isinstance(result, Type), "{} is not a {}".format(v, Type) return result
from use_case import * from domain import Direction, initial_input, Map, Material, Position MAP = Map(materials=[ Material.FLOOR, Material.FLOOR, Material.FLOOR, Material.FLOOR, Material.FLOOR, Material.FLOOR, Material.FLOOR, Material.FLOOR, Material.FLOOR ], objects=[], width=3) PLAYER = Player(name='test', position=Position(1.5, 1.5), forward=Direction(0.0, -1.0)) def test_move_forward(): state = initial_state(initial_input, PLAYER, MAP, 3, 5) state, _ = handle_event(state, 'input', initial_input._replace(forward=True)) state, _ = handle_event(state, 'tick', 0.070) assert state.player.position.x == 1.5 assert state.player.position.y < 1.5 assert state.player.forward.x == 0.0 assert state.player.forward.y == -1.0 def test_turn_right(): state = initial_state(initial_input, PLAYER, MAP, 3, 5) state, _ = handle_event(state, 'input', initial_input._replace(turn_right=True)) state, _ = handle_event(state, 'tick', 0.070)
def __init__(self, controller): self.controller = controller self.m = Map() self.drone = Drone(0, 0)
def __init__(self): self.map = Map() self.map.random_map() self.controller = Controller(Drone(PARAM_BATTERY), self.map)