手机游戏引擎框架的初步想法

阅读更多
一直想重构下手机游戏客户端的引擎
主要思路:
1. Main.java 入口类,集成mildet, 负责初始化engine对象,启动线程

2. Engine.java 核心
   主要方法就是 run(), 线程体,负责调用logic()方法和draw(g)方法,来分别处理逻辑和界面更新
   构造方法,引入view.java 对象,后者负责接收按键输入和paint的分发
   状态切换函数,因为引擎基于state机来处理,需要切换不同的状态
   后续考虑如何将网络的数据接口整合进来

3. MyGameEngine.java
   引擎的子类,来实现具体某个场景的界面和逻辑处理
   重点在于flow(). draw(g)

下面是基本代码
package com.nbgame.engine;

import javax.microedition.lcdui.Display;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;


public class Main extends MIDlet {

	static Display display = null;
	protected void destroyApp(boolean flag) throws MIDletStateChangeException {
		// TODO Auto-generated method stub

	}

	protected void pauseApp() {
		// TODO Auto-generated method stub

	}

	static Engine engine;
	protected void startApp() throws MIDletStateChangeException {
		// TODO Auto-generated method stub		
		display = Display.getDisplay(this);
//		engine = Engine.createEngine();
//		engine = new Engine();
		engine = new MyGameEngine();		
		new Thread(engine).start();
	}
	
	public static Display getDisplay(){
		return display;
	}

}




package com.nbgame.engine;

import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;

import com.nbgame.ui.core.View;

public class Engine extends View implements Runnable {
	protected int mainState;
	protected final static int LOGIN=0;


	private static long sleepTime=100;

	static Display display = null;
	Main midlet = null;
	static View view = null;
	Engine engine = this;
	private long startTime;
	private boolean isRunning;

	public Engine() {
		 view = new View(this);
		 view.setFullScreenMode(true);
		 display = Main.getDisplay();
		 display.setCurrent(view);
		 
		 isRunning = true;
	}

	public void run() {
		// TODO Auto-generated method stub
		mainState = LOGIN;
		while (isRunning) {
//			long start = System.currentTimeMillis();
			flow();
			View.key = View.NOKEY;
			view.repaint();

//			long end = System.currentTimeMillis();
			try {
				Thread.sleep(sleepTime);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	void flow() {
		;
		
	}

	public void draw(Graphics g) {
		;

	}
	
	protected void nextMainState(int mainState) {
		// TODO Auto-generated method stub
		startTime = System.currentTimeMillis();
		this.mainState = mainState;		
	}
	
	protected void stop() {
		isRunning = false;
		System.gc();
	}
}



package com.nbgame.engine;

import javax.microedition.lcdui.Graphics;

import com.nbgame.ui.core.Dialog;
import com.nbgame.ui.core.Menu;
import com.nbgame.ui.core.View;

public class MyGameEngine extends Engine {
	
	
	private static final int HALL = 1;
	Menu menu = null;
	String[] menuItems = {
			"菜单1","菜单2","菜单3"
	};
	private Dialog dlg;
		
	public void flow() {
		// TODO Auto-generated method stub
		System.out.println("key" + key);
		switch (mainState) {
		case LOGIN:
			menu = new Menu(menuItems, 0);			
			if(view.key == View.VKEY_UP){
				menu.selectUp();
				return;
			}else if(view.key == View.VKEY_DOWN){
				menu.selectDown();
				return;
			}else if(view.key == View.VKEY_FIRE){
				nextMainState(HALL);
				return;
			}
			break;
		
		case HALL:{
			if(dlg ==null){
				dlg = new Dialog("dlg content", "ttitle", 0);				
			}
			if(view.key == View.VKEY_LEFT){
				dlg.confirm();
				return;
			}else if(view.key == View.VKEY_RIGHT){
				dlg.cancel();
				return;
			}
		};break;
		}
	}
	
	public void draw(Graphics g) {
		// TODO Auto-generated method stub
//		System.out.println("draw");
		switch (mainState) {
		case LOGIN:
			if(menu!=null){
				menu.paint(g);
			}
			break;
		case HALL:{
			if(dlg != null){
				dlg.paint(g);
			}
		}
		default:
			break;
		}
	}
}




有时间这两天再完善,加油

你可能感兴趣的:(游戏,框架,UI,thread,UP)