Skip to content

Commit b549c9c

Browse files
committed
fix assets caching
1 parent 3b45726 commit b549c9c

File tree

11 files changed

+143
-89
lines changed

11 files changed

+143
-89
lines changed

lib/game/constants.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const double kStartPlatformHeight = 400;
1414
const double kMeanPlatformInterval = 370;
1515
const double kPlatformIntervalVariation = 100;
1616
const double kPlatformMinWidth = 60;
17-
const double kPlatformWidthVariation = 160;
17+
const double kPlatformWidthVariation = 100;
1818
const double kPlatformHeight = 10;
1919
const double kPlatformPreloadArea = 1600;
2020

lib/game/crystal_ball.dart

Lines changed: 66 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
import 'dart:async';
22
import 'dart:math';
3+
import 'dart:typed_data';
34
import 'dart:ui';
45

56
import 'package:crystal_ball/game/game.dart';
7+
import 'package:crystal_ball/gen/assets.gen.dart';
68
import 'package:flame/components.dart';
9+
import 'package:flame/flame.dart';
710
import 'package:flame/game.dart';
811
import 'package:flame/input.dart';
912
import 'package:flame_bloc/flame_bloc.dart';
1013
import 'package:flutter/painting.dart';
14+
import 'package:flutter/widgets.dart' show FlutterError, FlutterErrorDetails;
1115

