StoryEdit 開発日誌

ウェブアプリ StoryEditを作ってましたが延期。普通のブログ。

Slick2DでRPG風 タイトルスプラッシュを作成

StateBasedで作成してみた。まだStateTransitionを使ってないのでなんともいえないが、
シンプルにいえば以下のような感じで作る。(チュートリアルより噛み砕く)

StateBasedGameをextendしたメインクラス

public class Ruie extends StateBasedGame {
	public static final int MAINMENUSTATE          = 0;
	public Ruie(String title) {
		super(title);
		this.addState(new MainMenuState(MAINMENUSTATE));
	}
	public static void main(String[] args) {
		Ruie game = new Ruie("mygame");
		AppGameContainer app = null;
		try {
			app = new AppGameContainer(game);
			app.setTargetFrameRate(60);
			app.start();
		} catch (SlickException e) {
			// ignore
		} finally {
			app.destroy();
		}
	}

	@Override
	public void initStatesList(GameContainer gc) throws SlickException {
		this.getState(MAINMENUSTATE).init(gc, this);
	}

mygameでは心もとなかったので、名前ジェネレータでゲームの名前をRuieとした。
で、簡単なシナリオを書いてみて、やはりいま考えているStoryEditの機能では物足りない気がしてきた。(なぜRPG風ゲームを作っているのか、これで少し理由がつけれた。)

BasicGameStateをextendした各画面クラス

次に、メニュー画面。シンプルに構造だけ示す。

public class MainMenuState extends BasicGameState {
	int stateID = -1;
	UnicodeFont ufont;
	Image background;
	Music titleMusic;
	MenuBox menubox;
	
	public MainMenuState(int stateID) {
		this.stateID = stateID;
	}
	
	@Override
	public int getID() {
		return stateID;
	}

	@Override
	public void init(GameContainer gc, StateBasedGame sbg)
			throws SlickException {
		if (menubox == null){
			menubox = new MenuBox(225, 330);
			menubox.addMenuString(new ArrayList<String>() {{
				add("はじめる");
				add("続ける");
			}});
			
			background = new Image("img/title.png");
			try {
				loadJapaneseCharSets();
				ufont.getEffects().add(new ColorEffect(java.awt.Color.WHITE));
				ufont.loadGlyphs();
			} catch (SlickException e) {
				//ignore
				e.printStackTrace();
			}
			titleMusic = new Music("music/opening.ogg");
			titleMusic.loop();
			titleMusic.setVolume(0f);
			titleMusic.fade(3000, 0.4f, false);
		}
	}
	
	private void loadJapaneseCharSets () throws SlickException {
		ufont = new UnicodeFont("/Users/xxxxx/Library/Fonts/migmix-1m-regular.ttf", 16, false, false);
		ufont.addAsciiGlyphs();
		ufont.addGlyphs(0x3000, 0x30ff); // Hiragana + katakanab + fullwidth punctuations
		ufont.addGlyphs(0x4e00,0x9fc0); // Kanji
	}

	@Override
	public void render(GameContainer gc, StateBasedGame sbg, Graphics g)
			throws SlickException {
		// draw splash
		background.draw(30, 150);
		// draw menu
		g.setFont(ufont);
		g.setColor(Color.white);
		menubox.draw(250, 350, g);
	}

	@Override
	public void update(GameContainer gc, StateBasedGame sbg, int delta)
			throws SlickException {
		Input input  = gc.getInput();
		if (input.isKeyPressed(Input.KEY_UP)) {
			menubox.moveUp();
		}
		else if (input.isKeyPressed(Input.KEY_DOWN)) {
			menubox.moveDown();
		}
		else if (input.isKeyPressed(Input.KEY_RETURN) || input.isKeyPressed(Input.KEY_SPACE)) {
			menubox.choose();
		}
	}
}

MenuBoxクラスは自前。これで、以下のようなタイトルスプラッシュができあがる。
やはり日本語フォントのロードが長いが、まぁいいだろう。

f:id:welovy:20130124193844p:plain