forked from flame-engine/flame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.dart
127 lines (100 loc) · 3.65 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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
import 'dart:math';
import 'package:pogo/game_engine.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Screen.setFullScreen();
await Screen.setPortrait();
GestureInitializer.detectTaps = true;
await Assets.svgCache.load("planet.svg", scale: 2.0);
await Assets.svgCache.load("moon.svg", scale: 0.27 * 2.0);
runApp(Game().widget);
await Screen.waitForStartupSizing();
MainEntity();
}
class MainEntity extends GameEntity {
static const double rotationFactor = 4.0;
static const double stretchFactor = 0.25;
static const double moonScaleFactor = 0.5;
static const double moonDistance = 200.0;
double startTime = Time.now;
GameEntity planet;
Moon moon;
Vector2 moonCenter = Vector2.zero();
// Construct the MainEntity and other entities and content.
MainEntity() {
// Create some sprite entities, in various ways.
planet = SpritePrefab(
SpriteComponent.fromSvgCache("planet.svg"),
position: Vector2(Screen.size.width / 2, Screen.size.height / 2),
zOrder: 100,
);
moonCenter = Vector2(Screen.size.width / 2, Screen.size.height / 2);
moon = Moon(moonCenter.clone(), 0);
}
// Main game loop.
@override
void update() {
// Rotate the moon (on the wrong axis... this is 2D so why not?).
moon.rotation += moon.spinDirection * rotationFactor * Time.deltaTime;
moon.rotation %= 2 * pi; // trims the rotation value to keep it in bounds
// Set up for the next trick: clock-based transformations.
double delta = Time.now - startTime;
if (delta >= 4.0) {
delta -= 4.0;
startTime += 4.0; // reset the sequence start time
}
// Do more scientifically-wrong stuff while showing off more features
// like scaling and dynamic Z ordering!
if (delta < 1.0 || delta >= 4.0) {
planet.scale.x = 1 + delta * stretchFactor;
planet.scale.y = 1 - delta * stretchFactor;
moon.scale.x = moon.scale.y = 1 + sin((1 - delta) * pi / 2) * moonScaleFactor;
moon.position.y = moonCenter.y - moonDistance * cos((1 - delta) * pi / 2);
} else if (delta < 2.0) {
delta -= 1.0;
planet.scale.x = 1 + (1 - delta) * stretchFactor;
planet.scale.y = 1 - (1 - delta) * stretchFactor;
moon.scale.x = moon.scale.y = 1 - sin(delta * pi / 2) * moonScaleFactor;
moon.position.y = moonCenter.y - moonDistance * cos(delta * pi / 2);
moon.zOrder = 200;
} else if (delta < 3.0) {
delta -= 2.0;
planet.scale.x = 1 - delta * stretchFactor;
planet.scale.y = 1 + delta * stretchFactor;
moon.scale.x = moon.scale.y = 1 - sin((1 - delta) * pi / 2) * moonScaleFactor;
moon.position.y = moonCenter.y + moonDistance * cos((1 - delta) * pi / 2);
} else if (delta < 4.0) {
delta -= 3.0;
planet.scale.x = 1 - (1 - delta) * stretchFactor;
planet.scale.y = 1 + (1 - delta) * stretchFactor;
moon.scale.x = moon.scale.y = 1 + sin(delta * pi / 2) * moonScaleFactor;
moon.position.y = moonCenter.y + moonDistance * cos(delta * pi / 2);
moon.zOrder = 0;
}
}
}
class Moon extends GameEntity with GestureArea, TapDetector {
SpriteComponent moonSprite;
int spinDirection = -1;
Moon(Vector2 position, int zOrder) {
moonSprite = SpriteComponent.fromSvgCache("moon.svg");
this.position = position;
this.zOrder = zOrder;
gestureAreaSize = moonSprite.frameSize;
}
@override
void update() {
moonSprite.render();
}
@override
void onTapDown(TapDownDetails details) {
// Toggle the spin direction.
spinDirection *= -1;
}
@override
void onTapUp(TapUpDetails details) {
}
@override
void onTapCancel() {
}
}