📄 ship3d.java
字号:
import java.applet.*;
import java.awt.*;
class Ship3D extends Object3D
{
static final int SHIP_ROT_SPEED = 3;
static final int SHIP_ZROT_SPEED = 3;
static final int SHIP_Z_SPEED = 6;
static final int MAX_SHOTS = 4;
double Object[] = { 0, -20, 0,
20, 20, 0,
-20, 20, 0,
0, 0, -4,
0, 0, 4};
int Lines[] = { 0,1, 0,2, 1,3, 2,3, 1,4, 2,4};
int Winkel = 0;
int XRadius = 240;
int YRadius = 200;
static int LaserCount;
static int Destroyed;
static int CenterXPos;
static int CenterZPos;
static Object3D Player;
public Ship3D()
{
Name = "Ship3D";
LaserCount = 0;
Destroyed = 0;
CenterXPos = 0;
CenterZPos = 0;
Player = this;
VertNum = Object.length / 3;
LineNum = Lines.length / 2;
Vertices = new Vector3D[VertNum];
Points = new Point2D[VertNum];
for (int i=0; i<VertNum; i++)
{
Points[i] = new Point2D();
Vertices[i] = new Vector3D();
Vertices[i].x = Object[i*3+X];
Vertices[i].y = Object[i*3+Y];
Vertices[i].z = Object[i*3+Z];
}
Visible = true;
XRot = 20;
}
public void Draw(Graphics g, Matrix3D CameraMatrix)
{
if (Visible)
{
Matrix3D MyMatrix = new Matrix3D();
MyMatrix.RotateZ(ZRot);
MyMatrix.RotateX(XRot);
MyMatrix.RotateY(YRot);
MyMatrix.Scale(XScale,YScale,ZScale);
MyMatrix.Shear(XShear,YShear);
MyMatrix.Translate(XPos,YPos,ZPos);
MyMatrix.Merge(CameraMatrix.GetMatrix());
for (int i=0; i<VertNum; i++)
{
MyMatrix.Transform(Vertices[i]);
Points[i] = Point2D.Projection(Vertices[i]);
}
for (int i=0; i<LineNum; i++)
{
g.setColor(Color.green);
g.drawLine(Points[Lines[i*2]].x,Points[Lines[i*2]].y,Points[Lines[i*2+1]].x,Points[Lines[i*2+1]].y);
}
}
}
public boolean Handler()
{
if (Destroyed > 0)
{
if (Destroyed > 61)
{
Destroyed--;
return true;
}
else
{
Destroyed--;
Visible = !(Visible);
}
}
XPos = (int)(XRadius * Matrix3D.Sin(Winkel));
YPos = (int)(YRadius * Matrix3D.Cos(Winkel));
if (WebWar.Key_U)
{
if (CenterZPos < 32)
{
CenterZPos++;
ZPos += SHIP_Z_SPEED;
Camera3D.YPos += 3;
}
}
if (WebWar.Key_D)
{
if (CenterZPos > 0)
{
CenterZPos--;
ZPos -= SHIP_Z_SPEED;
Camera3D.YPos -= 3;
}
}
if (WebWar.Key_L)
{
if (CenterXPos < 32)
{
CenterXPos++;
ZRot += SHIP_ZROT_SPEED;
if (ZRot >= 360) ZRot -= 360;
Winkel -= SHIP_ROT_SPEED;
if (Winkel < 0) Winkel += 360;
if (CenterXPos >= 0)
{
XRot = 20 + (int)(CenterXPos * 0.4);
if (XRot < 0) XRot += 360;
YRot = (int)(CenterXPos * 2.5);
}
else
{
XRot = 20 + (int)(CenterXPos * -0.4);
if (XRot < 0) XRot += 360;
YRot = 360 + (int)(CenterXPos * 2.5);
}
Camera3D.XPos += 6;
Camera3D.ZRot += 0.5;
if (Camera3D.ZRot >= 360) Camera3D.ZRot -= 360;
}
}
if (WebWar.Key_R)
{
if (CenterXPos > -32)
{
CenterXPos--;
ZRot -= SHIP_ZROT_SPEED;
if (ZRot < 0) ZRot += 360;
Winkel += SHIP_ROT_SPEED;
if (Winkel >= 360) Winkel -= 360;
if (CenterXPos >= 0)
{
XRot = 20 + (int)(CenterXPos * 0.4);
if (XRot < 0) XRot += 360;
YRot = (int)(CenterXPos * 2.5);
}
else
{
XRot = 20 + (int)(CenterXPos * 0.4);
if (XRot < 0) XRot += 360;
YRot = 360 + (int)(CenterXPos * 2.5);
}
Camera3D.XPos -= 6;
Camera3D.ZRot -= 0.5;
if (Camera3D.ZRot < 0) Camera3D.ZRot += 360;
}
}
if (WebWar.KeyDownCode == 32)
{
WebWar.KeyDownCode = 0;
if (LaserCount < MAX_SHOTS)
{
WebWar.GetNewObject(new Shot3D(XPos,YPos,ZPos,XRot,YRot,ZRot));
LaserCount++;
}
}
return true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -