main.dart 6.48 KB
Newer Older
1 2 3 4
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
import 'dart:async';

7
import 'package:sky/material.dart';
8
import 'package:sky/painting.dart';
9
import 'package:sky/rendering.dart';
10
import 'package:sky/services.dart';
11
import 'package:sky/widgets.dart';
12
import 'package:skysprites/skysprites.dart';
13

14
import 'game_demo.dart';
15 16 17 18

AssetBundle _initBundle() {
  if (rootBundle != null)
    return rootBundle;
19
  return new NetworkAssetBundle(new Uri.directory(Uri.base.origin));
20 21 22 23
}

final AssetBundle _bundle = _initBundle();

24
ImageMap _imageMap;
25
SpriteSheet _spriteSheet;
26
SpriteSheet _spriteSheetUI;
27
Map<String,SoundEffect> _sounds = {};
28 29

main() async {
30 31 32 33
  _imageMap = new ImageMap(_bundle);

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

35
  loads.add(_imageMap.load([
36 37 38
    'assets/nebula.png',
    'assets/sprites.png',
    'assets/starfield.png',
39
    'assets/game_ui.png',
40
  ]));
41

42 43 44 45 46 47 48 49 50 51 52
  // 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'));

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

  await Future.wait(loads);

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

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

60
  assert(_spriteSheet.image != null);
61

62
  SoundTrackPlayer stPlayer = SoundTrackPlayer.sharedInstance();
63
  SoundTrack music = await stPlayer.load(_bundle.load('assets/temp_music.aac'));
64 65
  stPlayer.play(music);

66
  runApp(new GameDemo());
67 68
}

69 70 71 72 73 74 75 76 77 78
// TODO(viktork): The task bar purple is the wrong purple, we may need
// a custom theme swatch to match the purples in the sprites.
final ThemeData _theme = new ThemeData(
  brightness: ThemeBrightness.light,
  primarySwatch: Colors.purple
);

class GameDemo extends StatefulComponent {
  GameDemoState createState() => new GameDemoState();
}
79

80
class GameDemoState extends State<GameDemo> {
81
  NodeWithSize _game;
82
  int _lastScore = 0;
83

84 85 86 87 88 89 90 91
  Widget build(BuildContext context) {
    return new App(
      title: 'Asteroids',
      theme: _theme,
      routes: {
        '/': _buildMainScene,
        '/game': _buildGameScene
      }
92 93 94
    );
  }

95
  Widget _buildGameScene(NavigatorState navigator, Route route) {
96
    return new SpriteWidget(_game, SpriteBoxTransformMode.fixedWidth);
97 98 99
  }

  Widget _buildMainScene(navigator, route) {
100
    return new Stack([
101
      new SpriteWidget(new MainScreenBackground(), SpriteBoxTransformMode.fixedWidth),
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
      new Column([
          new TextureButton(
            onPressed: () {
              _game = new GameDemoNode(
                _imageMap,
                _spriteSheet,
                _spriteSheetUI,
                _sounds,
                (lastScore) {
                  setState(() {_lastScore = lastScore;});
                  navigator.pop();
                }
              );
              navigator.pushNamed('/game');
            },
            texture: _spriteSheetUI['btn_play_up.png'],
            textureDown: _spriteSheetUI['btn_play_down.png'],
            width: 128.0,
            height: 128.0
          ),
          new Text(
            "Last Score: $_lastScore",
            style: new TextStyle(fontSize:20.0)
          )
        ],
        justifyContent: FlexJustifyContent.center
      )
129
    ]);
130 131
  }
}
132

133
class TextureButton extends StatefulComponent {
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
  TextureButton({
    Key key,
    this.onPressed,
    this.texture,
    this.textureDown,
    this.width: 128.0,
    this.height: 128.0
  }) : super(key: key);

  final Function onPressed;
  final Texture texture;
  final Texture textureDown;
  final double width;
  final double height;

149 150 151 152 153
  TextureButtonState createState() => new TextureButtonState();
}

class TextureButtonState extends ButtonState<TextureButton> {
  Widget buildContent(BuildContext context) {
154 155
    return new Listener(
      child: new Container(
156 157
        width: config.width,
        height: config.height,
158 159 160 161
        child: new CustomPaint(
          callback: paintCallback,
          token: new _TextureButtonToken(
            highlight,
162 163 164 165
            config.texture,
            config.textureDown,
            config.width,
            config.height
166 167 168 169
          )
        )
      ),
      onPointerUp: (_) {
170 171
        if (config.onPressed != null)
          config.onPressed();
172 173 174 175 176
      }
    );
  }

  void paintCallback(PaintingCanvas canvas, Size size) {
177
    if (config.texture == null)
178 179
      return;

180
    canvas.save();
181
    if (highlight && config.textureDown != null) {
182
      // Draw down state
183 184
      canvas.scale(size.width / config.textureDown.size.width, size.height / config.textureDown.size.height);
      config.textureDown.drawTexture(canvas, Point.origin, new Paint());
185 186
    } else {
      // Draw up state
187 188
      canvas.scale(size.width / config.texture.size.width, size.height / config.texture.size.height);
      config.texture.drawTexture(canvas, Point.origin, new Paint());
189
    }
190
    canvas.restore();
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
  }
}

class _TextureButtonToken {
  _TextureButtonToken(
    this._highlight,
    this._texture,
    this._textureDown,
    this._width,
    this._height
  );

  final bool _highlight;
  final Texture _texture;
  final Texture _textureDown;
  final double _width;
  final double _height;

  bool operator== (other) {
    return
      other is _TextureButtonToken &&
      _highlight == other._highlight &&
      _texture == other._texture &&
      _textureDown == other._textureDown &&
      _width == other._width &&
      _height == other._height;
  }

  int get hashCode {
    int value = 373;
    value = 37 * value * _highlight.hashCode;
    value = 37 * value * _texture.hashCode;
    value = 37 * value * _textureDown.hashCode;
    value = 37 * value * _width.hashCode;
    value = 37 * value * _height.hashCode;
    return value;
  }
}
229 230

class MainScreenBackground extends NodeWithSize {
231
  MainScreenBackground() : super(new Size(320.0, 320.0)) {
232 233
    assert(_spriteSheet.image != null);

234 235 236
    StarField starField = new StarField(_spriteSheet, 200, true);
    addChild(starField);
  }
237 238 239 240 241

  void paint(PaintingCanvas canvas) {
    canvas.drawRect(new Rect.fromLTWH(0.0, 0.0, 320.0, 320.0), new Paint()..color=new Color(0xff000000));
    super.paint(canvas);
  }
242
}