Esempio n. 1
0
    def constructor(self, vertices, link_length, radius, share=True):
        pos1 = vertices[0][:]
        body1 = find_body(self.game.world, pos1)
        if body1 is None:
            body1 = self.game.world.add.ball(
                pos1, radius, dynamic=True, density=1.0, restitution=0.16,
                friction=0.1)
            body1.userData['color'] = (0, 0, 0)
        for i, pos2 in enumerate(vertices):
            if i == 0:
                continue
            body2 = find_body(self.game.world, pos2)
            if body2 is None:
                body2 = self.game.world.add.ball(
                    pos2, radius, dynamic=True, density=1.0, restitution=0.16,
                    friction=0.1)
                body2.userData['color'] = (0, 0, 0)

            self.make_chain(body1, body2, pos1, pos2, link_length, radius)
            body1 = body2
            pos1 = pos2[:]

        # Close the chain if the start and end were near each other
        if distance(vertices[0], vertices[-1]) < link_length * 2:
            pos1 = vertices[0][:]
            body1 = find_body(self.game.world, pos1)
            pos2 = vertices[-1][:]
            body2 = find_body(self.game.world, pos2)
            if body1 != body2:
                self.make_chain(body1, body2, pos1, pos2, link_length, radius)

        if share:
            data = json.dumps([vertices, link_length, radius])
            self.game.activity.send_event('c:' + data)
Esempio n. 2
0
    def constructor(self, pos1, pos2, share=True):
        body1 = find_body(self.game.world, pos1)
        body2 = find_body(self.game.world, pos2)
        if body1 is None or body2 is None:
            return

        self.game.world.add.joint(body1, body2, pos1, pos2)

        if share:
            data = json.dumps([pos1, pos2])
            self.game.activity.send_event('j:' + data)
Esempio n. 3
0
    def constructor(self, pos1, pos2, share=True):
        body1 = find_body(self.game.world, pos1)
        body2 = find_body(self.game.world, pos2)
        if body1 is None or body2 is None:
            return

        self.game.world.add.joint(body1, body2, pos1, pos2)

        if share:
            data = json.dumps([pos1, pos2])
            self.game.activity.send_event('j:' + data)
Esempio n. 4
0
    def constructor(self, pos, color, share=True):
        body = find_body(self.game.world, pos)
        track_circle = self.game.world.add.ball(pos,
                                                self.radius,
                                                dynamic=True,
                                                density=0.001,
                                                restitution=0.16,
                                                friction=0.1)
        trackdex = self.game.tracked_bodies
        track_circle.userData['track_index'] = trackdex
        dictkey = 'pen{0}'.format(trackdex)
        self.game.world.add.joint(track_circle, body, pos, pos, False)

        if 'track_indices' in body.userData:
            body.userData['track_indices'].append(trackdex)
        else:
            body.userData['track_indices'] = [trackdex]

        self.game.trackinfo[dictkey] = [0, 1, 2, 4, 5]
        self.game.trackinfo[dictkey][0] = body
        self.game.trackinfo[dictkey][1] = track_circle
        self.game.trackinfo[dictkey][2] = color
        self.game.trackinfo[dictkey][3] = False  # Pen destroyed or not
        self.game.trackinfo[dictkey][4] = trackdex  # Tracking index.
        self.game.tracked_bodies += 1  # counter of tracked bodies

        if share:
            data = json.dumps([pos, color])
            self.game.activity.send_event('t:' + data)
Esempio n. 5
0
    def constructor(self, pos, color, share=True):
        body = find_body(self.game.world, pos)
        track_circle = self.game.world.add.ball(
            pos, self.radius, dynamic=True, density=0.001,
            restitution=0.16, friction=0.1)
        trackdex = self.game.tracked_bodies
        track_circle.userData['track_index'] = trackdex
        dictkey = 'pen{0}'.format(trackdex)
        self.game.world.add.joint(track_circle, body, pos, pos, False)

        if 'track_indices' in body.userData:
            body.userData['track_indices'].append(trackdex)
        else:
            body.userData['track_indices'] = [trackdex]

        self.game.trackinfo[dictkey] = [0, 1, 2, 4, 5]
        self.game.trackinfo[dictkey][0] = body
        self.game.trackinfo[dictkey][1] = track_circle
        self.game.trackinfo[dictkey][2] = color
        self.game.trackinfo[dictkey][3] = False  # Pen destroyed or not
        self.game.trackinfo[dictkey][4] = trackdex  # Tracking index.
        self.game.tracked_bodies += 1  # counter of tracked bodies

        if share:
            data = json.dumps([pos, color])
            self.game.activity.send_event('t:' + data)
Esempio n. 6
0
    def handleToolEvent(self, event):
        Tool.handleToolEvent(self, event)
        if pygame.mouse.get_pressed()[0] and hasattr(event, "pos"):
            if not self.vertices:
                self.vertices = []
            self.vertices.append(tuple_to_int(event.pos))
            if len(self.vertices) > 10:
                self.vertices.pop(0)

            body_to_remove = find_body(self.game.world, event.pos)
            if body_to_remove is not None:
                tracklist = self.game.trackinfo.items()
                destroyed_body = False
                for key, info in tracklist:
                    trackdex = info[4]
                    if 'track_indices' in body_to_remove.userData and \
                       trackdex in body_to_remove.userData['track_indices'] \
                       and info[3] is False:
                        self.game.world.world.DestroyBody(info[1])
                        self.game.trackinfo[key][3] = True
                        destroyed_body = True
                        break

                jointnode = body_to_remove.joints
                if jointnode and not destroyed_body:
                    joint = jointnode[-1].joint
                    self.game.world.world.DestroyJoint(joint)
                elif not destroyed_body:
                    self.game.world.world.DestroyBody(body_to_remove)
        elif event.type == MOUSEBUTTONUP and event.button == 1:
            self.cancel()
Esempio n. 7
0
    def handleToolEvent(self, event):
        Tool.handleToolEvent(self, event)
        if pygame.mouse.get_pressed()[0] and hasattr(event, "pos"):
            if not self.vertices:
                self.vertices = []
            self.vertices.append(tuple_to_int(event.pos))
            if len(self.vertices) > 10:
                self.vertices.pop(0)

            body_to_remove = find_body(self.game.world, event.pos)
            if body_to_remove is not None:
                tracklist = list(self.game.trackinfo.items())
                destroyed_body = False
                for key, info in tracklist:
                    trackdex = info[4]
                    if 'track_indices' in body_to_remove.userData and \
                       trackdex in body_to_remove.userData['track_indices'] \
                       and info[3] is False:
                        self.game.world.world.DestroyBody(info[1])
                        self.game.trackinfo[key][3] = True
                        destroyed_body = True
                        break

                jointnode = body_to_remove.joints
                if jointnode and not destroyed_body:
                    joint = jointnode[-1].joint
                    self.game.world.world.DestroyJoint(joint)
                elif not destroyed_body:
                    self.game.world.world.DestroyBody(body_to_remove)
        elif event.type == MOUSEBUTTONUP and event.button == 1:
            self.cancel()
Esempio n. 8
0
 def handleToolEvent(self, event):
     Tool.handleToolEvent(self, event)
     if event.type == MOUSEBUTTONDOWN:
         self.jb1pos = tuple_to_int(event.pos)
         self.jb1 = find_body(self.game.world, event.pos)
         if self.jb1 is not None:
             self.constructor(self.jb1pos)
         self.jb1 = self.jb1pos = None
Esempio n. 9
0
 def handleToolEvent(self, event):
     Tool.handleToolEvent(self, event)
     if event.type == MOUSEBUTTONDOWN:
         self.jb1pos = tuple_to_int(event.pos)
         self.jb1 = find_body(self.game.world, event.pos)
         if self.jb1 is not None:
             self.constructor(self.jb1pos)
         self.jb1 = self.jb1pos = None
Esempio n. 10
0
 def handleToolEvent(self, event):
     Tool.handleToolEvent(self, event)
     if event.type == MOUSEBUTTONDOWN:
         if event.button >= 1:
             # Grab the first body
             self.jb1pos = tuple_to_int(event.pos)
             self.jb1 = find_body(self.game.world, event.pos)
             if self.jb1 is not None:
                 self.constructor(self.jb1pos, self.palette_data['speed'])
             self.jb1 = self.jb1pos = None
Esempio n. 11
0
 def handleToolEvent(self, event):
     Tool.handleToolEvent(self, event)
     if event.type == MOUSEBUTTONDOWN:
         if event.button >= 1:
             # Grab the first body
             self.jb1pos = tuple_to_int(event.pos)
             self.jb1 = find_body(self.game.world, event.pos)
             if self.jb1 is not None:
                 self.constructor(self.jb1pos, self.palette_data['speed'])
             self.jb1 = self.jb1pos = None
