| OLD | NEW |
| (Empty) |
| 1 library game; | |
| 2 | |
| 3 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 import 'package:poppopwin/canvas.dart'; | |
| 9 import 'package:poppopwin/platform_target.dart'; | |
| 10 import 'package:poppopwin/html.dart'; | |
| 11 | |
| 12 import 'texture_data.dart'; | |
| 13 | |
| 14 part '_audio.dart'; | |
| 15 | |
| 16 const String _TRANSPARENT_TEXTURE = 'images/transparent_animated.png'; | |
| 17 const String _OPAQUE_TEXTURE = 'images/dart_opaque_01.jpg'; | |
| 18 const String _TRANSPARENT_STATIC_TEXTURE = 'images/transparent_static.png'; | |
| 19 | |
| 20 const int _LOADING_BAR_PX_WIDTH = 398; | |
| 21 | |
| 22 DivElement _loadingBar; | |
| 23 ImageLoader _imageLoader; | |
| 24 | |
| 25 _Audio _audio; | |
| 26 | |
| 27 void startGame(PlatformTarget platform) { | |
| 28 initPlatform(platform); | |
| 29 | |
| 30 _loadingBar = querySelector('.sprite.loading_bar'); | |
| 31 _loadingBar.style.display = 'block'; | |
| 32 _loadingBar.style.width = '0'; | |
| 33 | |
| 34 _imageLoader = new ImageLoader([_TRANSPARENT_TEXTURE, | |
| 35 _OPAQUE_TEXTURE]); | |
| 36 _imageLoader.loaded.listen(_onLoaded); | |
| 37 _imageLoader.progress.listen(_onProgress); | |
| 38 _imageLoader.load(); | |
| 39 | |
| 40 _audio = new _Audio(); | |
| 41 } | |
| 42 | |
| 43 void _onProgress(args) { | |
| 44 int completedBytes = _imageLoader.completedBytes; | |
| 45 int totalBytes = _imageLoader.totalBytes; | |
| 46 | |
| 47 completedBytes += _audio.completedBytes; | |
| 48 totalBytes += _audio.totalBytes; | |
| 49 | |
| 50 final percent = completedBytes / totalBytes; | |
| 51 final percentClean = (percent * 1000).floor() / 10; | |
| 52 | |
| 53 final barWidth = percent * _LOADING_BAR_PX_WIDTH; | |
| 54 _loadingBar.style.width = '${barWidth.toInt()}px'; | |
| 55 } | |
| 56 | |
| 57 void _onLoaded(args) { | |
| 58 if(_imageLoader.state == ResourceLoader.StateLoaded && _audio.done) { | |
| 59 | |
| 60 // | |
| 61 // load textures | |
| 62 // | |
| 63 final opaqueImage = _imageLoader.getResource(_OPAQUE_TEXTURE); | |
| 64 final transparentImage = _imageLoader.getResource(_TRANSPARENT_TEXTURE); | |
| 65 | |
| 66 // already loaded. Used in CSS. | |
| 67 final staticTransparentImage = new ImageElement(src: _TRANSPARENT_STATIC_TEX
TURE); | |
| 68 | |
| 69 final textures = getTextures(transparentImage, opaqueImage, staticTransparen
tImage); | |
| 70 | |
| 71 final textureData = new TextureData(textures); | |
| 72 | |
| 73 // run the app | |
| 74 querySelector('#loading').style.display = 'none'; | |
| 75 _runPPW(textureData); | |
| 76 } | |
| 77 } | |
| 78 | |
| 79 void _runPPW(TextureData textureData) { | |
| 80 _updateAbout(); | |
| 81 | |
| 82 targetPlatform.aboutChanged.listen((_) => _updateAbout()); | |
| 83 | |
| 84 final size = targetPlatform.renderBig ? 16 : 7; | |
| 85 final int m = (size * size * 0.15625).toInt(); | |
| 86 | |
| 87 final CanvasElement gameCanvas = querySelector('#gameCanvas'); | |
| 88 gameCanvas.style.userSelect = 'none'; | |
| 89 | |
| 90 final gameRoot = new GameRoot(size, size, m, gameCanvas, textureData); | |
| 91 | |
| 92 // disable touch events | |
| 93 window.onTouchMove.listen((args) => args.preventDefault()); | |
| 94 | |
| 95 window.onKeyDown.listen(_onKeyDown); | |
| 96 | |
| 97 querySelector('#popup').onClick.listen(_onPopupClick); | |
| 98 | |
| 99 titleClickedEvent.listen((args) => targetPlatform.toggleAbout(true)); | |
| 100 } | |
| 101 | |
| 102 void _onPopupClick(MouseEvent args) { | |
| 103 if(!(args.toElement is AnchorElement)) { | |
| 104 targetPlatform.toggleAbout(false); | |
| 105 } | |
| 106 } | |
| 107 | |
| 108 void _onKeyDown(KeyboardEvent args) { | |
| 109 switch(args.keyCode) { | |
| 110 case 27: // esc | |
| 111 targetPlatform.toggleAbout(false); | |
| 112 break; | |
| 113 case 72: // h | |
| 114 targetPlatform.toggleAbout(); | |
| 115 break; | |
| 116 } | |
| 117 } | |
| 118 | |
| 119 void _updateAbout() { | |
| 120 var popDisplay = targetPlatform.showAbout ? 'inline-block' : 'none'; | |
| 121 querySelector('#popup').style.display = popDisplay; | |
| 122 } | |
| OLD | NEW |