⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 493-501.html

📁 java game programming e-book
💻 HTML
字号:
<HTML>
<HEAD>
<META name=vsisbn content="1571690433"><META name=vstitle content="Black Art of Java Game Programming"><META name=vsauthor content="Joel Fan"><META name=vsimprint content="Sams"><META name=vspublisher content="Macmillan Computer Publishing"><META name=vspubdate content="11/01/96"><META name=vscategory content="Web and Software Development: Programming, Scripting, and Markup Languages: Java"><TITLE>Black Art of Java Game Programming:Building 3D Applets with App3Dcore</TITLE>
<!-- HEADER --><STYLE type="text/css">  <!-- A:hover  { 	color : Red; } --></STYLE><META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW"><script><!--function displayWindow(url, width, height) {         var Win = window.open(url,"displayWindow",'width=' + width +',height=' + height + ',resizable=1,scrollbars=yes');	if (Win) {		Win.focus();	}}//--></script><SCRIPT><!--function popUp(url) {        var Win = window.open(url,"displayWindow",'width=400,height=300,resizable=1,scrollbars=yes');	if (Win) {		Win.focus();	}}//--></SCRIPT><script language="JavaScript1.2"><!--function checkForQuery(fm) {  /* get the query value */  var i = escape(fm.query.value);  if (i == "") {      alert('Please enter a search word or phrase');      return false;  }                  /* query is blank, dont run the .jsp file */  else return true;  /* execute the .jsp file */}//--></script></HEAD><BODY> 
<TABLE border=0 cellspacing=0 cellpadding=0>
<tr>
<td width=75 valign=top>
<img src="../1571690433.gif" width=60 height=73 alt="Black Art of Java Game Programming" border="1">
</td>
<td align="left">
    <font face="arial, helvetica" size="-1" color="#336633"><b>Black Art of Java Game Programming</b></font>
    <br>
    <font face="arial, helvetica" size="-1"><i>by Joel Fan</i>
    <br>
    Sams,&nbsp;Macmillan Computer Publishing
    <br>
    <b>ISBN:</b>&nbsp;1571690433<b>&nbsp;&nbsp;&nbsp;Pub Date:</b>&nbsp;11/01/96</font>&nbsp;&nbsp;
</td>
</tr>
</table>
<P>

<!--ISBN=1571690433//-->
<!--TITLE=Black Art of Java Game Programming//-->
<!--AUTHOR=Joel Fan//-->
<!--AUTHOR=Eric Ries//-->
<!--AUTHOR=Calin Tenitchi//-->
<!--PUBLISHER=Macmillan Computer Publishing//-->
<!--IMPRINT=Sams//-->
<!--CHAPTER=12//-->
<!--PAGES=493-501//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="491-493.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="501-502.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="CENTER"><A NAME="Heading74"></A><FONT COLOR="#000077">The Constructor</FONT></H4>
<P>The arguments of the constructor control with what speed the explosion expands and contracts, as shown in Figure 12-17.
</P>
<P><A NAME="Fig17"></A><A HREF="javascript:displayWindow('images/12-17.jpg',600,406 )"><IMG SRC="images/12-17t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/12-17.jpg',600,406)"><FONT COLOR="#000077"><B>Figure 12-17</B></FONT></A>&nbsp;&nbsp;The explosion expansion and contraction</P>
<P><I>s0</I>is the initial scale of the polyhedron. In <I>t0</I> seconds the explosion will expand to the maximum size of <I>s1</I>. Immediately after that, the explosion will start contracting, and in <I>t1</I> seconds it will reach the minimum scale of <I>s2</I> and then disappear.</P>
<DL>
<DD><B>&#149;</B>&nbsp;&nbsp;Uses a polyhedron scaled depending on arguments to the constructor.
<DD><B>&#149;</B>&nbsp;&nbsp;Creates some fragments to give the impression of things flying up in the air from the power of the explosion.
<DD><B>&#149;</B>&nbsp;&nbsp;Calculates the growth and contraction rate for the explosion.
</DL>
<H4 ALIGN="CENTER"><A NAME="Heading75"></A><FONT COLOR="#000077">The update() Method</FONT></H4>
<P>The scale of the polyhedron instance is changed.
</P>
<P>The position of the object is adjusted so that the bottom of the model always touches the ground.</P>
<H3><A NAME="Heading76"></A><FONT COLOR="#000077">Putting Together a Virtual World</FONT></H3>
<P>We now have all the objects we need to make a virtual world. The construction of the world is simple but hard work, since we don&#146;t have access to a world editor.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading77"></A><FONT COLOR="#000077">The cmWorld, Extension of fWorld</FONT></H4>
<P>The extension of fWorld (see Listing 12-33) is our own customized virtual world. In the constructor all objects are created (several hundreds), which are then left to the mercy of the core. The full source code can be found on the CD-ROM.
</P>
<P><B>Listing 12-33</B> The cmWorld</P>
<!-- CODE //-->
<PRE>
class cmWorld extends fWorld&#123;
   cmWorld (Applet app) &#123;
      //-- create a world with the size 1km x 1km divided into
      //-- 20x20 squares
      super(app,-500,-500,1000,20);
      app.showStatus(" Creating CombatMachines96 world.");
      //-- reset the virtual world
      reset();
   &#125;
   /*
   * Returns the active player that is controlled by the keyboard.
   */
      public cmAbstractPlayer getActivePlayer()&#123;
         return theActivePlayer;
   &#125;

      protected cmAbstractPlayer theActivePlayer;
      public static final double gravity=-10;

      /*
       * Override the base class method.
       */
      public void reset()&#123;
         super.reset();
         //--
         //-- creating the initial world by
         //-- making new objects
         //-- the full source is on the CD
         //--
         //--
   &#125;
