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

📄 483-488.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=483-488//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="478-483.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="488-491.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading52"></A><FONT COLOR="#000077">The Missile, Extension of cmAbstractRound</FONT></H4>
<P>The generic missile (see Figure 12-13 and Listing 12-24) is a primitive missile that simply travels in the direction that it is fired until it hits something. Upon impact with an object or the ground, it will explode into a fireworks of fragments.
</P>
<P><A NAME="Fig13"></A><A HREF="javascript:displayWindow('images/12-13.jpg',134,104 )"><IMG SRC="images/12-13t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/12-13.jpg',134,104)"><FONT COLOR="#000077"><B>Figure 12-13</B></FONT></A>&nbsp;&nbsp;The missile</P>
<P><B>Listing 12-24</B> The generic missile</P>
<!-- CODE //-->
<PRE>
class cmGenericMissile extends cmAbstractRound&#123;
   cmGenericMissile (fWorld w, fObject shooter,
      fPoint3d pos, fAngle3d agl)
   &#123;
      super(w, shooter, pos, new fVelocityVector(agl.y,agl.x,0),
         turningRate, pitchRate, acceleration, brakeRate,
         maxVelocity, 0, 0, 1,impactDamage);

      usePolyhedronInstance(new
fPolyhedronInstance(ourDefaultPolyhedron,ourScale));
  &#125;

   public void update (double dt) &#123;
      super.update(dt);
      increaseVelocity(1,dt);

      fPoint3d p=getPosition();
      if((getAge()&gt;4) || (p.y&lt;0))&#123;
         p.y=0;
         setPosition(p);
         die();
      &#125;
   &#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;
   &#125;

   public static void initiateClass (Applet app) &#123;
      //--
      //-- lots of simple monotone code that retrieves the class
      //-- constants. The full source can be found on the CD
      //--
   &#125;
   //--
   //-- lots of class constants
   //--
&#125;
</PRE>
<!-- END CODE //-->
<H4 ALIGN="CENTER"><A NAME="Heading53"></A><FONT COLOR="#000077">The Constructor</FONT></H4>
<P>No surprises here.
</P>
<H4 ALIGN="CENTER"><A NAME="Heading54"></A><FONT COLOR="#000077">The update() Method</FONT></H4>
<P>The missile has a lifetime. If it exceeds a certain &#147;age&#148; or hits the ground, it will die. Otherwise, it just keeps accelerating.
</P>
<H4 ALIGN="CENTER"><A NAME="Heading55"></A><FONT COLOR="#000077">The die() Method</FONT></H4>
<P>When the missile dies, it will produce fragments.
</P>
<H4 ALIGN="CENTER"><A NAME="Heading56"></A><FONT COLOR="#000077">The Mini-Cannon, Extension of cmAbstractWeapon</FONT></H4>
<P>This weapon, shown in Listing 12-25, is very much like the missile launcher except that the round for this weapon is a mini-cannon round. Other than that, this class is identical to the missile launcher.
</P>
<P><B>Listing 12-25</B> The mini-cannon</P>
<!-- CODE //-->
<PRE>
class cmMinicannon extends cmAbstractWeapon &#123;
   cmMinicannon(cmAbstractVehicle host,fPoint3d relPos)&#123;
      super(host,relPos,loadingtime,defaultammo);
   &#125;

   public boolean fire()&#123;
      if(super.fire())&#123;
         fPoint3d p=theHost.getPosition();
         p.plus(relOrigin);
         new cmMinicannonRound( theHost.getWorld(), theHost, p,
            theHost.getAngle());
         return true;
      &#125;
      return false;
   &#125;

   public String getName()&#123;
      return name;
   &#125;
   public static void initiateClass (Applet app) &#123;
      //--
      //-- lots of simple monotone code that retrieves the class
      //-- constants. The full source can be found on the CD
      //--
   &#125;
   //--
   //-- lots of class constants
   //--
