<commit_before>#!/usr/bin/python2
# animations.py
# nroberts 04/16/2017

from __future__ import division
from bridge import Bridge
from colors import Colors

BALL_PRIORITY = 3
PLAYER_PRIORITY = 4
SCORE_PRIORITY = 5
ANIMATION_PRIORITY = 6

# 1 if positive, -1 if negative
def sign(x):
    return 1 if x > 0 else -1

# Common abstract class for objects with location, color, starting point, and velocity
class Animation(object):
    def __init__(self, origin, color, velocity, **kwargs):
        self.color = color
        self.origin = origin
        self.x = origin # current sequence
        self.velocity = velocity
        self.is_active = False
        self.init(**kwargs)

    def init(self):
        """ Override me! """
        raise NotImplementedError

    def update(self):
        """ Override me! """
        raise NotImplementedError

    def render(self, bridge, fade_frames):
        if self.is_active:
            seq = self.get_seq()
            prev_seq = int(self.x - self.velocity)

            # if not moving, just light up current panel
            if self.velocity == 0:
                bridge.set_fade(seq, self.color, fade_frames, self.priority)
            else:
                # also light up the panels that were skipped in last velocity update
                for i in xrange(seq, prev_seq, -sign(self.velocity)):
                    bridge.set_fade(i, self.color, fade_frames, self.priority)

    # returns current sequence
    def get_seq(self):
        return int(self.x)

# Bars for displaying score to each other
class ScoreBars(Animation):
    def init(self, end_seq):
        self.end_x = end_seq
        self.is_active = True
        self.priority = SCORE_PRIORITY

    def update(self):
        self.x += self.velocity

        # it has reached its spot, so stop it
        if ((self.velocity < 0 and self.x <= self.end_x)
         or (self.velocity > 0 and self.x >= self.end_x)):
            self.x = self.end_x
            self.velocity = 0
            self.frames_till_end = 80 # frames to hang suspended

        # run until dead
        if self.velocity == 0:
            # die
            if self.frames_till_end == 0:
                self.is_active = False

            self.frames_till_end -= 1

# start game flashing where to stand
class StartAnimation(object):
    FADE_TIME = 40 # 1 second
    def __init__(self, start, end, color=Colors.WHITE):
        self.fade_level = 0
        self.start_seq = start
        self.end_seq = end
        self.fading_in = True
        self.color = color
        self.is_active = True # need this to be an animation

    def update(self, color=None):
        # update color if set
        if color: self.color = color

        if self.fading_in: self.fade_level += 1
        else:              self.fade_level -= 1

        # switch direction of fade
        if self.fade_level == StartAnimation.FADE_TIME:
            self.fading_in = False
        if self.fade_level == 0:
            self.fading_in = True

    def render(self, bridge):
        for i in xrange(self.start_seq, self.end_seq+1): #inclusive
            frac = self.fade_level / StartAnimation.FADE_TIME # python3 division
            # fade color appropriately
            color = tuple(map(lambda x: frac * x, self.color))
            bridge.set_fade(i, color, 1, ANIMATION_PRIORITY) # 1 frame

# Class for the pulse animation after someone scores
# Also displays score for each player
class ScoreAnimation(object):

    # distance between consecutive score panels
    SEPARATION = 3 # sequences
    VELOCITY = 4 # sequences per frame for score bars
    FADE_FRAMES = 10 # how long it takes to fade

    def __init__(self, p1, p2, color):
        self.p1 = p1
        self.p2 = p2
        self.color = color

        # start animation immediately
        self.state = "pulse" # pulse is where we make the whole bridge the same color
        self.pulse_frames = 60
        self.is_active = True

    def update(self):
        if self.pulse_frames > 0:
            self.pulse_frames -= 1

    def make_scorebar_animations(self):
        animations = []
        p1, p2 = self.p1, self.p2
        p1_offset = p1.origin + p1.max_swing - 4 * ScoreAnimation.SEPARATION
        p2_offset = p2.origin - p2.max_swing - 4 * ScoreAnimation.SEPARATION

        # constants used when creating animations
        v = ScoreAnimation.VELOCITY
        hi = Bridge.SEQ_HI
        lo = Bridge.SEQ_LO

        # make animations for p1 score
        for i in xrange(0, self.p1.score):
            # origin is +/- 1 so both the p1 and p2 scores display
            s1 = ScoreBars(origin=hi-1, end_seq=p1_offset, color=self.p1.color, velocity=-v)
            s2 = ScoreBars(origin=lo+1, end_seq=p2_offset, color=self.p1.color, velocity=v)
            animations.append(s1)
            animations.append(s2)
            p1_offset += ScoreAnimation.SEPARATION
            p2_offset -= ScoreAnimation.SEPARATION

        # make animations for p2 score
        for i in xrange(0, self.p2.score):
            s1 = ScoreBars(origin=hi, end_seq=p1_offset, color=self.p2.color, velocity=-v)
            s2 = ScoreBars(origin=lo, end_seq=p2_offset, color=self.p2.color, velocity=v)
            animations.append(s1)
            animations.append(s2)
            p1_offset += ScoreAnimation.SEPARATION
            p2_offset -= ScoreAnimation.SEPARATION
<commit_msg>Tweaks how far animations go<commit_after>#!/usr/bin/python2
# animations.py
# nroberts 04/16/2017

