-
Notifications
You must be signed in to change notification settings - Fork 9
/
helpers.dart
100 lines (83 loc) · 2.78 KB
/
helpers.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
import 'dart:async';
import 'dart:ui' as ui;
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_3d_raycast_engine/configurations.dart';
import 'package:flutter_3d_raycast_engine/map_information.dart';
import 'package:shared_preferences/shared_preferences.dart';
Color getColorBasedOnDepth({
required Color color,
required double depth,
required double maxDepth,
double adjustment = 0.1,
}) {
final ratio = (1 - depth / (maxDepth + epsilon)) + adjustment;
var red = (color.red * ratio).toInt();
var green = (color.green * ratio).toInt();
var blue = (color.blue * ratio).toInt();
red = red.clamp(0, 255);
green = green.clamp(0, 255);
blue = blue.clamp(0, 255);
return Color.fromARGB(255, red, green, blue);
}
Future<ui.Image> loadImageFromAsset(String assetName) async {
if (kIsWeb) {
WidgetsFlutterBinding.ensureInitialized();
final image = AssetImage(assetName);
final key = await image.obtainKey(ImageConfiguration.empty);
final completer = Completer<ui.Image>();
image
.loadBuffer(
key,
// ignore: deprecated_member_use
PaintingBinding.instance.instantiateImageCodecFromBuffer,
)
.addListener(
ImageStreamListener(
(image, synchronousCall) => completer.complete(image.image),
),
);
return completer.future;
}
final buffer = await ui.ImmutableBuffer.fromAsset(assetName);
final codec = await ui.instantiateImageCodecFromBuffer(buffer);
final frame = await codec.getNextFrame();
return frame.image;
}
List<MapInformation> generateMap() => List.generate(
mapSize * mapSize,
(index) => index % mapSize == 0 ||
index % mapSize == mapSize - 1 ||
index < mapSize ||
index >= mapSize * (mapSize - 1)
? MapInformation(materialIndex: materials.last.index)
: MapInformation(),
);
Future<void> saveMap(List<MapInformation> map) async {
await (await SharedPreferences.getInstance()).setStringList(
'map_material',
map.map((e) => e.materialIndex.toString()).toList(),
);
await (await SharedPreferences.getInstance()).setStringList(
'map_sprite',
map.map((e) => e.spriteIndex.toString()).toList(),
);
}
Future<List<MapInformation>> loadMap() async {
final map =
(await SharedPreferences.getInstance()).getStringList('map_material');
final sprite =
(await SharedPreferences.getInstance()).getStringList('map_sprite');
final emptyMap = generateMap();
if (map == null || sprite == null) {
return emptyMap;
} else {
return List.generate(
emptyMap.length,
(index) => MapInformation(
materialIndex: int.parse(map[index]),
spriteIndex: int.parse(sprite[index]),
),
);
}
}