Esempio n. 12
0
 def handleToolEvent(self, event):
     Tool.handleToolEvent(self, event)
     if event.type == MOUSEBUTTONDOWN:
         if event.button == 1:
             self.jb1pos = tuple_to_int(event.pos)
             self.jb1 = find_body(self.game.world, event.pos)
             if self.jb1 is not None:
                 self.jb1[0].userData['rollMotor'] = {}
                 self.jb1[0].userData['rollMotor']['targetVelocity'] = -10
                 self.jb1[0].userData['rollMotor']['strength'] = 40
             self.jb1 = self.jb1pos = None
Esempio n. 13
0
 def handleToolEvent(self, event):
     Tool.handleToolEvent(self, event)
     if event.type == MOUSEBUTTONDOWN:
         if event.button == 1:
             self.jb1pos = tuple_to_int(event.pos)
             self.jb1 = find_body(self.game.world, event.pos)
             if self.jb1 is not None:
                 self.jb1[0].userData['rollMotor'] = {}
                 self.jb1[0].userData['rollMotor']['targetVelocity'] = -10
                 self.jb1[0].userData['rollMotor']['strength'] = 40
             self.jb1 = self.jb1pos = None
Esempio n. 14
0
    def constructor(self, vertices, link_length, radius, share=True):
        pos1 = vertices[0][:]
        body1 = find_body(self.game.world, pos1)
        if body1 is None:
            body1 = self.game.world.add.ball(pos1,
                                             radius,
                                             dynamic=True,
                                             density=1.0,
                                             restitution=0.16,
                                             friction=0.1)
            body1.userData['color'] = (0, 0, 0)
        for i, pos2 in enumerate(vertices):
            if i == 0:
                continue
            body2 = find_body(self.game.world, pos2)
            if body2 is None:
                body2 = self.game.world.add.ball(pos2,
                                                 radius,
                                                 dynamic=True,
                                                 density=1.0,
                                                 restitution=0.16,
                                                 friction=0.1)
                body2.userData['color'] = (0, 0, 0)

            self.make_chain(body1, body2, pos1, pos2, link_length, radius)
            body1 = body2
            pos1 = pos2[:]

        # Close the chain if the start and end were near each other
        if distance(vertices[0], vertices[-1]) < link_length * 2:
            pos1 = vertices[0][:]
            body1 = find_body(self.game.world, pos1)
            pos2 = vertices[-1][:]
            body2 = find_body(self.game.world, pos2)
            if body1 != body2:
                self.make_chain(body1, body2, pos1, pos2, link_length, radius)

        if share:
            data = json.dumps([vertices, link_length, radius])
            self.game.activity.send_event('c:' + data)
Esempio n. 15
0
    def constructor(self, pos, speed, share=True):
        body = find_body(self.game.world, pos)
        self.game.world.add.motor(body, pos, speed=speed)

        if share:
            data = json.dumps([pos, speed])
            self.game.activity.send_event('m:' + data)

        if not self.added_badge:
            self.add_badge(icon='trophy-icon-physics',
                           name='Nikola Tesla',
                           message='Congratulations! Great Motor you '
                           'got there, friend')
            self.added_badge = True
Esempio n. 16
0
    def constructor(self, pos, share=True):
        body = find_body(self.game.world, pos)
        self.game.world.add.joint(body, pos)

        if share:
            data = json.dumps([pos])
            self.game.activity.send_event('p:' + data)

        if not self.added_badge:
            self.add_badge(
                icon='trophy-icon-physics',
                name='Werner Heisenberg',
                message='Congratulations! You certainly did a great job'
                ' by adding a Pin to your machine!')
            self.added_badge = True
Esempio n. 17
0
    def handleToolEvent(self, event):
        Tool.handleToolEvent(self, event)

        if pygame.mouse.get_pressed()[0]:
            body = find_body(self.game.world, event.pos)
            if body is not None:
                color = body.userData['color']
                point_pos = tuple_to_int(event.pos)
                self.constructor(point_pos, color)

                if not self.added_badge:
                    self.add_badge(icon='trophy-icon-physics',
                                   name='Isaac Newton',
                                   message='Congratulations! You just added a'
                                   ' Pen to your machine!')
                    self.added_badge = True
Esempio n. 18
0
    def constructor(self, pos, speed, share=True):
        body = find_body(self.game.world, pos)
        self.game.world.add.motor(body, pos, speed=speed)

        if share:
            data = json.dumps([pos, speed])
            self.game.activity.send_event('m:' + data)

        if not self.added_badge:
            self.add_badge(
                icon='trophy-icon-physics',
                name='Nikola Tesla',
                message='Congratulations! Great Motor you '
                        'got there, friend'
            )
            self.added_badge = True
