PageRenderTime 50ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/renpy-player-as3/src/renpyas3/view/RenpyPlayer.as

http://renpy-player-as3.googlecode.com/
ActionScript | 259 lines | 170 code | 42 blank | 47 comment | 9 complexity | 51d5e467ed92a67704bb878358f528ce MD5 | raw file
  1. package renpyas3.view
  2. {
  3. import br.com.stimuli.loading.BulkLoader;
  4. import br.com.stimuli.loading.BulkProgressEvent;
  5. import flash.display.DisplayObject;
  6. import flash.display.Loader;
  7. import flash.net.URLRequest;
  8. import flash.text.TextField;
  9. import flash.text.TextFormat;
  10. import flash.text.TextFormatAlign;
  11. import org.osflash.thunderbolt.Logger;
  12. import renpyas3.model.RenpyPlayerModel;
  13. import renpyas3.model.structs.RenpyMenu;
  14. import renpyas3.view.components.Hud;
  15. import renpyas3.view.components.MenuChoiceButton;
  16. import renpyas3.view.components.TransitionEngine;
  17. import flash.display.Sprite;
  18. import flash.display.Stage;
  19. import flash.events.Event;
  20. import flash.events.MouseEvent;
  21. import flash.media.SoundChannel;
  22. /**
  23. * this is the main view class,
  24. * takes a Screen model and Plays the script in it.
  25. */
  26. public class RenpyPlayer
  27. {
  28. //display state
  29. private var _stage:Stage;
  30. private var _backgroundLayer:Sprite;
  31. private var _imagesLayer:Sprite;
  32. private var _buttonsLayer:Sprite;
  33. private var _optionsMenuLayer:Sprite;
  34. private var _hud:Hud;
  35. private var _soundChannel:SoundChannel;
  36. //model
  37. private var _model:RenpyPlayerModel;
  38. //subsystems
  39. private var _instructionsProcessor:RenpyPlayerInstructionsProcessor;
  40. private var _transtionEngine:TransitionEngine;
  41. private var _optionsMenuProgressText:TextField;
  42. public function RenpyPlayer(model:RenpyPlayerModel, stage:Stage)
  43. {
  44. _stage = stage;
  45. _model = model;
  46. _instructionsProcessor = new RenpyPlayerInstructionsProcessor(this, model);
  47. _transtionEngine = new TransitionEngine();
  48. initLayers();
  49. initLoadMenu();
  50. loadAssets();
  51. }
  52. private function initLoadMenu():void
  53. {
  54. //draw transparent layer to catch clicks
  55. _optionsMenuLayer.graphics.beginFill(0x0000FF, 0);
  56. _optionsMenuLayer.graphics.drawRect(0, 0, _stage.stageWidth, _stage.stageHeight); //(size of the stage)
  57. _optionsMenuLayer.graphics.endFill();
  58. //load options menu bg if any
  59. if (_model.options.bgImageFilename)
  60. {
  61. var optionsMenuBg:Object;
  62. var classDef:Object = _model.assetsLoader.getEmbeddedObjectDefinition(_model.path + _model.options.bgImageFilename)
  63. if (classDef != null)
  64. {
  65. //'load' from embed
  66. optionsMenuBg = new classDef();
  67. }
  68. else
  69. {
  70. optionsMenuBg = new Loader();
  71. //load from file
  72. //trace("optionsMenuBg:",_model.path + _model.options.bgImageFilename);
  73. optionsMenuBg.load(new URLRequest(_model.path + _model.options.bgImageFilename));
  74. }
  75. _optionsMenuLayer.addChild(optionsMenuBg as DisplayObject);
  76. var scaleFactorX:Number = _stage.stageWidth / _model.options.screenWidth;
  77. var scaleFactorY:Number = _stage.stageHeight/ _model.options.screenHeight;
  78. optionsMenuBg.scaleX = scaleFactorX;
  79. optionsMenuBg.scaleY = scaleFactorY;
  80. }
  81. //add loading progress text
  82. _optionsMenuProgressText = new TextField();
  83. _optionsMenuProgressText.width = _stage.stageWidth;
  84. _optionsMenuProgressText.height = 100;
  85. _optionsMenuProgressText.selectable = false;
  86. _optionsMenuProgressText.wordWrap = true;
  87. _optionsMenuProgressText.x = 0;
  88. _optionsMenuProgressText.y = _stage.stageHeight/2;
  89. _optionsMenuProgressText.defaultTextFormat = new TextFormat("Verdana", 24, 0xFFFFFF, false,false,null,null,null,TextFormatAlign.CENTER);
  90. _optionsMenuProgressText.text = "Loading:0%";
  91. _optionsMenuLayer.addChild(_optionsMenuProgressText)
  92. }
  93. private function loadAssets():void
  94. {
  95. trace("RenpyPlayer::loadAssets --_model.assetsLoader.itemsTotal:"+_model.assetsLoader.itemsTotal);
  96. trace("RenpyPlayer::loadAssets --_model.assetsLoader.itemsLoaded:"+_model.assetsLoader.itemsLoaded);
  97. if (_model.assetsLoader.itemsLoaded == _model.assetsLoader.itemsTotal)
  98. {
  99. showMenuReadyToStart();
  100. }
  101. else
  102. {
  103. _model.assetsLoader.addEventListener(BulkLoader.PROGRESS, handleAsstesLoadProgress);
  104. }
  105. }
  106. private function handleAsstesLoadProgress(e:BulkProgressEvent):void
  107. {
  108. //trace("PercentLoaded --e.percentLoaded, e.weightPercent:" + e.percentLoaded, e.weightPercent);
  109. //trace("PercentLoaded --e.itemsLoaded, e.itemsTotal:" + e.itemsLoaded, e.itemsTotal);
  110. _optionsMenuProgressText.text = "Loading:"+(Math.round(e.itemsLoaded/e.itemsTotal*100))+"%";
  111. if (e.itemsLoaded == e.itemsTotal)
  112. {
  113. showMenuReadyToStart();
  114. }
  115. }
  116. /**
  117. * show this when everyting is loaded and we need a click to start
  118. */
  119. private function showMenuReadyToStart():void
  120. {
  121. _optionsMenuProgressText.text = "click to start";
  122. _optionsMenuLayer.addEventListener(MouseEvent.CLICK, handleClickToStart);
  123. }
  124. private function handleClickToStart(e:MouseEvent):void
  125. {
  126. _optionsMenuLayer.removeEventListener(MouseEvent.CLICK, handleClickToStart);
  127. runFirstInstruction();
  128. }
  129. /**
  130. * start running instructions when load is complete
  131. */
  132. private function runFirstInstruction():void
  133. {
  134. //hide options menu
  135. _optionsMenuLayer.visible = false;
  136. //run first instruction
  137. _instructionsProcessor.runNextInstruction();
  138. }
  139. /**
  140. * init text, graphic, HUD
  141. */
  142. private function initLayers():void
  143. {
  144. _backgroundLayer = new Sprite();
  145. _stage.addChild(_backgroundLayer);
  146. _imagesLayer = new Sprite();
  147. _stage.addChild(_imagesLayer);
  148. _hud = new Hud(_model.options);
  149. _stage.addChild(_hud);
  150. //detect clicks
  151. _hud.addEventListener(MouseEvent.CLICK, handleHudClick);
  152. //layer for the choice buttons
  153. _buttonsLayer = new Sprite();
  154. _stage.addChild(_buttonsLayer);
  155. _optionsMenuLayer = new Sprite();
  156. _stage.addChild(_optionsMenuLayer);
  157. }
  158. public function showMenu(menuData:RenpyMenu):void
  159. {
  160. _hud.visible = false; //hide hud for choices
  161. //Logger.info("RenpyScreen::runInstruction menu");
  162. for (var i:uint = 0; i < menuData.choicesList.length; i++ )
  163. {
  164. var newButton:MenuChoiceButton = new MenuChoiceButton(menuData.choicesList[i].choiceText, menuData.choicesList[i].nextInstructionIndex);
  165. newButton.x = _buttonsLayer.stage.stageWidth / 2;
  166. newButton.y = 200 +(i*50)
  167. newButton.addEventListener(MouseEvent.CLICK, handleChoiceButtonClicked);
  168. _buttonsLayer.addChild(newButton);
  169. Logger.info("RenpyScreen::runInstruction menu, choice->"+menuData.choicesList[i].choiceText+" nextInstructionIndex:"+menuData.choicesList[i].nextInstructionIndex)
  170. }
  171. //jumpToLabelBlock(menuData.choicesList[0].jumpToLabel);
  172. }
  173. /**
  174. * handle any of the choices during the game
  175. * jumping to the next code line in the labelBlock
  176. */
  177. private function handleChoiceButtonClicked(e:Event):void
  178. {
  179. while(_buttonsLayer.numChildren != 0)
  180. {
  181. _buttonsLayer.removeEventListener(MouseEvent.CLICK, handleChoiceButtonClicked);
  182. _buttonsLayer.removeChildAt(0);
  183. }
  184. _hud.visible = true; //show hud after choices
  185. _model.moveInstructionPointerToIndex((e.target as MenuChoiceButton).nextInstructionIndex);
  186. _instructionsProcessor.runNextInstruction();
  187. }
  188. /**
  189. * receives clicks on any part of the screen
  190. * to advance story
  191. */
  192. private function handleHudClick(e:MouseEvent):void
  193. {
  194. _instructionsProcessor.runNextInstruction();
  195. }
  196. public function get hud():Hud { return _hud; }
  197. public function get backgroundLayer():Sprite { return _backgroundLayer; }
  198. public function get imagesLayer():Sprite { return _imagesLayer; }
  199. public function get buttonsLayer():Sprite { return _buttonsLayer; }
  200. public function get instructionsProcessor():RenpyPlayerInstructionsProcessor { return _instructionsProcessor; }
  201. /**
  202. * the chanel to start/stop playback of music
  203. */
  204. public function get soundChannel():SoundChannel { return _soundChannel; }
  205. /**
  206. * the chanel to start/stop playback of music
  207. */
  208. public function set soundChannel(value:SoundChannel):void
  209. {
  210. _soundChannel = value;
  211. }
  212. /**
  213. * engine to start/stop image transitions
  214. */
  215. public function get transtionEngine():TransitionEngine { return _transtionEngine; }
  216. }
  217. }