예제 #1
0
    def __init__(self) -> None:
        """Instantiates an Actor in the current ba.Activity."""

        if __debug__:
            self._root_actor_init_called = True
        activity = _ba.getactivity()
        self._activity = weakref.ref(activity)
        activity.add_actor_weak_ref(self)
예제 #2
0
    def preload(cls) -> None:
        """Preload map media.

        This runs the class's on_preload() method as needed to prep it to run.
        Preloading should generally be done in a ba.Activity's __init__ method.
        Note that this is a classmethod since it is not operate on map
        instances but rather on the class itself before instances are made
        """
        activity = _ba.getactivity()
        if cls not in activity.preloads:
            activity.preloads[cls] = cls.on_preload()
예제 #3
0
 def player_was_killed(self,
                       player: ba.Player,
                       killed: bool = False,
                       killer: ba.Player = None) -> None:
     """Should be called when a player is killed."""
     from ba._lang import Lstr
     name = player.get_name()
     prec = self._player_records[name]
     prec.streak = 0
     if killed:
         prec.accum_killed_count += 1
         prec.killed_count += 1
     try:
         if killed and _ba.getactivity().announce_player_deaths:
             if killer == player:
                 _ba.screenmessage(Lstr(resource='nameSuicideText',
                                        subs=[('${NAME}', name)]),
                                   top=True,
                                   color=player.color,
                                   image=player.get_icon())
             elif killer is not None:
                 if killer.team == player.team:
                     _ba.screenmessage(Lstr(resource='nameBetrayedText',
                                            subs=[('${NAME}',
                                                   killer.get_name()),
                                                  ('${VICTIM}', name)]),
                                       top=True,
                                       color=killer.color,
                                       image=killer.get_icon())
                 else:
                     _ba.screenmessage(Lstr(resource='nameKilledText',
                                            subs=[('${NAME}',
                                                   killer.get_name()),
                                                  ('${VICTIM}', name)]),
                                       top=True,
                                       color=killer.color,
                                       image=killer.get_icon())
             else:
                 _ba.screenmessage(Lstr(resource='nameDiedText',
                                        subs=[('${NAME}', name)]),
                                   top=True,
                                   color=player.color,
                                   image=player.get_icon())
     except Exception:
         from ba import _error
         _error.print_exception('error announcing kill')
예제 #4
0
    def __init__(self, request: str, request_type: str,
                 data: Optional[Dict[str, Any]],
                 callback: Optional[ServerCallbackType],
                 response_type: ServerResponseType):
        super().__init__()
        self._request = request
        self._request_type = request_type
        if not isinstance(response_type, ServerResponseType):
            raise Exception(f'Invalid response type: {response_type}')
        self._response_type = response_type
        self._data = {} if data is None else copy.deepcopy(data)
        self._callback: Optional[ServerCallbackType] = callback
        self._context = _ba.Context('current')

        # Save and restore the context we were created from.
        activity: Optional[ba.Activity] = _ba.getactivity(doraise=False)
        self._activity = weakref.ref(
            activity) if activity is not None else None
예제 #5
0
    def __init__(self, settings: dict):
        """Creates an Activity in the current ba.Session.

        The activity will not be actually run until ba.Session.setactivity
        is called. 'settings' should be a dict of key/value pairs specific
        to the activity.

        Activities should preload as much of their media/etc as possible in
        their constructor, but none of it should actually be used until they
        are transitioned in.
        """
        super().__init__()

        # Create our internal engine data.
        self._activity_data = _ba.register_activity(self)

        assert isinstance(settings, dict)
        assert _ba.getactivity() is self

        self._globalsnode: Optional[ba.Node] = None

        # Player/Team types should have been specified as type args;
        # grab those.
        self._playertype: type[PlayerType]
        self._teamtype: type[TeamType]
        self._setup_player_and_team_types()

        # FIXME: Relocate or remove the need for this stuff.
        self.paused_text: Optional[ba.Actor] = None

        self._session = weakref.ref(_ba.getsession())

        # Preloaded data for actors, maps, etc; indexed by type.
        self.preloads: dict[type, Any] = {}

        # Hopefully can eventually kill this; activities should
        # validate/store whatever settings they need at init time
        # (in a more type-safe way).
        self.settings_raw = settings

        self._has_transitioned_in = False
        self._has_begun = False
        self._has_ended = False
        self._activity_death_check_timer: Optional[ba.Timer] = None
        self._expired = False
        self._delay_delete_players: list[PlayerType] = []
        self._delay_delete_teams: list[TeamType] = []
        self._players_that_left: list[weakref.ref[PlayerType]] = []
        self._teams_that_left: list[weakref.ref[TeamType]] = []
        self._transitioning_out = False

        # A handy place to put most actors; this list is pruned of dead
        # actors regularly and these actors are insta-killed as the activity
        # is dying.
        self._actor_refs: list[ba.Actor] = []
        self._actor_weak_refs: list[weakref.ref[ba.Actor]] = []
        self._last_prune_dead_actors_time = _ba.time()
        self._prune_dead_actors_timer: Optional[ba.Timer] = None

        self.teams = []
        self.players = []

        self.lobby = None
        self._stats: Optional[ba.Stats] = None
        self._customdata: Optional[dict] = {}
