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

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

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="465-467.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="470-474.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="CENTER"><A NAME="Heading32"></A><FONT COLOR="#000077">The cmAbstractVehicle Class</FONT></H4>
<P>This is the most comprehensive class in the game layer. What distinguishes a vehicle from all other moving objects is that it can be controlled. It also travels in the direction that it faces. Controlling the vehicle is done through the methods supplied in the class. The abstract vehicle is interested in collisions with structures, and the default behavior is to stop.
</P>
<P>The steering of the vehicle is done through methods like turnLeft(), turnRight(), and so on. The <I>factor</I> argument in these methods must be between 0 and 1 and tells the vehicle how much of its steering capability it should use. If we used an analogous joystick to control an object, then the factor would depend on the position of the stick.</P>
<P>Listing 12-14 shows the abstract vehicle class. Due to the very large source, only the &#147;header&#148; is shown. The full source can be found on the CD-ROM.</P>
<P><B>Listing 12-14</B> The abstract vehicle</P>
<!-- CODE //-->
<PRE>
abstract class cmAbstractVehicle extends cmAbstractMovingObject&#123;
   //-- the constructor for flying vehicles
   protected cmAbstractVehicle (fWorld w, fPoint3d pos,
             fVelocityVector v, double turningrate,
             double pitchrate, double acceleration,
             double brakingrate, double maxVelocity,
             double climbrate, double decentrate,
             double pitchClimbrateRelation)
   //-- the constructor for ground vehicles
   protected cmAbstractVehicle (fWorld w, fPoint3d pos,
             fVelocityVector v, double turningrate0,
             double acceleration0, double brakingrate0,
             double maxVelocity0)

   public void addWeapon(cmAbstractWeapon wep)

   public void removeWeapon(cmAbstractWeapon wep)

   public boolean selectWeapon(int wepnbr)

   public void update (double dt)

   //--
   //-- event creating methods
   //--
   public void fireSelectedWeapon()

   public void turnLeft (double factor, double dt)

   public void turnRight (double factor, double dt)

   public void increaseVelocity (double factor, double dt)

   public void decreaseVelocity (double factor, double dt)

   public void brake (double factor, double dt)

   public void climb (double factor, double dt)

   public void decent (double factor, double dt)

   public void pitchUp (double factor, double dt)

   public void pitchDown (double factor, double dt)
&#125;
</PRE>
<!-- END CODE //-->
<H4 ALIGN="CENTER"><A NAME="Heading33"></A><FONT COLOR="#000077">The cmAbstractPlayer Class</FONT></H4>
<P>An abstract player is an extension of a vehicle. The code for this class is shown in Listing 12-15. A player can either be controlled directly by a human or by a computer-controlled player. The computer players are controlled by the cmAbstractBrain. Describing how to implement an artificial intelligence is, however, beyond the scope of this chapter; therefore it will not be discussed.
</P>
<P><B>Listing 12-15</B> The abstract player</P>
<!-- CODE //-->
<PRE>
abstract class cmAbstractPlayer extends cmAbstractVehicle&#123;
   protected cmAbstractPlayer (fWorld w, fPoint3d pos, fVelocityVector v,
                              double turningrate, double pitchrate,
                              double acceleration, double brakingrate,
                              double maxVelocity, double climbrate,
                              double decentrate,
                              double pitchClimbrateFactor)
   &#123;
   super(w,pos,v,turningrate,pitchrate,acceleration,
         brakingrate,maxVelocity,climbrate,
         decentrate,pitchClimbrateFactor);
   &#125;

   protected cmAbstractPlayer(fWorld w, fPoint3d pos,fVelocityVector v,
                              double turningrate,double acceleration,
                              double brakingrate,double maxVelocity)
   &#123;
      super(w,pos,v,turningrate,acceleration,brakingrate,maxVelocity);
   &#125;

   public void update (double dt) &#123;
      super.update(dt);
      if(myBrain!=null)&#123;
         myBrain.update(dt);
      &#125;
   &#125;

   public void setBrain(cmAbstractBrain brain)&#123;
      myBrain=brain;
   &#125;

   protected cmAbstractBrain myBrain;
&#125;
</PRE>
<!-- END CODE //-->
<H4 ALIGN="CENTER"><A NAME="Heading34"></A><FONT COLOR="#000077">The cmAbstractWeapon Class</FONT></H4>
<P>This class, shown in Listing 12-16, implements the abstract behavior of a weapon. All weapons are mounted on a host vehicle at a relative position. Every weapon has a loading time and ammo.
</P>
<P><B>Listing 12-16</B> The abstract weapon</P>
<!-- CODE //-->
<PRE>
abstract class cmAbstractWeapon extends Object &#123;
   protected cmAbstractWeapon(cmAbstractVehicle host,fPoint3d relPos,
                             double loadingTime0,int ammo0)
   &#123;
      loadingTime=loadingTime0;
      ammo=ammo0;
      theHost=host;
      relOrigin=relPos;
   &#125;

   public void addAmmo(int nbr)&#123;
      ammo&#43;=nbr;
   &#125;

      public void update(double dt)&#123;
      lastFire-=dt;
      if(lastFire&lt;0)&#123;
         lastFire=0;
      &#125;
   &#125;

      public boolean fire()&#123;
      if(lastFire&gt;0) return false;
      if(ammo&lt;=0) return false;
      ammo--;
      lastFire=loadingTime;
      return true;
   &#125;

   public int getAmmo()&#123;
      return ammo;
   &#125;

   public String getName()&#123;
      return null;
   &#125;

   protected fPoint3d relOrigin;
   protected double loadingTime;
   protected double lastFire;
   protected int ammo;
   protected cmAbstractVehicle theHost;
&#125;
</PRE>
<!-- END CODE //-->
<P>This class must be extended and the fire() method must be overridden by creating the actual projectile or round of this particular weapon.
</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="465-467.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="470-474.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>


</BODY>

⌨️ 快捷键说明

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