001         package com.croftsoft.apps.mars.view;
002    
003         import java.awt.*;
004         import java.io.*;
005         import java.util.*;
006         import javax.swing.JComponent;
007    
008         import com.croftsoft.core.animation.ComponentAnimator;
009         import com.croftsoft.core.animation.painter.ColorPainter;
010         import com.croftsoft.core.awt.image.ImageCache;
011         import com.croftsoft.core.lang.NullArgumentException;
012         import com.croftsoft.core.media.sound.AudioClipCache;
013    
014         import com.croftsoft.apps.mars.model.GameAccessor;
015         import com.croftsoft.apps.mars.model.TankAccessor;
016         import com.croftsoft.apps.mars.model.WorldAccessor;
017    
018         /*********************************************************************
019         * ComponentAnimator that maps models to views.
020         *
021         * @version
022         *   2003-07-17
023         * @since
024         *   2003-04-01
025         * @author
026         *   <a href="https://www.croftsoft.com/">David Wallace Croft</a>
027         *********************************************************************/
028    
029         public final class  GameAnimator
030           implements ComponentAnimator
031         //////////////////////////////////////////////////////////////////////
032         //////////////////////////////////////////////////////////////////////
033         {
034    
035         private final GameAccessor    gameAccessor;
036    
037         private final AudioClipCache  audioClipCache;
038    
039         private final ColorPainter    backgroundColorPainter;
040    
041         private final ImageCache      imageCache;
042    
043         //
044    
045         private int               oldLevel;
046    
047         private PathAnimator      pathAnimator;
048    
049         private TankAmmoAnimator  tankAmmoAnimator;
050    
051         private WorldAnimator     worldAnimator;
052    
053         //////////////////////////////////////////////////////////////////////
054         //////////////////////////////////////////////////////////////////////
055    
056         public  GameAnimator (
057           GameAccessor  gameAccessor,
058           JComponent    component,
059           ClassLoader   classLoader,
060           String        mediaDir,
061           Color         backgroundColor )
062         //////////////////////////////////////////////////////////////////////
063         {
064           NullArgumentException.check ( this.gameAccessor = gameAccessor );
065    
066           NullArgumentException.check ( component );
067    
068           audioClipCache = new AudioClipCache ( classLoader, mediaDir );
069    
070           imageCache = new ImageCache (
071             Transparency.BITMASK,
072             component,
073             classLoader,
074             mediaDir );
075    
076           backgroundColorPainter = new ColorPainter ( backgroundColor );
077    
078           updateAnimators ( component );
079         }
080    
081         //////////////////////////////////////////////////////////////////////
082         // accessor methods
083         //////////////////////////////////////////////////////////////////////
084    
085         public AudioClipCache  getAudioClipCache ( )
086         //////////////////////////////////////////////////////////////////////
087         {
088           return audioClipCache;
089         }
090    
091         //////////////////////////////////////////////////////////////////////
092         // interface ComponentAnimator methods
093         //////////////////////////////////////////////////////////////////////
094    
095         public void  update ( JComponent  component )
096         //////////////////////////////////////////////////////////////////////
097         {
098           int  level = gameAccessor.getLevel ( );
099    
100           if ( level != oldLevel )
101           {
102             oldLevel = level;
103    
104             updateAnimators ( component );
105    
106             component.repaint ( );
107           }
108    
109           worldAnimator.update ( component );
110    
111           if ( pathAnimator != null )
112           {
113             pathAnimator.update ( component );
114           }
115    
116           if ( tankAmmoAnimator != null )
117           {
118             tankAmmoAnimator.update ( component );
119           }
120         }
121    
122         public void  paint (
123           JComponent  component,
124           Graphics2D  graphics )
125         //////////////////////////////////////////////////////////////////////
126         {
127           backgroundColorPainter.paint ( component, graphics );
128    
129           worldAnimator         .paint ( component, graphics );
130    
131           if ( pathAnimator != null )
132           {
133             pathAnimator        .paint ( component, graphics );
134           }
135    
136           if ( tankAmmoAnimator != null )
137           {
138             tankAmmoAnimator    .paint ( component, graphics );
139           }
140         }
141    
142         //////////////////////////////////////////////////////////////////////
143         //////////////////////////////////////////////////////////////////////
144    
145         public void  togglePathAnimator ( )
146         //////////////////////////////////////////////////////////////////////
147         {
148           if ( pathAnimator != null )
149           {
150             pathAnimator.toggle ( );
151           }
152         }
153    
154         //////////////////////////////////////////////////////////////////////
155         //////////////////////////////////////////////////////////////////////
156    
157         private void  updateAnimators ( JComponent  component )
158         //////////////////////////////////////////////////////////////////////
159         {
160           // Caches not cleared since images and audio same for each level.
161    
162           // imageCache.clear ( );
163    
164           // audioClipCache.clear ( );
165    
166           WorldAccessor  worldAccessor = gameAccessor.getWorldAccessor ( );
167    
168           worldAnimator = new WorldAnimator (
169             worldAccessor, audioClipCache, imageCache );
170    
171           TankAccessor  playerTankAccessor
172             = gameAccessor.getPlayerTankAccessor ( );
173    
174           if ( playerTankAccessor != null )
175           {
176             pathAnimator = new PathAnimator (
177               gameAccessor, playerTankAccessor.getRadius ( ) );
178    
179             try
180             {
181               tankAmmoAnimator = new TankAmmoAnimator (
182                 playerTankAccessor, imageCache, component );
183             }
184             catch ( IOException  ex )
185             {
186               ex.printStackTrace ( );
187             }
188           }
189           else
190           {
191             pathAnimator     = null;
192    
193             tankAmmoAnimator = null;
194           }
195         }
196    
197         //////////////////////////////////////////////////////////////////////
198         //////////////////////////////////////////////////////////////////////
199         }