예제 #6
0
    def __init__(self,
                 vr_overlay_offset: Optional[Sequence[float]] = None) -> None:
        """Instantiate a map."""
        from ba import _gameutils
        super().__init__()

        # This is expected to always be a ba.Node object (whether valid or not)
        # should be set to something meaningful by child classes.
        self.node: Optional[_ba.Node] = None

        # Make our class' preload-data available to us
        # (and instruct the user if we weren't preloaded properly).
        try:
            self.preloaddata = _ba.getactivity().preloads[type(self)]
        except Exception:
            from ba import _error
            raise _error.NotFoundError(
                'Preload data not found for ' + str(type(self)) +
                '; make sure to call the type\'s preload()'
                ' staticmethod in the activity constructor')

        # Set various globals.
        gnode = _ba.getactivity().globalsnode

        # Set area-of-interest bounds.
        aoi_bounds = self.get_def_bound_box('area_of_interest_bounds')
        if aoi_bounds is None:
            print('WARNING: no "aoi_bounds" found for map:', self.getname())
            aoi_bounds = (-1, -1, -1, 1, 1, 1)
        gnode.area_of_interest_bounds = aoi_bounds

        # Set map bounds.
        map_bounds = self.get_def_bound_box('map_bounds')
        if map_bounds is None:
            print('WARNING: no "map_bounds" found for map:', self.getname())
            map_bounds = (-30, -10, -30, 30, 100, 30)
        _ba.set_map_bounds(map_bounds)

        # Set shadow ranges.
        try:
            gnode.shadow_range = [
                self.defs.points[v][1] for v in [
                    'shadow_lower_bottom', 'shadow_lower_top',
                    'shadow_upper_bottom', 'shadow_upper_top'
                ]
            ]
        except Exception:
            pass

        # In vr, set a fixed point in space for the overlay to show up at.
        # By default we use the bounds center but allow the map to override it.
        center = ((aoi_bounds[0] + aoi_bounds[3]) * 0.5,
                  (aoi_bounds[1] + aoi_bounds[4]) * 0.5,
                  (aoi_bounds[2] + aoi_bounds[5]) * 0.5)
        if vr_overlay_offset is not None:
            center = (center[0] + vr_overlay_offset[0],
                      center[1] + vr_overlay_offset[1],
                      center[2] + vr_overlay_offset[2])
        gnode.vr_overlay_center = center
        gnode.vr_overlay_center_enabled = True

        self.spawn_points = (self.get_def_points('spawn')
                             or [(0, 0, 0, 0, 0, 0)])
        self.ffa_spawn_points = (self.get_def_points('ffa_spawn')
                                 or [(0, 0, 0, 0, 0, 0)])
        self.spawn_by_flag_points = (self.get_def_points('spawn_by_flag')
                                     or [(0, 0, 0, 0, 0, 0)])
        self.flag_points = self.get_def_points('flag') or [(0, 0, 0)]

        # We just want points.
        self.flag_points = [p[:3] for p in self.flag_points]
        self.flag_points_default = (self.get_def_point('flag_default')
                                    or (0, 1, 0))
        self.powerup_spawn_points = self.get_def_points('powerup_spawn') or [
            (0, 0, 0)
        ]

        # We just want points.
        self.powerup_spawn_points = ([
            p[:3] for p in self.powerup_spawn_points
        ])
        self.tnt_points = self.get_def_points('tnt') or []

        # We just want points.
        self.tnt_points = [p[:3] for p in self.tnt_points]

        self.is_hockey = False
        self.is_flying = False

        # FIXME: this should be part of game; not map.
        self._next_ffa_start_index = 0