Esempio n. 19
0
    def constructor(self, pos, share=True):
        body = find_body(self.game.world, pos)
        self.game.world.add.joint(body, pos)

        if share:
            data = json.dumps([pos])
            self.game.activity.send_event('p:' + data)

        if not self.added_badge:
            self.add_badge(
                icon='trophy-icon-physics',
                name='Werner Heisenberg',
                message='Congratulations! You certainly did a great job'
                        ' by adding a Pin to your machine!'
            )
            self.added_badge = True
Esempio n. 20
0
    def handleToolEvent(self, event):
        Tool.handleToolEvent(self, event)

        if pygame.mouse.get_pressed()[0]:
            body = find_body(self.game.world, event.pos)
            if body is not None:
                color = body.userData['color']
                point_pos = tuple_to_int(event.pos)
                self.constructor(point_pos, color)

                if not self.added_badge:
                    self.add_badge(
                        icon='trophy-icon-physics',
                        name='Isaac Newton',
                        message='Congratulations! You just added a'
                                ' Pen to your machine!'
                    )
                    self.added_badge = True
Esempio n. 21
0
 def handleToolEvent(self, event):
     Tool.handleToolEvent(self, event)
     if event.type == MOUSEBUTTONDOWN:
         if event.button >= 1:
             # Grab the first body
             self.jb1pos = tuple_to_int(event.pos)
             self.jb1 = find_body(self.game.world, event.pos)
             self.jb2pos = None
     elif event.type == MOUSEBUTTONUP:
         if event.button == 1:
             # Grab the second position
             self.jb2pos = tuple_to_int(event.pos)
             self.constructor(self.jb1pos, self.jb2pos)
             # add joint to ground body
             # elif self.jb1:
             #    groundBody = self.game.world.world.GetGroundBody()
             #    self.game.world.add.joint(self.jb1[0], groundBody,
             #                              self.jb1pos, self.jb2pos)
             # regardless, clean everything up
             self.jb1 = self.jb1pos = self.jb2pos = None
Esempio n. 22
0
 def handleToolEvent(self, event):
     Tool.handleToolEvent(self, event)
     if event.type == MOUSEBUTTONDOWN:
         if event.button >= 1:
             # Grab the first body
             self.jb1pos = tuple_to_int(event.pos)
             self.jb1 = find_body(self.game.world, event.pos)
             self.jb2pos = None
     elif event.type == MOUSEBUTTONUP:
         if event.button == 1:
             # Grab the second position
             self.jb2pos = tuple_to_int(event.pos)
             self.constructor(self.jb1pos, self.jb2pos)
             # add joint to ground body
             # elif self.jb1:
             #    groundBody = self.game.world.world.GetGroundBody()
             #    self.game.world.add.joint(self.jb1[0], groundBody,
             #                              self.jb1pos, self.jb2pos)
             # regardless, clean everything up
             self.jb1 = self.jb1pos = self.jb2pos = None
Esempio n. 23
0
    def handleToolEvent(self, event):
        Tool.handleToolEvent(self, event)

        # We handle two types of 'grab' depending on simulation running or not
        if event.type == MOUSEBUTTONDOWN:
            if event.button == 1:
                # Give preference to pins and motors being caught
                for joint in self.game.world.world.joints:
                    x, y = joint.anchorA
                    ppm = self.game.world.ppm
                    x, y = self.game.world.to_screen((x * ppm, y * ppm))
                    wh_half = self.PIN_MOTOR_RADIUS
                    wh = 2 * self.PIN_MOTOR_RADIUS
                    rect = pygame.Rect(x - wh_half, y - wh_half, wh, wh)
                    if isinstance(joint, box2d.b2RevoluteJoint) \
                       and rect.collidepoint(tuple_to_int(event.pos)):
                        logging.debug("found a pin or motor")

                        self._moving_pm = joint
                        self.pm_mode_active = True
                        self.pm_x = x
                        self.pm_y = y
                        self.game.world.world.DestroyJoint(joint)

                        break

                if self.pm_mode_active:
                    # Game is stopped when moving pins and motors
                    # So that the game doesn't mess up, and for user
                    # convenience
                    self._game_run_prev_state = self.game.world.run_physics
                    self.game.world.run_physics = False

                    return
                    # Don't want to register the body under too

                # Grab the first object at the mouse pointer
                bodylist = self.game.world.get_bodies_at_pos(
                    tuple_to_int(event.pos),
                    include_static=False)
                if bodylist and len(bodylist) > 0:
                    if self.game.world.run_physics:
                        self.game.world.add.mouseJoint(bodylist[0],
                                                       tuple_to_int(event.pos))
                    else:
                        self._current_body = bodylist[0]
        elif event.type == MOUSEBUTTONUP:
            # Let it go
            if event.button == 1:
                if self.game.world.run_physics:
                    self.game.world.add.remove_mouseJoint()
                else:
                    if self.pm_mode_active:
                        pos = event.pos
                        body = find_body(self.game.world, pos)
                        if body is not None:
                            if self._moving_pm.enableMotor:
                                self.game.world.add.motor(
                                    body, pos,
                                    speed=MotorTool.palette_data['speed'])
                            else:
                                self.game.world.add.joint(body, pos)

                        # Check motor/pin and add joint accordingly

                        self.pm_mode_active = False
                        self._moving_pm = None

                        self.game.world.run_physics = self._game_run_prev_state

                    self._current_body = None
        elif event.type == MOUSEMOTION:  # and event.buttons[0]:
            # Move it around
            if self.game.world.run_physics:
                # Use box2D mouse motion
                self.game.world.mouse_move(tuple_to_int(event.pos))
            else:
                if self.pm_mode_active:
                    self.pm_x, self.pm_y = event.pos
                    return

                # Position directly (if we have a current body)
                if self._current_body is not None:
                    x, y = self.game.world.to_world(tuple_to_int(event.pos))
                    x /= self.game.world.ppm
                    y /= self.game.world.ppm
                    self._current_body.position = (x, y)
