test_physics.dart 3.07 KB
Newer Older
1
import 'dart:ui';
2

3 4 5 6
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
7
import 'package:flutter_sprites/flutter_sprites.dart';
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

AssetBundle _initBundle() {
  if (rootBundle != null)
    return rootBundle;
  return new NetworkAssetBundle(Uri.base);
}

final AssetBundle _bundle = _initBundle();

ImageMap _images;
SpriteSheet _spriteSheet;

main() async {
  _images = new ImageMap(_bundle);

  await _images.load([
    'assets/sprites.png'
  ]);

  String json = await _bundle.loadString('assets/sprites.json');
  _spriteSheet = new SpriteSheet(_images['assets/sprites.png'], json);

Adam Barth's avatar
Adam Barth committed
30
  runApp(new MaterialApp(
31 32
    title: 'Test Physics',
    theme: new ThemeData(
33 34
      brightness: ThemeBrightness.light,
      primarySwatch: Colors.purple
35 36
    ),
    routes: {
37
      '/': (RouteArguments args) {
38
        return new SpriteWidget(
39 40
          new TestBed(),
          SpriteBoxTransformMode.letterbox
41 42 43 44
        );
      }
    }
  ));
45 46 47 48
}

class TestBed extends NodeWithSize {
  Sprite _obstacle;
49
  PhysicsNode _physicsNode;
50 51

  TestBed() : super(new Size(1024.0, 1024.0)) {
52
    _physicsNode = new PhysicsNode(new Offset(0.0, 100.0));
53 54 55 56 57 58 59 60 61 62

    _obstacle = new Sprite(_spriteSheet["ship.png"]);
    _obstacle.position = new Point(532.0, 800.0);
    _obstacle.size = new Size(64.0, 64.0);
    _obstacle.physicsBody = new PhysicsBody(
      new PhysicsShapeCircle(Point.origin, 32.0),
      type: PhysicsBodyType.static,
      friction: 0.5,
      tag: "obstacle"
    );
63 64
    _physicsNode.addChild(_obstacle);
    _physicsNode.addContactCallback(myCallback, "obstacle", "ship", PhysicsContactType.begin);
65

66
    addChild(_physicsNode);
67 68 69 70 71 72 73 74 75 76 77

    userInteractionEnabled = true;
  }

  void myCallback(PhysicsContactType type, PhysicsContact contact) {
    print("CONTACT type: $type");
  }

  bool handleEvent(SpriteBoxEvent event) {
    if (event.type == "pointerdown") {
      Point pos = convertPointToNodeSpace(event.boxPosition);
78 79 80

      Sprite shipA;
      shipA = new Sprite(_spriteSheet["ship.png"]);
81
      shipA.opacity = 0.3;
82 83 84 85
      shipA.position = new Point(pos.x - 40.0, pos.y);
      shipA.size = new Size(64.0, 64.0);
      shipA.physicsBody = new PhysicsBody(new PhysicsShapeCircle(Point.origin, 32.0),
        friction: 0.5,
86
        restitution: 0.5,
87 88 89 90
        tag: "ship"
      );
      _physicsNode.addChild(shipA);
      shipA.physicsBody.applyLinearImpulse(
91
        new Offset(randomSignedDouble() * 5000.0, randomSignedDouble() * 5000.0),
92 93 94 95 96
        shipA.position
      );

      Sprite shipB;
      shipB = new Sprite(_spriteSheet["ship.png"]);
97
      shipB.opacity = 0.3;
98 99 100 101
      shipB.position = new Point(pos.x + 40.0, pos.y);
      shipB.size = new Size(64.0, 64.0);
      shipB.physicsBody = new PhysicsBody(new PhysicsShapePolygon([new Point(-25.0, -25.0), new Point(25.0, -25.0), new Point(25.0, 25.0), new Point(-25.0, 25.0)]),
        friction: 0.5,
102
        restitution: 0.5,
103 104 105 106
        tag: "ship"
      );
      _physicsNode.addChild(shipB);

107
      new PhysicsJointRevolute(shipA.physicsBody, shipB.physicsBody, pos);
108 109 110 111
    }
    return true;
  }
}