AndEngine之DEMO学习(九)CardinalSplineMoveModifierExample

这个例子又新介绍了一种修改器的实现,CardinalSplineMoveModifier(基数样条移动修改器)。demo的效果是依靠矩形的运动创造出一个心形,当然心形是一个不规则的形状,这里就得使用基数样条的概念。盗用百科:

基数样条是一组单个曲线按照一定的顺序连接而成的一条较大曲线。样条由一系列点指定,并通过每一个指定的点。由于基数样条平滑地穿过组中的每一个点(不出现尖角),因而它比用直线连接创建的路径更精确。

程序中定义了两组X,Y坐标,分别表示心形左右两边的控制点,矩形的移动平滑的穿过几个控制点。下面上代码:

package org.andengine.examples;

import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.modifier.CardinalSplineMoveModifier;
import org.andengine.entity.modifier.CardinalSplineMoveModifier.CardinalSplineMoveModifierConfig;
import org.andengine.entity.modifier.DelayModifier;
import org.andengine.entity.modifier.LoopEntityModifier;
import org.andengine.entity.modifier.ParallelEntityModifier;
import org.andengine.entity.modifier.RotationModifier;
import org.andengine.entity.modifier.SequenceEntityModifier;
import org.andengine.entity.primitive.Rectangle;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.util.FPSLogger;
import org.andengine.ui.activity.SimpleBaseGameActivity;
import org.andengine.util.math.MathUtils;
import org.andengine.util.modifier.ease.EaseLinear;

import android.opengl.GLES20;

public class CardinalSplineMoveModifierExample extends SimpleBaseGameActivity {

	private static final int CAMERA_WIDTH = 720;
	private static final int CAMERA_HEIGHT = 480;

	private static final int COUNT = 400;
	private static final float DURATION = 4;
	private static final float SIZE = 25;

	private static final float[] CONTROLPOINT_1_XS = {
		2 * (CAMERA_WIDTH / 4),
		1 * (CAMERA_WIDTH / 4),
		1.5f * (CAMERA_WIDTH / 4),
		2 * (CAMERA_WIDTH / 4)
	};

	private static final float[] CONTROLPOINT_2_XS = {
		2 * (CAMERA_WIDTH / 4),
		3 * (CAMERA_WIDTH / 4),
		2.5f * (CAMERA_WIDTH / 4),
		2 * (CAMERA_WIDTH / 4)
	};

	private static final float[] CONTROLPOINT_YS = {
		3.5f * (CAMERA_HEIGHT / 4),
		2 * (CAMERA_HEIGHT / 4),
		1 * (CAMERA_HEIGHT / 4),
		1.5f * (CAMERA_HEIGHT / 4),
	};

	@Override
	public EngineOptions onCreateEngineOptions() {
		final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);

		return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera);
	}

	@Override
	public void onCreateResources() {

	}

	@Override
	public Scene onCreateScene() {
		this.mEngine.registerUpdateHandler(new FPSLogger());

		final Scene scene = new Scene();
		scene.setBackground(new Background(0, 0, 0));

		for(int i = 0; i < COUNT; i++) {
			final float tension = MathUtils.random(-0.5f, 0.5f);
			this.addRectangleWithTension(scene, tension, MathUtils.random(0, DURATION * 2f));
		}

		return scene;
	}

	private void addRectangleWithTension(final Scene pScene, final float pTension, float pDelay) {
		//生成矩形
		final Rectangle rectangle = new Rectangle(-SIZE, -SIZE, SIZE, SIZE, this.getVertexBufferObjectManager());
		rectangle.setBlendFunction(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE);
		if(pTension < 0) {
			rectangle.setColor(1 - pTension, 0, 0, 0.5f);
		} else {
			rectangle.setColor(pTension, 0, 0, 0.5f);
		}

		//包含控制点的配置
		final CardinalSplineMoveModifierConfig catmullRomMoveModifierConfig1 = new CardinalSplineMoveModifierConfig(CardinalSplineMoveModifierExample.CONTROLPOINT_1_XS.length, pTension);
		final CardinalSplineMoveModifierConfig catmullRomMoveModifierConfig2 = new CardinalSplineMoveModifierConfig(CardinalSplineMoveModifierExample.CONTROLPOINT_1_XS.length, pTension);

		//填充控制点
		for(int i = 0; i < CardinalSplineMoveModifierExample.CONTROLPOINT_1_XS.length; i++) {
			catmullRomMoveModifierConfig1.setControlPoint(i, CardinalSplineMoveModifierExample.CONTROLPOINT_1_XS[i] - SIZE / 2, CardinalSplineMoveModifierExample.CONTROLPOINT_YS[i] - SIZE / 2);
			catmullRomMoveModifierConfig2.setControlPoint(i, CardinalSplineMoveModifierExample.CONTROLPOINT_2_XS[i] - SIZE / 2, CardinalSplineMoveModifierExample.CONTROLPOINT_YS[i] - SIZE / 2);
		}

		rectangle.registerEntityModifier(
			new SequenceEntityModifier(
				new DelayModifier(pDelay),
				//无限循环,相当于根据控制点从下到上跑完左边,在从下到上跑完右边。
				new LoopEntityModifier(
					//顺序容器
					new SequenceEntityModifier(
						new ParallelEntityModifier(
							//基数样条移动改变器
							new CardinalSplineMoveModifier(CardinalSplineMoveModifierExample.DURATION, catmullRomMoveModifierConfig1, EaseLinear.getInstance()),
							//旋转改变器
							new RotationModifier(CardinalSplineMoveModifierExample.DURATION, -45, -315)
						),
						new ParallelEntityModifier(
							//基数样条移动改变器
							new CardinalSplineMoveModifier(CardinalSplineMoveModifierExample.DURATION, catmullRomMoveModifierConfig2, EaseLinear.getInstance()),
							//旋转改变器
							new RotationModifier(CardinalSplineMoveModifierExample.DURATION, 45, 315)
						)
					)
				)
			)
		);

		pScene.attachChild(rectangle);
	}
}

代码也很少,说的也很清晰,CardinalSplineMoveModifierConfig中记录了控制点和张力,CardinalSplineMoveModifier根据配置文件计算出改变的路径。

你可能感兴趣的:(AndEngine)