001         package com.croftsoft.apps.mars.view;
002    
003         import java.awt.*;
004         import java.util.*;
005         import javax.swing.JComponent;
006    
007         import com.croftsoft.core.animation.ComponentAnimator;
008         import com.croftsoft.core.animation.painter.ColorPainter;
009         import com.croftsoft.core.awt.image.ImageCache;
010         import com.croftsoft.core.lang.NullArgumentException;
011         import com.croftsoft.core.media.sound.AudioClipCache;
012    
013         import com.croftsoft.apps.mars.model.AmmoDumpAccessor;
014         import com.croftsoft.apps.mars.model.BulletAccessor;
015         import com.croftsoft.apps.mars.model.ModelAccessor;
016         import com.croftsoft.apps.mars.model.ObstacleAccessor;
017         import com.croftsoft.apps.mars.model.TankAccessor;
018         import com.croftsoft.apps.mars.model.WorldAccessor;
019    
020         /*********************************************************************
021         * ComponentAnimator that maps Models to views.
022         *
023         * @version
024         *   2003-04-17
025         * @since
026         *   2003-04-14
027         * @author
028         *   <a href="https://www.croftsoft.com/">David Wallace Croft</a>
029         *********************************************************************/
030    
031         public final class  WorldAnimator
032           implements ComponentAnimator
033         //////////////////////////////////////////////////////////////////////
034         //////////////////////////////////////////////////////////////////////
035         {
036    
037         private static final Color  COLOR_BULLET   = Color.MAGENTA;
038    
039         private static final Color  COLOR_OBSTACLE = Color.BLACK;
040    
041         //
042    
043         private final AudioClipCache  audioClipCache;
044    
045         private final ImageCache      imageCache;
046    
047         private final Map             componentAnimatorMap;
048    
049         private final WorldAccessor   worldAccessor;
050    
051         //
052    
053         private ModelAccessor [ ]  modelAccessors;
054    
055         //////////////////////////////////////////////////////////////////////
056         //////////////////////////////////////////////////////////////////////
057    
058         public  WorldAnimator (
059           WorldAccessor   worldAccessor,
060           AudioClipCache  audioClipCache,
061           ImageCache      imageCache )
062         //////////////////////////////////////////////////////////////////////
063         {
064           NullArgumentException.check ( this.worldAccessor  = worldAccessor );
065    
066           NullArgumentException.check ( this.audioClipCache = audioClipCache);
067    
068           NullArgumentException.check ( this.imageCache     = imageCache    );
069    
070           componentAnimatorMap = new HashMap ( );
071    
072           modelAccessors = new ModelAccessor [ 0 ];
073         }
074    
075         //////////////////////////////////////////////////////////////////////
076         // interface ComponentAnimator methods
077         //////////////////////////////////////////////////////////////////////
078    
079         public void  update ( JComponent  component )
080         //////////////////////////////////////////////////////////////////////
081         {
082           modelAccessors = worldAccessor.getModelAccessors ( modelAccessors );       
083    
084           for ( int  i = 0; i < modelAccessors.length; i++ )
085           {
086             ModelAccessor  modelAccessor = modelAccessors [ i ];
087    
088             if ( modelAccessor == null )
089             {
090               break;
091             }
092    
093             ComponentAnimator  componentAnimator
094               = getComponentAnimator ( modelAccessor );
095    
096             componentAnimator.update ( component );
097           }
098         }
099    
100         public void  paint (
101           JComponent  component,
102           Graphics2D  graphics )
103         //////////////////////////////////////////////////////////////////////
104         {
105           for ( int  i = 0; i < modelAccessors.length; i++ )
106           {
107             ModelAccessor  modelAccessor = modelAccessors [ i ];
108    
109             if ( modelAccessor == null )
110             {
111               break;
112             }
113    
114             ComponentAnimator  componentAnimator
115               = getComponentAnimator ( modelAccessors [ i ] );
116    
117             componentAnimator.paint ( component, graphics );
118           }
119         }
120    
121         //////////////////////////////////////////////////////////////////////
122         // private methods
123         //////////////////////////////////////////////////////////////////////
124    
125         private ComponentAnimator  getComponentAnimator (
126           ModelAccessor  modelAccessor )
127         //////////////////////////////////////////////////////////////////////
128         {
129           ComponentAnimator  componentAnimator = ( ComponentAnimator )
130             componentAnimatorMap.get ( modelAccessor );
131    
132           if ( componentAnimator == null )
133           {
134             if ( modelAccessor instanceof AmmoDumpAccessor )
135             {
136               componentAnimator = new AmmoDumpAnimator (
137                 ( AmmoDumpAccessor ) modelAccessor, audioClipCache );
138             }
139             else if ( modelAccessor instanceof BulletAccessor )
140             {
141               componentAnimator
142                 = new ModelAnimator ( modelAccessor, COLOR_BULLET );
143             }
144             else if ( modelAccessor instanceof ObstacleAccessor )
145             {
146               componentAnimator
147                 = new ModelAnimator ( modelAccessor, COLOR_OBSTACLE );
148             }
149             else if ( modelAccessor instanceof TankAccessor )
150             {
151               componentAnimator = new TankAnimator (
152                 ( TankAccessor ) modelAccessor, imageCache, audioClipCache );
153             }
154             else
155             {
156               componentAnimator = new ModelAnimator ( modelAccessor );
157             }
158    
159             componentAnimatorMap.put ( modelAccessor, componentAnimator );
160           }
161    
162           return componentAnimator;
163         }
164    
165         //////////////////////////////////////////////////////////////////////
166         //////////////////////////////////////////////////////////////////////
167         }