Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(11)

Side by Side Diff: sky/examples/game/lib/game_world.dart

Issue 1161023006: Adds basic support for images in game example (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 part of game;
2
3 class GameWorld extends TransformNode {
4
5 World world;
6 List<Body> bodies = [];
7 Image _image;
8
9 GameWorld(double width, double height) {
10 this.width = width;
11 this.height = height;
12
13 world = new World.withGravity(new Vector2(0.0, 0.0));
14
15 // Load and add background
16 Image imgBg = new Image()..src="https://raw.githubusercontent.com/slembcke/G alacticGuardian.spritebuilder/GDC/Packages/SpriteBuilder%20Resources.sbpack/reso urces-auto/BurnTexture.png";
17 SpriteNode sprtBg = new SpriteNode.withImage(imgBg);
18 sprtBg.width = width;
19 sprtBg.height = height;
20 sprtBg.pivot = new Vector2(0.0, 0.0);
21 this.children.add(sprtBg);
22
23 // Load asteroid image
24 _image = new Image()..src="https://raw.githubusercontent.com/slembcke/Galact icGuardian.spritebuilder/GDC/Packages/SpriteBuilder%20Resources.sbpack/Sprites/r esources-auto/asteroid_big_002.png";
25 }
26
27 void addAsteroid([double radius=20.0]) {
28
29 // Create shape
30 final CircleShape shape = new CircleShape();
31 shape.radius = radius;
32
33 // Define fixture (links body and shape)
34 final FixtureDef activeFixtureDef = new FixtureDef();
35 activeFixtureDef.restitution = 1.0;
36 activeFixtureDef.density = 0.05;
37 activeFixtureDef.shape = shape;
38
39 // Define body
40 final BodyDef bodyDef = new BodyDef();
41 bodyDef.type = BodyType.DYNAMIC;
42 bodyDef.position = new Vector2(0.0, 30.0);
43 bodyDef.linearDamping = 0.0;
44 bodyDef.angularDamping = 0.0;
45
46 // Create body and fixture from definitions
47 final Body body = world.createBody(bodyDef);
48 body.createFixtureFromFixtureDef(activeFixtureDef);
49
50 // Set position of object
51 Math.Random rand = new Math.Random();
52 body.setTransform(new Vector2(rand.nextDouble() * this.width, rand.nextDoubl e() * this.height), 0.0);
53 body.applyLinearImpulse(new Vector2(rand.nextDouble()*10000.0-5000.0, rand.n extDouble()*10000.0-5000.0), new Vector2(0.0, 0.0), true);
54
55 // Add to list
56 bodies.add(body);
57
58 SpriteNode sprt = new SpriteNode.withImage(_image);
59 sprt.width = radius*2;
60 sprt.height = radius*2;
61 body.userData = sprt;
62 this.children.add(sprt);
63 }
64
65 void update(double dt) {
66 world.stepDt(1.0/60.0, 10, 10); // Pass in dt
67
68 bodies.forEach(updateBody);
69 }
70
71 void updateBody(Body body) {
72 SpriteNode sprt = body.userData;
73 double rot = 0.0; //body.getRotation();
74
75 // Check bounds and warp objects
76 if (body.position[0] < -sprt.width/2) {
77 body.setTransform(new Vector2(body.position[0] + this.width + sprt.width, body.position[1]), rot);
78 }
79 if (body.position[0] > this.width + sprt.width/2) {
80 body.setTransform(new Vector2(body.position[0] - (this.width + sprt.width) , body.position[1]), rot);
81 }
82 if (body.position[1] < -sprt.height/2) {
83 body.setTransform(new Vector2(body.position[0], body.position[1] + this.he ight + sprt.height), rot);
84 }
85 if (body.position[1] > this.height + sprt.height/2) {
86 body.setTransform(new Vector2(body.position[0], body.position[1] - (this.h eight + sprt.height)), rot);
87 }
88
89 // Update sprite
90 sprt.position = body.position;
91 sprt.rotation = body.getAngle();
92 }
93 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698