예제 #7
0
    def show_completion_banner(self, sound: bool = True) -> None:
        """Create the banner/sound for an acquired achievement announcement."""
        from ba import _account
        from ba import _gameutils
        from bastd.actor.text import Text
        from bastd.actor.image import Image
        from ba._general import WeakCall
        from ba._lang import Lstr
        from ba._messages import DieMessage
        from ba._enums import TimeType, SpecialChar
        app = _ba.app
        app.last_achievement_display_time = _ba.time(TimeType.REAL)

        # Just piggy-back onto any current activity
        # (should we use the session instead?..)
        activity = _ba.getactivity(doraise=False)

        # If this gets called while this achievement is occupying a slot
        # already, ignore it. (probably should never happen in real
        # life but whatevs).
        if self._completion_banner_slot is not None:
            return

        if activity is None:
            print('show_completion_banner() called with no current activity!')
            return

        if sound:
            _ba.playsound(_ba.getsound('achievement'), host_only=True)
        else:
            _ba.timer(
                0.5,
                lambda: _ba.playsound(_ba.getsound('ding'), host_only=True))

        in_time = 0.300
        out_time = 3.5

        base_vr_depth = 200

        # Find the first free slot.
        i = 0
        while True:
            if i not in app.achievement_completion_banner_slots:
                app.achievement_completion_banner_slots.add(i)
                self._completion_banner_slot = i

                # Remove us from that slot when we close.
                # Use a real-timer in the UI context so the removal runs even
                # if our activity/session dies.
                with _ba.Context('ui'):
                    _ba.timer(in_time + out_time,
                              self._remove_banner_slot,
                              timetype=TimeType.REAL)
                break
            i += 1
        assert self._completion_banner_slot is not None
        y_offs = 110 * self._completion_banner_slot
        objs: List[ba.Actor] = []
        obj = Image(_ba.gettexture('shadow'),
                    position=(-30, 30 + y_offs),
                    front=True,
                    attach=Image.Attach.BOTTOM_CENTER,
                    transition=Image.Transition.IN_BOTTOM,
                    vr_depth=base_vr_depth - 100,
                    transition_delay=in_time,
                    transition_out_delay=out_time,
                    color=(0.0, 0.1, 0, 1),
                    scale=(1000, 300)).autoretain()
        objs.append(obj)
        assert obj.node
        obj.node.host_only = True
        obj = Image(_ba.gettexture('light'),
                    position=(-180, 60 + y_offs),
                    front=True,
                    attach=Image.Attach.BOTTOM_CENTER,
                    vr_depth=base_vr_depth,
                    transition=Image.Transition.IN_BOTTOM,
                    transition_delay=in_time,
                    transition_out_delay=out_time,
                    color=(1.8, 1.8, 1.0, 0.0),
                    scale=(40, 300)).autoretain()
        objs.append(obj)
        assert obj.node
        obj.node.host_only = True
        obj.node.premultiplied = True
        combine = _ba.newnode('combine', owner=obj.node, attrs={'size': 2})
        _gameutils.animate(
            combine, 'input0', {
                in_time: 0,
                in_time + 0.4: 30,
                in_time + 0.5: 40,
                in_time + 0.6: 30,
                in_time + 2.0: 0
            })
        _gameutils.animate(
            combine, 'input1', {
                in_time: 0,
                in_time + 0.4: 200,
                in_time + 0.5: 500,
                in_time + 0.6: 200,
                in_time + 2.0: 0
            })
        combine.connectattr('output', obj.node, 'scale')
        _gameutils.animate(obj.node,
                           'rotate', {
                               0: 0.0,
                               0.35: 360.0
                           },
                           loop=True)
        obj = Image(self.get_icon_texture(True),
                    position=(-180, 60 + y_offs),
                    attach=Image.Attach.BOTTOM_CENTER,
                    front=True,
                    vr_depth=base_vr_depth - 10,
                    transition=Image.Transition.IN_BOTTOM,
                    transition_delay=in_time,
                    transition_out_delay=out_time,
                    scale=(100, 100)).autoretain()
        objs.append(obj)
        assert obj.node
        obj.node.host_only = True

        # Flash.
        color = self.get_icon_color(True)
        combine = _ba.newnode('combine', owner=obj.node, attrs={'size': 3})
        keys = {
            in_time: 1.0 * color[0],
            in_time + 0.4: 1.5 * color[0],
            in_time + 0.5: 6.0 * color[0],
            in_time + 0.6: 1.5 * color[0],
            in_time + 2.0: 1.0 * color[0]
        }
        _gameutils.animate(combine, 'input0', keys)
        keys = {
            in_time: 1.0 * color[1],
            in_time + 0.4: 1.5 * color[1],
            in_time + 0.5: 6.0 * color[1],
            in_time + 0.6: 1.5 * color[1],
            in_time + 2.0: 1.0 * color[1]
        }
        _gameutils.animate(combine, 'input1', keys)
        keys = {
            in_time: 1.0 * color[2],
            in_time + 0.4: 1.5 * color[2],
            in_time + 0.5: 6.0 * color[2],
            in_time + 0.6: 1.5 * color[2],
            in_time + 2.0: 1.0 * color[2]
        }
        _gameutils.animate(combine, 'input2', keys)
        combine.connectattr('output', obj.node, 'color')

        obj = Image(_ba.gettexture('achievementOutline'),
                    model_transparent=_ba.getmodel('achievementOutline'),
                    position=(-180, 60 + y_offs),
                    front=True,
                    attach=Image.Attach.BOTTOM_CENTER,
                    vr_depth=base_vr_depth,
                    transition=Image.Transition.IN_BOTTOM,
                    transition_delay=in_time,
                    transition_out_delay=out_time,
                    scale=(100, 100)).autoretain()
        assert obj.node
        obj.node.host_only = True

        # Flash.
        color = (2, 1.4, 0.4, 1)
        combine = _ba.newnode('combine', owner=obj.node, attrs={'size': 3})
        keys = {
            in_time: 1.0 * color[0],
            in_time + 0.4: 1.5 * color[0],
            in_time + 0.5: 6.0 * color[0],
            in_time + 0.6: 1.5 * color[0],
            in_time + 2.0: 1.0 * color[0]
        }
        _gameutils.animate(combine, 'input0', keys)
        keys = {
            in_time: 1.0 * color[1],
            in_time + 0.4: 1.5 * color[1],
            in_time + 0.5: 6.0 * color[1],
            in_time + 0.6: 1.5 * color[1],
            in_time + 2.0: 1.0 * color[1]
        }
        _gameutils.animate(combine, 'input1', keys)
        keys = {
            in_time: 1.0 * color[2],
            in_time + 0.4: 1.5 * color[2],
            in_time + 0.5: 6.0 * color[2],
            in_time + 0.6: 1.5 * color[2],
            in_time + 2.0: 1.0 * color[2]
        }
        _gameutils.animate(combine, 'input2', keys)
        combine.connectattr('output', obj.node, 'color')
        objs.append(obj)

        objt = Text(Lstr(value='${A}:',
                         subs=[('${A}', Lstr(resource='achievementText'))]),
                    position=(-120, 91 + y_offs),
                    front=True,
                    v_attach=Text.VAttach.BOTTOM,
                    vr_depth=base_vr_depth - 10,
                    transition=Text.Transition.IN_BOTTOM,
                    flatness=0.5,
                    transition_delay=in_time,
                    transition_out_delay=out_time,
                    color=(1, 1, 1, 0.8),
                    scale=0.65).autoretain()
        objs.append(objt)
        assert objt.node
        objt.node.host_only = True

        objt = Text(self.display_name,
                    position=(-120, 50 + y_offs),
                    front=True,
                    v_attach=Text.VAttach.BOTTOM,
                    transition=Text.Transition.IN_BOTTOM,
                    vr_depth=base_vr_depth,
                    flatness=0.5,
                    transition_delay=in_time,
                    transition_out_delay=out_time,
                    flash=True,
                    color=(1, 0.8, 0, 1.0),
                    scale=1.5).autoretain()
        objs.append(objt)
        assert objt.node
        objt.node.host_only = True

        objt = Text(_ba.charstr(SpecialChar.TICKET),
                    position=(-120 - 170 + 5, 75 + y_offs - 20),
                    front=True,
                    v_attach=Text.VAttach.BOTTOM,
                    h_align=Text.HAlign.CENTER,
                    v_align=Text.VAlign.CENTER,
                    transition=Text.Transition.IN_BOTTOM,
                    vr_depth=base_vr_depth,
                    transition_delay=in_time,
                    transition_out_delay=out_time,
                    flash=True,
                    color=(0.5, 0.5, 0.5, 1),
                    scale=3.0).autoretain()
        objs.append(objt)
        assert objt.node
        objt.node.host_only = True

        objt = Text('+' + str(self.get_award_ticket_value()),
                    position=(-120 - 180 + 5, 80 + y_offs - 20),
                    v_attach=Text.VAttach.BOTTOM,
                    front=True,
                    h_align=Text.HAlign.CENTER,
                    v_align=Text.VAlign.CENTER,
                    transition=Text.Transition.IN_BOTTOM,
                    vr_depth=base_vr_depth,
                    flatness=0.5,
                    shadow=1.0,
                    transition_delay=in_time,
                    transition_out_delay=out_time,
                    flash=True,
                    color=(0, 1, 0, 1),
                    scale=1.5).autoretain()
        objs.append(objt)
        assert objt.node
        objt.node.host_only = True

        # Add the 'x 2' if we've got pro.
        if _account.have_pro():
            objt = Text('x 2',
                        position=(-120 - 180 + 45, 80 + y_offs - 50),
                        v_attach=Text.VAttach.BOTTOM,
                        front=True,
                        h_align=Text.HAlign.CENTER,
                        v_align=Text.VAlign.CENTER,
                        transition=Text.Transition.IN_BOTTOM,
                        vr_depth=base_vr_depth,
                        flatness=0.5,
                        shadow=1.0,
                        transition_delay=in_time,
                        transition_out_delay=out_time,
                        flash=True,
                        color=(0.4, 0, 1, 1),
                        scale=0.9).autoretain()
            objs.append(objt)
            assert objt.node
            objt.node.host_only = True

        objt = Text(self.description_complete,
                    position=(-120, 30 + y_offs),
                    front=True,
                    v_attach=Text.VAttach.BOTTOM,
                    transition=Text.Transition.IN_BOTTOM,
                    vr_depth=base_vr_depth - 10,
                    flatness=0.5,
                    transition_delay=in_time,
                    transition_out_delay=out_time,
                    color=(1.0, 0.7, 0.5, 1.0),
                    scale=0.8).autoretain()
        objs.append(objt)
        assert objt.node
        objt.node.host_only = True

        for actor in objs:
            _ba.timer(out_time + 1.000,
                      WeakCall(actor.handlemessage, DieMessage()))