Esempio n. 24
0
    def handleToolEvent(self, event):
        Tool.handleToolEvent(self, event)

        # We handle two types of 'grab' depending on simulation running or not
        if event.type == MOUSEBUTTONDOWN:
            if event.button == 1:
                # Give preference to pins and motors being caught
                for joint in self.game.world.world.joints:
                    x, y = joint.anchorA
                    ppm = self.game.world.ppm
                    x, y = self.game.world.to_screen((x * ppm, y * ppm))
                    wh_half = self.PIN_MOTOR_RADIUS
                    wh = 2 * self.PIN_MOTOR_RADIUS
                    rect = pygame.Rect(x - wh_half, y - wh_half, wh, wh)
                    if isinstance(joint, box2d.b2RevoluteJoint) \
                       and rect.collidepoint(tuple_to_int(event.pos)):
                        logging.debug("found a pin or motor")

                        self._moving_pm = joint
                        self.pm_mode_active = True
                        self.pm_x = x
                        self.pm_y = y
                        self.game.world.world.DestroyJoint(joint)

                        break

                if self.pm_mode_active:
                    # Game is stopped when moving pins and motors
                    # So that the game doesn't mess up, and for user
                    # convenience
                    self._game_run_prev_state = self.game.world.run_physics
                    self.game.world.run_physics = False

                    return
                    # Don't want to register the body under too

                # Grab the first object at the mouse pointer
                bodylist = self.game.world.get_bodies_at_pos(
                    tuple_to_int(event.pos), include_static=False)
                if bodylist and len(bodylist) > 0:
                    if self.game.world.run_physics:
                        self.game.world.add.mouseJoint(bodylist[0],
                                                       tuple_to_int(event.pos))
                    else:
                        self._current_body = bodylist[0]
        elif event.type == MOUSEBUTTONUP:
            # Let it go
            if event.button == 1:
                if self.game.world.run_physics:
                    self.game.world.add.remove_mouseJoint()
                else:
                    if self.pm_mode_active:
                        pos = event.pos
                        body = find_body(self.game.world, pos)
                        if body is not None:
                            if self._moving_pm.enableMotor:
                                self.game.world.add.motor(
                                    body,
                                    pos,
                                    speed=MotorTool.palette_data['speed'])
                            else:
                                self.game.world.add.joint(body, pos)

                        # Check motor/pin and add joint accordingly

                        self.pm_mode_active = False
                        self._moving_pm = None

                        self.game.world.run_physics = self._game_run_prev_state

                    self._current_body = None
        elif event.type == MOUSEMOTION:  # and event.buttons[0]:
            # Move it around
            if self.game.world.run_physics:
                # Use box2D mouse motion
                self.game.world.mouse_move(tuple_to_int(event.pos))
            else:
                if self.pm_mode_active:
                    self.pm_x, self.pm_y = event.pos
                    return

                # Position directly (if we have a current body)
                if self._current_body is not None:
                    x, y = self.game.world.to_world(tuple_to_int(event.pos))
                    x /= self.game.world.ppm
                    y /= self.game.world.ppm
                    self._current_body.position = (x, y)