最終更新: xna_game_studio 2009年02月12日(木) 01:21:21履歴
ゲームを構成する「スプラッシュ」「タイトル」「ゲームメイン」「コンフィグ画面」などのタスクをどのように管理するのかを調べます。
まともにゲームを作ったことないので試行錯誤でいきます。
http://www.microsoft.com/japan/msdn/vstudio/expres...
「ゲームの部品化」を参考にします。
以下やりたいこと。
- 1. 最初は「開始」タスクから始まって「終了」タスクで終わる。
開始 = スプラッシュ表示など
↓
タイトル = ゲームメイン or コンフィグ or 終了
↓
コンフィグ = ゲーム設定変更など
↓
タイトル
↓
ゲームメイン = ゲームメイン
↓
タイトル
↓
終了 = 終了ロゴ表示など
(開始タスクが何もしないでタイトルタスクへつなぐ形でもOK)
- 2. 有効化されている各タスクは並列実行される。
タイトルタスクとコンフィグタスクは並列実行される。たとえばタイトル画面で背景がスクロールしつつ、コンフィグ画面でゲーム設定変更できる。
開始タスクからタイトルタスクへ遷移した場合、開始タスクは無効にする。
- 3. コンフィグ画面などのタイトルからもゲームメインからも同じユーザーインターフェイスで実行できるタスクがあると良い。処理は同じでも描画は別の方がいいかも?
- 4. 入力はフォアグラウンドタスクのみ受け付けるようにする必要があるかもしれない。
タイトルタスクからコンフィグタスクへ遷移した場合、タイトルタスクは有効だけど入力は受け付けない、コンフィグタスクのみ入力を受け付ける。
なかなか良い資料がウェブ上にないなぁと思って、XNA Creators Club Onlineのサンプル集を見ていたら、なんとやりたいことを全てやっているサンプルが見つかりました。
http://creators.xna.com/en-us/samples/gamestateman...
その名も「Game State Management」
内容を読んでみると、
・各タスクを「GameScreen」という単位に分けて、全てを「ScreenManager」に任せる。
・ゲームの初期化時にScreenManager.addScreen(最初のGameScreen)をして終了。続きは最初のGameScreenに任せる。
・各GameScreenはUpdateとDrawを持つが、それらを呼ぶ処理はScreenManagerが管理する。
・ScreenManagerはDrawableGameComponentを継承する
・入力はScreenManagerが管理する
/// <summary> /// A screen is a single layer that has update and draw logic, and which /// can be combined with other layers to build up a complex menu system. /// For instance the main menu, the options menu, the "are you sure you /// want to quit" message box, and the main game itself are all implemented /// as screens. /// </summary>スクリーンはUpdateとDrawを持つシングルレイヤーで、他のレイヤーと組み合わせて複雑なメニューシステムを作り上げることが可能です。メインメニュー、オプションメニュー、"終了しますか?"のメッセージボックス、そしてメインゲーム、これらは全てスクリーンとして実装されています。
- ScreenState列挙体
/// <summary> /// Enum describes the screen transition state. /// </summary> public enum ScreenState { TransitionOn, Active, TransitionOff, Hidden, }
ScreenState | 状態 |
---|---|
TransitionOn | フェードイン状態かな? |
Active | アクティブ状態 |
TransitionOff | フェードアウト状態かな? |
Hidden | 非表示状態 |
- 遷移(Transition)
GameScreen基底クラスには基本的な遷移処理が実装されている。TransitionPositionというプロパティで0.0〜1.0の遷移位置が取得できるので、それを継承先クラスで利用すれば、フェードイン+横からスクロールインみたいなことができる。
- LoadContent
/// <summary> /// Load graphics content for the screen. /// </summary> public virtual void LoadContent() { }LoadContentは仮想関数となっているので、継承先のクラスで実装する。
- UnloadContent
/// <summary> /// Unload content for the screen. /// </summary> public virtual void UnloadContent() { }UnloadContentも同じく仮想関数となっている。
- Update
/// <summary> /// Allows the screen to run logic, such as updating the transition position. /// Unlike HandleInput, this method is called regardless of whether the screen /// is active, hidden, or in the middle of a transition. /// </summary> public virtual void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen) { this.otherScreenHasFocus = otherScreenHasFocus; if (isExiting) { // If the screen is going away to die, it should transition off. screenState = ScreenState.TransitionOff; if (!UpdateTransition(gameTime, transitionOffTime, 1)) { // When the transition finishes, remove the screen. ScreenManager.RemoveScreen(this); } } else if (coveredByOtherScreen) { // If the screen is covered by another, it should transition off. if (UpdateTransition(gameTime, transitionOffTime, 1)) { // Still busy transitioning. screenState = ScreenState.TransitionOff; } else { // Transition finished! screenState = ScreenState.Hidden; } } else { // Otherwise the screen should transition on and become active. if (UpdateTransition(gameTime, transitionOnTime, -1)) { // Still busy transitioning. screenState = ScreenState.TransitionOn; } else { // Transition finished! screenState = ScreenState.Active; } } }1. isExitingという終了フラグが立っている場合はScreenStateをTransitionOffにする。その際遷移が終了したならScreenManagerから自分自身を削除する。
2. coveredByOtherScreenの場合はやはりScreenStateをTransitionOffにする。その際遷移が終了したならScreenStateをHiddenにする。
3. それ以外の場合はScreenStateをTransitionOnにする。その際遷移が終了したならScreenStateをActiveにする。
- UpdateTransition
/// <summary> /// Helper for updating the screen transition position. /// </summary> bool UpdateTransition(GameTime gameTime, TimeSpan time, int direction) { // How much should we move by? float transitionDelta; if (time == TimeSpan.Zero) transitionDelta = 1; else transitionDelta = (float)(gameTime.ElapsedGameTime.TotalMilliseconds / time.TotalMilliseconds); // Update the transition position. transitionPosition += transitionDelta * direction; // Did we reach the end of the transition? if ((transitionPosition <= 0) || (transitionPosition >= 1)) { transitionPosition = MathHelper.Clamp(transitionPosition, 0, 1); return false; } // Otherwise we are still busy transitioning. return true; }遷移の状態を進行する。
- HandleInput
/// <summary> /// Allows the screen to handle user input. Unlike Update, this method /// is only called when the screen is active, and not when some other /// screen has taken the focus. /// </summary> public virtual void HandleInput(InputState input) { }HandleInputは仮想関数となっているので、継承先のクラスで実装する。
- Draw
/// <summary> /// This is called when the screen should draw itself. /// </summary> public virtual void Draw(GameTime gameTime) { }Drawも仮想関数となっているの。
複数のGameScreenをまとめて管理する。
- InputState
InputState input = new InputState();InputState自体は、キーボードとゲームパッドの入力状態を一括で管理するためのラッパークラスなので、これは独自で作ったほうが良いかも。
何をやっているのかというと、例えば、メニュー画面で「決定」ボタンはキーボードのEnterキーかSpaceキー、ゲームパッドのAボタンかStartボタンの何れかが押されていれば良いので、IsMenuSelectというプロパティがそれらのorを取った結果を返すようになっていて、ゲームロジックではIsMenuSelectでメニュー画面の決定ボタンが押されているかどうかをチェックする。
- LoadContent
/// <summary> /// Load your graphics content. /// </summary> protected override void LoadContent() { // Load content belonging to the screen manager. ContentManager content = Game.Content; spriteBatch = new SpriteBatch(GraphicsDevice); font = content.Load<SpriteFont>("menufont"); blankTexture = content.Load<Texture2D>("blank"); // Tell each of the screens to load their content. foreach (GameScreen screen in screens) { screen.LoadContent(); } }GameScreenが共通で使うSpriteBatch、SpriteFontと背景色塗りつぶしようの空のTexture2Dをロード、続いて各GameScreenのLoadContentを呼ぶ。
- UnloadContent
/// <summary> /// Unload your graphics content. /// </summary> protected override void UnloadContent() { // Tell each of the screens to unload their content. foreach (GameScreen screen in screens) { screen.UnloadContent(); } }各GameScreenのUnloadContentを呼ぶ。
- Update
/// <summary> /// Allows each screen to run logic. /// </summary> public override void Update(GameTime gameTime) { // Read the keyboard and gamepad. input.Update(); // Make a copy of the master screen list, to avoid confusion if // the process of updating one screen adds or removes others. screensToUpdate.Clear(); foreach (GameScreen screen in screens) screensToUpdate.Add(screen); bool otherScreenHasFocus = !Game.IsActive; bool coveredByOtherScreen = false; // Loop as long as there are screens waiting to be updated. while (screensToUpdate.Count > 0) { // Pop the topmost screen off the waiting list. GameScreen screen = screensToUpdate[screensToUpdate.Count - 1]; screensToUpdate.RemoveAt(screensToUpdate.Count - 1); // Update the screen. screen.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen); if (screen.ScreenState == ScreenState.TransitionOn || screen.ScreenState == ScreenState.Active) { // If this is the first active screen we came across, // give it a chance to handle input. if (!otherScreenHasFocus) { screen.HandleInput(input); otherScreenHasFocus = true; } // If this is an active non-popup, inform any subsequent // screens that they are covered by it. if (!screen.IsPopup) coveredByOtherScreen = true; } } // Print debug trace? if (traceEnabled) TraceScreens(); }1. GameScreenがUpdate処理の途中で追加・削除したときにややこしくならないように、Update処理を実行する用のリスト「screenToUpdate」に現時点でのGameScreenリストのマスターコピーを作成する。
2. bool otherScreenHasFocus = !Game.IsActive; これはゲームがアクティブか非アクティブかを取得。
3. bool coveredByOtherScreen = false; これは・・・何?
4. screenToUpdateを後ろからUpdate処理するために、screenToUpdateリストの要素数が0になるまでループするようにして、screenToUpdateの一番最後の要素をscreenとして取得してUpdate処理を実行、その際Screenの状態がTransitionOnかActiveでかつ、それが最初の実行なら、入力をハンドルするHandleInputも実行。IsPopup属性がある場合、それ以降coveredByOtherScreenがTrueになる。(ポップアップウィンドウの後ろにいるという意味かな?)
(逆順のリストを作ってRemoveFirstすればいいのに、ややこしいことしてるな・・・)
5. トレース処理
- Draw
/// <summary> /// Tells each screen to draw itself. /// </summary> public override void Draw(GameTime gameTime) { foreach (GameScreen screen in screens) { if (screen.ScreenState == ScreenState.Hidden) continue; screen.Draw(gameTime); } }Hidden以外の各GameScreenのDrawを呼ぶ。
MenuEntryというメニューエントリーを複数追加して、カーソルを上下したりメニューを選択することが出来る基底クラス。
MenuEntryにOnSelectEntryというイベントハンドラーを持たせてコールバックさせる仕組みになっている。
- MenuEntries
/// <summary> /// Gets the list of menu entries, so derived classes can add /// or change the menu contents. /// </summary> protected IList<MenuEntry> MenuEntries { get { return menuEntries; } }MenuEntries.Add(MenuEntry)とできるようするプロパティ。
- HandleInput
/// <summary> /// Responds to user input, changing the selected entry and accepting /// or cancelling the menu. /// </summary> public override void HandleInput(InputState input) { // Move to the previous menu entry? if (input.MenuUp) { selectedEntry--; if (selectedEntry < 0) selectedEntry = menuEntries.Count - 1; } // Move to the next menu entry? if (input.MenuDown) { selectedEntry++; if (selectedEntry >= menuEntries.Count) selectedEntry = 0; } // Accept or cancel the menu? if (input.MenuSelect) { OnSelectEntry(selectedEntry); } else if (input.MenuCancel) { OnCancel(); } }先に解説したとおり入力処理はScreenManagerが現在アクティブなGameScreenに対してのみHandleInputを呼ぶ仕組みになっている。
もしこのGameScreenがアクティブなら、このHandleInputが呼ばれて、カーソルを上下したりメニューを選択することができる。
1. 上キーが押されたらカーソルを1つ上に移動。一番上のエントリーで上キーが押された場合は一番下のエントリーに移動する。
2. 下キーが押されたらカーソルを1つ下に移動。一番下のエントリーで下キーが押された場合は一番上のエントリーに移動する。
3. 決定キーが押されたらそのメニューエントリーに割り当てられているイベントハンドラーを呼ぶ。
4. キャンセルキーが押されたらOnCancelでExitScreenが呼ばれる。
- Update
/// <summary> /// Updates the menu. /// </summary> public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen) { base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen); // Update each nested MenuEntry object. for (int i = 0; i < menuEntries.Count; i++) { bool isSelected = IsActive && (i == selectedEntry); menuEntries[i].Update(this, isSelected, gameTime); } }1. 全てのメニューエントリーに対してUpdateを呼ぶ。このとき選択されているメニューエントリーはisSelectedになる。
- Draw
/// <summary> /// Draws the menu. /// </summary> public override void Draw(GameTime gameTime) { SpriteBatch spriteBatch = ScreenManager.SpriteBatch; SpriteFont font = ScreenManager.Font; Vector2 position = new Vector2(100, 150); // Make the menu slide into place during transitions, using a // power curve to make things look more interesting (this makes // the movement slow down as it nears the end). float transitionOffset = (float)Math.Pow(TransitionPosition, 2); if (ScreenState == ScreenState.TransitionOn) position.X -= transitionOffset * 256; else position.X += transitionOffset * 512; spriteBatch.Begin(); // Draw each menu entry in turn. for (int i = 0; i < menuEntries.Count; i++) { MenuEntry menuEntry = menuEntries[i]; bool isSelected = IsActive && (i == selectedEntry); menuEntry.Draw(this, position, isSelected, gameTime); position.Y += menuEntry.GetHeight(this); } // Draw the menu title. Vector2 titlePosition = new Vector2(426, 80); Vector2 titleOrigin = font.MeasureString(menuTitle) / 2; Color titleColor = new Color(192, 192, 192, TransitionAlpha); float titleScale = 1.25f; titlePosition.Y -= transitionOffset * 100; spriteBatch.DrawString(font, menuTitle, titlePosition, titleColor, 0, titleOrigin, titleScale, SpriteEffects.None, 0); spriteBatch.End(); }1. TransitionPositionにより基準となるX座標を算出。
2. 各メニューエントリーを縦に描画する。描画自体はMenuEntry.Drawに任せてある。X座標は1.で算出された位置を基準とする。このとき選択されているメニューエントリーはisSelectedになる。
3. このメニューのタイトルを描画。
ゲーム中にStartボタンを押したときにポーズ状態になる処理を実装しているクラス。
ソースコードの解説は省略するが、だいたい下記のような内容だった。
1. Resume GameとQuit Gameという2つのメニューエントリーを生成して、それらにそれぞれOnCancelとQuitGameMenuEntrySelectedのイベントハンドラーを割り当てる。
2. Resume Gameが選択されるとOnCancelが呼ばれて、基底クラスのExitScreenが呼ばれる。
3. Quit Gameが選択されるとQuitGameMenuEntrySelectedが呼ばれて、タイトルに戻る。(実際にはLoadingScreenに遷移して同期を取る処理がある。)
コメントをかく