def animate(node: ba.Node,
            attr: str,
            keys: dict[float, float],
            loop: bool = False,
            offset: float = 0,
            timetype: ba.TimeType = TimeType.SIM,
            timeformat: ba.TimeFormat = TimeFormat.SECONDS,
            suppress_format_warning: bool = False) -> ba.Node:
    """Animate values on a target ba.Node.

    Category: Gameplay Functions

    Creates an 'animcurve' node with the provided values and time as an input,
    connect it to the provided attribute, and set it to die with the target.
    Key values are provided as time:value dictionary pairs.  Time values are
    relative to the current time. By default, times are specified in seconds,
    but timeformat can also be set to MILLISECONDS to recreate the old behavior
    (prior to ba 1.5) of taking milliseconds. Returns the animcurve node.
    """
    if timetype is TimeType.SIM:
        driver = 'time'
    else:
        raise Exception('FIXME; only SIM timetype is supported currently.')
    items = list(keys.items())
    items.sort()

    # Temp sanity check while we transition from milliseconds to seconds
    # based time values.
    if __debug__:
        if not suppress_format_warning:
            for item in items:
                _ba.time_format_check(timeformat, item[0])

    curve = _ba.newnode('animcurve',
                        owner=node,
                        name='Driving ' + str(node) + ' \'' + attr + '\'')

    if timeformat is TimeFormat.SECONDS:
        mult = 1000
    elif timeformat is TimeFormat.MILLISECONDS:
        mult = 1
    else:
        raise ValueError(f'invalid timeformat value: {timeformat}')

    curve.times = [int(mult * time) for time, val in items]
    curve.offset = _ba.time(timeformat=TimeFormat.MILLISECONDS) + int(
        mult * offset)
    curve.values = [val for time, val in items]
    curve.loop = loop

    # If we're not looping, set a timer to kill this curve
    # after its done its job.
    # FIXME: Even if we are looping we should have a way to die once we
    #  get disconnected.
    if not loop:
        _ba.timer(int(mult * items[-1][0]) + 1000,
                  curve.delete,
                  timeformat=TimeFormat.MILLISECONDS)

    # Do the connects last so all our attrs are in place when we push initial
    # values through.

    # We operate in either activities or sessions..
    try:
        globalsnode = _ba.getactivity().globalsnode
    except ActivityNotFoundError:
        globalsnode = _ba.getsession().sessionglobalsnode

    globalsnode.connectattr(driver, curve, 'in')
    curve.connectattr('out', node, attr)
    return curve
