@@ -8,12 +8,13 @@ import 'package:lightrunners/game/lightrunners_game.dart';
8
8
import 'package:lightrunners/ui/palette.dart' ;
9
9
import 'package:lightrunners/utils/delaunay.dart' ;
10
10
import 'package:lightrunners/utils/flame_utils.dart' ;
11
+ import 'package:lightrunners/utils/mutable_color.dart' ;
11
12
12
13
const _margin = 200.0 ;
13
14
14
15
const _numberShades = 5 ;
15
16
const _shadeStep = 0.1 ;
16
- const _colorMoveSpeed = 30 ;
17
+ const _colorMoveSpeed = 0.8 ;
17
18
final _emptyColor = GamePalette .black.brighten (0.1 );
18
19
final _borderPaint = Paint ()
19
20
..color = GamePalette .black
@@ -33,7 +34,7 @@ class Background extends PositionComponent
33
34
late Rect clipArea;
34
35
late List <ShadedTriangle > mesh;
35
36
36
- Color currentColor = _emptyColor;
37
+ MutableColor currentColor = MutableColor . fromColor ( _emptyColor) ;
37
38
38
39
@override
39
40
void onGameResize (Vector2 gameSize) {
@@ -72,8 +73,7 @@ class Background extends PositionComponent
72
73
super .update (dt);
73
74
74
75
final targetColor = _computeTargetColor ();
75
- currentColor =
76
- _moveTowards (currentColor, targetColor, _colorMoveSpeed * dt);
76
+ currentColor.moveTowards (targetColor, _colorMoveSpeed * dt);
77
77
}
78
78
79
79
@override
@@ -82,7 +82,8 @@ class Background extends PositionComponent
82
82
canvas.clipRect (clipArea);
83
83
84
84
for (final t in mesh) {
85
- final shadedColor = currentColor.brighten (t.shadeLevel * _shadeStep);
85
+ final shadedColor =
86
+ currentColor.toColor ().brighten (t.shadeLevel * _shadeStep);
86
87
canvas.drawPath (t.path, paint..color = shadedColor);
87
88
}
88
89
@@ -91,60 +92,19 @@ class Background extends PositionComponent
91
92
}
92
93
}
93
94
94
- ( int , int , int ) _computeTargetColor () {
95
+ MutableColor _computeTargetColor () {
95
96
final sortedShips = game.ships.values.sortedBy <num >((ship) => - ship.score);
96
97
final maxScore = sortedShips.first.score;
97
98
if (maxScore == 0 ) {
98
- return _fromColor (_emptyColor);
99
+ return MutableColor . fromColor (_emptyColor);
99
100
}
100
101
final colors = sortedShips
101
102
.takeWhile ((ship) => ship.score == maxScore)
102
103
.map ((ship) => ship.paint.color.darken (0.75 ))
103
104
.toList ();
104
- return colors.map (_fromColor).reduce ((value, element) => value + element) /
105
+ return colors
106
+ .map (MutableColor .fromColor)
107
+ .reduce ((value, element) => value + element) /
105
108
colors.length.toDouble ();
106
109
}
107
-
108
- Color _moveTowards (Color currentColor, (int , int , int ) target, double ds) {
109
- final color = _fromColor (currentColor);
110
- if (color == target) {
111
- return currentColor;
112
- }
113
- return color.moveTowards (target, ds).toColor ();
114
- }
115
110
}
116
-
117
- extension on (int , int , int ) {
118
- Color toColor () => Color .fromARGB (255 , $1, $2, $3);
119
-
120
- (int , int , int ) operator + ((int , int , int ) other) =>
121
- (this .$1 + other.$1, this .$2 + other.$2, this .$3 + other.$3);
122
-
123
- (int , int , int ) operator - ((int , int , int ) other) => this + (- other);
124
-
125
- (int , int , int ) operator / (double other) =>
126
- _fromDoubles (this .$1 / other, this .$2 / other, this .$3 / other);
127
-
128
- (int , int , int ) operator * (double other) =>
129
- _fromDoubles (this .$1 * other, this .$2 * other, this .$3 * other);
130
-
131
- (int , int , int ) operator - () => (- this .$1, - this .$2, - this .$3);
132
-
133
- double get length => sqrt ($1 * $1 + $2 * $2 + $3 * $3);
134
-
135
- (int , int , int ) normalized () => this / length;
136
-
137
- (int , int , int ) moveTowards ((int , int , int ) target, double ds) {
138
- final diff = target - this ;
139
- if (diff.length < ds) {
140
- return target;
141
- } else {
142
- return this + diff.normalized () * ds;
143
- }
144
- }
145
- }
146
-
147
- (int , int , int ) _fromDoubles (double r, double g, double b) =>
148
- (r.round (), g.round (), b.round ());
149
-
150
- (int , int , int ) _fromColor (Color color) => (color.red, color.green, color.blue);
0 commit comments