| OLD | NEW |
| 1 library pop_pop_win; | 1 library pop_pop_win; |
| 2 | 2 |
| 3 import 'dart:async'; |
| 3 import 'dart:html'; | 4 import 'dart:html'; |
| 4 import 'dart:web_audio'; | |
| 5 import 'package:bot/bot.dart'; | |
| 6 import 'package:bot_web/bot_html.dart'; | |
| 7 import 'package:bot_web/bot_texture.dart'; | |
| 8 | 5 |
| 9 import 'package:pop_pop_win/src/canvas.dart'; | 6 import 'package:stagexl/stagexl.dart'; |
| 10 import 'package:pop_pop_win/platform_target.dart'; | |
| 11 import 'package:pop_pop_win/src/html.dart'; | |
| 12 | 7 |
| 13 import 'src/textures.dart'; | 8 import 'platform_target.dart'; |
| 14 | 9 import 'src/audio.dart'; |
| 15 part 'src/pop_pop_win/audio.dart'; | 10 import 'src/platform.dart'; |
| 11 import 'src/stage.dart'; |
| 16 | 12 |
| 17 const String _ASSET_DIR = 'resources/'; | 13 const String _ASSET_DIR = 'resources/'; |
| 18 | 14 |
| 19 const String _TRANSPARENT_TEXTURE = | 15 const String _TRANSPARENT_TEXTURE = '${_ASSET_DIR}images/transparent.json'; |
| 20 '${_ASSET_DIR}images/transparent_animated.png'; | 16 const String _OPAQUE_TEXTURE = '${_ASSET_DIR}images/opaque.json'; |
| 21 const String _OPAQUE_TEXTURE = '${_ASSET_DIR}images/dart_opaque_01.jpg'; | 17 const String _TRANSPARENT_STATIC_TEXTURE = '${_ASSET_DIR}images/static.json'; |
| 22 const String _TRANSPARENT_STATIC_TEXTURE = | |
| 23 '${_ASSET_DIR}images/transparent_static.png'; | |
| 24 | 18 |
| 25 const int _LOADING_BAR_PX_WIDTH = 398; | 19 Future startGame(PlatformTarget platform) { |
| 26 | |
| 27 DivElement _loadingBar; | |
| 28 ImageLoader _imageLoader; | |
| 29 | |
| 30 final _Audio _audio = new _Audio(); | |
| 31 | |
| 32 void startGame(PlatformTarget platform) { | |
| 33 initPlatform(platform); | 20 initPlatform(platform); |
| 34 | 21 |
| 35 _loadingBar = querySelector('.sprite.loading_bar'); | 22 var stage = new Stage(querySelector('#gameCanvas'), webGL: true, |
| 36 _loadingBar.style.display = 'block'; | 23 color: 0xb4ad7f, frameRate: 60); |
| 37 _loadingBar.style.width = '0'; | |
| 38 | 24 |
| 39 _imageLoader = new ImageLoader([_TRANSPARENT_TEXTURE, | 25 var renderLoop = new RenderLoop() |
| 40 _OPAQUE_TEXTURE]); | 26 ..addStage(stage); |
| 41 _imageLoader.loaded.listen(_onLoaded); | 27 |
| 42 _imageLoader.progress.listen(_onProgress); | 28 BitmapData.defaultLoadOptions.webp = true; |
| 43 _imageLoader.load(); | 29 |
| 30 //have to load the loading bar first... |
| 31 var resourceManager = new ResourceManager() |
| 32 ..addTextureAtlas("static", "resources/images/static.json", |
| 33 TextureAtlasFormat.JSON); |
| 34 |
| 35 return resourceManager.load() |
| 36 .then((resMan) => _initialLoad(resMan, stage)); |
| 44 } | 37 } |
| 45 | 38 |
| 46 void _onProgress(_) { | 39 void _initialLoad(ResourceManager resourceManager, Stage stage) { |
| 47 int completedBytes = _imageLoader.completedBytes; | 40 var atlas = resourceManager.getTextureAtlas('static'); |
| 48 int totalBytes = _imageLoader.totalBytes; | |
| 49 | 41 |
| 50 completedBytes += _audio.completedBytes; | 42 var bar = new Gauge(atlas.getBitmapData('loading_bar'), Gauge.DIRECTION_RIGHT) |
| 51 totalBytes += _audio.totalBytes; | 43 ..x = 51 |
| 44 ..y = 8 |
| 45 ..ratio = 0; |
| 52 | 46 |
| 53 var percent = completedBytes / totalBytes; | 47 var loadingText = new Bitmap(atlas.getBitmapData('loading_text')) |
| 54 if (percent == double.INFINITY) percent = 0; | 48 ..x = 141 |
| 55 var percentClean = (percent * 1000).floor() / 10; | 49 ..y = 10; |
| 56 | 50 |
| 57 var barWidth = percent * _LOADING_BAR_PX_WIDTH; | 51 var loadingSprite = new Sprite() |
| 58 _loadingBar.style.width = '${barWidth.toInt()}px'; | 52 ..addChild(new Bitmap(atlas.getBitmapData('loading_background'))) |
| 53 ..addChild(bar) |
| 54 ..addChild(loadingText) |
| 55 ..x = stage.sourceWidth ~/ 2 - 1008 ~/ 2 |
| 56 ..y = 400 |
| 57 ..scaleX = 2 |
| 58 ..scaleY = 2 |
| 59 ..addTo(stage); |
| 60 |
| 61 resourceManager |
| 62 ..addTextureAtlas('opaque', 'resources/images/opaque.json', |
| 63 TextureAtlasFormat.JSON) |
| 64 ..addTextureAtlas('animated', 'resources/images/animated.json', |
| 65 TextureAtlasFormat.JSON); |
| 66 |
| 67 resourceManager.addSoundSprite('audio', 'resources/audio/audio.json'); |
| 68 |
| 69 resourceManager.onProgress.listen((e) { |
| 70 bar.ratio = resourceManager.finishedResources.length / |
| 71 resourceManager.resources.length; |
| 72 }); |
| 73 |
| 74 resourceManager.load().then((resMan) => |
| 75 _secondaryLoad(resMan, stage, loadingSprite)); |
| 59 } | 76 } |
| 60 | 77 |
| 61 void _onLoaded(_) { | 78 void _secondaryLoad(ResourceManager resourceManager, Stage stage, |
| 62 if (_imageLoader.state == ResourceLoader.StateLoaded && _audio.done) { | 79 Sprite loadingSprite) { |
| 80 var tween = stage.juggler.tween(loadingSprite, .5) |
| 81 ..animate.alpha.to(0) |
| 82 ..onComplete = () => stage.removeChild(loadingSprite); |
| 63 | 83 |
| 64 // | |
| 65 // load textures | |
| 66 // | |
| 67 var opaqueImage = _imageLoader.getResource(_OPAQUE_TEXTURE); | |
| 68 var transparentImage = _imageLoader.getResource(_TRANSPARENT_TEXTURE); | |
| 69 | |
| 70 // already loaded. Used in CSS. | |
| 71 var staticTransparentImage = | |
| 72 new ImageElement(src: _TRANSPARENT_STATIC_TEXTURE); | |
| 73 | |
| 74 var textures = getTextures(transparentImage, opaqueImage, | |
| 75 staticTransparentImage); | |
| 76 | |
| 77 var textureData = new TextureData(textures); | |
| 78 | |
| 79 // run the app | |
| 80 querySelector('#loading').style.display = 'none'; | |
| 81 _runGame(textureData); | |
| 82 } | |
| 83 } | |
| 84 | |
| 85 void _runGame(TextureData textureData) { | |
| 86 _updateAbout(); | 84 _updateAbout(); |
| 87 | 85 |
| 88 targetPlatform.aboutChanged.listen((_) => _updateAbout()); | 86 targetPlatform.aboutChanged.listen((_) => _updateAbout()); |
| 89 | 87 |
| 90 final size = targetPlatform.renderBig ? 16 : 7; | 88 var size = targetPlatform.size; |
| 91 final int m = (size * size * 0.15625).toInt(); | 89 var m = (size * size * 0.15625).toInt(); |
| 92 | 90 |
| 93 final CanvasElement gameCanvas = querySelector('#gameCanvas'); | 91 GameAudio.initialize(resourceManager); |
| 94 gameCanvas.style.userSelect = 'none'; | 92 var gameRoot = new GameRoot(size, size, m, stage, resourceManager); |
| 95 | |
| 96 final gameRoot = new GameRoot(size, size, m, gameCanvas, textureData); | |
| 97 | 93 |
| 98 // disable touch events | 94 // disable touch events |
| 99 window.onTouchMove.listen((args) => args.preventDefault()); | 95 window.onTouchMove.listen((args) => args.preventDefault()); |
| 100 | 96 |
| 101 window.onKeyDown.listen(_onKeyDown); | 97 window.onKeyDown.listen(_onKeyDown); |
| 102 | 98 |
| 103 querySelector('#popup').onClick.listen(_onPopupClick); | 99 querySelector('#popup').onClick.listen(_onPopupClick); |
| 104 | 100 |
| 105 titleClickedEvent.listen((args) => targetPlatform.toggleAbout(true)); | 101 titleClickedEvent.listen((args) => targetPlatform.toggleAbout(true)); |
| 106 } | 102 } |
| 107 | 103 |
| 108 void _onPopupClick(MouseEvent args) { | 104 void _onPopupClick(args) { |
| 109 if (args.toElement is! AnchorElement) { | 105 if (args.toElement is! AnchorElement) { |
| 110 targetPlatform.toggleAbout(false); | 106 targetPlatform.toggleAbout(false); |
| 111 } | 107 } |
| 112 } | 108 } |
| 113 | 109 |
| 114 void _onKeyDown(KeyboardEvent args) { | 110 void _onKeyDown(args) { |
| 115 var keyEvent = new KeyEvent.wrap(args); | 111 var keyEvent = new KeyEvent.wrap(args); |
| 116 switch (keyEvent.keyCode) { | 112 switch (keyEvent.keyCode) { |
| 117 case KeyCode.ESC: // esc | 113 case KeyCode.ESC: // esc |
| 118 targetPlatform.toggleAbout(false); | 114 targetPlatform.toggleAbout(false); |
| 119 break; | 115 break; |
| 120 case KeyCode.H: // h | 116 case KeyCode.H: // h |
| 121 targetPlatform.toggleAbout(); | 117 targetPlatform.toggleAbout(); |
| 122 break; | 118 break; |
| 123 } | 119 } |
| 124 } | 120 } |
| 125 | 121 |
| 126 void _updateAbout() { | 122 void _updateAbout() { |
| 127 var popDisplay = targetPlatform.showAbout ? 'inline-block' : 'none'; | 123 var popDisplay = targetPlatform.showAbout ? 'inline-block' : 'none'; |
| 128 querySelector('#popup').style.display = popDisplay; | 124 querySelector('#popup').style.display = popDisplay; |
| 129 } | 125 } |
| OLD | NEW |