📄 478-483.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, Macmillan Computer Publishing
<br>
<b>ISBN:</b> 1571690433<b> Pub Date:</b> 11/01/96</font>
</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=478-483//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="474-478.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="483-488.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="CENTER"><A NAME="Heading47"></A><FONT COLOR="#000077">The Glider Remains, Extending cmAbstractMovingScenery</FONT></H4>
<P>This class, shown in Listing 12-21, is almost identical to the remains of the tank. The only difference is that the junk left after a glider is destroyed will simply fall to the ground instead of being thrown up in the air. Figure 12-11 displays the glider remains.
</P>
<P><B>Listing 12-21</B> The glider remains</P>
<!-- CODE //-->
<PRE>
class cmGliderRemains extends cmAbstractMovingScenery {
cmGliderRemains(fWorld theWorld,cmGlider g){
super(theWorld,g.getPosition(),g.getAngle(),
((fPoint3d)g.getdPosition()),g.getdAngle());
//-- use the default polyhedron instance
usePolyhedronInstance(new
fPolyhedronInstance(ourDefaultPolyhedron,ourScale));
//-- set a random rotation on the remaining glider
setdAngle(new fAngle3d(fWorld.rand(-ourRandRot,ourRandRot),
fWorld.rand(-ourRandRot,ourRandRot),
fWorld.rand(-ourRandRot,ourRandRot)));
}
public void update(double dt){
super.update(dt);
fPoint3d p=getPosition();
//-- check if collision with ground
if(p.y<ourScale.y){
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);
} else if(p.y>ourScale.y) {
//-- gravity
fPoint3d dp=getdPosition();
dp.y+=((cmWorld)getWorld()).gravity*dt;
setdPosition(dp);
}
}
public static void initiateClass (Applet app) {
//--
//-- lots of simple monotone code that retrieves the class
//-- constants. The full source can be found on the CD
//--
}
//--
//-- lots of class constants
//--
}
</PRE>
<!-- END CODE //-->
<P><A NAME="Fig11"></A><A HREF="javascript:displayWindow('images/12-11.jpg',212,80 )"><IMG SRC="images/12-11t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/12-11.jpg',212,80)"><FONT COLOR="#000077"><B>Figure 12-11</B></FONT></A> The glider remains</P>
<H4 ALIGN="LEFT"><A NAME="Heading48"></A><FONT COLOR="#000077">The Buildings, Extensions of cmAbstractStaticStructure</FONT></H4>
<P>There are a couple of different buildings in the game, but the only difference between them is their 3D model and scaling—both types are shown in Figure 12-12 and Listing 12-22. The buildings have no additional behavior on top of the default for their base class. They simply stand there until they are destroyed. The code for these two classes is similar, so only one of the listings is shown.
</P>
<P><A NAME="Fig12"></A><A HREF="javascript:displayWindow('images/12-12.jpg',364,264 )"><IMG SRC="images/12-12t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/12-12.jpg',364,264)"><FONT COLOR="#000077"><B>Figure 12-12</B></FONT></A> The generic buildings</P>
<P><B>Listing 12-22</B> The generic buildings</P>
<!-- CODE //-->
<PRE>
class cmGenericBuilding extends cmAbstractStaticStructure{
/**
* Generic building with an angle.
*/
cmGenericBuilding (fWorld world, double x, double z, fAngle3d agl,
double w, double b, double h)
{
super(world,x,z,agl,w,b,h,ourHealth);
//-- make a building
myWidth=w; myBredth=b; myHeight=h;
usePolyhedronInstance(new
fPolyhedronInstance(ourDefaultPolyhedron,new fPoint3d(w,h,b)));
}
/**
* Generic building with the default 0,0,0 angle.
*/
cmGenericBuilding (fWorld world, double x, double z, double w,
double b, double h)
{
super(world,x,z,new fAngle3d(),w,b,h,ourHealth);
//-- make a building
myWidth=w; myBredth=b; myHeight=h;
usePolyhedronInstance(new
fPolyhedronInstance(ourDefaultPolyhedron,new fPoint3d(w,h,b)));
}
protected void die () {
super.die();
fPoint3d pos=getPosition();
new cmGenericBuildingRuin(getWorld(),pos.x,pos.z,getAngle(),
myWidth,myBredth,myHeight*.2);
int nbr=(int)(relFragsWhenDead*myHeight);
for(int n=0;n<nbr;n++){
new cmGenericFragment(getWorld(),relFragSize*myHeight,
getPosition(),relFragSpread*myHeight,
(int)(myHeight*relFragGens),myHeight*relFragSpeed,
myHeight*relFragRot);
}
}
public static void initiateClass (Applet app) {
//--
//-- lots of simple monotone code that retrieves the class
//-- constants. The full source can be found on the CD
//--
}
//--
//-- lots of class constants
//--
}
</PRE>
<!-- END CODE //-->
<H4 ALIGN="LEFT"><A NAME="Heading49"></A><FONT COLOR="#000077">The Missile Launcher, Extension of cmAbstractWeapon</FONT></H4>
<P>The missile launcher, shown in Listing 12-23, is a weapon that shoots missiles. The terms “weapon” and “round” are very abstract in these circumstances. This means that the round for this weapon is a missile.
</P>
<P><B>Listing 12-23</B> The missile launcher</P>
<!-- CODE //-->
<PRE>
class cmMissileLauncher extends cmAbstractWeapon {
cmMissileLauncher(cmAbstractVehicle host,fPoint3d relPos){
super(host,relPos,loadingtime,defaultammo);
}
public boolean fire(){
if(super.fire()){
//-- create a new missile
fPoint3d p=theHost.getPosition();
p.plus(relOrigin);
new cmGenericMissile(theHost.getWorld(), theHost,
p, theHost.getAngle());
return true;
}
return false;
}
public String getName(){
return name;
}
public static void initiateClass (Applet app) {
//--
//-- lots of simple monotone code that retrieves the class
//-- constants. The full source can be found on the CD
//--
}
//--
//-- lots of class constants
//--
}
</PRE>
<!-- END CODE //-->
<H4 ALIGN="CENTER"><A NAME="Heading50"></A><FONT COLOR="#000077">The Constructor</FONT></H4>
<P>The only parameters are the host vehicle and the relative position where the weapon is mounted. The relative position decides where the actual round for this weapon is created.
</P>
<H4 ALIGN="CENTER"><A NAME="Heading51"></A><FONT COLOR="#000077">The fire() Method</FONT></H4>
<P>If the fire() method of the abstract weapon returns <I>true,</I> then the weapon has succeeded in firing a round. The round for this weapon is a generic missile that is created. The rest is taken care of in the missile class.</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="474-478.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="483-488.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</BODY>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -