def __init__(self, player, size, field_of_view): self.position = player.position self.__fov_scale = math.tan(field_of_view.to_radians() / 2) self.__aspect_ratio = size.width / (size.height + smallest_number) self.__width = size.width self.forward = player.forward.normalize() self.__right = Direction(-self.forward.y, self.forward.x)
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)
class _Camera: def __init__(self, player, size, field_of_view): self.position = player.position self.__fov_scale = math.tan(field_of_view.to_radians() / 2) self.__aspect_ratio = size.width / (size.height + smallest_number) self.__width = size.width self.forward = player.forward.normalize() self.__right = Direction(-self.forward.y, self.forward.x) def direction_for_column(self, column): normalized_x = self.__convert_column_to_normalized_coordinate(column) right_scale = self.__fov_scale * self.__aspect_ratio * normalized_x direction = self.forward + self.__right * right_scale return direction.normalize() def column_for_direction(self, direction_in_view_coordinates): direction = direction_in_view_coordinates.normalize() right_scale = direction.x / (direction.y + smallest_number) normalized_x = right_scale / (self.__fov_scale * self.__aspect_ratio + smallest_number) return self.__convert_normalized_coordinate_to_column(normalized_x) def __convert_column_to_normalized_coordinate(self, column): return (2 * column) / (self.__width + smallest_number) - 1 def __convert_normalized_coordinate_to_column(self, coordinate): return int((self.__width * (coordinate + 1)) / 2) def to_view_position(self, position): delta = position - self.position return Position(self.__right.dot(delta), self.forward.dot(delta))
def test_other_players(): state = initial_state(initial_input, PLAYER, MAP, 3, 5) bengt = Player(name='bengt', position=Position(1.5, 1.5), forward=Direction(0.0, -1.0)) state, _ = handle_event(state, 'player', bengt) state, _ = handle_event(state, 'tick', 0.070) assert state.other_players['bengt'] == bengt
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 _direction_from_json(list): return Direction(*map(float, list))