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.util.DisplayMetrics;
/**
* CardinalSplineMoveModifier(基数样条移动修改器)。
* demo的效果是依靠矩形的运动创造出一个W形
*/
public class CardinalSplineMoveModifierWActivity 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[][] controlPointXy;
@Override
public EngineOptions onCreateEngineOptions()
{
setScreenDisplay();
Camera camera = new Camera(0, 0, winWidth, winHeight);
return new EngineOptions
(
true,
ScreenOrientation.LANDSCAPE_FIXED,
new RatioResolutionPolicy(winWidth, winHeight),
camera
);
}
@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);
//控制点的配置(记录了控制点和张力,CardinalSplineMoveModifier根据配置改变路径)
CardinalSplineMoveModifierConfig cardinalSplineMoveModifierConfig = new CardinalSplineMoveModifierConfig(controlPointXy.length, MathUtils.random(-1.0f, 1.0f));
//填充(设置)控制点
for (int i = 0; i < controlPointXy.length; i++)
{
cardinalSplineMoveModifierConfig.setControlPoint(i, controlPointXy[i][0], controlPointXy[i][1]);
}
SequenceEntityModifier sequenceEntityModifier = new SequenceEntityModifier
(
new DelayModifier(MathUtils.random(0.0f, DURATION)),
//无限循环,跑完一遍,然后重复跑
new LoopEntityModifier
(
new ParallelEntityModifier
(
new CardinalSplineMoveModifier(DURATION, cardinalSplineMoveModifierConfig, 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
controlPointXy = new float[][]
{
{1.0f*(winWidth/4)-RECTANGLE_SIZE/2 , 1.0f*(winHeight/4)-RECTANGLE_SIZE/2},
{1.5f*(winWidth/4)-RECTANGLE_SIZE/2 , 3.0f*(winHeight/4)-RECTANGLE_SIZE/2},
{2.0f*(winWidth/4)-RECTANGLE_SIZE/2 , 2.0f*(winHeight/4)-RECTANGLE_SIZE/2},
{2.5f*(winWidth/4)-RECTANGLE_SIZE/2 , 3.0f*(winHeight/4)-RECTANGLE_SIZE/2},
{3.0f*(winWidth/4)-RECTANGLE_SIZE/2 , 1.0f*(winHeight/4)-RECTANGLE_SIZE/2}
};
}
}