Skip to content

Commit

Permalink
Make Asteroids.apk use background music from its bundle
Browse files Browse the repository at this point in the history
This was the last instance of loading assets off of Github.

@abarth
  • Loading branch information
eseidelGoogle committed Aug 18, 2015
1 parent 9f0d108 commit 908490d
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 24 deletions.
6 changes: 4 additions & 2 deletions examples/game/README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
Assets for this game are taken from Galactic Guardian:
https://github.com/slembcke/GalacticGuardian.spritebuilder
Assets for this game are from Galactic Guardian:
https://github.com/slembcke/GalacticGuardian.spritebuilder
And are used under MIT license:
https://github.com/slembcke/GalacticGuardian.spritebuilder/pull/2
Binary file added examples/game/assets/temp_music.aac
Binary file not shown.
40 changes: 25 additions & 15 deletions examples/game/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';

import 'package:sky/mojo/asset_bundle.dart';
import 'package:sky/theme/colors.dart' as colors;
import 'package:sky/rendering/object.dart';
Expand All @@ -24,40 +26,48 @@ AssetBundle _initBundle() {

final AssetBundle _bundle = _initBundle();

ImageMap _loader;
ImageMap _imageMap;
SpriteSheet _spriteSheet;
SpriteSheet _spriteSheetUI;
GameDemoApp _app;
Map<String,SoundEffect> _sounds = {};

main() async {
_loader = new ImageMap(_bundle);
_imageMap = new ImageMap(_bundle);

// Use a list to wait on all loads in parallel just before starting the app.
List loads = [];

await _loader.load([
loads.add(_imageMap.load([
'assets/nebula.png',
'assets/sprites.png',
'assets/starfield.png',
'assets/game_ui.png',
]);
]));

// TODO(eseidel): These load in serial which is bad for startup!
String json = await _bundle.loadString('assets/sprites.json');
_spriteSheet = new SpriteSheet(_loader['assets/sprites.png'], json);
_spriteSheet = new SpriteSheet(_imageMap['assets/sprites.png'], json);

json = await _bundle.loadString('assets/game_ui.json');
_spriteSheetUI = new SpriteSheet(_loader["assets/game_ui.png"], json);
_spriteSheetUI = new SpriteSheet(_imageMap['assets/game_ui.png'], json);

_app = new GameDemoApp();

_sounds["explosion"] = new SoundEffect(_bundle.load('assets/explosion.wav'));
_sounds["laser"] = new SoundEffect(_bundle.load('assets/laser.wav'));
// TODO(eseidel): SoundEffect doesn't really do anything except hold a future.
_sounds['explosion'] = new SoundEffect(_bundle.load('assets/explosion.wav'));
_sounds['laser'] = new SoundEffect(_bundle.load('assets/laser.wav'));

await _sounds["explosion"].load();
await _sounds["laser"].load();
loads.addAll([
_sounds['explosion'].load(),
_sounds['laser'].load(),
]);

SoundTrackPlayer stPlayer = SoundTrackPlayer.sharedInstance();
SoundTrack music = await stPlayer.load('https://github.com/slembcke/GalacticGuardian.spritebuilder/raw/GDC/Source/Resources/TempMusic.aac');
SoundTrack music = await stPlayer.load(_bundle.load('assets/temp_music.aac'));
stPlayer.play(music);

await Future.wait(loads);
runApp(_app);
}

Expand Down Expand Up @@ -111,7 +121,7 @@ class GameDemoApp extends App {
_game = new GameDemoWorld(
_app,
navigator,
_loader,
_imageMap,
_spriteSheet,
_spriteSheetUI,
_sounds,
Expand All @@ -121,8 +131,8 @@ class GameDemoApp extends App {
);
navigator.pushNamed('/game');
},
texture: _spriteSheetUI["btn_play_up.png"],
textureDown: _spriteSheetUI["btn_play_down.png"],
texture: _spriteSheetUI['btn_play_up.png'],
textureDown: _spriteSheetUI['btn_play_down.png'],
width: 128.0,
height: 128.0
),
Expand Down Expand Up @@ -231,7 +241,7 @@ class _TextureButtonToken {

class MainScreenBackground extends NodeWithSize {
MainScreenBackground() : super(new Size(1024.0, 1024.0)) {
Sprite sprtBackground = new Sprite.fromImage(_loader["assets/starfield.png"]);
Sprite sprtBackground = new Sprite.fromImage(_imageMap['assets/starfield.png']);
sprtBackground.position = new Point(512.0, 512.0);
addChild(sprtBackground);

Expand Down
7 changes: 2 additions & 5 deletions examples/game/lib/sound.dart
Original file line number Diff line number Diff line change
Expand Up @@ -170,16 +170,13 @@ class SoundTrackPlayer {

MediaServiceProxy _mediaService;

Future<SoundTrack> load(String url) async {
Future<SoundTrack> load(Future<MojoDataPipeConsumer> pipe) async {
// Create media player
SoundTrack soundTrack = new SoundTrack();
soundTrack._player = new MediaPlayerProxy.unbound();
_mediaService.ptr.createPlayer(soundTrack._player);

// Load and prepare
UrlResponse response = await fetchUrl(url);
await soundTrack._player.ptr.prepare(response.body);

await soundTrack._player.ptr.prepare(await pipe);
return soundTrack;
}

Expand Down
2 changes: 0 additions & 2 deletions examples/game/lib/sprites.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ import 'dart:typed_data';
import 'dart:sky';

import 'package:mojo/core.dart';
import 'package:mojo/mojo/url_response.mojom.dart';
import 'package:sky/animation/curves.dart';
import 'package:sky/base/scheduler.dart' as scheduler;
import 'package:sky/mojo/asset_bundle.dart';
import 'package:sky/mojo/net/fetch.dart';
import 'package:sky/mojo/shell.dart' as shell;
import 'package:sky/rendering/box.dart';
import 'package:sky/rendering/object.dart';
Expand Down
1 change: 1 addition & 0 deletions examples/game/sky.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ assets:
- assets/laser.wav
- assets/levelup.wav
- assets/pickup.wav
- assets/temp_music.aac

0 comments on commit 908490d

Please sign in to comment.