Allow rendering explosion chunks with either image portions or rectangles

of a specified color.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@879 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Walter Korman
2002-01-19 06:06:05 +00:00
parent a0a1a48057
commit 5366ae4ca3
@@ -1,5 +1,5 @@
// //
// $Id: ExplodeAnimation.java,v 1.4 2002/01/15 18:10:25 shaper Exp $ // $Id: ExplodeAnimation.java,v 1.5 2002/01/19 06:06:05 shaper Exp $
package com.threerings.media.animation; package com.threerings.media.animation;
@@ -13,62 +13,108 @@ import com.threerings.media.Log;
import com.threerings.media.util.RandomUtil; import com.threerings.media.util.RandomUtil;
/** /**
* An animation that displays an image exploding into chunks. The * An animation that displays an object exploding into chunks. The
* animation ends when all image chunks have exited the animation bounds. * animation ends when all chunks have exited the animation bounds.
*/ */
public class ExplodeAnimation extends Animation public class ExplodeAnimation extends Animation
{ {
/** /**
* Constructs an explode 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 xchunk, ychunk;
/** The maximum chunk velocity on each axis in pixels per
* millisecond. */
public float xvel, yvel;
/** The y-axis chunk acceleration in pixels per millisecond. */
public float yacc;
/** The chunk rotational velocity in rotations per millisecond. */
public float rvel;
}
/**
* Constructs an explode animation with the chunks represented as
* filled rectangles of the specified color.
* *
* @param bounds the bounds within which to animate. * @param color the color to render the chunks in.
* @param image the image to animate. * @param info the explode info object.
* @param xchunk the number of image chunks on the x-axis. * @param x the x-position of the object.
* @param ychunk the number of image chunks on the y-axis. * @param y the y-position of the object.
* @param sx the starting x-position. * @param width the width of the object.
* @param sy the starting y-position. * @param height the height of the object.
* @param xvel the maximum x-axis chunk velocity in pixels per millisecond.
* @param yvel the maximum y-axis chunk velocity in pixels per millisecond.
* @param yacc the y-axis chunk acceleration in pixels per millisecond.
* @param rvel the chunk rotation velocity in rotations per millisecond.
*/ */
public ExplodeAnimation ( public ExplodeAnimation (
Rectangle bounds, Image image, int xchunk, int ychunk, Color color, ExplodeInfo info, int x, int y, int width, int height)
int sx, int sy, float xvel, float yvel, float yacc, float rvel)
{ {
super(bounds); super(info.bounds);
_color = color;
init(info, x, y, width, height);
}
/**
* Constructs an explode animation with the chunks represented as
* portions of the actual image.
*
* @param image the image to animate.
* @param info the explode info object.
* @param x the x-position of the object.
* @param y the y-position of the object.
* @param width the width of the object.
* @param height the height of the object.
*/
public ExplodeAnimation (
Image image, ExplodeInfo info, int x, int y, int width, int height)
{
super(info.bounds);
// save things off
_image = image; _image = image;
_xchunk = xchunk; init(info, x, y, width, height);
_ychunk = ychunk; }
_sx = sx;
_sy = sy;
_yacc = yacc;
_rvel = (float)((2.0f * Math.PI) * rvel); /**
_chunkcount = (_xchunk * _ychunk); * Initializes the animation with the attributes of the given explode
* info object.
*/
protected void init (ExplodeInfo info, int x, int y, int width, int height)
{
_info = info;
_ox = x;
_oy = y;
_owid = width;
_ohei = height;
_info.rvel = (float)((2.0f * Math.PI) * _info.rvel);
_chunkcount = (_info.xchunk * _info.ychunk);
_cpos = new int[_chunkcount]; _cpos = new int[_chunkcount];
_sxvel = new float[_chunkcount]; _sxvel = new float[_chunkcount];
_syvel = new float[_chunkcount]; _syvel = new float[_chunkcount];
// determine chunk dimensions // determine chunk dimensions
_cwid = image.getWidth(null) / _xchunk; _cwid = _owid / _info.xchunk;
_chei = image.getHeight(null) / _ychunk; _chei = _ohei / _info.ychunk;
_hcwid = _cwid / 2; _hcwid = _cwid / 2;
_hchei = _chei / 2; _hchei = _chei / 2;
// initialize all chunks // initialize all chunks
for (int ii = 0; ii < _chunkcount; ii++) { for (int ii = 0; ii < _chunkcount; ii++) {
// initialize chunk position // initialize chunk position
int xpos = ii % _xchunk; int xpos = ii % _info.xchunk;
int ypos = ii / _xchunk; int ypos = ii / _info.xchunk;
_cpos[ii] = ((sx + (xpos * _cwid)) << 16 | (sy + (ypos * _chei))); _cpos[ii] = ((_ox + (xpos * _cwid)) << 16 |
(_oy + (ypos * _chei)));
// initialize chunk velocity // initialize chunk velocity
_sxvel[ii] = RandomUtil.getFloat(xvel) * _sxvel[ii] = RandomUtil.getFloat(_info.xvel) *
((xpos < (_xchunk / 2)) ? -1.0f : 1.0f); ((xpos < (_info.xchunk / 2)) ? -1.0f : 1.0f);
_syvel[ii] = -(RandomUtil.getFloat(yvel)); _syvel[ii] = -(RandomUtil.getFloat(_info.yvel));
} }
// initialize the chunk rotation angle // initialize the chunk rotation angle
@@ -98,16 +144,16 @@ public class ExplodeAnimation extends Animation
// determine the chunk travel distance // determine the chunk travel distance
int xtrav = (int)(_sxvel[ii] * msecs); int xtrav = (int)(_sxvel[ii] * msecs);
int ytrav = int ytrav = (int)((_syvel[ii] * msecs) +
(int)((_syvel[ii] * msecs) + (0.5f * _yacc * (msecs * msecs))); (0.5f * _info.yacc * (msecs * msecs)));
// determine the chunk movement direction // determine the chunk movement direction
int xpos = ii % _xchunk; int xpos = ii % _info.xchunk;
int ypos = ii / _xchunk; int ypos = ii / _info.xchunk;
// update the chunk position // update the chunk position
x = _sx + (xpos * _cwid) + xtrav; x = _ox + (xpos * _cwid) + xtrav;
y = _sy + (ypos * _chei) + ytrav; y = _oy + (ypos * _chei) + ytrav;
_cpos[ii] = (x << 16 | y); _cpos[ii] = (x << 16 | y);
// note whether this chunk is still within our bounds // note whether this chunk is still within our bounds
@@ -117,7 +163,7 @@ public class ExplodeAnimation extends Animation
} }
// increment the rotation angle // increment the rotation angle
_angle += _rvel; _angle += _info.rvel;
_finished = (inside == 0); _finished = (inside == 0);
invalidate(); invalidate();
@@ -132,8 +178,8 @@ public class ExplodeAnimation extends Animation
int y = _cpos[ii] & 0xFFFF; int y = _cpos[ii] & 0xFFFF;
// get the chunk position within the image // get the chunk position within the image
int xpos = ii % _xchunk; int xpos = ii % _info.xchunk;
int ypos = ii / _xchunk; int ypos = ii / _info.xchunk;
// calculate image chunk offset // calculate image chunk offset
int xoff = -(xpos * _cwid); int xoff = -(xpos * _cwid);
@@ -149,10 +195,17 @@ public class ExplodeAnimation extends Animation
// set up the desired rotation // set up the desired rotation
gfx.rotate(_angle); gfx.rotate(_angle);
// draw the image chunk if (_image != null) {
gfx.clipRect(-_hcwid, -_hchei, _cwid, _chei); // draw the image chunk
gfx.drawImage(_image, -_hcwid + xoff, -_hchei + yoff, null); gfx.clipRect(-_hcwid, -_hchei, _cwid, _chei);
gfx.setClip(oclip); gfx.drawImage(_image, -_hcwid + xoff, -_hchei + yoff, null);
gfx.setClip(oclip);
} else {
// draw the color chunk
gfx.setColor(_color);
gfx.fillRect(-_hcwid, -_hchei, _cwid, _chei);
}
// restore the original transform // restore the original transform
gfx.rotate(-_angle); gfx.rotate(-_angle);
@@ -165,8 +218,8 @@ public class ExplodeAnimation extends Animation
{ {
super.toString(buf); super.toString(buf);
buf.append(", sx=").append(_sx); buf.append(", sx=").append(_ox);
buf.append(", sy=").append(_sy); buf.append(", sy=").append(_oy);
} }
/** The current chunk rotation. */ /** The current chunk rotation. */
@@ -178,9 +231,6 @@ public class ExplodeAnimation extends Animation
/** The starting y-axis velocity of each chunk. */ /** The starting y-axis velocity of each chunk. */
protected float[] _syvel; protected float[] _syvel;
/** The y-axis acceleration in pixels per millisecond. */
protected float _yacc;
/** The current position of each chunk. */ /** The current position of each chunk. */
protected int[] _cpos; protected int[] _cpos;
@@ -191,19 +241,19 @@ public class ExplodeAnimation extends Animation
* repeated calculations. */ * repeated calculations. */
protected int _hcwid, _hchei; protected int _hcwid, _hchei;
/** The chunk rotational velocity in radians. */
protected float _rvel;
/** The starting position. */
protected int _sx, _sy;
/** The total number of image chunks. */ /** The total number of image chunks. */
protected int _chunkcount; protected int _chunkcount;
/** The number of image chunks on each axis. */ /** The explode info. */
protected int _xchunk, _ychunk; protected ExplodeInfo _info;
/** The image to animate. */ /** 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 Image _image; protected Image _image;
/** The starting animation time. */ /** The starting animation time. */