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.cast;
 
 import java.awt.Point;
 
 
 
 import static com.threerings.cast.Log.log;

A character sprite is a sprite that animates itself while walking about in a scene.
 
 public class CharacterSprite extends ImageSprite
     implements StandardActions
 {
    
Initializes this character sprite with the specified character descriptor and character manager. It will obtain animation data from the supplied character manager.
 
     public void init (CharacterDescriptor descripCharacterManager charmgr)
     {
         // keep track of this stuff
          = descrip;
          = charmgr;
 
         // sanity check our values
         sanityCheckDescrip();
 
         // assign an arbitrary starting orientation
          = ;
 
         // pass the buck to derived classes
         didInit();
     }

    
Called after this sprite has been initialized with its character descriptor and character manager. Derived classes can do post-init business here.
 
     protected void didInit ()
     {
     }

    
Reconfigures this sprite to use the specified character descriptor.
 
     public void setCharacterDescriptor (CharacterDescriptor descrip)
     {
         // keep the new descriptor
          = descrip;
 
         // sanity check our values
         sanityCheckDescrip();
 
         // update our action frames
         updateActionFrames();
     }

    
Specifies the action to use when the sprite is at rest. The default is STANDING.
 
     public void setRestingAction (String action)
     {
          = action;
     }

    
Returns the action to be used when the sprite is at rest. Derived classes may wish to override this method and vary the action based on external parameters (or randomly).
 
     public String getRestingAction ()
     {
         return ;
     }

    
Specifies the action to use when the sprite is following a path. The default is WALKING.
    public void setFollowingPathAction (String action)
    {
         = action;
    }

    
Returns the action to be used when the sprite is following a path. Derived classes may wish to override this method and vary the action based on external parameters (or randomly).
    public String getFollowingPathAction ()
    {
        return ;
    }

    
Sets the action sequence used when rendering the character, from the set of available sequences.
    public void setActionSequence (String action)
    {
        // sanity check
        if (action == null) {
            .warning("Refusing to set null action sequence " + this + "."new Exception());
            return;
        }
        // no need to noop
        if (action.equals()) {
            return;
        }
         = action;
        updateActionFrames();
    }
    @Override
    public void setOrientation (int orient)
    {
        if (orient < 0 || orient >= ) {
            .info("Refusing to set invalid orientation",
                "sprite"this"orient"orientnew Exception());
            return;
        }
        int oorient = ;
        super.setOrientation(orient);
        if ( != oorient) {
             = null;
        }
    }
    @Override
    public boolean hitTest (int xint y)
    {
        // the irect adjustments are to account for our decorations
        return ( != null && .contains(xy) &&
                .hitTest(x - .y - .));
    }
    @Override
    public void tick (long tickStamp)
    {
        // composite our action frames if something since the last call to
        // tick caused them to become invalid
        compositeActionFrames();
        super.tick(tickStamp);
        // composite our action frames if something during tick() caused them to become invalid
        compositeActionFrames();
    }
    @Override
    public void cancelMove ()
    {
        super.cancelMove();
        halt();
    }
    @Override
    public void pathBeginning ()
    {
        super.pathBeginning();
        // enable walking animation
    }
    @Override
    public void pathCompleted (long timestamp)
    {
        super.pathCompleted(timestamp);
        halt();
    }
    @Override
    public void paint (Graphics2D gfx)
    {
        if ( != null) {
            decorateBehind(gfx);
            // paint the image using _ibounds rather than _bounds which
            // has been modified to include the bounds of our decorations
            .paintFrame(gfx..);
            decorateInFront(gfx);
        } else {
            super.paint(gfx);
        }
    }

    
Called to paint any decorations that should appear behind the character sprite image.
    protected void decorateBehind (Graphics2D gfx)
    {
    }

    
Called to paint any decorations that should appear in front of the character sprite image.
    protected void decorateInFront (Graphics2D gfx)
    {
    }

    
Rebuilds our action frames given our current character descriptor and action sequence. This is called when either of those two things changes.
    protected void updateActionFrames ()
    {
        // get a reference to the action sequence so that we can obtain
        // our animation frames and configure our frames per second
        ActionSequence actseq = .getActionSequence();
        if (actseq == null) {
            String errmsg = "No such action '" +  + "'.";
            throw new IllegalArgumentException(errmsg);
        }
        try {
            // obtain our animation frames for this action sequence
             = .getActionFrames();
            // clear out our frames so that we recomposite on next tick
             = null;
            // update the sprite render attributes
            setFrameRate(actseq.framesPerSecond);
        } catch (NoSuchComponentException nsce) {
            .warning("Character sprite references non-existent component",
                "sprite"this"err"nsce);
        } catch (Exception e) {
            .warning("Failed to obtain action frames",
                "sprite"this"descrip""action"e);
        }
    }

    
Called to recomposite our action frames if needed.
    protected final void compositeActionFrames ()
    {
        if ( == null &&  != null) {
            setFrames(.getFrames());
        }
    }

    
Makes it easier to track down problems with bogus character descriptors.
    protected void sanityCheckDescrip ()
    {
        if (.getComponentIds() == null ||
            .getComponentIds().length == 0) {
            .warning("Invalid character descriptor [sprite=" + this +
                        ", descrip=" +  + "]."new Exception());
        }
    }
    @Override
    protected boolean tickPath (long tickStamp)
    {
        boolean moved = super.tickPath(tickStamp);
        // composite our action frames if our path caused them to become invalid
        compositeActionFrames();
        return moved;
    }
    @Override
    protected void updateRenderOrigin ()
    {
        super.updateRenderOrigin();
        // adjust our image bounds to reflect the new location
        . = . + .;
        . = . + .;
    }
    @Override
    protected void accomodateFrame (int frameIdxint widthint height)
    {
        // this will update our width and height
        super.accomodateFrame(frameIdxwidthheight);
        // we now need to update the render offset for this frame
        if ( == null) {
            .warning("Have no action frames! " +  + ".");
        } else {
             = .getXOrigin(frameIdx);
             = .getYOrigin(frameIdx);
        }
        // and cause those changes to be reflected in our bounds
        updateRenderOrigin();
        // start out with our bounds the same as our image bounds
        .setBounds();
        // now we can call down and incorporate the dimensions of any
        // decorations that will be rendered along with our image
        // compute our new render origin
         =  - .;
         =  - .;
        // track the offset from our expanded bounds to our image bounds
        . = . - .;
        . = . - .;
    }

    
Called by accomodateFrame(int,int,int) to give derived classes an opportunity to incorporate the bounds of any decorations that will be drawn along with this sprite. The _ibounds rectangle will contain the bounds of the image that comprises the undecorated sprite. From that the position and size of decorations can be computed and unioned with the supplied bounds rectangle (most likely by using javax.swing.SwingUtilities.computeUnion(int,int,int,int,java.awt.Rectangle) which applies the union in place rather than creating a new rectangle).
    protected void unionDecorationBounds (Rectangle bounds)
    {
    }

    
Updates the sprite animation frame to reflect the cessation of movement and disables any further animation.
    protected void halt ()
    {
        // only do something if we're actually animating
        if ( != ) {
            // disable animation
            setAnimationMode();
            // come to a halt looking settled and at peace
            String rest = getRestingAction();
            if (rest != null) {
                setActionSequence(rest);
            }
        }
    }

    
The action to use when at rest.
    protected String _restingAction = ;

    
The action to use when following a path.
    protected String _followingPathAction = ;

    
A reference to the descriptor for the character that we're visualizing.
    protected CharacterDescriptor _descrip;

    
A reference to the character manager that created us.
    protected CharacterManager _charmgr;

    
The action we are currently displaying.
    protected String _action;

    
The animation frames for the active action sequence in each orientation.
    protected ActionFrames _aframes;

    
The offset from the upper-left of the total sprite bounds to the upper-left of the image within those bounds.
    protected Point _ioff = new Point();

    
The bounds of the current sprite image.
    protected Rectangle _ibounds = new Rectangle();
New to GrepCode? Check out our FAQ X