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

📄 462-465.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=462-465//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="458-461.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="465-467.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
</P>
<H4 ALIGN="LEFT"><A NAME="Heading24"></A><FONT COLOR="#000077">How the Game Layer Works</FONT></H4>
<P>In all 3D games we find buildings, vehicles, weapons, and scenery, among other things. All objects within such sets have something in common. All buildings, for example, are static objects. All vehicles have a maximum velocity, turning rate, and so on. To make the development of games easier, we will implement a set of abstract classes that cover most types of objects. This abstract layer will also implement their default behavior. Once this layer is implemented, the designing of new objects is trivial. The game layer will be a tree of classes, as shown in Figure 12-6.
</P>
<P><A NAME="Fig6"></A><A HREF="javascript:displayWindow('images/12-06.jpg',640,432 )"><IMG SRC="images/12-06t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/12-06.jpg',640,432)"><FONT COLOR="#000077"><B>Figure 12-6</B></FONT></A>&nbsp;&nbsp;The game layer</P>
<P>Since Java lacks multiple inheritance, the game layer is divided into two major branches: moving objects and static objects. Each of these branches is further divided into specialized types of moving/static objects. For example, the cmAbstractStaticStructure implements the default behavior and what is in common to all buildings.
</P>
<P>The good thing about this tree structure is that an object can express interest in collisions with a whole branch of classes. For example, if a vehicle is interested in collisions with buildings, it will get all collisions with all buildings, whether the building is the Empire State Building or a small cottage in the Canadian wilderness. This is because both of these buildings are subclasses of cmAbstractStaticStructure. If a new object is introduced into the game, the already-implemented objects will react to it in a somewhat realistic manner. Say that all objects know what to do if they collide with a structure. Suppose we wish to insert a new type of structure called PentagonBuilding. Even though the rest of the objects in the world don&#146;t know exactly what PentagonBuilding is, they know that it is a cmAbstractStaticStructure and treat it accordingly. This means that no additional coding is needed if the objects don&#146;t have a reason to treat it differently.</P>
<P>The classes in the game layer implement the default behavior for the different types of objects. For example, all vehicles are interested in collisions with structures. If a vehicle collides with a building, its default behavior is to stop. On the other hand, structures are never interested in collisions with vehicles. This can, of course, be discussed, since a small cottage should definitely be interested in collisions with bulldozers, for example.</P>
<P>The behavior of a game layer class can be extended but should not be ignored. An extension of cmAbstractVehicle might be interested in collisions with other objects on top of the default for cmAbstractVehicle. It can also change the behavior when a collision occurs. Another thing that all vehicles have in common is that they can be steered and are limited by their capabilities. Some vehicles might be able to take tighter turns than others, or accelerate faster, for example. The steering of a vehicle is done through the methods that cmAbstractVehicle supplies. This means that all extensions of this class can be controlled through these methods.</P>
<H4 ALIGN="LEFT"><A NAME="Heading25"></A><FONT COLOR="#000077">The Game Layer Classes</FONT></H4>
<P>Most of these classes contain very little or trivial code, except for the cmAbstractVehicle, which contains large amounts of code.
</P>
<H4 ALIGN="CENTER"><A NAME="Heading26"></A><FONT COLOR="#000077">The cmAbstractStaticObject Class</FONT></H4>
<P>This is the abstract representation of a static object. The code for it is shown in Listing 12-8. The only reason this class exists is to draw a clear line between App3Dcore and the game layer. It also contains a variable that specifies the amount of &#147;health&#148; this object has. If no health is specified, then it is assumed that this object is indestructible.
</P>
<P><B>Listing 12-8</B> The abstract static object</P>
<!-- CODE //-->
<PRE>
abstract class cmAbstractStaticObject extends fObject&#123;
   protected cmAbstractStaticObject (fWorld w, fPoint3d pos,
                                   fAngle3d ang)

   &#123;
      super(w,pos,ang);
      myHealth=Double.MAX_VALUE;
   &#125;
   protected cmAbstractStaticObject (fWorld w, fPoint3d pos,
                                   fAngle3d ang,double health)
   &#123;
      super(w,pos,ang);
      myHealth=health;
   &#125;
   public double getHealth()&#123;
      return myHealth;
   &#125;
   public void setHealth(double e)&#123;
      myHealth=e;
   &#125;
   protected double myHealth;
&#125;
</PRE>
<!-- END CODE //-->
<H4 ALIGN="CENTER"><A NAME="Heading27"></A><FONT COLOR="#000077">The cmAbstractMovingObject Class</FONT></H4>
<P>This is an abstract representation of a moving object. The code is shown in Listing 12-9.
</P>
<P><B>Listing 12-9</B> The abstract moving object</P>
<!-- CODE //-->
<PRE>
abstract class cmAbstractMovingObject extends fMovingObject&#123;
   protected cmAbstractMovingObject (fWorld w, fPoint3d pos,
                                   fAngle3d agl, fPoint3d dpos,
                                   fAngle3d dagl)
  &#123;
      super(w,pos,agl,dpos,dagl);
      myHealth=Double.MAX_VALUE;
   &#125;
   protected cmAbstractMovingObject (fWorld w, fPoint3d pos,
                                   fAngle3d agl, fPoint3d dpos,
                                   fAngle3d dagl,double health)
  &#123;
      super(w,pos,agl,dpos,dagl);
      myHealth=health;
   &#125;
   public double getHealth()&#123;
      return myHealth;
   &#125;
   public void setHealth(double h)&#123;
      myHealth=h;
   &#125;
   protected double myHealth;
&#125;
</PRE>
<!-- END CODE //-->
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="458-461.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="465-467.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>


</BODY>

⌨️ 快捷键说明

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