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

📄 470-474.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=470-474//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="467-470.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="474-478.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="CENTER"><A NAME="Heading35"></A><FONT COLOR="#000077">The cmAbstractRound Class</FONT></H4>
<P>An abstract round, shown in Listing 12-17, is what is fired from an abstract weapon. Its base class is cmAbstractVehicle, since that class already implements most of the methods needed. Most &#147;rounds&#148; have a simple behavior. For example, a bullet travels with its maximum velocity straight forward. Other &#147;rounds,&#148; like missiles, accelerate.
</P>
<P><B>Listing 12-17</B> The abstract round</P>
<!-- CODE //-->
<PRE>
abstract class cmAbstractRound extends cmAbstractVehicle&#123;
   protected cmAbstractRound (fWorld w, fObject shooter, fPoint3d pos,
                               fVelocityVector v, double turningrate0,
                               double pitchrate0, double acceleration0,
                               double brakingrate0, double maxVelocity0,
                               double climbrate0, double decentrate0,
                               double pitchClimbrateRelation0,
                               double impactDamage)
   &#123;
   super(w,pos,v,turningrate0,pitchrate0,acceleration0,
         brakingrate0,maxVelocity0,climbrate0,decentrate0,
         pitchClimbrateRelation0);
      theShooter = shooter;
      this.impactDamage=impactDamage;
   &#125;

   public fObject getShooter () &#123;
      return theShooter;
   &#125;

      protected boolean handleCollisionWith (fObject obj,double dt) &#123;
         die();
         return false;
   &#125;

   public double getImpactDamage()&#123;
      return impactDamage;
   &#125;

   protected fObject theShooter;
   protected double impactDamage;
&#125;
</PRE>
<!-- END CODE //-->
<P>Each weapon has an &#147;impact damage.&#148; This damage is by default inflicted on any object interested in collision with rounds. Some objects might take special actions when colliding with certain weapons. Think of a tank getting hit by an antitank missile. This weapon will inflict extra damage on that particular object.
</P>
<P>Homing missiles and other smart weapons have a more complex behavior, which involves steering toward the target. This is where the methods in the base class cmAbstractVehicle come into really good use.</P>
<H3><A NAME="Heading36"></A><FONT COLOR="#000077">Implementing a Simple 3D Game</FONT></H3>
<P>In this section, we will look at a simplified version of the fully developed Combat Machines 96 (see Figure 12-7). This version will include a small city and two different vehicles: a tank and an airborne vehicle. Each of these objects can carry three different weapons: a mini-cannon, missiles, or bombs.
</P>
<P><A NAME="Fig7"></A><A HREF="javascript:displayWindow('images/12-07.jpg',404,466 )"><IMG SRC="images/12-07t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/12-07.jpg',404,466)"><FONT COLOR="#000077"><B>Figure 12-7</B></FONT></A>&nbsp;&nbsp;Combat Machines 96</P>
<P>The classes used in the game are direct extensions of the classes in the game layer. The source code for the classes is shown below.Parts of the source code like the initiateClass methods will not be shown here, since they take large amounts of space, even though they involve simple and uninteresting operations. However, the full source code can be found on the CD-ROM.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading37"></A><FONT COLOR="#000077">The Tank, Extending cmAbstractPlayer</FONT></H4>
<P>The tank (see Figure 12-8 and Listing 12-18) is a rapid, maneuverable light vehicle armed with a mini-cannon and light missiles. It doesn&#146;t have any special behavior on top of what is already implemented in the abstract vehicle class.
</P>
<P><A NAME="Fig8"></A><A HREF="javascript:displayWindow('images/12-08.jpg',274,162 )"><IMG SRC="images/12-08t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/12-08.jpg',274,162)"><FONT COLOR="#000077"><B>Figure 12-8</B></FONT></A>&nbsp;&nbsp;The tank</P>
<P><B>Listing 12-18</B> The Fesse tank</P>
<!-- CODE //-->
<PRE>
class cmFesseTank extends cmAbstractPlayer&#123;
    cmFesseTank (fWorld w, double x, double z, double a) &#123;

      super(w,new fPoint3d(x,ourScale.y,z),
         new fVelocityVector(a,0,0),turningRate,acceleration,
         brakeRate,maxVelocity,ourHealth);

      usePolyhedronInstance(
         new fPolyhedronInstance(ourDefaultPolyhedron,ourScale));
      addWeapon(new cmMinicannon(this,new fPoint3d(0,ourScale.y,0)));

      addWeapon(new cmMissileLauncher(this,
         new fPoint3d(0,ourScale.y,0)));
      selectWeapon(0);
    &#125;

   protected void die () &#123;
      super.die();
      for(int n=0;n&lt;fragmentsWhenDead;n&#43;&#43;)&#123;
      new cmGenericFragment(getWorld(),fragmentSize,getPosition(),
         fragmentSpread,fragmentGenerations,fragmentSpeed,3);
      &#125;
      new cmFesseTankRemains(getWorld(),this);
   &#125;

   public static void initiateClass (Applet app) &#123;
      //--
      //-- lots of code with that can be found on the CD
      //--
   &#125;
   //--
   //-- lots of class constants that can be found on the CD
   //--
&#125;
</PRE>
<!-- END CODE //-->
<H4 ALIGN="CENTER"><A NAME="Heading38"></A><FONT COLOR="#000077">The Constructor</FONT></H4>
<DL>
<DD><B>&#149;</B>&nbsp;&nbsp;The base class is constructed with the default values for the tank.
<DD><B>&#149;</B>&nbsp;&nbsp;The object is instructed to use the default polyhedron for this class.
<DD><B>&#149;</B>&nbsp;&nbsp;A mini-cannon and a missile launcher are mounted on top of the tank.
<DD><B>&#149;</B>&nbsp;&nbsp;The mini-cannon, being the first weapon, is selected.
</DL>
<H4 ALIGN="CENTER"><A NAME="Heading39"></A><FONT COLOR="#000077">The die() Method</FONT></H4>
<P>Since the tank doesn&#146;t have any special behavior, it is treated as a regular vehicle. When it is hit by rounds, its health will decrease and eventually the tank will be destroyed. This is, however, taken care of in the base class. When the tank is removed from the world, the die() method is called. This method is overridden to insert some new features instead of just letting the tank disappear:
</P>
<DL>
<DD><B>&#149;</B>&nbsp;&nbsp;At death the tank creates fragments.
<DD><B>&#149;</B>&nbsp;&nbsp;The remains of a tank are created.
</DL>
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="467-470.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="474-478.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>


</BODY>

⌨️ 快捷键说明

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