from __future__ import division
from bridge import Bridge
from colors import Colors

BALL_PRIORITY = 3
PLAYER_PRIORITY = 4
SCORE_PRIORITY = 5
ANIMATION_PRIORITY = 6

# 1 if positive, -1 if negative
def sign(x):
    return 1 if x > 0 else -1

# Common abstract class for objects with location, color, starting point, and velocity
class Animation(object):
    def __init__(self, origin, color, velocity, **kwargs):
        self.color = color
        self.origin = origin
        self.x = origin # current sequence
        self.velocity = velocity
        self.is_active = False
        self.init(**kwargs)

    def init(self):
        """ Override me! """
        raise NotImplementedError

    def update(self):
        """ Override me! """
        raise NotImplementedError

    def render(self, bridge, fade_frames):
        if self.is_active:
            seq = self.get_seq()
            prev_seq = int(self.x - self.velocity)

            # if not moving, just light up current panel
            if self.velocity == 0:
                bridge.set_fade(seq, self.color, fade_frames, self.priority)
            else:
                # also light up the panels that were skipped in last velocity update
                for i in xrange(seq, prev_seq, -sign(self.velocity)):
                    bridge.set_fade(i, self.color, fade_frames, self.priority)

    # returns current sequence
    def get_seq(self):
        return int(self.x)

# Bars for displaying score to each other
class ScoreBars(Animation):
    def init(self, end_seq):
        self.end_x = end_seq
        self.is_active = True
        self.priority = SCORE_PRIORITY

    def update(self):
        self.x += self.velocity

        # it has reached its spot, so stop it
        if ((self.velocity < 0 and self.x <= self.end_x)
         or (self.velocity > 0 and self.x >= self.end_x)):
            self.x = self.end_x
            self.velocity = 0
            self.frames_till_end = 80 # frames to hang suspended

        # run until dead
        if self.velocity == 0:
            # die
            if self.frames_till_end == 0:
                self.is_active = False

            self.frames_till_end -= 1

# start game flashing where to stand
class StartAnimation(object):
    FADE_TIME = 40 # 1 second
    def __init__(self, start, end, color=Colors.WHITE):
        self.fade_level = 0
        self.start_seq = start
        self.end_seq = end
        self.fading_in = True
        self.color = color
        self.is_active = True # need this to be an animation

    def update(self, color=None):
        # update color if set
        if color: self.color = color

        if self.fading_in: self.fade_level += 1
        else:              self.fade_level -= 1

        # switch direction of fade
        if self.fade_level == StartAnimation.FADE_TIME:
            self.fading_in = False
        if self.fade_level == 0:
            self.fading_in = True

    def render(self, bridge):
        for i in xrange(self.start_seq, self.end_seq+1): #inclusive
            frac = self.fade_level / StartAnimation.FADE_TIME # python3 division
            # fade color appropriately
            color = tuple(map(lambda x: frac * x, self.color))
            bridge.set_fade(i, color, 1, ANIMATION_PRIORITY) # 1 frame

# Class for the pulse animation after someone scores
# Also displays score for each player
class ScoreAnimation(object):

    # distance between consecutive score panels
    SEPARATION = 3 # sequences
    VELOCITY = 4 # sequences per frame for score bars
    FADE_FRAMES = 10 # how long it takes to fade

    def __init__(self, p1, p2, color):
        self.p1 = p1
        self.p2 = p2
        self.color = color

        # start animation immediately
        self.state = "pulse" # pulse is where we make the whole bridge the same color
        self.pulse_frames = 60
        self.is_active = True

    def update(self):
        if self.pulse_frames > 0:
            self.pulse_frames -= 1

    def make_scorebar_animations(self):
        animations = []
        p1, p2 = self.p1, self.p2
        p1_offset = p1.origin + p1.max_swing - 8 * ScoreAnimation.SEPARATION
        p2_offset = p2.origin - p2.max_swing - 8 * ScoreAnimation.SEPARATION

        # constants used when creating animations
        v = ScoreAnimation.VELOCITY
        hi = Bridge.SEQ_HI
        lo = Bridge.SEQ_LO

        # make animations for p1 score
        for i in xrange(0, self.p1.score):
            # origin is +/- 1 so both the p1 and p2 scores display
            s1 = ScoreBars(origin=hi-1, end_seq=p1_offset, color=self.p1.color, velocity=-v)
            s2 = ScoreBars(origin=lo+1, end_seq=p2_offset, color=self.p1.color, velocity=v)
            animations.append(s1)
            animations.append(s2)
            p1_offset += ScoreAnimation.SEPARATION
            p2_offset -= ScoreAnimation.SEPARATION

        # make animations for p2 score
        for i in xrange(0, self.p2.score):
            s1 = ScoreBars(origin=hi, end_seq=p1_offset, color=self.p2.color, velocity=-v)
            s2 = ScoreBars(origin=lo, end_seq=p2_offset, color=self.p2.color, velocity=v)
            animations.append(s1)
            animations.append(s2)
            p1_offset += ScoreAnimation.SEPARATION
            p2_offset -= ScoreAnimation.SEPARATION

{'fraction_non_alphanumeric': 0.08548910523875754, 'fraction_numerical': 0.01761706073249884, 'mean_word_length': 2.112842712842713, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4374212', 'n_tokens_mistral': 3398, 'n_tokens_neox': 3186, 'n_words': 946}