diff --git a/examples/game/lib/game_demo.dart b/examples/game/lib/game_demo.dart index ee45d52307d..88ac1160b99 100644 --- a/examples/game/lib/game_demo.dart +++ b/examples/game/lib/game_demo.dart @@ -5,6 +5,7 @@ import 'dart:math' as math; import 'dart:sky' as sky; import 'package:sky/rendering/object.dart'; +import 'package:sky/painting/text_style.dart'; import 'package:sky/widgets/framework.dart'; import 'package:skysprites/skysprites.dart'; import 'package:vector_math/vector_math.dart'; diff --git a/examples/game/lib/game_demo_node.dart b/examples/game/lib/game_demo_node.dart index 457c81ea563..aa405d30e55 100644 --- a/examples/game/lib/game_demo_node.dart +++ b/examples/game/lib/game_demo_node.dart @@ -4,7 +4,7 @@ final double _gameSizeWidth = 320.0; double _gameSizeHeight = 320.0; final double _chunkSpacing = 640.0; -final int _chunksPerLevel = 5; +final int _chunksPerLevel = 6; final bool _drawDebug = false; @@ -190,25 +190,22 @@ class GameDemoNode extends NodeWithSize { } void addLevelChunk(int chunk, double yPos) { - if (chunk == 0) { - // Leave the first chunk empty - return; - } - - chunk -= 1; - int level = chunk ~/ _chunksPerLevel; int part = chunk % _chunksPerLevel; if (part == 0) { + LevelLabel lbl = new LevelLabel(_objectFactory, level + 1); + lbl.position = new Point(0.0, yPos + _chunkSpacing / 2.0 - 150.0); + _level.addChild(lbl); + } else if (part == 1) { _objectFactory.addAsteroids(10 + level * 4, yPos, 0.0 + (level * 0.2).clamp(0.0, 0.7)); - } else if (part == 1) { - _objectFactory.addEnemyScoutSwarm(4 + level * 2, yPos); } else if (part == 2) { - _objectFactory.addAsteroids(10 + level * 4, yPos, 0.0 + (level * 0.2).clamp(0.0, 0.7)); + _objectFactory.addEnemyScoutSwarm(4 + level * 2, yPos); } else if (part == 3) { - _objectFactory.addEnemyDestroyerSwarm(2 + level, yPos); + _objectFactory.addAsteroids(10 + level * 4, yPos, 0.0 + (level * 0.2).clamp(0.0, 0.7)); } else if (part == 4) { + _objectFactory.addEnemyDestroyerSwarm(2 + level, yPos); + } else if (part == 5) { _objectFactory.addAsteroids(10 + level * 4, yPos, 0.0 + (level * 0.2).clamp(0.0, 0.7)); } } diff --git a/examples/game/lib/game_objects.dart b/examples/game/lib/game_objects.dart index fca5096f3d2..185e548903a 100644 --- a/examples/game/lib/game_objects.dart +++ b/examples/game/lib/game_objects.dart @@ -86,6 +86,23 @@ abstract class GameObject extends Node { } } +class LevelLabel extends GameObject { + LevelLabel(GameObjectFactory f, int level) : super(f) { + canDamageShip = false; + canBeDamaged = false; + + Label lbl = new Label( + "LEVEL $level", + new TextStyle( + textAlign: TextAlign.center, + color:new Color(0xffffffff), + fontSize: 24.0, + fontWeight: FontWeight.w600 + )); + addChild(lbl); + } +} + class Ship extends GameObject { Ship(GameObjectFactory f) : super(f) { // Add main ship sprite diff --git a/skysprites/lib/label.dart b/skysprites/lib/label.dart new file mode 100644 index 00000000000..673a30d6cc6 --- /dev/null +++ b/skysprites/lib/label.dart @@ -0,0 +1,58 @@ +part of skysprites; + +class Label extends Node { + + Label(this._text, [this._textStyle]) { + if (_textStyle == null) { + _textStyle = new TextStyle(); + } + } + + String _text; + + String get text => _text; + + set text(String text) { + _text = text; + _painter = null; + } + + TextStyle _textStyle; + + TextStyle get textStyle => _textStyle; + + set textStyle(TextStyle textStyle) { + _textStyle = textStyle; + _painter = null; + } + + TextPainter _painter; + double _width; + + void paint(PaintingCanvas canvas) { + if (_painter == null) { + PlainTextSpan textSpan = new PlainTextSpan(_text); + StyledTextSpan styledTextSpan = new StyledTextSpan(_textStyle, [textSpan]); + _painter = new TextPainter(styledTextSpan); + + _painter.maxWidth = double.INFINITY; + _painter.minWidth = 0.0; + _painter.layout(); + + _width = _painter.maxContentWidth.ceil().toDouble(); + + _painter.maxWidth = _width; + _painter.minWidth = _width; + _painter.layout(); + } + + Offset offset = Offset.zero; + if (_textStyle.textAlign == TextAlign.center) { + offset = new Offset(-_width / 2.0, 0.0); + } else if (_textStyle.textAlign == TextAlign.right) { + offset = new Offset(-_width, 0.0); + } + + _painter.paint(canvas, offset); + } +} diff --git a/skysprites/lib/skysprites.dart b/skysprites/lib/skysprites.dart index cdffb4a3901..e079cdaff9c 100644 --- a/skysprites/lib/skysprites.dart +++ b/skysprites/lib/skysprites.dart @@ -17,6 +17,7 @@ import 'package:sky/mojo/asset_bundle.dart'; import 'package:sky/mojo/shell.dart' as shell; import 'package:sky/rendering/box.dart'; import 'package:sky/rendering/object.dart'; +import 'package:sky/painting/text_painter.dart'; import 'package:sky/widgets/framework.dart'; import 'package:sky_services/media/media.mojom.dart'; import 'package:vector_math/vector_math.dart'; @@ -26,6 +27,7 @@ part 'constraint.dart'; part 'action_spline.dart'; part 'color_secuence.dart'; part 'image_map.dart'; +part 'label.dart'; part 'layer.dart'; part 'node.dart'; part 'node3d.dart';