def cameraflash(duration: float = 999.0) -> None:
    """Create a strobing camera flash effect.

    Category: Gameplay Functions

    (as seen when a team wins a game)
    Duration is in seconds.
    """
    # pylint: disable=too-many-locals
    import random
    from ba._nodeactor import NodeActor
    x_spread = 10
    y_spread = 5
    positions = [[-x_spread, -y_spread], [0, -y_spread], [0, y_spread],
                 [x_spread, -y_spread], [x_spread, y_spread],
                 [-x_spread, y_spread]]
    times = [0, 2700, 1000, 1800, 500, 1400]

    # Store this on the current activity so we only have one at a time.
    # FIXME: Need a type safe way to do this.
    activity = _ba.getactivity()
    activity.camera_flash_data = []  # type: ignore
    for i in range(6):
        light = NodeActor(
            _ba.newnode('light',
                        attrs={
                            'position': (positions[i][0], 0, positions[i][1]),
                            'radius': 1.0,
                            'lights_volumes': False,
                            'height_attenuated': False,
                            'color': (0.2, 0.2, 0.8)
                        }))
        sval = 1.87
        iscale = 1.3
        tcombine = _ba.newnode('combine',
                               owner=light.node,
                               attrs={
                                   'size': 3,
                                   'input0': positions[i][0],
                                   'input1': 0,
                                   'input2': positions[i][1]
                               })
        assert light.node
        tcombine.connectattr('output', light.node, 'position')
        xval = positions[i][0]
        yval = positions[i][1]
        spd = 0.5 + random.random()
        spd2 = 0.5 + random.random()
        animate(tcombine,
                'input0', {
                    0.0: xval + 0,
                    0.069 * spd: xval + 10.0,
                    0.143 * spd: xval - 10.0,
                    0.201 * spd: xval + 0
                },
                loop=True)
        animate(tcombine,
                'input2', {
                    0.0: yval + 0,
                    0.15 * spd2: yval + 10.0,
                    0.287 * spd2: yval - 10.0,
                    0.398 * spd2: yval + 0
                },
                loop=True)
        animate(light.node,
                'intensity', {
                    0.0: 0,
                    0.02 * sval: 0,
                    0.05 * sval: 0.8 * iscale,
                    0.08 * sval: 0,
                    0.1 * sval: 0
                },
                loop=True,
                offset=times[i])
        _ba.timer((times[i] + random.randint(1, int(duration)) * 40 * sval),
                  light.node.delete,
                  timeformat=TimeFormat.MILLISECONDS)
        activity.camera_flash_data.append(light)  # type: ignore
