class SineMotion(Animation): # takes the max distance from center - amplitude # and number of frames it takes to complete one half period - half_period def __init__(self, actor, amplitude, half_period=10): Animation.__init__(self, actor, permanent=False) self.vector = Vector(0, 0) self.amplitude = amplitude self.tick = 0 self.half_period = float(half_period) self.period = half_period * 2 * pi def update(self): t = self.tick / self.half_period y = self.amplitude * sin(t) self.vector.set(0, y) self.tick += 1 if self.tick > self.period: self.wave_complete() def update_vector(self, vector): vector.add(self.vector) def wave_complete(self): self.end()
class HitRumble(Animation): def __init__(self, actor, heavy=False): Animation.__init__(self, actor, permanent=False) self.vector = Vector(0, 0) self.tick = 0 self.duration = 12 if heavy: self.duration += 10 self.x_shake = choice((1, -1)) self.y_shake = choice((1, -1)) def update(self): if self.tick/3 % 2 == 0: self.vector.set(self.x_shake, self.y_shake) else: self.vector.set(-self.x_shake, -self.y_shake) self.tick += 1 if self.tick > self.duration: self.is_complete() def update_vector(self, vector): vector.add(self.vector)
class Transform(object): def __init__(self, node, (x, y)): self.node = node self.base_position = Vector(x, y) self.position = Vector(x, y) self.animations = AnimationGroup()
def screen_pos_vector(self): if self.node.parent: return Vector( self.position.get_add_tuple( self.node.parent.transform.screen_pos_vector())) else: return Vector(self.position.get_tuple_int())
def relative_pos_vector(self, node): if node is self.node.parent: return Vector(self.position.get_tuple_int()) else: return Vector( self.position.get_add_tuple( self.node.parent.transform.screen_pos_vector()))
def __init__(self, actor, amplitude, half_period=10): Animation.__init__(self, actor, permanent=False) self.vector = Vector(0, 0) self.amplitude = amplitude self.tick = 0 self.half_period = float(half_period) self.period = half_period * 2 * pi
def __init__(self, actor, heavy=False): Animation.__init__(self, actor, permanent=False) self.vector = Vector(0, 0) self.tick = 0 self.duration = 12 if heavy: self.duration += 10 self.x_shake = choice((1, -1)) self.y_shake = choice((1, -1))
def load_vertices(self, card_width, card_height, x_margin, top_y_margin, bot_y_margin): cw = card_width / 2 ch = card_height / 2 vertices = { 'a': Vector(-cw, -ch + top_y_margin), 'b': Vector(-cw + x_margin, -ch), 'c': Vector(cw - x_margin, -ch), 'd': Vector(cw, -ch + top_y_margin), 'e': Vector(cw, ch - bot_y_margin), 'f': Vector(cw - x_margin, ch), 'g': Vector(-cw + x_margin, ch), 'h': Vector(-cw, ch - bot_y_margin), } inner_vertices = {} for v in vertices: v2 = Vector(vertices[v].get_mult_tuple(.95)) v2_key = ''.join((v, '_inner')) inner_vertices[v2_key] = v2 vertices.update(inner_vertices) return vertices
def __init__(self, actor, peak, duration): Animation.__init__(self, actor, permanent=False) self.duration = duration peak = -float(peak) duration = float(duration) - 1 self.a = self.get_a(peak, duration) self.func = self.get_func(duration) self.vector = Vector() self.t = 0
def __init__(self, actor, start, dest, time, snap=False): Animation.__init__(self, actor, permanent=False) self.position = Vector(0, 0) self.vector = Vector(dest) self.vector.sub(Vector(start)) self.vector.div(time) self.tick = 0 self.time = time self.snap = snap
def __init__(self, color): vertices = { 'a': Vector(0, -5), 'b': Vector(2.7, 1), 'd': Vector(-2.7, 1), 'c': Vector(0, 2) } components = [Polygon(self, ('a', 'b', 'c', 'd'), color, width=2)] VectorModel.__init__(self, vertices, components)
def __init__(self, actor, radius, duration, start_angle=0, clockwise=True): Animation.__init__(self, actor, permanent=False) self.position = Vector(radius, 0) self.angle = float(start_angle) self.radius = radius self.duration = duration self.incr = 360.0 / self.duration if not clockwise: self.incr *= -1
def make_disc(self, n, m, layer, radius): vertices = {} components = [] seq = [] scale, disc_y = self.get_disc_pos(radius, layer, m) face_angle = 360.0 / n for i in range(n): angle = i * face_angle v = Vector.from_angle(angle, scale) x = v.x y = disc_y z = v.y disc_id = (i, layer) vertices[disc_id] = Vector3(x, y, z) seq.append(disc_id) components.append(LineSequence(self, seq, self.color_id, closed=True)) return vertices, components
class Lerp(Animation): # takes in destination position, and smoothly transitions to it then stops def __init__(self, actor, start, dest, time, snap=False): Animation.__init__(self, actor, permanent=False) self.position = Vector(0, 0) self.vector = Vector(dest) self.vector.sub(Vector(start)) self.vector.div(time) self.tick = 0 self.time = time self.snap = snap def update(self): self.position.add(self.vector) self.tick += 1 if self.snap and self.tick == self.time: self.permanent = True if self.tick > self.time: self.end() def update_vector(self, vector): vector.add(self.position)
class QuadraticHop(Animation): def __init__(self, actor, peak, duration): Animation.__init__(self, actor, permanent=False) self.duration = duration peak = -float(peak) duration = float(duration) - 1 self.a = self.get_a(peak, duration) self.func = self.get_func(duration) self.vector = Vector() self.t = 0 def get_a(self, peak, duration): d = duration / 2 * -(duration / 2) return peak / d def get_func(self, duration): def func(x): return self.a * x * (x - duration) return func def update(self): self.vector.set(0, self.func(self.t)) self.t += 1 if self.t > self.duration: self.end() def update_vector(self, vector): vector.add(self.vector)
class Orbit(Animation): # takes in radius of rotation, start angle, speed def __init__(self, actor, radius, duration, start_angle=0, clockwise=True): Animation.__init__(self, actor, permanent=False) self.position = Vector(radius, 0) self.angle = float(start_angle) self.radius = radius self.duration = duration self.incr = 360.0 / self.duration if not clockwise: self.incr *= -1 def update(self): self.position.set_at_angle(self.angle) self.angle += self.incr def update_vector(self, vector): vector.add(self.position)
def __init__(self, actor, vec): Animation.__init__(self, actor) self.vector = Vector(vec)
class Particle(object): def __init__(self, group, (x, y)): self.coord = Vector(x, y) self.group = group self.group.add(self)
def get_disc_pos(self, diameter, layer, m): face_angle = 360.0 / (m * 2) disc_angle = face_angle * layer v = Vector.from_angle(disc_angle, diameter) return v.x, v.y