</PRE>
<!-- END CODE //-->
<P>The class variable <I>theActivePlayer</I> is the player that is controlled from the keyboard while all other players are controlled by the computer. By default the player is a tank, but you can change it to an arbitrary abstract player. You can even make your own machine of destruction, armed to the teeth and possessing extreme capabilities.</P>
<H4 ALIGN="LEFT"><A NAME="Heading78"></A><FONT COLOR="#000077">The Display Panel</FONT></H4>
<P>The display panel that will be used in the final applet is a simple class with standard Java coding. There shouldn&#146;t be any problems for you at this point.
</P>
<P>There are a few buttons that change the function of the panel from camera to radar to vehicle gauges and so on. There is really nothing tricky about it. The source code is self-explanatory and can be found on the CD-ROM.</P>
<H4 ALIGN="LEFT"><A NAME="Heading79"></A><FONT COLOR="#000077">The Applet</FONT></H4>
<P>It is now time to wrap it all up and make the actual applet (see Listing 12-34) in which the world will be displayed. The applet contains a display panel with the camera that tracks the active player in the world.
</P>
<P><B>Listing 12-34</B> The applet</P>
<!-- CODE //-->
<PRE>
public class cmApplet extends Applet implements Runnable&#123;
   Thread myThread;
   boolean alive;
   boolean key [ ];
   cmWorld world;
   cmAbstractPlayer player;
   cmDisplayPanel display1;
   Panel panel;
   public void init () &#123;
      double viewAngle=new
Double(getParameter("cmApplet_viewAngle")).doubleValue();
      viewDistance=new
Double(getParameter("cmApplet_viewDistance")).doubleValue();
      gridSize=new
Double(getParameter("cmApplet_gridSize")).doubleValue();

      key=new boolean[100];

      showStatus(" Initiating Combat Machines 96 ");

      world=new cmWorld(this);

      setLayout(new GridLayout(1,1));
      //-- add a display panel with a camera
      add(display1=new cmDisplayPanel(world.getActivePlayer(),"C"));

      myThread=new Thread(this);
      myThread.start();
      myThread.setPriority(Thread.MAX_PRIORITY);
      alive=true;
   &#125;

   public final void run () &#123;
      lastUpdate=System.currentTimeMillis()   ;
      while(alive)&#123;
         long currentTime=System.currentTimeMillis();
         long dtmillis=currentTime-lastUpdate;
         double dt=(double)dtmillis/1000;
         lastUpdate=currentTime;

         if(dt&gt;0.2) dt=0.2;
         //-- get the active player
         player=world.getActivePlayer();
         //-- handle keyboard events
         handleKeyboard();
         //-- update the world
         world.update(dt);
         //-- update the display
         display1.update(dt);
      &#125;
   &#125;

   public final void start () &#123;
      showStatus(" Starting Combat Machines 96 (gimped)");
      if(myThread==null)&#123;
         myThread=new Thread(this);
         myThread.setPriority(Thread.MAX_PRIORITY);
         myThread.start();
         alive=true;
      &#125;
   &#125;

   public final synchronized void stop () &#123;
      showStatus(" Stoping applet.");
      if(myThread!=null)&#123;
         myThread.stop();
         myThread=null;
         alive=false;
      &#125;
   &#125;

   public boolean keyDown (Event ev, int k) &#123;
      keyboardEvent(ev.key,true);
      return true;
   &#125;

   public boolean keyUp (Event ev, int k) &#123;
      keyboardEvent(ev.key,false);
      return true;
   &#125;

   protected void keyboardEvent (int k, boolean pressed) &#123;
      switch(k)&#123;
         case 'h': key[cmEventSteeringCommand.TURN_LEFT]=pressed;break;
         case 'k': key[cmEventSteeringCommand.TURN_RIGHT]=pressed;break;
         case 't':
key[cmEventSteeringCommand.INCREASE_VELOCITY]=pressed;break;
         case 'g': key[cmEventSteeringCommand.BRAKE]=pressed;break;
         case 'y': key[cmEventSteeringCommand.CLIMB]=pressed;break;
         case 'i': key[cmEventSteeringCommand.DECENT]=pressed;break;
         case 'u': key[cmEventSteeringCommand.PITCH_DOWN]=pressed;break;
         case 'j': key[cmEventSteeringCommand.PITCH_UP]=pressed;break;
         case 'a': key[cmEventWeaponCommand.FIRE]=pressed;break;
         case '1': key[cmEventWeaponCommand.MINICANNON]=pressed;break;
         case '2': key[cmEventWeaponCommand.MISSILE]=pressed;break;
         case '3': key[cmEventWeaponCommand.BOMB]=pressed;break;
      &#125;
   &#125;

   protected void handleKeyboard () &#123;
      //-- handle keyboard;
      if(key[cmEventSteeringCommand.TURN_LEFT]) player.turnLeft(1,0.1);
      if(key[cmEventSteeringCommand.TURN_RIGHT]) player.turnRight(1,0.1);
      if(key[cmEventSteeringCommand.INCREASE_VELOCITY]) &#123;
         player.increaseVelocity(1,0.1);
      &#125;
      if(key[cmEventSteeringCommand.BRAKE]) player.brake(1,0.1);
      if(key[cmEventSteeringCommand.CLIMB]) player.climb(1,0.1);
      if(key[cmEventSteeringCommand.DECENT]) player.decent(1,0.1);
      if(key[cmEventSteeringCommand.PITCH_UP]) player.pitchUp(1,0.1);
      if(key[cmEventSteeringCommand.PITCH_DOWN]) player.pitchDown(1,0.1);
      if(key[cmEventWeaponCommand.FIRE]) player.fireSelectedWeapon();
      if(key[cmEventWeaponCommand.MINICANNON])&#123;
         int com=cmEventWeaponCommand.SELECT;
         int arg=cmEventWeaponCommand.MINICANNON;
         player.addEvent(new
cmEventWeaponCommand(world.getTime(),com,arg));
      &#125;
      if(key[cmEventWeaponCommand.MISSILE])&#123;
         int com=cmEventWeaponCommand.SELECT;
         int arg=cmEventWeaponCommand.MISSILE;
         player.addEvent(new
cmEventWeaponCommand(world.getTime(),com,arg));
      &#125;
      if(key[cmEventWeaponCommand.BOMB])&#123;
         int com=cmEventWeaponCommand.SELECT;
         int arg=cmEventWeaponCommand.BOMB;
         player.addEvent(new
cmEventWeaponCommand(world.getTime(),com,arg));
      &#125;
   &#125;

   protected long lastUpdate;
   static double viewAngle;
   static double viewDistance;
   static double gridSize;
&#125;
</PRE>
<!-- END CODE //-->
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="491-493.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="501-502.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>


</BODY>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -