def __init__(self, size: int = 6, raycast_size: int = 17375000): super().__init__(size) ray = Edge(Vec2(), Vec2(raycast_size, 0)) self.raycast_size = raycast_size self.rays = [ray] for i in range(1, 8): ray = Edge(Vec2(), Vec2(raycast_size, 0).rotate((360 / 8) * i)) self.rays.append(ray) self.ray_color = (123, 0, 123)
def set_follow(self, e: PhysicObject): """ Change camera to entity and follow it :param e: :return: """ self._target = e if self._target is None: self._last_target_pos = Vec2() return self._last_target_pos = e.root self.pos = self._target.root - Vec2(self._width/2, self._height/2)
def get_collision_radius(self) -> float: """ Gets the radius of the collision sphere using furthest vertex. Intersecting this sphere makes actual collision possible but not guaranteed. Used for quickly checking if collision can occur. :return: """ return self._shape.furthest_vertices.get_distance(Vec2())
def __init__(self, size=6, direction: Vec2 = Vec2(1,0)): self.size = size lb = Vec2(-1*size, -1*size) lt = Vec2(-1*size, 1*size) rb = Vec2(1*size, -1*size) rt = Vec2(1*size, 1*size) v = [lb, lt, rb, rt] l1 = Edge(lb, lt) l2 = Edge(lb, rb) l3 = Edge(rt, rb) l4 = Edge(rt, lt) e = [l1, l2, l3, l4] super().__init__(v, e, (0, 204, 0), direction)
def __init__(self, vertices: list, edges: list, color: tuple, direction: Vec2 = Vec2(1, 0)): self.vertices = vertices # list of Vec3 assuming object root as origin self.edges = edges # list of lines self.color = color self._direction = direction
def move(self, delta: float): """ Move the camera fitting to the given Vec3 :param delta: :return: """ self._pos += self._velocity * delta if self._velocity.length > 1.: # slow down camera over time self._velocity.x /= 2 self._velocity.y /= 2 else: self._velocity = Vec2()
def __init__(self, radius: float, color: tuple): super().__init__(vertices=[], edges=[], color=color) vertices = [] for i in range(12): vertices.append(Vec2(radius, 0).rotate((360 / 64) * i)) self.vertices = vertices edges = [] last_vertex = self.vertices[-1] for i in range(len(self.vertices)): edges.append(Edge(last_vertex, self.vertices[i])) last_vertex = self.vertices[i] self.edges = edges
def tick(self, delta: float, world): """ Updates camera position and zoom based on user input. :param delta: :param world: :return: """ if self._target is not None: # follow the target target_velocity = (self._target.root - self._last_target_pos)*(1/delta) self._velocity += target_velocity self._last_target_pos = self._target.root self.move(delta) # make a camera move pressed = pg.key.get_pressed() if pressed[pg.K_w]: # move camera "up" (-y) self._velocity += Vec2(0, -1*self._step_size*delta) if pressed[pg.K_s]: self._velocity += Vec2(0, self._step_size*delta) if pressed[pg.K_a]: # move camera "left" (-x) self._velocity += Vec2(-1*self._step_size*delta, 0) if pressed[pg.K_d]: self._velocity += Vec2(self._step_size*delta, 0)
def tick(self, delta: float, world): final_force = Vec2() for obj in world.entities: if isinstance(obj, PhysicObject): g = self.get_gravitational_pull(obj) dist = 0.5 * g # distance to fall towards obj in a second direc = obj.root - self.root final_force += (direc.normalize() * dist) self.velocity += final_force self.move(delta) # move according to velocity for delta time self.do_collision_checks(delta, world)
def world_to_camera(self, orig: Vec2, interpolation: float) -> Vec2: """ Convert given Vec3 world coordinates onto the camera plane :param orig: :param interpolation: :return: """ interpol_camera_pos = self.pos + self._velocity * interpolation camera_pos = Vec2() camera_pos.x = orig.x - interpol_camera_pos.x camera_pos.y = orig.y - interpol_camera_pos.y if self._target is not None: camera_pos -= self._target.velocity*interpolation return camera_pos
def __init__(self, pos: Vec2, width: int, height: int, max_view_distance: int, window: Window, world): self._pos = pos # assumed to be top-left corner of camera self._width = width self._height = height self._max_view_distance = max_view_distance self._velocity = Vec2() self._window = window self._step_size = 1737.5*10 self._target: PhysicObject = None self._last_target_pos: Vec2 = None world.listen_for_events(self)
def camera_to_viewport(self, camera_pos: Vec2) -> Vec2: """ Transform from a point on the camera screen to a point on the viewport (aka computer screen window) :param camera_pos: :return: >>> print(Camera(Vec2(0,0), 100, 100, Window(None, 200, 200, True)).camera_to_viewport(Vec2(50, 50))) <100.00 100.00> >>> print(Camera(Vec2(0,0), 100, 100, Window(None, 200, 200, True)).camera_to_viewport(Vec2(25, 75))) <50.00 150.00> >>> print(Camera(Vec2(0,0), 1000, 1000, Window(None, 100, 100, True)).camera_to_viewport(Vec2(4, 7))) <0.00 1.00> """ scale_x = self._window.width/self.width scale_y = self._window.height/self.height return Vec2(round(camera_pos.x*scale_x), round(camera_pos.y*scale_y))
def __init__(self, game, bg_color: tuple = (0, 25, 51)): self.active = True self.time_warp = game.TIME_WARP # value to multiply passed time by self.event_listeners = [] self._tick_rate = game.TICK_RATE # ticks per second self._game = game self._bg_color = bg_color self._objects = [] # list for entities and static objs of the world self._players = [ ] # list for all player entities, they are also inside _objects self._collidables = [ ] # list for all collidable entities for physics, sublist of _objects self._interactives = [ ] # list for all entities that interact on collision, ignore physics self._camera = Camera(Vec2(0, 0), 1737500 * 16, 1737500 * 10, 1737500 * 4, game.window, self) self._ui_font = pg.font.SysFont("Arial", 20)
def __init__(self, game): super().__init__(game) planet1 = Planetoid("Planet 1", Vec2(1737500*3, 1737500*2.3), mass=(7.348 * 10**22), size=(1737.5*1000)) planet2 = Planetoid("Planet 2", Vec2(1737500 * 3*4, 1737500 * 4.3), mass=(7.348 * 10 ** 22), size=(1737.5 * 1000)) for i in range(10): sat1 = TrackedObject("Discovery {0}".format(i), Vec2(1737500 * 5, 1737500 * 2.7)) sat1.velocity = Vec2(1000*(10-i), 1000*i) self.add_entity(sat1) g = Goal("First Goal", Vec2(1737500 * 4, 1737500 * 4), 1737500//2, 45) self.add_entity(planet1) self.add_entity(planet2) self.add_entity(g) sat = Satellite("Player 1", Vec2(1737500 * 5, 1737500 * 2.7)) sat.velocity = Vec2(10000, 4000) self.add_player_entity(sat) self.camera.set_follow(sat)
def nearest_vertices(self) -> Vec2: if len(self.vertices) == 0: return Vec2() self.vertices.sort(key=lambda x: x.get_distance(Vec2())) return self.vertices[0]
def __init__(self, root: Vec2, shape: Shape, collidable: bool = True): self.root = root self._shape = shape self._collidable = collidable self._velocity = Vec2() self._angle = .0
def __init__(self, size: float): vertices = [Vec2(-1*size, 0), Vec2(size, 0)] edges = [Edge(vertices[0], vertices[1])] super().__init__(vertices, edges, (255, 255, 0))