def animate_array(node: ba.Node,
                  attr: str,
                  size: int,
                  keys: dict[float, Sequence[float]],
                  loop: bool = False,
                  offset: float = 0,
                  timetype: ba.TimeType = TimeType.SIM,
                  timeformat: ba.TimeFormat = TimeFormat.SECONDS,
                  suppress_format_warning: bool = False) -> None:
    """Animate an array of values on a target ba.Node.

    Category: Gameplay Functions

    Like ba.animate(), but operates on array attributes.
    """
    # pylint: disable=too-many-locals
    combine = _ba.newnode('combine', owner=node, attrs={'size': size})
    if timetype is TimeType.SIM:
        driver = 'time'
    else:
        raise Exception('FIXME: Only SIM timetype is supported currently.')
    items = list(keys.items())
    items.sort()

    # Temp sanity check while we transition from milliseconds to seconds
    # based time values.
    if __debug__:
        if not suppress_format_warning:
            for item in items:
                # (PyCharm seems to think item is a float, not a tuple)
                _ba.time_format_check(timeformat, item[0])

    if timeformat is TimeFormat.SECONDS:
        mult = 1000
    elif timeformat is TimeFormat.MILLISECONDS:
        mult = 1
    else:
        raise ValueError('invalid timeformat value: "' + str(timeformat) + '"')

    # We operate in either activities or sessions..
    try:
        globalsnode = _ba.getactivity().globalsnode
    except ActivityNotFoundError:
        globalsnode = _ba.getsession().sessionglobalsnode

    for i in range(size):
        curve = _ba.newnode('animcurve',
                            owner=node,
                            name=('Driving ' + str(node) + ' \'' + attr +
                                  '\' member ' + str(i)))
        globalsnode.connectattr(driver, curve, 'in')
        curve.times = [int(mult * time) for time, val in items]
        curve.values = [val[i] for time, val in items]
        curve.offset = _ba.time(timeformat=TimeFormat.MILLISECONDS) + int(
            mult * offset)
        curve.loop = loop
        curve.connectattr('out', combine, 'input' + str(i))

        # If we're not looping, set a timer to kill this
        # curve after its done its job.
        if not loop:
            # (PyCharm seems to think item is a float, not a tuple)
            _ba.timer(int(mult * items[-1][0]) + 1000,
                      curve.delete,
                      timeformat=TimeFormat.MILLISECONDS)
    combine.connectattr('output', node, attr)

    # If we're not looping, set a timer to kill the combine once
    # the job is done.
    # FIXME: Even if we are looping we should have a way to die
    #  once we get disconnected.
    if not loop:
        # (PyCharm seems to think item is a float, not a tuple)
        _ba.timer(int(mult * items[-1][0]) + 1000,
                  combine.delete,
                  timeformat=TimeFormat.MILLISECONDS)
