Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  //
  // Nenya library - tools for developing networked games
  // Copyright (C) 2002-2012 Three Rings Design, Inc., All Rights Reserved
  // https://github.com/threerings/nenya
  //
  // This library is free software; you can redistribute it and/or modify it
  // under the terms of the GNU Lesser General Public License as published
  // by the Free Software Foundation; either version 2.1 of the License, or
  // (at your option) any later version.
 //
 // This library is distributed in the hope that it will be useful,
 // but WITHOUT ANY WARRANTY; without even the implied warranty of
 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 // Lesser General Public License for more details.
 //
 // You should have received a copy of the GNU Lesser General Public
 // License along with this library; if not, write to the Free Software
 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
 package com.threerings.media.animation;
 
 import java.awt.Color;
 import java.awt.Shape;
 
 
An animation that displays an object exploding into chunks, fading out as they fly apart. The animation ends when all chunks have exited the animation bounds, or when the given delay time (if any is specified) has elapsed.
 
 public class ExplodeAnimation extends Animation
 {
    
A class that describes an explosion's attributes.
 
     public static class ExplodeInfo
     {
        
The bounds within which to animate.
 
         public Rectangle bounds;

        
The number of image chunks on each axis.
 
         public int xchunkychunk;

        
The maximum chunk velocity on each axis in pixels per millisecond.
 
         public float xvelyvel;

        
The y-axis chunk acceleration in pixels per millisecond.
 
         public float yacc;

        
The chunk rotational velocity in rotations per millisecond.
 
         public float rvel;

        
The animation length in milliseconds, or -1 if the animation should continue until all pieces are outside the bounds.
 
         public long delay;
 
         @Override
         public String toString () {
             return StringUtil.fieldsToString(this);
         }
     }

    
Constructs an explode animation with the chunks represented as filled rectangles of the specified color.

Parameters:
color the color to render the chunks in.
info the explode info object.
x the x-position of the object.
y the y-position of the object.
width the width of the object.
height the height of the object.
 
     public ExplodeAnimation (Color colorExplodeInfo infoint xint yint widthint height)
     {
         super(info.bounds);
 
          = color;
         init(infoxywidthheight);
     }

    
Constructs an explode animation with the chunks represented as portions of the actual image.

Parameters:
image the image to animate.
info the explode info object.
x the x-position of the object.
y the y-position of the object.
width the width of the object.
height the height of the object.
    public ExplodeAnimation (Mirage imageExplodeInfo infoint xint yint widthint height)
    {
        super(info.bounds);
         = image;
        init(infoxywidthheight);
    }

    
Initializes the animation with the attributes of the given explode info object.
    protected void init (ExplodeInfo infoint xint yint widthint height)
    {
         = info;
         = x;
         = y;
         = width;
         = height;
        . = (float)((2.0f * .) * .);
         = (. * .);
         = new int[];
         = new int[];
         = new float[];
         = new float[];
        // determine chunk dimensions
         =  / .;
         =  / .;
         =  / 2;
         =  / 2;
        // initialize all chunks
        for (int ii = 0; ii < ii++) {
            // initialize chunk position
            int xpos = ii % .;
            int ypos = ii / .;
            [ii] =  + (xpos * );
            [ii] =  + (ypos * );
            // initialize chunk velocity
            [ii] = RandomUtil.getFloat(.) *
                ((xpos < (. / 2)) ? -1.0f : 1.0f);
            [ii] = -(RandomUtil.getFloat(.));
        }
        // initialize the chunk rotation angle
         = 0.0f;
    }
    @Override
    public void fastForward (long timeDelta)
    {
        if ( > 0) {
             += timeDelta;
             += timeDelta;
        }
    }
    @Override
    public void tick (long timestamp)
    {
        if ( == 0) {
            // initialize our starting time
             = timestamp;
            if (. != -1) {
                 =  + .;
            }
        }
        // figure out the distance the chunks have traveled
        long msecs = timestamp - ;
        if (. != -1) {
            // calculate the alpha level with which to render the chunks
            float pctdone = msecs / (float).;
             = Math.max(0.1f, Math.min(1.0f, 1.0f - pctdone));
        }
        // move all chunks and check whether any remain to be animated
        int inside = 0;
        for (int ii = 0; ii < ii++) {
            // determine the chunk travel distance
            int xtrav = (int)([ii] * msecs);
            int ytrav = (int)
                (([ii] * msecs) + (0.5f * . * (msecs * msecs)));
            // determine the chunk movement direction
            int xpos = ii % .;
            int ypos = ii / .;
            // update the chunk position
            [ii] =  + (xpos * ) + xtrav;
            [ii] =  + (ypos * ) + ytrav;
            // note whether this chunk is still within our bounds
            .setBounds([ii], [ii], );
            if (.intersects()) {
                inside++;
            }
        }
        // increment the rotation angle
         += .;
        // note whether we're finished
         = (inside == 0) || (. != -1 && timestamp >= );
        // dirty ourselves
        invalidate();
    }
    @Override
    public void paint (Graphics2D gfx)
    {
        Shape oclip = gfx.getClip();
        Composite ocomp = null;
        if (. != -1) {
            // set the alpha composite to reflect the current fade-out
            ocomp = gfx.getComposite();
            gfx.setComposite(AlphaComposite.getInstance(.));
        }
        for (int ii = 0; ii < ii++) {
            // get the chunk position within the image
            int xpos = ii % .;
            int ypos = ii / .;
            // calculate image chunk offset
            int xoff = -(xpos * );
            int yoff = -(ypos * );
            // translate the origin to center on the chunk
            int tx = [ii] + ty = [ii] + ;
            gfx.translate(txty);
            // set up the desired rotation
            gfx.rotate();
            if ( != null) {
                // draw the image chunk
                gfx.clipRect(-, -);
                .paint(gfx, - + xoff, - + yoff);
            } else {
                // draw the color chunk
                gfx.setColor();
                gfx.fillRect(-, -);
            }
            // restore the original transform and clip
            gfx.rotate(-);
            gfx.translate(-tx, -ty);
            gfx.setClip(oclip);
        }
        if (. != -1) {
            // restore the original composite
            gfx.setComposite(ocomp);
        }
    }
    @Override
    protected void toString (StringBuilder buf)
    {
        super.toString(buf);
        buf.append(", ox=").append();
        buf.append(", oy=").append();
        buf.append(", cwid=").append();
        buf.append(", chei=").append();
        buf.append(", hcwid=").append();
        buf.append(", hchei=").append();
        buf.append(", chunkcount=").append();
        buf.append(", info=").append();
    }

    
The current chunk rotation.
    protected float _angle;

    
The starting x-axis velocity of each chunk.
    protected float[] _sxvel;

    
The starting y-axis velocity of each chunk.
    protected float[] _syvel;

    
The current x-axis position of each chunk.
    protected int[] _cxpos;

    
The current y-axis position of each chunk.
    protected int[] _cypos;

    
The individual chunk dimensions in pixels.
    protected int _cwid_chei;

    
The individual chunk dimensions in pixels, halved for handy use in repeated calculations.
    protected int _hcwid_hchei;

    
The total number of image chunks.
    protected int _chunkcount;

    
The explode info.
    protected ExplodeInfo _info;

    
The exploding object position and dimensions.
    protected int _ox_oy_owid_ohei;

    
The color to render the object chunks in if we're using a color.
    protected Color _color;

    
The image to animate if we're using an image.
    protected Mirage _image;

    
The starting animation time.
    protected long _start;

    
The ending animation time.
    protected long _end;

    
The percent alpha with which to render the chunks.
    protected float _alpha;

    
A reusable working rectangle.
    protected Rectangle _wrect = new Rectangle();
New to GrepCode? Check out our FAQ X