Skip to content

Commit

Permalink
source sync from Dart SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
kevmoo committed Apr 23, 2014
1 parent f217007 commit 8aa1422
Show file tree
Hide file tree
Showing 87 changed files with 4,918 additions and 3,029 deletions.
26 changes: 26 additions & 0 deletions LICENSE
@@ -0,0 +1,26 @@
Copyright 2014, the Dart project authors. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* Neither the name of Google Inc. nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
6 changes: 3 additions & 3 deletions lib/platform_target.dart
Expand Up @@ -22,7 +22,7 @@ abstract class PlatformTarget {

Future<String> getValue(String key);

bool get renderBig;
int get size;

bool get showAbout;

Expand All @@ -36,7 +36,7 @@ class _DefaultPlatform extends PlatformTarget {
final StreamController _aboutController = new StreamController(sync: true);
bool _about = false;

_DefaultPlatform(): super.base();
_DefaultPlatform() : super.base();

@override
Future clearValues() => new Future(_values.clear);
Expand All @@ -48,7 +48,7 @@ class _DefaultPlatform extends PlatformTarget {
@override
Future<String> getValue(String key) => new Future(() => _values[key]);

bool get renderBig => false;
int get size => 7;

void toggleAbout([bool value]) {
assert(_about != null);
Expand Down
144 changes: 70 additions & 74 deletions lib/pop_pop_win.dart
@@ -1,99 +1,95 @@
library pop_pop_win;

import 'dart:async';
import 'dart:html';
import 'dart:web_audio';
import 'package:bot/bot.dart';
import 'package:bot_web/bot_html.dart';
import 'package:bot_web/bot_texture.dart';

import 'package:pop_pop_win/src/canvas.dart';
import 'package:pop_pop_win/platform_target.dart';
import 'package:pop_pop_win/src/html.dart';
import 'package:stagexl/stagexl.dart';

import 'src/textures.dart';

part 'src/pop_pop_win/audio.dart';
import 'platform_target.dart';
import 'src/audio.dart';
import 'src/platform.dart';
import 'src/stage.dart';

const String _ASSET_DIR = 'resources/';

const String _TRANSPARENT_TEXTURE =
'${_ASSET_DIR}images/transparent_animated.png';
const String _OPAQUE_TEXTURE = '${_ASSET_DIR}images/dart_opaque_01.jpg';
const String _TRANSPARENT_STATIC_TEXTURE =
'${_ASSET_DIR}images/transparent_static.png';

const int _LOADING_BAR_PX_WIDTH = 398;

DivElement _loadingBar;
ImageLoader _imageLoader;

final _Audio _audio = new _Audio();
const String _TRANSPARENT_TEXTURE = '${_ASSET_DIR}images/transparent.json';
const String _OPAQUE_TEXTURE = '${_ASSET_DIR}images/opaque.json';
const String _TRANSPARENT_STATIC_TEXTURE = '${_ASSET_DIR}images/static.json';

void startGame(PlatformTarget platform) {
Future startGame(PlatformTarget platform) {
initPlatform(platform);

_loadingBar = querySelector('.sprite.loading_bar');
_loadingBar.style.display = 'block';
_loadingBar.style.width = '0';
var stage = new Stage(querySelector('#gameCanvas'), webGL: true,
color: 0xb4ad7f, frameRate: 60);

_imageLoader = new ImageLoader([_TRANSPARENT_TEXTURE,
_OPAQUE_TEXTURE]);
_imageLoader.loaded.listen(_onLoaded);
_imageLoader.progress.listen(_onProgress);
_imageLoader.load();
}

void _onProgress(_) {
int completedBytes = _imageLoader.completedBytes;
int totalBytes = _imageLoader.totalBytes;
var renderLoop = new RenderLoop()
..addStage(stage);

completedBytes += _audio.completedBytes;
totalBytes += _audio.totalBytes;
BitmapData.defaultLoadOptions.webp = true;

var percent = completedBytes / totalBytes;
if (percent == double.INFINITY) percent = 0;
var percentClean = (percent * 1000).floor() / 10;
//have to load the loading bar first...
var resourceManager = new ResourceManager()
..addTextureAtlas("static", "resources/images/static.json",
TextureAtlasFormat.JSON);

var barWidth = percent * _LOADING_BAR_PX_WIDTH;
_loadingBar.style.width = '${barWidth.toInt()}px';
return resourceManager.load()
.then((resMan) => _initialLoad(resMan, stage));
}

void _onLoaded(_) {
if (_imageLoader.state == ResourceLoader.StateLoaded && _audio.done) {

//
// load textures
//
var opaqueImage = _imageLoader.getResource(_OPAQUE_TEXTURE);
var transparentImage = _imageLoader.getResource(_TRANSPARENT_TEXTURE);

// already loaded. Used in CSS.
var staticTransparentImage =
new ImageElement(src: _TRANSPARENT_STATIC_TEXTURE);

var textures = getTextures(transparentImage, opaqueImage,
staticTransparentImage);

var textureData = new TextureData(textures);

// run the app
querySelector('#loading').style.display = 'none';
_runGame(textureData);
}
void _initialLoad(ResourceManager resourceManager, Stage stage) {
var atlas = resourceManager.getTextureAtlas('static');

var bar = new Gauge(atlas.getBitmapData('loading_bar'), Gauge.DIRECTION_RIGHT)
..x = 51
..y = 8
..ratio = 0;

var loadingText = new Bitmap(atlas.getBitmapData('loading_text'))
..x = 141
..y = 10;

var loadingSprite = new Sprite()
..addChild(new Bitmap(atlas.getBitmapData('loading_background')))
..addChild(bar)
..addChild(loadingText)
..x = stage.sourceWidth ~/ 2 - 1008 ~/ 2
..y = 400
..scaleX = 2
..scaleY = 2
..addTo(stage);

resourceManager
..addTextureAtlas('opaque', 'resources/images/opaque.json',
TextureAtlasFormat.JSON)
..addTextureAtlas('animated', 'resources/images/animated.json',
TextureAtlasFormat.JSON);

resourceManager.addSoundSprite('audio', 'resources/audio/audio.json');

resourceManager.onProgress.listen((e) {
bar.ratio = resourceManager.finishedResources.length /
resourceManager.resources.length;
});

resourceManager.load().then((resMan) =>
_secondaryLoad(resMan, stage, loadingSprite));
}

void _runGame(TextureData textureData) {
void _secondaryLoad(ResourceManager resourceManager, Stage stage,
Sprite loadingSprite) {
var tween = stage.juggler.tween(loadingSprite, .5)
..animate.alpha.to(0)
..onComplete = () => stage.removeChild(loadingSprite);

_updateAbout();

targetPlatform.aboutChanged.listen((_) => _updateAbout());

final size = targetPlatform.renderBig ? 16 : 7;
final int m = (size * size * 0.15625).toInt();

final CanvasElement gameCanvas = querySelector('#gameCanvas');
gameCanvas.style.userSelect = 'none';
var size = targetPlatform.size;
var m = (size * size * 0.15625).toInt();

final gameRoot = new GameRoot(size, size, m, gameCanvas, textureData);
GameAudio.initialize(resourceManager);
var gameRoot = new GameRoot(size, size, m, stage, resourceManager);

// disable touch events
window.onTouchMove.listen((args) => args.preventDefault());
Expand All @@ -105,13 +101,13 @@ void _runGame(TextureData textureData) {
titleClickedEvent.listen((args) => targetPlatform.toggleAbout(true));
}

void _onPopupClick(MouseEvent args) {
void _onPopupClick(args) {
if (args.toElement is! AnchorElement) {
targetPlatform.toggleAbout(false);
}
}

void _onKeyDown(KeyboardEvent args) {
void _onKeyDown(args) {
var keyEvent = new KeyEvent.wrap(args);
switch (keyEvent.keyCode) {
case KeyCode.ESC: // esc
Expand Down
53 changes: 53 additions & 0 deletions lib/src/audio.dart
@@ -0,0 +1,53 @@
library pop_pop_win.audio;

import 'dart:math';

import 'package:stagexl/stagexl.dart';

class GameAudio {
static final Random _rnd = new Random();

static ResourceManager _resourceManager;

static const String _WIN = 'win',
_CLICK = 'click',
_POP = 'Pop',
_FLAG = 'flag',
_UNFLAG = 'unflag',
_BOMB = 'Bomb',
_THROW_DART = 'throw';

static void initialize(ResourceManager resourceManager) {
if (_resourceManager != null) throw new StateError('already initialized');
_resourceManager = resourceManager;
}

static void win() => _playAudio(_WIN);

static void click() => _playAudio(_CLICK);

static void pop() => _playAudio(_POP);

static void flag() => _playAudio(_FLAG);

static void unflag() => _playAudio(_UNFLAG);

static void bomb() => _playAudio(_BOMB);

static void throwDart() => _playAudio(_THROW_DART);

static void _playAudio(String name) {
if (_resourceManager == null) throw new StateError('Not initialized');
switch (name) {
case GameAudio._POP:
var i = _rnd.nextInt(8);
name = '${GameAudio._POP}$i';
break;
case GameAudio._BOMB:
var i = _rnd.nextInt(4);
name = '${GameAudio._BOMB}$i';
break;
}
_resourceManager.getSoundSprite('audio').play(name);
}
}
27 changes: 0 additions & 27 deletions lib/src/canvas.dart

This file was deleted.

63 changes: 0 additions & 63 deletions lib/src/canvas/board_element.dart

This file was deleted.

0 comments on commit 8aa1422

Please sign in to comment.