예제 #11
0
    def __init__(self, settings: Dict[str, Any]):
        """Creates an activity in the current ba.Session.

        The activity will not be actually run until ba.Session.set_activity()
        is called. 'settings' should be a dict of key/value pairs specific
        to the activity.

        Activities should preload as much of their media/etc as possible in
        their constructor, but none of it should actually be used until they
        are transitioned in.
        """
        super().__init__()

        # FIXME: Relocate this stuff.
        self.sharedobjs: Dict[str, Any] = {}
        self.paused_text: Optional[ba.Actor] = None
        self.spaz_respawn_icons_right: Dict[int, RespawnIcon]

        # Create our internal engine data.
        self._activity_data = _ba.register_activity(self)

        session = _ba.getsession()
        if session is None:
            raise Exception("No current session")
        self._session = weakref.ref(session)

        # Preloaded data for actors, maps, etc; indexed by type.
        self.preloads: Dict[Type, Any] = {}

        if not isinstance(settings, dict):
            raise Exception("expected dict for settings")
        if _ba.getactivity(doraise=False) is not self:
            raise Exception('invalid context state')

        self.settings = settings

        self._has_transitioned_in = False
        self._has_begun = False
        self._has_ended = False
        self._should_end_immediately = False
        self._should_end_immediately_results: (
            Optional[ba.TeamGameResults]) = None
        self._should_end_immediately_delay = 0.0
        self._called_activity_on_transition_in = False
        self._called_activity_on_begin = False

        self._activity_death_check_timer: Optional[ba.Timer] = None
        self._expired = False

        # Whether to print every time a player dies. This can be pertinent
        # in games such as Death-Match but can be annoying in games where it
        # doesn't matter.
        self.announce_player_deaths = False

        # Joining activities are for waiting for initial player joins.
        # They are treated slightly differently than regular activities,
        # mainly in that all players are passed to the activity at once
        # instead of as each joins.
        self.is_joining_activity = False

        # Whether game-time should still progress when in menus/etc.
        self.allow_pausing = False

        # Whether idle players can potentially be kicked (should not happen in
        # menus/etc).
        self.allow_kick_idle_players = True

        # In vr mode, this determines whether overlay nodes (text, images, etc)
        # are created at a fixed position in space or one that moves based on
        # the current map. Generally this should be on for games and off for
        # transitions/score-screens/etc. that persist between maps.
        self.use_fixed_vr_overlay = False

        # If True, runs in slow motion and turns down sound pitch.
        self.slow_motion = False

        # Set this to True to inherit slow motion setting from previous
        # activity (useful for transitions to avoid hitches).
        self.inherits_slow_motion = False

        # Set this to True to keep playing the music from the previous activity
        # (without even restarting it).
        self.inherits_music = False

        # Set this to true to inherit VR camera offsets from the previous
        # activity (useful for preventing sporadic camera movement
        # during transitions).
        self.inherits_camera_vr_offset = False

        # Set this to true to inherit (non-fixed) VR overlay positioning from
        # the previous activity (useful for prevent sporadic overlay jostling
        # during transitions).
        self.inherits_vr_overlay_center = False

        # Set this to true to inherit screen tint/vignette colors from the
        # previous activity (useful to prevent sudden color changes during
        # transitions).
        self.inherits_tint = False

        # If the activity fades or transitions in, it should set the length of
        # time here so that previous activities will be kept alive for that
        # long (avoiding 'holes' in the screen)
        # This value is given in real-time seconds.
        self.transition_time = 0.0

        # Is it ok to show an ad after this activity ends before showing
        # the next activity?
        self.can_show_ad_on_death = False

        # This gets set once another activity has begun transitioning in but
        # before this one is killed. The on_transition_out() method is also
        # called at this time.  Make sure to not assign player inputs,
        # change music, or anything else with global implications once this
        # happens.
        self._transitioning_out = False

        # A handy place to put most actors; this list is pruned of dead
        # actors regularly and these actors are insta-killed as the activity
        # is dying.
        self._actor_refs: List[ba.Actor] = []
        self._actor_weak_refs: List[ReferenceType[ba.Actor]] = []
        self._last_dead_object_prune_time = _ba.time()

        # This stuff gets filled in just before on_begin() is called.
        self.teams = []
        self.players = []
        self._stats: Optional[ba.Stats] = None

        self.lobby = None
        self._prune_dead_objects_timer: Optional[ba.Timer] = None
