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;
import android.util.DisplayMetrics;
/**
* CardinalSplineMoveModifier(基数样条移动修改器)。
* demo的效果是依靠矩形的运动创造出一个心形,当然心形是一个不规则的形状,这里就得使用基数样条的概念。
* 盗用百科:
* 基数样条是一组单个曲线按照一定的顺序连接而成的一条较大曲线。样条由一系列点指定,并通过每一个指定的点。
* 由于基数样条平滑地穿过组中的每一个点(不出现尖角),因而它比用直线连接创建的路径更精确。
* 程序中定义了两组X,Y坐标,分别表示心形左右两边的控制点,矩形的移动平滑的穿过几个控制点。
*/
public class CardinalSplineMoveModifierActivity extends SimpleBaseGameActivity
{
private static int winWidth = 854;
private static int winHeight = 480;
private static final float RECTANGLE_SIZE = 25.0f;
//时间(秒)
private static final float DURATION = 4.0f;
private static float[][] controlPointXy1;
private static float[][] controlPointXy2;
@Override
public EngineOptions onCreateEngineOptions()
{
setScreenDisplay();
Camera carmea = new Camera(0, 0, winWidth, winHeight);
return new EngineOptions
(
true,
ScreenOrientation.LANDSCAPE_FIXED,
new RatioResolutionPolicy(winWidth, winHeight),
carmea
);
}
@Override
protected void onCreateResources()
{
}
@Override
protected Scene onCreateScene()
{
this.mEngine.registerUpdateHandler(new FPSLogger());
Scene scene = new Scene();
scene.setBackground(new Background(0, 0, 0));
//创建并添加矩形
for (int j = 0; j < 400; j++)
{
createRectangle(scene);
}
return scene;
}
private void createRectangle(Scene scene)
{
Rectangle rectangle = new Rectangle(-RECTANGLE_SIZE, -RECTANGLE_SIZE, RECTANGLE_SIZE, RECTANGLE_SIZE, this.getVertexBufferObjectManager());
rectangle.setBlendFunction(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE);
//设置矩形颜色(使用了引擎的随机数)
rectangle.setColor(MathUtils.random(0.0f, 1.0f), MathUtils.random(0.0f, 1.0f), MathUtils.random(0.0f, 1.0f), 0.5f);
/*
* 控制点的配置
* CardinalSplineMoveModifierConfig中记录了控制点和张力,CardinalSplineMoveModifier根据配置文件计算出改变的路径。
* 通过源码发现:(控制点)pControlPointCount最小为4个,(张力)pTension取值范围 [-1, 1]
* 其中张力越小,角就越圆,路径越弯,否则张力越大,角就月尖,路径越直
*/
CardinalSplineMoveModifierConfig cardinalSplineMoveModifierConfig1 = new CardinalSplineMoveModifierConfig(controlPointXy1.length, MathUtils.random(-1.0f, 1.0f));
CardinalSplineMoveModifierConfig cardinalSplineMoveModifierConfig2 = new CardinalSplineMoveModifierConfig(controlPointXy2.length, MathUtils.random(-1.0f, 1.0f));
//填充(设置)控制点(跟上面配置的个数要匹配)
for (int i = 0; i < controlPointXy1.length; i++)
{
// pIndex :控制点下标;pX,pY : 控制点左边(x,y)
cardinalSplineMoveModifierConfig1.setControlPoint(i, controlPointXy1[i][0], controlPointXy1[i][1]);
}
//填充(设置)控制点(跟上面配置的个数要匹配)
for (int i = 0; i < controlPointXy2.length; i++)
{
// pIndex :控制点下标;pX,pY : 控制点左边(x,y)
cardinalSplineMoveModifierConfig2.setControlPoint(i, controlPointXy2[i][0], controlPointXy2[i][1]);
}
//顺序实体修改器
SequenceEntityModifier sequenceEntityModifier = new SequenceEntityModifier
(
//延时播放
new DelayModifier(MathUtils.random(0, DURATION * 2f)),
//无限循环,相当于根据控制点从下到上跑完左边,再从下到上跑完右边,然后重复跑
new LoopEntityModifier
(
//顺序执行,先走完左边,再走右边,最后停在终点
new SequenceEntityModifier
(
new ParallelEntityModifier
(
/*
* 基数样条移动修改器
* CardinalSplineMoveModifier:类似于PathModifier,但移动的效果更真实。
* 实体接近弯道时,转弯更流畅,或扭转方向更平滑,这其实看起来相当不错
*/
new CardinalSplineMoveModifier(DURATION, cardinalSplineMoveModifierConfig1, EaseLinear.getInstance()),
new RotationModifier(DURATION, -45, -315)
),
new ParallelEntityModifier
(
/*
* 基数样条移动修改器
* CardinalSplineMoveModifier:类似于PathModifier,但移动的效果更真实。
* 实体接近弯道时,转弯更流畅,或扭转方向更平滑,这其实看起来相当不错
*/
new CardinalSplineMoveModifier(DURATION, cardinalSplineMoveModifierConfig2, EaseLinear.getInstance()),
new RotationModifier(DURATION, 45, 315)
)
)
)
);
rectangle.registerEntityModifier(sequenceEntityModifier);
scene.attachChild(rectangle);
}
/**
* 设置屏幕大小
*/
private void setScreenDisplay()
{
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
winWidth = displayMetrics.widthPixels;
winHeight = displayMetrics.heightPixels;
//左边控制点的x,y
controlPointXy1 = new float[][]
{
{2.0f*(winWidth/4)-RECTANGLE_SIZE/2 , 3.5f*(winHeight/4)-RECTANGLE_SIZE/2},
{1.0f*(winWidth/4)-RECTANGLE_SIZE/2 , 2.0f*(winHeight/4)-RECTANGLE_SIZE/2},
{1.5f*(winWidth/4)-RECTANGLE_SIZE/2 , 1.0f*(winHeight/4)-RECTANGLE_SIZE/2},
{2.0f*(winWidth/4)-RECTANGLE_SIZE/2 , 1.5f*(winHeight/4)-RECTANGLE_SIZE/2}
};
//右边控制点的x,y
controlPointXy2 = new float[][]
{
{2.0f*(winWidth/4)-RECTANGLE_SIZE/2 , 3.5f*(winHeight/4)-RECTANGLE_SIZE/2},
{3.0f*(winWidth/4)-RECTANGLE_SIZE/2 , 2.0f*(winHeight/4)-RECTANGLE_SIZE/2},
{2.5f*(winWidth/4)-RECTANGLE_SIZE/2 , 1.0f*(winHeight/4)-RECTANGLE_SIZE/2},
{2.0f*(winWidth/4)-RECTANGLE_SIZE/2 , 1.5f*(winHeight/4)-RECTANGLE_SIZE/2}
};
}
}