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

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

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="470-474.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="478-483.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading40"></A><FONT COLOR="#000077">The Tank Remains, Extending cmAbstractMovingScenery</FONT></H4>
<P>I have always hated games in which things just explode into nothing. For this reason I have decided that each dead object of significant size should leave something after its death&#151;as does the tank in Figure 12-9. The remains class, shown in Listing 12-19, is constructed when a tank is killed. The behavior is to be thrown up in the air at a random rotation. When this object hits the ground it will remain there for the rest of the game.
</P>
<P><A NAME="Fig9"></A><A HREF="javascript:displayWindow('images/12-09.jpg',230,194 )"><IMG SRC="images/12-09t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/12-09.jpg',230,194)"><FONT COLOR="#000077"><B>Figure 12-9</B></FONT></A>&nbsp;&nbsp;The tank remains</P>
<P><B>Listing 12-19</B> The Fesse tank remains</P>
<!-- CODE //-->
<PRE>
class cmFesseTankRemains extends cmAbstractMovingScenery &#123;

   cmFesseTankRemains(fWorld w, cmFesseTank deadTank)&#123;
      super(w,deadTank.getPosition(), deadTank.getAngle(),
          deadTank.getdPosition(), deadTank.getdAngle());

      usePolyhedronInstance(new fPolyhedronInstance(
         ourDefaultPolyhedron,ourScale));

      //-- throw the remaining tank up in the air
      fPoint3d dp=getdPosition();
      dp.y=fWorld.rand(0,ourSpeedUp);
      setdPosition(dp);
      //-- set a random rotation on the remaining tank
      setdAngle(new fAngle3d(fWorld.rand(-ourRandRot,ourRandRot),
         fWorld.rand(-ourRandRot,ourRandRot),
         fWorld.rand(-ourRandRot,ourRandRot)));
   &#125;

public void update(double dt)&#123;
      super.update(dt);
      fPoint3d p=getPosition();
      //-- check if hit the ground
      if(p.y&lt;ourScale.y)&#123;
         fPoint3d dp=getdPosition();
         p.y=ourScale.y;
         dp.x=dp.y=dp.z=0;
         setPosition(p);
         fAngle3d a=getAngle();
         a.x=a.z=0;
         setAngle(a);
         setdAngle(new fAngle3d(0,0,0));
         setdPosition(dp);
      &#125; else if(p.y&gt;ourScale.y) &#123;
         fPoint3d dp=getdPosition();
         dp.y&#43;=((cmWorld)getWorld()).gravity*dt;
         setdPosition(dp);
      &#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="Heading41"></A><FONT COLOR="#000077">The Constructor</FONT></H4>
<DL>
<DD><B>&#149;</B>&nbsp;&nbsp;The superclass is initiated with the dead tank&#146;s position, angle, velocity, and angular velocity.
<DD><B>&#149;</B>&nbsp;&nbsp;The velocity with respect to the y-axis is set to a random value so that the remains of the tank lift off the ground.
<DD><B>&#149;</B>&nbsp;&nbsp;The angular velocity is set to a random value so that the tank remains spin in the air.
</DL>
<H4 ALIGN="CENTER"><A NAME="Heading42"></A><FONT COLOR="#000077">The update() Method</FONT></H4>
<DL>
<DD><B>&#149;</B>&nbsp;&nbsp;Let the base class do the default updating.
<DD><B>&#149;</B>&nbsp;&nbsp;Check if the object has hit the ground.
<DD><B>&#149;</B>&nbsp;&nbsp;If that is the case, then set velocity to zero and the angle about the x- and z-axes to zero.
<DD><B>&#149;</B>&nbsp;&nbsp;If it is still airborne, just let the gravity do its share.
</DL>
<H4 ALIGN="LEFT"><A NAME="Heading43"></A><FONT COLOR="#000077">The Glider, Extending cmAbstractPlayer</FONT></H4>
<P>The glider in Combat Machines 96 (see Figure 12-10 and Listing 12-20) is some sort of an airplane that can hover and turn in any direction it wants. It is not your regular Kmart glider, but a vehicle with special abilities. The glider carries three weapons: a mini-cannon, dumb missiles, and bombs. The only additional behavior for this vehicle is that it can hit the ground, in which case it will lose health depending on the velocity.
</P>
<P><A NAME="Fig10"></A><A HREF="javascript:displayWindow('images/12-10.jpg',170,74 )"><IMG SRC="images/12-10t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/12-10.jpg',170,74)"><FONT COLOR="#000077"><B>Figure 12-10</B></FONT></A>&nbsp;&nbsp;The glider</P>
<P><B>Listing 12-20</B> The glider</P>
<!-- CODE //-->
<PRE>
class cmGlider extends cmAbstractPlayer&#123;
   cmGlider (fWorld w, double x, double z, double y,
      double turn, double pitch)
   &#123;
      super(w,new fPoint3d(x,ourScale.y&#43;y,z),
         new fVelocityVector(turn,pitch,0),turningRate, pitchRate,
         acceleration, brakeRate, maxVelocity, climbRate, decentRate, 1,
         ourHealth);

      //-- use the default polyhedron instance
      usePolyhedronInstance(new
fPolyhedronInstance(ourDefaultPolyhedron,ourScale));

      //-- add the weapons
      addWeapon(new cmMinicannon(this,new fPoint3d(0,ourScale.y,0)));
      addWeapon(new cmMissileLauncher(this,
         new fPoint3d(0,ourScale.y,0)));
      addWeapon(new cmBombBay(this,new fPoint3d(0,ourScale.y,0)));
      selectWeapon(0);
   &#125;

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

      //-- check collision with ground
      fPoint3d p=getPosition();
      if(p.y&lt;ourScale.y)&#123;
      p.y=ourScale.y;
         setPosition(p);
         fVelocityVector dp=(fVelocityVector)getdPosition();
         dp.setAngleAboutXaxis(0);
      dp.setVelocity(0);
      setdPosition(dp);
      //-- some damage depending on the speed
      double vel=((fVelocityVector)getdPosition()).getVelocity();
      setHealth(getHealth()-vel);
      &#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;
      new cmGliderRemains(getWorld(),this);

   &#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="Heading44"></A><FONT COLOR="#000077">The Constructor</FONT></H4>
<P>The constructor is pretty much a copy of the constructor for cmFesseTank, except that it contains more parameters. The new parameters are specific for airborne vehicles and are pretty much self-explanatory.
</P>
<P>The glider also has a new weapon: the bomb.</P>
<H4 ALIGN="CENTER"><A NAME="Heading45"></A><FONT COLOR="#000077">The update() Method</FONT></H4>
<P>The position of the glider is checked to be sure that it is above the ground. If that is not the case, then the glider has crashed and the health is decreased depending on the velocity.
</P>
<H4 ALIGN="CENTER"><A NAME="Heading46"></A><FONT COLOR="#000077">The die() Method</FONT></H4>
<P>The glider leaves some fragments and some remains after its unfortunate death.
</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="470-474.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="478-483.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>


</BODY>

⌨️ 快捷键说明

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