001         package com.croftsoft.apps.zombie;
002    
003         import java.awt.Component;
004         import java.awt.Graphics;
005         import java.awt.Graphics2D;
006         import java.awt.Rectangle;
007         import java.util.Random;
008         import javax.swing.Icon;
009         import javax.swing.JComponent;
010    
011         import com.croftsoft.core.lang.NullArgumentException;
012         import com.croftsoft.core.animation.ComponentUpdater;
013         import com.croftsoft.core.animation.sprite.IconSprite;
014         import com.croftsoft.core.animation.updater.NullComponentUpdater;
015    
016         /*********************************************************************
017         * An IconSprite subclass representing a zombie.
018         *
019         * @version
020         *   2003-07-11
021         * @since
022         *   2002-03-15
023         * @author
024         *   <a href="https://www.croftsoft.com/">David Wallace Croft</a>
025         *********************************************************************/
026    
027         public class  ZombieSprite
028           extends IconSprite
029         //////////////////////////////////////////////////////////////////////
030         //////////////////////////////////////////////////////////////////////
031         {
032    
033         private final ZombieReanimator  zombieReanimator;
034    
035         private final Rectangle         repaintBounds;
036    
037         //
038    
039         private boolean  hit;
040    
041         private boolean  destroyed;
042    
043         //////////////////////////////////////////////////////////////////////
044         // constructor methods
045         //////////////////////////////////////////////////////////////////////
046    
047    
048         public  ZombieSprite (
049           double            x,
050           double            y,
051           double            z,
052           double            heading,
053           double            velocity,
054           Icon              icon,
055           ZombieReanimator  zombieReanimator )
056         //////////////////////////////////////////////////////////////////////
057         {
058           super ( x, y, z, heading, velocity,
059             NullComponentUpdater.INSTANCE, icon );
060    
061           NullArgumentException.check (
062             this.zombieReanimator = zombieReanimator );
063    
064           repaintBounds = new Rectangle ( );
065         }
066    
067         public  ZombieSprite (
068           Icon              icon,
069           ZombieReanimator  zombieReanimator )
070         //////////////////////////////////////////////////////////////////////
071         {
072           this ( 0.0, 0.0, 0.0, 0.0, 0.0, icon, zombieReanimator );
073         }
074    
075         //////////////////////////////////////////////////////////////////////
076         // accessor methods
077         //////////////////////////////////////////////////////////////////////
078    
079         public boolean  isDestroyed ( ) { return destroyed; }
080    
081         //////////////////////////////////////////////////////////////////////
082         // mutator methods
083         //////////////////////////////////////////////////////////////////////
084    
085         public void  setHit ( )
086         //////////////////////////////////////////////////////////////////////
087         {
088           hit = true;
089         }
090    
091         public void  reset ( )
092         //////////////////////////////////////////////////////////////////////
093         {
094           Random  random = zombieReanimator.getRandom ( );
095    
096           setX ( random.nextInt (
097             zombieReanimator.getComponentWidth  ( ) + 1 ) );
098    
099           setY ( random.nextInt (
100             zombieReanimator.getComponentHeight ( ) + 1 ) );
101    
102           setHeading ( random.nextDouble ( ) * 2.0 * Math.PI );
103    
104           setVelocity ( 50.0 + 250.0 * random.nextDouble ( ) );
105    
106           hit       = false;
107    
108           destroyed = false;
109         }
110    
111    /*
112         //////////////////////////////////////////////////////////////////////
113         // interface Sprite methods
114         //////////////////////////////////////////////////////////////////////
115    
116         public boolean  contains (
117           double  x,
118           double  y )
119         //////////////////////////////////////////////////////////////////////
120         {
121           if ( destroyed )
122           {
123             return false;
124           }
125    
126           return super.contains ( x, y );
127         }
128    */
129             
130         //////////////////////////////////////////////////////////////////////
131         // interface ComponentAnimator methods
132         //////////////////////////////////////////////////////////////////////
133    
134         public void  update ( JComponent  component )
135         //////////////////////////////////////////////////////////////////////
136         {
137           if ( hit )
138           {
139             getPaintBounds ( repaintBounds );
140    
141             setX ( Integer.MIN_VALUE );
142    
143             component.repaint ( repaintBounds );
144    
145             destroyed = true;
146           }
147           else if ( !destroyed )
148           {
149             super.update ( component );
150           }
151         }
152    
153         //////////////////////////////////////////////////////////////////////
154         //////////////////////////////////////////////////////////////////////
155         }