1216
class CrystalBallGame extends FlameGame<CrystalWorld>
1317
with HasKeyboardHandlerComponents, HasCollisionDetection {
1418
CrystalBallGame({
1519
required this.textStyle,
1620
required this.random,
1721
required this.gameCubit,
18-
required this.platformsShader,
19-
required this.theBallShader,
20-
required this.groundShader,
22+
required this.assetsCache,
2123
required this.pixelRatio,
2224
}) : super(
2325
world: CrystalWorld(
@@ -40,7 +42,6 @@ class CrystalBallGame extends FlameGame<CrystalWorld>
4042
add(cameraWithCameras);
4143
add(camerasWorld);
4244

43-
4445
world.addAll([
4546
directionalController = KeyboardHandlerSync(),
4647
]);
@@ -51,11 +52,9 @@ class CrystalBallGame extends FlameGame<CrystalWorld>
5152
final GameCubit gameCubit;
5253
final double pixelRatio;
5354

54-
late final KeyboardHandlerSync directionalController;
55+
final AssetsCache assetsCache;
5556

56-
final FragmentShader platformsShader;
57-
final FragmentShader theBallShader;
58-
final FragmentShader groundShader;
57+
late final KeyboardHandlerSync directionalController;
5958

6059
FutureOr<void> addCamera(CameraComponent component) {
6160
return add(component..follow(world.cameraTarget));
@@ -69,12 +68,11 @@ class CrystalBallGame extends FlameGame<CrystalWorld>
6968
world: camera.world,
7069
);
7170

72-
7371
late final camerasWorld = World();
7472

7573
late final cameraWithCameras = SamplerCamera(
7674
samplerOwner: GroundSamplerOwner(
77-
groundShader,
75+
assetsCache.groundShader,
7876
world,
7977
),
8078
world: camerasWorld,
@@ -83,12 +81,11 @@ class CrystalBallGame extends FlameGame<CrystalWorld>
8381
theBallGlowCamera..follow(world.cameraTarget),
8482
],
8583
pixelRatio: pixelRatio,
86-
8784
);
8885

8986
late final platformGlowCamera = SamplerCamera.withFixedResolution(
9087
samplerOwner: PlatformsSamplerOwner(
91-
platformsShader,
88+
assetsCache.platformsShader,
9289
world,
9390
),
9491
world: camera.world,
@@ -99,7 +96,7 @@ class CrystalBallGame extends FlameGame<CrystalWorld>
9996

10097
late final theBallGlowCamera = SamplerCamera.withFixedResolution(
10198
samplerOwner: TheBallSamplerOwner(
102-
theBallShader,
99+
assetsCache.theBallShader,
103100
world,
104101
),
105102
world: camera.world,
@@ -116,3 +113,59 @@ class CrystalBallGame extends FlameGame<CrystalWorld>
116113
@override
117114
Future<void> onLoad() async {}
118115
}
116+
117+
class AssetsCache {
118+
AssetsCache({
119+
required this.concreteImage,
120+
required this.platformsShader,
121+
required this.theBallShader,
122+
required this.groundShader,
123+
}) : super();
124+
125+
static Future<AssetsCache> loadAll() async {
126+
final [concrete] = await Future.wait([
127+
_loadImage(Assets.images.concrete.keyName),
128+
]);
129+
130+
final [
131+
platformsShader,
132+
theBallShader,
133+
groundShader,
134+
] = await Future.wait([
135+
_loadShader('shaders/platforms.glsl'),
136+
_loadShader('shaders/the_ball.glsl'),
137+
_loadShader('shaders/ground.glsl'),
138+
]);
139+
140+
return AssetsCache(
141+
concreteImage: concrete,
142+
platformsShader: platformsShader,
143+
theBallShader: theBallShader,
144+
groundShader: groundShader,
145+
);
146+
}
147+
148+
static Future<Image> _loadImage(String name) async {
149+
final data = await Flame.bundle.load(name);
150+
final bytes = Uint8List.view(data.buffer);
151+
return decodeImageFromList(bytes);
152+
}
153+
154+
static Future<FragmentShader> _loadShader(String name) async {
155+
try {
156+
final program = await FragmentProgram.fromAsset(name);
157+
return program.fragmentShader();
158+
} catch (error, stackTrace) {
159+
FlutterError.reportError(
160+
FlutterErrorDetails(exception: error, stack: stackTrace),
161+
);
162+
rethrow;
163+
}
164+
}
165+
166+
final Image concreteImage;
167+
168+
final FragmentShader platformsShader;
169+
final FragmentShader theBallShader;
170+
final FragmentShader groundShader;
171+
}

lib/game/entities/ground.dart

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ class Ground extends Component {
1414
rectangle = children.first as Rectangle;
1515
}
1616

17-
1817
late final Rectangle rectangle;
1918

2019
@override
@@ -64,8 +63,5 @@ class Rectangle extends PositionComponent
6463

6564
final double ogY;
6665

67-
6866
double get top => absolutePositionOfAnchor(Anchor.topCenter).y;
69-
70-
7167
}

lib/game/entities/platform.dart

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ class Platform extends PositionComponent with HasGameRef<CrystalBallGame> {
9090
final PlatformColor color;
9191
double _distanceToBall = 0;
9292

93-
9493
double? initialGlowGama;
9594
double glowGama = 0;
9695

@@ -110,7 +109,11 @@ class Platform extends PositionComponent with HasGameRef<CrystalBallGame> {
110109
void onMount() {
111110
super.onMount();
112111
scheduleMicrotask(() {
113-
_glowTo(to: initialGlowGama?? _getGlowGama(), duration: 0.3, curve: Curves.ease);
112+
_glowTo(
113+
to: initialGlowGama ?? _getGlowGama(),
114+
duration: 0.3,
115+
curve: Curves.ease,
116+
);
114117
});
115118
}
116119

@@ -143,7 +146,6 @@ class Platform extends PositionComponent with HasGameRef<CrystalBallGame> {
143146

144147
if (!game.gameCubit.isPlaying) return;
145148

146-
147149
if (y > game.world.reaper.y) {
148150
removeFromParent();
149151
}

lib/game/entities/the_ball.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class TheBall extends PositionComponent
5454
switch (state) {
5555
case GameState.initial:
5656
position = Vector2.zero();
57-
_glowTo(to: 0.1, duration: 0.1);
57+
_glowTo(to: 0.1);
5858
case GameState.starting:
5959
position = Vector2.zero();
6060
_glowTo(to: 0.6, duration: kOpeningDuration);

lib/game/samplers/ground_sampler.dart

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import 'dart:ui';
22

33
import 'package:crystal_ball/game/game.dart';
4-
import 'package:flame/extensions.dart';
54
import 'package:flutter_shaders/flutter_shaders.dart';
65

76
class GroundSamplerOwner extends SamplerOwner {
@@ -28,16 +27,13 @@ class GroundSamplerOwner extends SamplerOwner {
2827

2928
final uvGround = (groundpos - originY) / (kCameraSize.asVector2.y);
3029

31-
final origin = cameraComponent!.visibleWorldRect.topLeft.toVector2();
32-
33-
final boundaryL = world.cameraTarget.x - kCameraSize.width / 2 + 100;
34-
final boundaryR = world.cameraTarget.x + kCameraSize.width / 2 - 100;
35-
36-
final uvBoundaryL = (boundaryL - origin.x) /
37-
(cameraComponent!.visibleWorldRect.size.toVector2().x);
38-
final uvBoundaryR = (boundaryR - origin.x) /
39-
(cameraComponent!.visibleWorldRect.size.toVector2().x);
40-
30+
// final boundaryL = world.cameraTarget.x - kCameraSize.width / 2 + 100;
31+
// final boundaryR = world.cameraTarget.x + kCameraSize.width / 2 - 100;
32+
//
33+
// final uvBoundaryL = (boundaryL - origin.x) /
34+
// (cameraComponent!.visibleWorldRect.size.toVector2().x);
35+
// final uvBoundaryR = (boundaryR - origin.x) /
36+
// (cameraComponent!.visibleWorldRect.size.toVector2().x);
4137

4238
// print(cameraComponent!.visibleWorldRect.size);
4339

@@ -46,9 +42,9 @@ class GroundSamplerOwner extends SamplerOwner {
4642
value
4743
..setSize(size)
4844
..setFloat(uvGround)
49-
..setFloat(uvBoundaryL.clamp(0, 1))
50-
..setFloat(uvBoundaryR.clamp(0, 1))..setFloat(time)
51-
;
45+
..setFloat(0)
46+
..setFloat(1)
47+
..setFloat(time);
5248
})
5349
..setImageSampler(0, images[0]);
5450

@@ -64,8 +60,4 @@ class GroundSamplerOwner extends SamplerOwner {
6460
}
6561
}
6662

67-
extension on UniformsSetter {
68-
void setVector64(Vector vector) {
69-
setFloats(vector.storage);
70-
}
71-
}
63+
extension on UniformsSetter {}

lib/game/samplers/platforms_sampler.dart

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,12 @@ class PlatformsSamplerOwner extends SamplerOwner {
3434

3535
canvas
3636
..save()
37-
..drawRect(Offset.zero & size, Paint()..shader = shader)
37+
..drawRect(
38+
Offset.zero & size,
39+
Paint()
40+
..shader = shader
41+
..blendMode = BlendMode.multiply,
42+
)
3843
..restore();
3944
}
4045
}

0 commit comments

Comments
 (0)