本节书摘来异步社区《Java 2D游戏编程入门》一书中的第8章,第8.3节,作者:【美】Timothy Wright(莱特),更多章节内容可以访问云栖社区“异步社区”公众号查看。
8.3 创建一个原型编辑器
现在是时候创建一些多边形了。尽管有可能可以猜到每个点,但要创建9个小行星形状,手动进行的话,工作量还是很大的。就像大多数程序一样,我也很懒。编写一个编辑器来创建多边形,这样会容易很多。位于javagames.prototype包中的PrototypeEditor,如图8.9所示。
如果模型存储为文件,它们将在运行时加载。然而,做这些事情所需的代码还没有介绍过,因此,编辑器将会作弊。当按下空格键的时候,编辑器会产生出能够粘贴到其他源文件中的代码。本书的第二部分将会介绍使用真正的文件,因此,你不必为此而担心。
polygon列表保存了模型的点。mousePos是鼠标的当前位置,布尔变量closed所保存的值,确定了在绘制时多边形的最后一个点是否和第一个点相连。
processInput()方法处理编辑器的交互。当按下鼠标左键时,会给多边形添加一个点。鼠标中键用于切换closed变量。鼠标右键从多边形删除点。C键清除整个多边形,空格键打印出多边形代码。printPolygon()方法向控制台打印出当前的多边形,以便可以将其粘贴到一个文本文件中或复制到其他的代码文件中。打印到调试控制台的输出如下所示:
Vector2f[] v = new Vector2f[] {
new Vector2f(-0.2895149f, 0.3865415f),
new Vector2f(-0.47104853f, -0.28012514f),
new Vector2f(-0.14241004f, 0.15805948f),
new Vector2f(-0.13615024f, -0.30516434f),
new Vector2f(0.28012514f, -0.2832551f),
new Vector2f(0.17370892f, 0.21752739f),
new Vector2f(-0.19248825f, 0.40219092f),
new Vector2f(-0.18622851f, 0.25195616f),
};```
render()方法显示常用的指令,并且绘制多边形以及轴线。drawAxisLines()方法在编辑器中绘制两条线,表示x轴和y轴。
drawPolygon()方法负责渲染当前模型。如果多边形只是一个点,那么,绘制单个的点。接下来,如果多边形有多个点,那么所有的点(除了最后一个点以外)都绘制。如果多边形是闭合的,那么,最后一个点连接到第一个点。如果多边形不是闭合的,那么,从最后一个点到当前鼠标位置绘制一条线。drawPoint()和drawLine()方法都是标准版本。
package javagames.prototype;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javagames.util.*;
public class PrototypeEditor extends SimpleFramework {
private ArrayList polygon;
private Vector2f mousePos;
private boolean closed;
public PrototypeEditor() {
appTitle = "Prototype Editor 1.0";
appWidth = 640;
appHeight = 640;
appBackground = Color.WHITE;
appFPSColor = Color.BLACK;
}
protected void initialize() {
super.initialize();
polygon = new ArrayList();
}
protected void processInput( float delta ) {
super.processInput( delta );
mousePos = getWorldMousePosition();
if( mouse.buttonDownOnce( MouseEvent.BUTTON1 ) ) {
polygon.add( mousePos );
}
if( mouse.buttonDownOnce( MouseEvent.BUTTON2 ) ) {
closed = !closed;
}
if( mouse.buttonDownOnce( MouseEvent.BUTTON3 ) ) {
if( !polygon.isEmpty() ) {
polygon.remove( polygon.size() - 1 );
}
}
if( keyboard.keyDownOnce( KeyEvent.VK_C ) ) {
polygon.clear();
}
if( keyboard.keyDownOnce( KeyEvent.VK_SPACE ) ) {
printPolygon();
}
}
private void printPolygon() {
System.out.println( "Vector2f[] v = new Vector2f[] { " };
for( Vector2f v : polygon ) {
System.out.print( " new Vector2f(" );
System.out.print( v.x + "f, " );
System.out.print( v.y + "f)" );
System.out.println( "," );
}
System.out.println( ");" );
}
protected void render( Graphics g ) {
super.render( g );
g.drawString( "Close Polygon: " + closed, 20, 35 );
g.drawString( "Left Mouse: Add point", 20, 50 );
g.drawString( "Right Mouse: Remove point", 20, 65 );
g.drawString( "Center Mouse: Toggle Close", 20, 80 );
g.drawString( "C: Clear Polygon", 20, 95 );
g.drawString( "Space Bar: Print Polygon", 20, 110 );
drawAxisLines( g );
drawPolygon( g );
}
private void drawAxisLines( Graphics g ) {
g.setColor( Color. BLUE );
Vector2f left = new Vector2f( appWorldWidth / 2.0f, 0.0f );
Vector2f right = new Vector2f( -left.x, 0.0f );
drawLine( g, left, right );
Vector2f top = new Vector2f( 0.0f, appWorldHeight / 2.0f );
Vector2f bottom = new Vector2f( 0.0f, -top.y );
drawLine( g, top, bottom );
}
private void drawPolygon( Graphics g ) {
g.setColor( Color.BLACK );
if( polygon.size() == 1 ) {
drawPoint( g, polygon.get( 0 ) );
}
for( int i = 0; i < polygon.size() - 1; ++i ) {
drawLine( g, polygon.get(i), polygon.get( i+1 ) );
}
if( closed && polygon.size() > 1 ) {
Vector2f P = polygon.get( polygon.size() - 1 );
Vector2f S = polygon.get( 0 );
drawLine( g, S, P );
}
if( !(polygon.isEmpty() || closed) ) {
Vector2f P = polygon.get( polygon.size() - 1 );
Vector2f S = mousePos;
drawLine( g, S, P );
}
}
private void drawPoint( Graphics g, Vector2f v ) {
Matrix3x3f view = getViewportTransform();
Vector2f s = view.mul( v );
g.drawRect( (int)s.x, (int)s.y, 1, 1 );
}
private void drawLine( Graphics g, Vector2f v0, Vector2f v1 ) {
Matrix3x3f view = getViewportTransform();
Vector2f S = view.mul( v0 );
Vector2f P = view.mul( v1 );
g.drawLine( (int)S.x, (int)S.y, (int)P.x, (int)P.y );
}
public static void main( String[] args ) {
launchApp( new PrototypeEditor() );
}
}`