&#125;
</PRE>
<!-- END CODE //-->
<H4 ALIGN="CENTER"><A NAME="Heading57"></A><FONT COLOR="#000077">The Mini-Cannon Round, Extension of cmAbstractRound</FONT></H4>
<P>The mini-cannon round (see Figure 12-14 and Listing 12-26) is an object with a simple behavior. It is very much like the missile, except that this round doesn&#146;t accelerate but starts with its maximum velocity and travels until it hits something or dies because of old age.
</P>
<P><A NAME="Fig14"></A><A HREF="javascript:displayWindow('images/12-14.jpg',356,240 )"><IMG SRC="images/12-14t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/12-14.jpg',356,240)"><FONT COLOR="#000077"><B>Figure 12-14</B></FONT></A>&nbsp;&nbsp;A mini-cannon projectile</P>
<P><B>Listing 12-26</B> The mini-cannon round</P>
<!-- CODE //-->
<PRE>
class cmMinicannonRound extends cmAbstractRound&#123;
    cmMinicannonRound (fWorld w, fObject shooter,
       fPoint3d pos, fAngle3d agl)
    &#123;
      super(w,shooter,pos,new fVelocityVector(agl.y,agl.x,maxVelocity),
         0,0,0,0,maxVelocity,0,0,1,impactDamage);

      //-- the polyhedron instance
      usePolyhedronInstance(new
fPolyhedronInstance(ourDefaultPolyhedron,ourScale));
      //-- create an empty shell
      new cmMinicannonShell(getWorld(),pos,agl,
         ((cmAbstractMovingObject)shooter).getdPosition());
    &#125;

   public void update (double dt) &#123;
      super.update(dt);
      //-- check if it is time to die
      if(dieNextUpdate==true)&#123;
         die();
         return;

      &#125;

      fPoint3d p=getPosition();
      //-- if bullet hits the ground then die
      //-- next round so that a collision detection
      //-- can be done
      if(p.y&lt;0)&#123;
         p.y=0;
         setPosition(p);
         dieNextUpdate=true;
      &#125;
      //-- if life is out
      if(getAge()&gt;lifeTime)&#123;
         deadOfAge=true;
         die();
      &#125;
   &#125;

   protected void die () &#123;
      super.die();
      //-- if this round has died by hitting something
      if(deadOfAge==false)&#123;
         for(int n=0;n&lt;fragmentsWhenDead;n&#43;&#43;)&#123;
            new cmGenericFragment(getWorld(), fragmentSize,
               getPosition(), fragmentSpread, fragmentGenerations,
               fragmentSpeed, fragmentRotation);
         &#125;
      &#125;
   &#125;
   protected boolean dieNextUpdate;
   protected boolean deadOfAge;

   public static void initiateClass (Applet app) &#123;
      //--
      //-- lots of simple monotone code that retrieves the class
      //-- constants. The full source can be found on the CD
      //--
   &#125;
   //--
   //-- lots of class constants
   //--
&#125;
</PRE>
<!-- END CODE //-->
<H4 ALIGN="CENTER"><A NAME="Heading58"></A><FONT COLOR="#000077">The Constructor</FONT></H4>
<DL>
<DD><B>&#149;</B>&nbsp;&nbsp;The superclass is a vehicle with very few capabilities. As you can see in the constructor, this round can only travel forward at maximum velocity.
<DD><B>&#149;</B>&nbsp;&nbsp;The standard assignment of a 3D model.
<DD><B>&#149;</B>&nbsp;&nbsp;An empty shell is created to enhance the effect.
</DL>
<H4 ALIGN="CENTER"><A NAME="Heading59"></A><FONT COLOR="#000077">The update() Method</FONT></H4>
<P>Since a bullet travels at very high speeds, a collision can be &#147;missed.&#148; The bullet might travel right through the object without ever actually touching the object. This is especially annoying when the round is fired from an airborne vehicle at a ground vehicle. For this reason, when a round hits the ground, it will remain there one frame so that a possible collision will be detected.
</P>
<P>A round can also die of different reasons. If it dies of age, then that is marked, because the round should just disappear without producing any fragments. On the other hand, if the round dies because of the result of an impact, it should produce fragments.</P>
<DL>
<DD><B>&#149;</B>&nbsp;&nbsp;If this round is already dead, complete it by calling die() and return.
<DD><B>&#149;</B>&nbsp;&nbsp;If the round is below ground level, then set it to ground level and mark it as dead.
</DL>
<H4 ALIGN="CENTER"><A NAME="Heading60"></A><FONT COLOR="#000077">The die() Method</FONT></H4>
<P>If the bullet has died of old age and not because of an impact, then just die without any further actions; otherwise, make a nice exit.
</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="478-483.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="488-491.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>


</BODY>

⌨️ 快捷键说明

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