您的位置:首页 > 产品设计 > UI/UE

Libgdx之封装简单的脚本机制

2015-07-16 10:41 603 查看
需求:

游戏中,在开始前经常会用到,ready->go->start game,或者其他情况。

简单的说,就是游戏一些UI展示需要按照步骤执行。

于是乎,为了让逻辑清晰,自己写了简单执行脚本,方便以后的开发。

package com.oahcfly.chgame.core.script;
// 接口
public interface IScript {

public void addSteps(String... args);

public void run(final Runnable callBackRunnable);
}

package com.oahcfly.chgame.core.script;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.actions.RunnableAction;
import com.badlogic.gdx.scenes.scene2d.actions.SequenceAction;
import com.badlogic.gdx.utils.Array;
import com.oahcfly.chgame.core.mvc.CHActor;
import com.oahcfly.chgame.core.mvc.CHScreen;

/**
*
* CH脚本:
* 1.add(方法名)
* 2.run(回调函数)
*
* @author haocao
*
*/
public class CHScript implements IScript {

private Array<String> stepArr = new Array<String>();

private CHScreen parentScreen;

private CHActor actor;

public CHScript(CHScreen screen) {
this.parentScreen = screen;
actor = new CHActor();
}

@Override
public void run(final Runnable callBackRunnable) {

// 调用screen内,一系列的方法名。method1,mthod2,method3...
this.parentScreen.addActor(actor);

SequenceAction sequenceAction = new SequenceAction();
for (int i = 0, size = stepArr.size; i < size; i++) {
final String methodName = stepArr.get(i);
RunnableAction runnableAction = new RunnableAction() {

@Override
public void run() {
invokeMethod(methodName);
}

};
sequenceAction.addAction(runnableAction);
}

sequenceAction.addAction(Actions.run(new Runnable() {

@Override
public void run() {
parentScreen.removeActor(actor);
CHScript.this.stepArr.clear();
callBackRunnable.run();
}
}));
actor.addAction(sequenceAction);

}

private void invokeMethod(String methodName) {

final Class<?> screenClass = parentScreen.getClass();
Method method;
try {
method = screenClass.getDeclaredMethod(methodName);
method.invoke(parentScreen);
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
Gdx.app.error(getClass().getSimpleName(), e.getLocalizedMessage());
} catch (SecurityException e) {
// TODO Auto-generated catch block
Gdx.app.error(getClass().getSimpleName(), e.getLocalizedMessage());
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
Gdx.app.error(getClass().getSimpleName(), e.getLocalizedMessage());
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
Gdx.app.error(getClass().getSimpleName(), e.getLocalizedMessage());
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
Gdx.app.error(getClass().getSimpleName(), e.getLocalizedMessage());
}
}

/**
* 方法需要public。
*/
@Override
public void addSteps(String... args) {
for (String methodName : args) {
stepArr.add(methodName);
}
}
}


功能核心是依靠libgdx中的SequenceAction,顺序执行Action。最后给出一个结束的回调。

用法简介:

CHScript script = new CHScript(this);
script.addSteps("step1", "step2", "step3");
script.run(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
// System.out.print("step执行完毕");
}
});


在Screen中,加入上面的代码,同时需要添加 3个方法:

public void step1() {
System.out.println(System.currentTimeMillis() + "  step1");
}

public void step2() {
System.out.println(System.currentTimeMillis() + "  step2");
}

public void step3() {
System.out.println(System.currentTimeMillis() + "  step3");
}

这样就实现了我们需求。

欢迎关注CHGame框架(基于Libgdx二次封装快速开发框架):

https://git.oschina.net/oahcfly/CHGame.git
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息