예제 #1
0
    def position(self, v):
        if not v:
            v = Point2(0, 0)

        if isinstance(v, tuple):
            self._position = Point2(v[0], v[1])
        elif isinstance(v, Point2):
            self._position = v
        else:
            raise ValueError("v is not of the correct type")
def setup_ecs() -> tuple:
    trans = Transform2DComponent(Point2(a=0, b=0))
    player = PlayerEntity(r=20).add(trans)
    kb_sys = KeyboardInputSystem()
    t_sys = RenderSystem(screen_width=640, screen_height=480)

    entities = [player]
    components = [trans]
    # we add some very large boxes that are out of view initially, but gradually become visible if we move to the left or right
    # end of the world
    box_pos_x = (-300, -200, -50, 0, 100, 300, 250, 500, 1000, -1000)
    box_pos_y = (-300, 100, -200, 300, -300, 40, 40, 100, 200, 300)
    box_width = (10, 20, 50, 10, 30, 10, 40, 10, 20, 100, 100)

    debug = False
    if debug:
        box_pos_x = ()
        box_pos_y = ()
        box_width = ()

    for (x, y, w) in zip(box_pos_x, box_pos_y, box_width):
        t = Transform2DComponent(pos=Vec2(x, y))
        b = BoxEntity(w=w, h=w).add(t)
        entities.append(b)
        components.append(t)

    kb_sys.on_key_pressed = handle_key_pressed_event(kb_sys)
    systems = {"KEYBOARD": kb_sys, "TRANSFORM": t_sys}
    return entities, components, systems
예제 #3
0
    def project_vp(self, nc_x):
        """Projects a point from normalized clip space into the viewport/ device space

        Args:
                        nc_x (tuple/Point2/Vec2): the point in nc space that we wish to transform

        Raises:
                        ValueError: no point provided.

        Returns:
                        Point2: the transformed point
        """
        if not nc_x:
            raise ValueError("point not provided")

        p = nc_x
        if isinstance(nc_x, tuple):
            p = Point3(nc_x[0], nc_x[1], 1.0)
        elif isinstance(nc_x, Vec2):
            p = Point3(nc_x.x, nc_x.y, 1.0)
        elif isinstance(nc_x, Point3):
            pass
        else:
            raise ValueError("Undefined type provided")

        projected = self._vp * p
        return Point2(projected.x, projected.y)
예제 #4
0
    def project_nc(self, ws_x) -> Point2:
        """Projects a point from world space into the normalized clip space.

        Args:
                        ws_x (tuple/Point2/Vec2): the point in world space that we wish to transform

        Raises:
                        ValueError: no point provided.

        Returns:
                        Point2: the transformed point
        """
        if not ws_x:
            raise ValueError("point not provided")

        p = ws_x
        if isinstance(ws_x, tuple):
            p = Point3(ws_x[0], ws_x[1], 1.0)
        elif isinstance(ws_x, Vec2):
            p = Point3(ws_x.x, ws_x.y, 1.0)
        elif isinstance(ws_x, Point3):
            pass
        else:
            raise ValueError("Undefined type provided")

        projected = self._nc * p
        return Point2(projected.x, projected.y)
예제 #5
0
    def translate(self, p: Point2) -> None:
        """Translate the camera to a point p

        Args:
                        p (Point2): Target of the translation

        Raises:
                        ValueError: if point is not provided
        """
        if not p:
            raise ValueError(
                "p? point to translate to not provided: {}".format(p))
        if Point2.is_zero(p):
            return None

        _update = False
        if self._m_trans[2] != p.x:
            self._m_trans[2] = p.x
            _update = True

        if self._m_trans[5] != p.y:
            self._m_trans[5] = p.y
            _update = True

        if _update:
            self._update_ws()
예제 #6
0
def init_ecs(screen_width, screen_height, verbose: bool = True):
    if verbose:
        print("Initializing ECS objects")
    # we put the player at 0,0 at let her view at positive x=+1
    entities, components, systems = [], [], []

    ctrans = CTransform2(pos=Point2(100, 100), v_dir=Vec2(1, 0))
    cvel = CVelocity(velocity=5)
    cfov = CFieldOfView(fov_angle_rad=pi / 4.0, fov_distance=100.0)
    crnd = CRenderable(render_color=(64, 64, 228))
    components.append(ctrans)
    components.append(cvel)
    components.append(cfov)
    components.append(crnd)

    e_player = EPlayer().add(ctrans).add(cvel).add(crnd).add(cfov)
    entities.append(e_player)

    box_pos_x = (50, 100, 200, 400, 450, 500, 550, 600, 620)
    box_pos_y = (100, 200, 50, 100, 300, 400, 250, 100, 225)
    box_width = (20, 50, 10, 40, 20, 30, 50, 10, 10)

    for i, p in enumerate(zip(box_pos_x, box_pos_y)):
        ctrans = CTransform2(p, Vec2(0, 0))
        crnd = CRenderable(
            render_color=(200, 200, 200), shape=Shape.Square, width=box_width[i]
        )
        components.append(ctrans)
        components.append(crnd)

        n1 = Vec2(0, 1)
        o1 = Vec2(p[0] + box_width[i] // 2, p[1])
        p1 = Plane2(n1, o1)

        n2 = Vec2(1, 0)
        o2 = Vec2(p[0] + box_width[i], p[1] + box_width[i] // 2)
        p2 = Plane2(n2, o2)

        n3 = Vec2(0, -1)
        o3 = Vec2(p[0] + box_width[i] // 2, p[1] + box_width[i])
        p3 = Plane2(n3, o3)

        n4 = Vec2(-1, 0)
        o4 = Vec2(p[0], p[1] + box_width[i] // 2)
        p4 = Plane2(n4, o4)

        planes = [p1, p2, p3, p4]
        ccol = CCollidable(planes=planes)
        components.append(ccol)
        e_box = EBox().add(ctrans).add(crnd).add(ccol)
        entities.append(e_box)

    systems.append(SInput())
    systems.append(SRender(screen_width, screen_height))

    return components, entities, systems
 def __init__(self, w, h):
     super(BoxEntity, self).__init__()
     w2 = w // 2
     h2 = h // 2
     self._center = Point2(0, 0)
     self._width = w
     self._height = h
     self._w2 = w2
     self._h2 = h2
     self._p0 = self._center + Vec2(-w2, h2)
     self._p1 = self._center + Vec2(-w2, -h2)
     self._p2 = self._center + Vec2(w2, -h2)
     self._p3 = self._center + Vec2(w2, h2)
예제 #8
0
    def project_ws(self, x):
        """Project points from their local coordinate system to the world coordinate system

        Args:
                        x (tuple/Point2/Vec2): the point in local object space that we wish to transform

        Returns:
                        Point2: the projected point
        """
        if not x:
            raise ValueError("Missing point")

        p = x
        if isinstance(x, tuple):
            p = Point3(x[0], x[1], 1.0)
        elif isinstance(x, Vec2):
            p = Point3(x.x, x.y, 1.0)
        elif isinstance(x, Point3):
            pass
        else:
            raise ValueError("Undefined type provided: {}".format(type(x)))

        projected = self._ws * p
        return Point2(projected.x, projected.y)
 def __init__(self, r: int):
     super(PlayerEntity, self).__init__()
     self._radius = r
     self._p0 = Point2(0, 0)