001 package com.croftsoft.apps.mars.net; 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.CroftSoftConstants; 014 import com.croftsoft.core.animation.*; 015 import com.croftsoft.core.animation.animator.*; 016 import com.croftsoft.core.animation.clock.Timekeeper; 017 import com.croftsoft.core.io.SerializableLib; 018 import com.croftsoft.core.media.sound.AudioClipCache; 019 020 import com.croftsoft.apps.mars.UserData; 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.view.GameAnimator; 026 027 /********************************************************************* 028 * Animated tank combat game. 029 * 030 * @version 031 * 2003-06-12 032 * @since 033 * 2003-01-23 034 * @author 035 * <a href="https://www.croftsoft.com/">David Wallace Croft</a> 036 *********************************************************************/ 037 038 public final class NetMain 039 extends AnimatedApplet 040 ////////////////////////////////////////////////////////////////////// 041 ////////////////////////////////////////////////////////////////////// 042 { 043 044 private static final String VERSION 045 = "2003-06-12"; 046 047 private static final String TITLE 048 = "CroftSoft Mars"; 049 050 private static final String APPLET_INFO 051 = "\n" + TITLE 052 + "\n" + CroftSoftConstants.COPYRIGHT 053 + "\n" + CroftSoftConstants.HOME_PAGE 054 + "\n" + "Version " + VERSION 055 + "\n" + CroftSoftConstants.DEFAULT_LICENSE 056 + "\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 = 30.0; 080 081 private static final Color FOREGROUND_COLOR 082 = Color.BLACK; 083 084 private static final Font FONT 085 = new Font ( "Arioso", Font.BOLD, 10 ); 086 087 private static final Cursor CURSOR 088 = new Cursor ( Cursor.CROSSHAIR_CURSOR ); 089 090 091 // cut and paste code 092 093 private static final Color BACKGROUND_COLOR 094 = new Color ( 255, 152, 109 ); 095 096 private static final String MEDIA_DIR = "media/mars/"; 097 098 ////////////////////////////////////////////////////////////////////// 099 // persistence constants 100 ////////////////////////////////////////////////////////////////////// 101 102 private static final String DATA_DIR 103 = ".croftsoft" + File.separator + "mars" + File.separator; 104 105 private static final String LATEST_FILENAME = DATA_DIR + "mars1.dat"; 106 107 private static final String BACKUP_FILENAME = DATA_DIR + "mars2.dat"; 108 109 private static final String FILE_CONTENTS_SPEC = "mars"; 110 111 private static final String PERSISTENCE_KEY = FILE_CONTENTS_SPEC; 112 113 private static final String RESOURCE_PATH_FILENAME = null; 114 115 ////////////////////////////////////////////////////////////////////// 116 // instance variables 117 ////////////////////////////////////////////////////////////////////// 118 119 private UserData userData; 120 121 private Synchronizer synchronizer; 122 123 ////////////////////////////////////////////////////////////////////// 124 ////////////////////////////////////////////////////////////////////// 125 126 public static void main ( String [ ] args ) 127 ////////////////////////////////////////////////////////////////////// 128 { 129 launch ( new NetMain ( ) ); 130 } 131 132 private static AnimationInit createAnimationInit ( ) 133 ////////////////////////////////////////////////////////////////////// 134 { 135 AnimationInit animationInit = new AnimationInit ( ); 136 137 animationInit.setAppletInfo ( APPLET_INFO ); 138 139 animationInit.setCursor ( CURSOR ); 140 141 animationInit.setFont ( FONT ); 142 143 animationInit.setForegroundColor ( FOREGROUND_COLOR ); 144 145 animationInit.setFrameIconFilename ( FRAME_ICON_FILENAME ); 146 147 animationInit.setFrameSize ( FRAME_SIZE ); 148 149 animationInit.setFrameTitle ( FRAME_TITLE ); 150 151 animationInit.setShutdownConfirmationPrompt ( 152 SHUTDOWN_CONFIRMATION_PROMPT ); 153 154 return animationInit; 155 } 156 157 ////////////////////////////////////////////////////////////////////// 158 ////////////////////////////////////////////////////////////////////// 159 160 public NetMain ( ) 161 ////////////////////////////////////////////////////////////////////// 162 { 163 super ( createAnimationInit ( ) ); 164 } 165 166 ////////////////////////////////////////////////////////////////////// 167 // interface Lifecycle methods 168 ////////////////////////////////////////////////////////////////////// 169 170 public void init ( ) 171 ////////////////////////////////////////////////////////////////////// 172 { 173 super.init ( ); 174 175 // persistent data 176 177 try 178 { 179 userData = ( UserData ) SerializableLib.load ( 180 LATEST_FILENAME, 181 BACKUP_FILENAME, 182 FILE_CONTENTS_SPEC, 183 ( Applet ) this, 184 PERSISTENCE_KEY, 185 getClass ( ).getClassLoader ( ), 186 RESOURCE_PATH_FILENAME ); 187 } 188 catch ( Exception ex ) 189 { 190 ex.printStackTrace ( ); 191 } 192 193 if ( userData == null ) 194 { 195 userData = new UserData ( ); 196 } 197 198 // model 199 200 String playerName 201 = Long.toString ( new Random ( ).nextLong ( ) ); 202 203 URL codeBaseURL = null; 204 205 try 206 { 207 codeBaseURL = getCodeBase ( ); 208 } 209 catch ( Exception ex ) 210 { 211 } 212 213 try 214 { 215 synchronizer 216 = new Synchronizer ( playerName, codeBaseURL ); 217 218 synchronizer.init ( ); 219 } 220 catch ( Exception ex ) 221 { 222 ex.printStackTrace ( ); 223 } 224 225 // view 226 227 GameAnimator gameAnimator = new GameAnimator ( 228 synchronizer, 229 animatedComponent, 230 getClass ( ).getClassLoader ( ), 231 MEDIA_DIR, 232 BACKGROUND_COLOR ); 233 234 addComponentAnimator ( gameAnimator ); 235 236 AudioClipCache audioClipCache 237 = gameAnimator.getAudioClipCache ( ); 238 239 audioClipCache.setMuted ( userData.isMuted ( ) ); 240 241 // controllers 242 243 new SoundController ( 244 audioClipCache, 245 userData, 246 animatedComponent ); 247 248 new NetController ( playerName, synchronizer, animatedComponent ); 249 } 250 251 public void start ( ) 252 ////////////////////////////////////////////////////////////////////// 253 { 254 synchronizer.start ( ); 255 256 super.start ( ); 257 } 258 259 public void stop ( ) 260 ////////////////////////////////////////////////////////////////////// 261 { 262 synchronizer.stop ( ); 263 264 super.stop ( ); 265 } 266 267 public void destroy ( ) 268 ////////////////////////////////////////////////////////////////////// 269 { 270 try 271 { 272 SerializableLib.save ( 273 userData, 274 LATEST_FILENAME, 275 BACKUP_FILENAME, 276 FILE_CONTENTS_SPEC, 277 ( Applet ) this, 278 PERSISTENCE_KEY ); 279 } 280 catch ( Exception ex ) 281 { 282 ex.printStackTrace ( ); 283 } 284 285 synchronizer.destroy ( ); 286 287 super.destroy ( ); 288 } 289 290 ////////////////////////////////////////////////////////////////////// 291 ////////////////////////////////////////////////////////////////////// 292 }