예제 #12
0
def sharedobj(name: str) -> Any:
    """Return a predefined object for the current Activity, creating if needed.

    Category: Gameplay Functions

    Available values for 'name':

    'globals': returns the 'globals' ba.Node, containing various global
      controls & values.

    'object_material': a ba.Material that should be applied to any small,
      normal, physical objects such as bombs, boxes, players, etc. Other
      materials often check for the  presence of this material as a
      prerequisite for performing certain actions (such as disabling collisions
      between initially-overlapping objects)

    'player_material': a ba.Material to be applied to player parts.  Generally,
      materials related to the process of scoring when reaching a goal, etc
      will look for the presence of this material on things that hit them.

    'pickup_material': a ba.Material; collision shapes used for picking things
      up will have this material applied. To prevent an object from being
      picked up, you can add a material that disables collisions against things
      containing this material.

    'footing_material': anything that can be 'walked on' should have this
      ba.Material applied; generally just terrain and whatnot. A character will
      snap upright whenever touching something with this material so it should
      not be applied to props, etc.

    'attack_material': a ba.Material applied to explosion shapes, punch
      shapes, etc.  An object not wanting to receive impulse/etc messages can
      disable collisions against this material.

    'death_material': a ba.Material that sends a ba.DieMessage() to anything
      that touches it; handy for terrain below a cliff, etc.

    'region_material':  a ba.Material used for non-physical collision shapes
      (regions); collisions can generally be allowed with this material even
      when initially overlapping since it is not physical.

    'railing_material': a ba.Material with a very low friction/stiffness/etc
      that can be applied to invisible 'railings' useful for gently keeping
      characters from falling off of cliffs.
    """
    # pylint: disable=too-many-branches
    from ba._messages import DieMessage

    # We store these on the current context; whether its an activity or
    # session.
    activity: Optional[ba.Activity] = _ba.getactivity(doraise=False)
    if activity is not None:

        # Grab shared-objs dict.
        sharedobjs = getattr(activity, 'sharedobjs', None)

        # Grab item out of it.
        try:
            return sharedobjs[name]
        except Exception:
            pass

        obj: Any

        # Hmm looks like it doesn't yet exist; create it if its a valid value.
        if name == 'globals':
            node_obj = _ba.newnode('globals')
            obj = node_obj
        elif name in [
                'object_material', 'player_material', 'pickup_material',
                'footing_material', 'attack_material'
        ]:
            obj = _ba.Material()
        elif name == 'death_material':
            mat = obj = _ba.Material()
            mat.add_actions(
                ('message', 'their_node', 'at_connect', DieMessage()))
        elif name == 'region_material':
            obj = _ba.Material()
        elif name == 'railing_material':
            mat = obj = _ba.Material()
            mat.add_actions(('modify_part_collision', 'collide', False))
            mat.add_actions(('modify_part_collision', 'stiffness', 0.003))
            mat.add_actions(('modify_part_collision', 'damping', 0.00001))
            mat.add_actions(conditions=('they_have_material',
                                        sharedobj('player_material')),
                            actions=(('modify_part_collision', 'collide',
                                      True), ('modify_part_collision',
                                              'friction', 0.0)))
        else:
            raise ValueError(
                "unrecognized shared object (activity context): '" + name +
                "'")
    else:
        session: Optional[ba.Session] = _ba.getsession(doraise=False)
        if session is not None:

            # Grab shared-objs dict (creating if necessary).
            sharedobjs = session.sharedobjs

            # Grab item out of it.
            obj = sharedobjs.get(name)
            if obj is not None:
                return obj

            # Hmm looks like it doesn't yet exist; create if its a valid value.
            if name == 'globals':
                obj = _ba.newnode('sessionglobals')
            else:
                raise ValueError('unrecognized shared object '
                                 "(session context): '" + name + "'")
        else:
            raise RuntimeError('no current activity or session context')

    # Ok, got a shiny new shared obj; store it for quick access next time.
    sharedobjs[name] = obj
    return obj
예제 #13
0
    def __init__(self, settings: Dict[str, Any]):
        """Creates an Activity in the current ba.Session.

        The activity will not be actually run until ba.Session.set_activity()
        is called. 'settings' should be a dict of key/value pairs specific
        to the activity.

        Activities should preload as much of their media/etc as possible in
        their constructor, but none of it should actually be used until they
        are transitioned in.
        """
        super().__init__()

        # Create our internal engine data.
        self._activity_data = _ba.register_activity(self)

        # Player/Team types should have been specified as type args;
        # grab those.
        self._playertype: Type[PlayerType]
        self._teamtype: Type[TeamType]
        self._setup_player_and_team_types()

        # FIXME: Relocate or remove the need for this stuff.
        self.sharedobjs: Dict[str, Any] = {}
        self.paused_text: Optional[ba.Actor] = None
        self.spaz_respawn_icons_right: Dict[int, RespawnIcon]

        session = _ba.getsession()
        if session is None:
            raise RuntimeError('No current session')
        self._session = weakref.ref(session)

        # Preloaded data for actors, maps, etc; indexed by type.
        self.preloads: Dict[Type, Any] = {}

        if not isinstance(settings, dict):
            raise TypeError('expected dict for settings')
        if _ba.getactivity(doraise=False) is not self:
            raise Exception('invalid context state')

        # Hopefully can eventually kill this; activities should
        # validate/store whatever settings they need at init time
        # (in a more type-safe way).
        self.settings_raw = settings

        self._has_transitioned_in = False
        self._has_begun = False
        self._has_ended = False
        self._should_end_immediately = False
        self._should_end_immediately_results: (
            Optional[ba.TeamGameResults]) = None
        self._should_end_immediately_delay = 0.0
        self._called_activity_on_transition_in = False
        self._called_activity_on_begin = False

        self._activity_death_check_timer: Optional[ba.Timer] = None
        self._expired = False

        # This gets set once another activity has begun transitioning in but
        # before this one is killed. The on_transition_out() method is also
        # called at this time.  Make sure to not assign player inputs,
        # change music, or anything else with global implications once this
        # happens.
        self._transitioning_out = False

        # A handy place to put most actors; this list is pruned of dead
        # actors regularly and these actors are insta-killed as the activity
        # is dying.
        self._actor_refs: List[ba.Actor] = []
        self._actor_weak_refs: List[ReferenceType[ba.Actor]] = []
        self._last_prune_dead_actors_time = _ba.time()
        self._prune_dead_actors_timer: Optional[ba.Timer] = None

        # This stuff gets filled in just before on_begin() is called.
        self.teams = []
        self.players = []
        self.lobby = None
        self._stats: Optional[ba.Stats] = None