View Javadoc

1   /*
2    * "Peko" Visual Novel System
3    *
4    * All Rights Reserved.
5    * Copyright (c) 1999-2003 Tsukuba Bunko.
6    *
7    * $Id: ScenarioProcessor.java,v 1.2 2005/11/23 05:42:55 ppoi Exp $
8    */
9   package tsukuba_bunko.peko.scenario;
10  
11  import	java.util.ArrayList;
12  
13  import	tsukuba_bunko.peko.ActionControler;
14  import	tsukuba_bunko.peko.Logger;
15  import	tsukuba_bunko.peko.PekoSystem;
16  
17  import	tsukuba_bunko.peko.session.Session;
18  
19  import	tsukuba_bunko.peko.scenario.select.SelectCoordinator;
20  
21  import	tsukuba_bunko.peko.scenario.stage.StageCoordinator;
22  
23  import	tsukuba_bunko.peko.scenario.text.TextCoordinator;
24  
25  
26  /***
27   * シナリオの処理を行うプロセッサモジュールです。
28   * @author	$Author: ppoi $
29   * @version	$Revision: 1.2 $
30   */
31  public class ScenarioProcessor	{
32  
33  	/***
34  	 * キュー
35  	 */
36  	protected ArrayList	_queue = new ArrayList( 5 );
37  
38  	/***
39  	 * キューの最大値
40  	 */
41  	protected int	_maxQueueSize = 4;
42  
43  	/***
44  	 * 生成した SceneProcessor 数
45  	 */
46  	protected int	_processorCount = 0;
47  
48  	/***
49  	 * 現在シーンを処理中の SceneProcessor
50  	 */
51  	protected SceneProcessor	_currentSceneProcessor = null;
52  
53  	/***
54  	 * 現在のコンテクスト
55  	 */
56  	protected Session	_session = null;
57  
58  
59  	/***
60  	 * TextCanvas コーディネータ
61  	 */
62  	protected TextCoordinator	_textCooridnator = null;
63  
64  	/***
65  	 * StageCanvas コーディネータ
66  	 */
67  	protected StageCoordinator	_stageCoordinator = null;
68  
69  	/***
70  	 * SelectCanvas コーディネータ
71  	 */
72  	protected SelectCoordinator	_selectCoordinator = null;
73  
74  
75  	/***
76  	 * <code>ScenarioProcessor</code> のインスタンスを生成します。
77  	 */
78  	public ScenarioProcessor()
79  	{
80  		super();
81  		_queue.add( new SceneProcessor(this) );
82  		_queue.add( new SceneProcessor(this) );
83  
84  		_textCooridnator = new TextCoordinator();
85  		_stageCoordinator = new StageCoordinator();
86  		_selectCoordinator = new SelectCoordinator();
87  	}
88  
89  
90  	/***
91  	 * 指定された第1シーン、セッションでシナリオの再生を開始します。
92  	 * @param	sceneName	第1シーン名
93  	 * @param	session	セッション
94  	 */
95  	public synchronized void playScenario( String sceneName, Session session )
96  	{
97  		if( _currentSceneProcessor != null )	{
98  			Logger.error( "[scene] BUG! current scene processor is still alive!" );
99  			exit();
100 		}
101 		SceneProcessor	processor = getSceneProcessor();
102 		processor.setTextCoordinator( _textCooridnator );
103 		processor.setStageCoordinator( _stageCoordinator );
104 		processor.setSelectCoordinator( _selectCoordinator );
105 		_currentSceneProcessor = processor;
106 		_session = session;
107 
108 		ActionControler	controler = PekoSystem.getInstance().getActionControler();
109 		controler.setActive( true );
110 		processor.process( sceneName, session );
111 	}
112 
113 	/***
114 	 * シナリオの再生を終了します。
115 	 */
116 	public synchronized void exit()
117 	{
118 		if( _currentSceneProcessor != null )	{
119 			Logger.debug( "[scenario] exit ScenarioProcessor" );
120 			_currentSceneProcessor.abort();
121 			_currentSceneProcessor = null;
122 			_session = null;
123 
124 			ActionControler	controler = PekoSystem.getInstance().getActionControler();
125 			controler.start();
126 		}
127 	}
128 
129 	/***
130 	 * シーンを処理するシーンプロセッサを取得します。
131 	 * @return	シーンプロセッサ
132 	 */
133 	protected synchronized SceneProcessor getSceneProcessor()
134 	{
135 		try	{
136 			if( _queue.isEmpty() )	{
137 				if( _processorCount < _maxQueueSize )	{
138 					SceneProcessor	processor = new SceneProcessor( this );
139 					_processorCount++;
140 					return processor;
141 				}
142 				else	{
143 					while( _queue.isEmpty() )	{
144 						wait( 50 );
145 					}
146 				}
147 			}
148 			return (SceneProcessor)_queue.remove( 0 );
149 		}
150 		catch( InterruptedException ie )	{
151 			Logger.debug( "[scene] interrupted", ie );
152 			return null;
153 		}
154 	}
155 
156 	/***
157 	 * 処理が終了した SceneProcessor をキューに戻します。
158 	 * @param	processor	キューに戻すシーンプロセッサ
159 	 */
160 	protected synchronized void pushSceneProcessor( SceneProcessor processor )
161 	{
162 		_queue.add( processor );
163 	}
164 
165 	/***
166 	 * シーンの終了を通知します。
167 	 * @param	processor	終了の通知を発行したシーンプロセッサ
168 	 */
169 	protected synchronized void sceneEnded( SceneProcessor processor )
170 	{
171 		if( _currentSceneProcessor == processor )	{
172 			SceneContext	context = processor.getSceneContext();
173 			String	nextSceneName = context.getNextSceneName();
174 			if( nextSceneName != null )	{
175 				if( "peko:end".equals(nextSceneName) )	{
176 					exit();
177 					returnTitle();
178 				}
179 				else	{
180 					_currentSceneProcessor.abort();
181 					_session.setSceneContext( null );
182 					PekoSystem.getInstance().getCanvasManager().advancesNewPage();
183 					PekoSystem.getInstance().getCanvasManager().getCurrentPage().commit();
184 					_currentSceneProcessor = getSceneProcessor();
185 					_currentSceneProcessor.setTextCoordinator( _textCooridnator );
186 					_currentSceneProcessor.setStageCoordinator( _stageCoordinator );
187 					_currentSceneProcessor.setSelectCoordinator( _selectCoordinator );
188 					_currentSceneProcessor.process( context.getNextSceneName(), _session );
189 				}
190 			}
191 			else	{
192 				Logger.error( MessageIDs.SCN0012E );
193 				exit();
194 				returnTitle();
195 			}
196 		}
197 	}
198 
199 	/***
200 	 * タイトル画面へ戻ります。
201 	 */
202 	private void returnTitle()
203 	{
204 		new Thread()	{
205 			public void run() {
206 				ActionControler	controler = PekoSystem.getInstance().getActionControler();
207 				controler.returnTitle( true );
208 			};
209 		}.start();
210 	}
211 }