001         package com.croftsoft.apps.mars;
002         
003         import java.applet.*;
004         import java.awt.*;
005         import java.awt.event.*;
006         import java.awt.geom.*;
007         import java.io.*;
008         import java.net.URL;
009         import java.util.*;
010         import javax.swing.*;
011         import javax.swing.event.*;
012    
013         import com.croftsoft.core.animation.*;
014         import com.croftsoft.core.animation.animator.*;
015         import com.croftsoft.core.animation.clock.Timekeeper;
016         import com.croftsoft.core.io.SerializableLib;
017         import com.croftsoft.core.media.sound.AudioClipCache;
018    
019         import com.croftsoft.apps.mars.controller.FrameRateController;
020         import com.croftsoft.apps.mars.controller.GameAnimatorController;
021         import com.croftsoft.apps.mars.controller.SoundController;
022         import com.croftsoft.apps.mars.controller.TankController;
023         import com.croftsoft.apps.mars.controller.TimeController;
024         import com.croftsoft.apps.mars.model.Game;
025         import com.croftsoft.apps.mars.model.seri.SeriGame;
026         import com.croftsoft.apps.mars.view.GameAnimator;
027    
028         /*********************************************************************
029         * Animated tank combat game.
030         *
031         * @version
032         *   2003-04-30
033         * @since
034         *   2003-01-23
035         * @author
036         *   <a href="https://www.croftsoft.com/">David Wallace Croft</a>
037         *********************************************************************/
038    
039         public final class  Main
040           extends AnimatedApplet
041         //////////////////////////////////////////////////////////////////////
042         //////////////////////////////////////////////////////////////////////
043         {
044    
045         private static final String  VERSION
046           = "2003-04-30";
047    
048         private static final String  TITLE
049           = "CroftSoft Mars";
050    
051         private static final String  APPLET_INFO
052           = "\n" + TITLE + "\n"
053           + "Copyright 2003 CroftSoft Inc\n"
054           + "https://www.croftsoft.com/\n"
055           + "Version " + VERSION + "\n"
056           + "Licensed under the Academic Free License version 1.2\n";
057    
058         //////////////////////////////////////////////////////////////////////
059         // frame constants
060         //////////////////////////////////////////////////////////////////////
061    
062         private static final String  FRAME_TITLE
063           = TITLE;
064    
065         private static final String  FRAME_ICON_FILENAME
066           = "/images/croftsoft.png";
067           
068         private static final Dimension  FRAME_SIZE
069           = null;
070    
071         private static final String  SHUTDOWN_CONFIRMATION_PROMPT
072           = "Close " + TITLE + "?";
073    
074         //////////////////////////////////////////////////////////////////////
075         // animation constants
076         //////////////////////////////////////////////////////////////////////
077    
078         /** frames per second */
079         private static final double  FRAME_RATE = 85.0;
080    
081         private static final Color   BACKGROUND_COLOR
082           = new Color ( 255, 152, 109 );
083    
084         private static final Color   FOREGROUND_COLOR
085           = Color.BLACK;
086    
087         private static final Font    FONT
088           = new Font ( "Arioso", Font.BOLD, 10 );
089    
090         private static final Cursor  CURSOR
091           = new Cursor ( Cursor.CROSSHAIR_CURSOR );
092    
093         private static final String  MEDIA_DIR = "media/mars/";
094    
095         private static final double  TIME_FACTOR_DELTA = 0.1;
096    
097         //////////////////////////////////////////////////////////////////////
098         // persistence constants
099         //////////////////////////////////////////////////////////////////////
100    
101         private static final String  DATA_DIR
102           = ".croftsoft" + File.separator + "mars" + File.separator;
103    
104         private static final String  LATEST_FILENAME = DATA_DIR + "mars1.dat";
105    
106         private static final String  BACKUP_FILENAME = DATA_DIR + "mars2.dat";
107    
108         private static final String  FILE_CONTENTS_SPEC = "mars";
109    
110         private static final String  PERSISTENCE_KEY = FILE_CONTENTS_SPEC;
111    
112         private static final String  RESOURCE_PATH_FILENAME = null;
113    
114         //////////////////////////////////////////////////////////////////////
115         // instance variables
116         //////////////////////////////////////////////////////////////////////
117    
118         private UserData  userData;
119    
120         private Game      game;
121    
122         //////////////////////////////////////////////////////////////////////
123         //////////////////////////////////////////////////////////////////////
124    
125         public static void  main ( String [ ]  args )
126         //////////////////////////////////////////////////////////////////////
127         {
128           launch ( new Main ( ) );
129         }
130    
131         private static AnimationInit  createAnimationInit ( )
132         //////////////////////////////////////////////////////////////////////
133         {
134           AnimationInit  animationInit = new AnimationInit ( );
135    
136           animationInit.setAppletInfo ( APPLET_INFO );
137    
138           animationInit.setBackgroundColor ( BACKGROUND_COLOR );
139    
140           animationInit.setCursor ( CURSOR );
141    
142           animationInit.setFont ( FONT );
143    
144           animationInit.setForegroundColor ( FOREGROUND_COLOR );
145    
146           animationInit.setFrameIconFilename ( FRAME_ICON_FILENAME );
147    
148           animationInit.setFrameSize ( FRAME_SIZE );
149    
150           animationInit.setFrameTitle ( FRAME_TITLE );
151    
152           animationInit.setShutdownConfirmationPrompt (
153             SHUTDOWN_CONFIRMATION_PROMPT );
154    
155           return animationInit;
156         }
157    
158         //////////////////////////////////////////////////////////////////////
159         //////////////////////////////////////////////////////////////////////
160    
161         public  Main ( )
162         //////////////////////////////////////////////////////////////////////
163         {
164           super ( createAnimationInit ( ) );
165         }
166    
167         //////////////////////////////////////////////////////////////////////
168         // interface Lifecycle methods
169         //////////////////////////////////////////////////////////////////////
170    
171         public void  init ( )
172         //////////////////////////////////////////////////////////////////////
173         {
174           super.init ( );
175    
176           // persistent data
177    
178           try
179           {
180             userData = ( UserData ) SerializableLib.load (
181               LATEST_FILENAME,
182               BACKUP_FILENAME,
183               FILE_CONTENTS_SPEC,
184               ( Applet ) this,
185               PERSISTENCE_KEY,
186               getClass ( ).getClassLoader ( ),
187               RESOURCE_PATH_FILENAME );
188           }
189           catch ( Exception  ex )
190           {
191             ex.printStackTrace ( );
192           }
193    
194           if ( userData == null )
195           {
196             userData = new UserData ( );
197           }
198    
199           // model
200    
201           game = new SeriGame ( );
202    
203           // view
204    
205           GameAnimator  gameAnimator = new GameAnimator (
206             game,
207             animatedComponent,
208             getClass ( ).getClassLoader ( ),
209             MEDIA_DIR,
210             BACKGROUND_COLOR );
211    
212           addComponentAnimator ( gameAnimator );
213    
214           AudioClipCache  audioClipCache
215             = gameAnimator.getAudioClipCache ( );
216    
217           audioClipCache.setMuted ( userData.isMuted ( ) );
218    
219           FrameRateAnimator  frameRateAnimator
220             = new FrameRateAnimator ( animatedComponent );
221    
222           frameRateAnimator.toggle ( );
223    
224           addComponentAnimator ( frameRateAnimator );
225    
226           // controllers
227    
228           new FrameRateController (
229             frameRateAnimator,
230             animatedComponent );
231    
232           new GameAnimatorController (
233             gameAnimator,
234             animatedComponent );
235    
236           new TimeController (
237             game.getTimekeeper ( ),
238             game.getTimeFactorDefault ( ),
239             TIME_FACTOR_DELTA,
240             animatedComponent );
241    
242           new SoundController (
243             audioClipCache,
244             userData,
245             animatedComponent );
246    
247           new TankController (
248             game.getPlayerTank ( ).getTankOperator ( ),
249             animatedComponent );
250         }
251    
252         public void  update ( JComponent  component )
253         //////////////////////////////////////////////////////////////////////
254         {
255           game.update ( );
256    
257           super.update ( component );
258         }
259    
260         public void  destroy ( )
261         //////////////////////////////////////////////////////////////////////
262         {
263           try
264           {
265             SerializableLib.save (
266               userData,
267               LATEST_FILENAME,
268               BACKUP_FILENAME,
269               FILE_CONTENTS_SPEC,
270               ( Applet ) this,
271               PERSISTENCE_KEY );
272           }
273           catch ( Exception  ex )
274           {
275             ex.printStackTrace ( );
276           }
277    
278           super.destroy ( );
279         }
280    
281         //////////////////////////////////////////////////////////////////////
282         //////////////////////////////////////////////////////////////////////
283         }