diff --git a/examples/game/lib/custom_actions.dart b/examples/game/lib/custom_actions.dart index 19e507f574b..f1c01ed38f3 100644 --- a/examples/game/lib/custom_actions.dart +++ b/examples/game/lib/custom_actions.dart @@ -17,3 +17,17 @@ class ActionCircularMove extends ActionInterval { setter(pos); } } + +class ActionOscillate extends ActionInterval { + ActionOscillate(this.setter, this.center, this.radius, double duration) : super(duration); + + final Function setter; + final Point center; + final double radius; + + void update(double t) { + double rad = radians(t * 360.0); + Offset offset = new Offset(math.sin(rad) * radius, 0.0); + setter(center + offset); + } +} diff --git a/examples/game/lib/game_demo.dart b/examples/game/lib/game_demo.dart index c3256132671..70c1244d04c 100644 --- a/examples/game/lib/game_demo.dart +++ b/examples/game/lib/game_demo.dart @@ -17,5 +17,6 @@ part 'game_demo_node.dart'; part 'game_objects.dart'; part 'game_object_factory.dart'; part 'player_state.dart'; +part 'power_bar.dart'; part 'repeated_image.dart'; part 'star_field.dart'; diff --git a/examples/game/lib/game_objects.dart b/examples/game/lib/game_objects.dart index dbe8657ca00..2cb1ccf8e87 100644 --- a/examples/game/lib/game_objects.dart +++ b/examples/game/lib/game_objects.dart @@ -426,9 +426,15 @@ class EnemyBoss extends Obstacle { maxDamage = 40.0; constraints = [new ConstraintRotationToNode(f.level.ship, dampening: 0.05)]; + + _powerBar = new PowerBar(new Size(60.0, 10.0)); + _powerBar.position = new Point(-80.0, 0.0); + _powerBar.pivot = new Point(0.5, 0.5); + addChild(_powerBar); } Sprite _sprt; + PowerBar _powerBar; int _countDown = randomInt(120) + 240; @@ -436,18 +442,49 @@ class EnemyBoss extends Obstacle { _countDown -= 1; if (_countDown <= 0) { // Shoot at player - EnemyLaser laser = new EnemyLaser(f, rotation, 5.0, new Color(0xffffe38e)); - laser.position = position; - f.level.addChild(laser); + fire(10.0); + fire(0.0); + fire(-10.0); _countDown = 60 + randomInt(120); } + + _powerBar.rotation = -rotation; + } + + void fire(double r) { + r += rotation; + EnemyLaser laser = new EnemyLaser(f, r, 5.0, new Color(0xffffe38e)); + + double rad = radians(r); + Offset startOffset = new Offset(math.cos(rad) * 30.0, math.sin(rad) * 30.0); + + laser.position = position + startOffset; + f.level.addChild(laser); + } + + void setupActions() { + ActionOscillate oscillate = new ActionOscillate((a) => position = a, position, 120.0, 3.0); + actions.run(new ActionRepeatForever(oscillate)); } void destroy() { f.playerState.boss = null; super.destroy(); } + + set damage(double d) { + super.damage = d; + _sprt.actions.stopAll(); + _sprt.actions.run(new ActionTween( + (a) =>_sprt.colorOverlay = a, + new Color.fromARGB(180, 255, 3, 86), + new Color(0x00000000), + 0.3 + )); + + _powerBar.power = (1.0 - (damage / maxDamage)).clamp(0.0, 1.0); + } } class Collectable extends GameObject { diff --git a/examples/game/lib/power_bar.dart b/examples/game/lib/power_bar.dart new file mode 100644 index 00000000000..53a41062617 --- /dev/null +++ b/examples/game/lib/power_bar.dart @@ -0,0 +1,21 @@ +part of game; + +class PowerBar extends NodeWithSize { + PowerBar(Size size, [this.power = 1.0]) : super(size); + + double power; + + Paint _paintFill = new Paint() + ..color = new Color(0xffffffff); + Paint _paintOutline = new Paint() + ..color = new Color(0xffffffff) + ..strokeWidth = 1.0 + ..setStyle(sky.PaintingStyle.stroke); + + void paint(PaintingCanvas canvas) { + applyTransformForPivot(canvas); + + canvas.drawRect(new Rect.fromLTRB(0.0, 0.0, size.width - 0.0, size.height - 0.0), _paintOutline); + canvas.drawRect(new Rect.fromLTRB(2.0, 2.0, (size.width - 2.0) * power, size.height - 2.0), _paintFill); + } +}