📄 488-491.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=488-491//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="483-488.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="491-493.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading61"></A><FONT COLOR="#000077">The Abstract Shell, Extension of cmAbstractMovingScenery</FONT></H4>
<P>Every time a projectile is fired, an empty shell will be tossed out of the weapon. There are different kinds of shells, but they all have something in common. The common behavior is that they are all tossed up and have a random spread and rotation.
</P>
<P>Note that this is an abstract class and therefore does not have the initializeClass() method. Like all abstract classes, the constructor is full of arguments. Listing 12-27 shows the abstract shell.</P>
<P><B>Listing 12-27</B> The abstract shell</P>
<!-- CODE //-->
<PRE>
abstract class cmAbstractShell extends cmAbstractMovingScenery{
protected cmAbstractShell (fWorld w, fPoint3d origin,
fAngle3d agl, fPoint3d dpos, fAngle3d dagl, double randomSpread,
double randomRotation, double lifeTime0)
{
super(w,origin,agl,
new fPoint3d(dpos.x+fWorld.rand(-randomSpread,randomSpread),
dpos.y+fWorld.rand(-randomSpread,randomSpread),
dpos.z+fWorld.rand(-randomSpread,randomSpread)),
new fAngle3d(dagl.x+fWorld.rand(-randomRotation,randomRotation),
dagl.y+fWorld.rand(-randomRotation,randomRotation),
dagl.z+fWorld.rand( randomRotation,randomRotation))
);
lifeTime = lifeTime0;
}
public void update (double dt) {
super.update(dt);
fPoint3d v=getdPosition();
v.y+=((cmWorld)getWorld()).gravity*dt;
setdPosition(v);
if( (getPosition().y<0) || (getAge()>lifeTime) ){
die();
}
}
protected double lifeTime;
}
</PRE>
<!-- END CODE //-->
<H4 ALIGN="CENTER"><A NAME="Heading62"></A><FONT COLOR="#000077">The Constructor</FONT></H4>
<P>The velocity and angular velocity of the object is affected by the random values supplied in the construct.
</P>
<H4 ALIGN="CENTER"><A NAME="Heading63"></A><FONT COLOR="#000077">The update() Method</FONT></H4>
<P>If the shell hits the ground or exceeds its lifetime, it will die.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading64"></A><FONT COLOR="#000077">The Mini-Cannon Empty Shell, Extension of cmAbstractShell</FONT></H4>
<P>The mini-cannon shell, unlike other shells, has a certain size and certain 3D model, except that it is just a regular shell. (Refer to Figure 12-14 for examples of shells.) A mini- cannon shell will fly perpendicular to the right of the projectile. Listing 12-28 shows the empty mini-cannon shell.
</P>
<P><B>Listing 12-28</B> The empty shell</P>
<!-- CODE //-->
<PRE>
class cmMinicannonShell extends cmAbstractShell{
cmMinicannonShell (fWorld w, fPoint3d origin, fAngle3d agl,
fPoint3d vel)
{
super(w,origin,agl,
new fPoint3d(vel.x+speed*Math.sin(agl.y+Math.PI/2),
vel.y+speed, vel.z+speed*Math.cos(agl.y+Math.PI/2)),
new fAngle3d(angleX,angleY,angleZ),
randomSpread, randomRotation,lifeTime);
usePolyhedronInstance(new
fPolyhedronInstance(ourDefaultPolyhedron,ourScale));
}
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="Heading65"></A><FONT COLOR="#000077">The Constructor</FONT></H4>
<P>The superclass is initialized by tossing the shell perpendicularly to the right of the projectile.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading66"></A><FONT COLOR="#000077">The Bomb Bay, Extension of cmAbstractWeapon</FONT></H4>
<P>This class is, as strange as it might sound, a weapon that has bombs as rounds. Other than that, it is just like any other weapon. It creates a bomb when fired. The bomb bay is shown in Listing 12-29.
</P>
<P><B>Listing 12-29</B> The bomb bay</P>
<!-- CODE //-->
<PRE>
class cmBombBay extends cmAbstractWeapon {
cmBombBay(cmAbstractVehicle host,fPoint3d relPos){
super(host,relPos,loadingtime,defaultammo);
}
public boolean fire(){
if(super.fire()){
fPoint3d p=theHost.getPosition();
p.plus(relOrigin);
new cmGenericBomb(theHost.getWorld(), theHost.getPosition(),
theHost.getAngle(), theHost.getdPosition(), 15);
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 //-->
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="483-488.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="491-493.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</BODY>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -