Skip to content

Commit b29ed98

Browse files
committed
📝 Update Docs
1 parent 36bde0e commit b29ed98

File tree

1 file changed

+99
-10
lines changed

1 file changed

+99
-10
lines changed

lib/presentation/game/widgets/fruit_component.dart

Lines changed: 99 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -100,20 +100,49 @@ class FruitComponent extends SpriteComponent with HasGameReference<MainRouterGam
100100
return;
101101
}
102102

103+
// NOTE: Removed fruit splitting feature because of unfixed 'toImageSync' bug on Flutter SDK
104+
// check here: https://github.com/flutter/flutter/issues/144451
105+
//
103106
if (game.isDesktop) {
104-
// Calculate the angle between the touch point and the fruit’s center.
107+
// 1. Calculate the angle between the touch point and the fruit’s center.
105108
// This angle helps determine the slicing direction.
109+
// Formula: `atan2(dy, dx)` where `dy = touchPoint.y - center.y` and `dx = touchPoint.x - center.x`.
110+
// `atan2` returns the angle between the positive x-axis and the vector from (0, 0) to (dx, dy) in radians.
111+
// The resulting angle helps identify whether the touch is in a vertical or horizontal slicing direction.
106112
final a = AppUtils.getAngleOfTouchPont(center: position, initAngle: angle, touch: vector2);
107113

108114
try {
109-
// Check if the calculated angle falls along a vertical or horizontal slice.
115+
// 2. Check if the calculated angle falls along a vertical or horizontal slice.
116+
// This check uses angle ranges to decide the slicing direction:
117+
// Vertical slices: angle between 0-45°, 135-225°, 315-360°
118+
// Horizontal slices: angle between 45-135° and 225-315°.
110119
if (a < 45 || (a > 135 && a < 225) || a > 315) {
111-
// Create two halves of the fruit for a vertical slice.
120+
// Vertical slice: Create two halves of the fruit.
112121
final dividedImage1 = await createDividedImage(image, Rect.fromLTWH(0, 0, image.width.toDouble(), image.height / 2));
113122
final dividedImage2 = await createDividedImage(image, Rect.fromLTWH(0, image.height / 2, image.width.toDouble(), image.height / 2));
114123

115-
// Add both halves of the fruit to the game after slicing.
124+
// 3. Position adjustments using cosine and sine for direction:
125+
// The formula used to adjust the position of each half of the fruit is based on vector rotation.
126+
// For the upper half, the position is adjusted by subtracting a vector: `Vector2(size.x / 2 * cos(angle), size.x / 2 * sin(angle))`
127+
// For the lower half, the position is adjusted similarly with the opposite direction for the slice.
116128
parentComponent.addAll([
129+
// Adjust position for the upper half of the fruit after slicing
130+
// The formula used here is based on vector rotation:
131+
132+
// 3.1. `size.x / 2`: This is half the width of the fruit, determining the distance
133+
// from the center to the edge of the fruit for the upper half after slicing.
134+
135+
// 3.2. `cos(angle)`: This gives the horizontal (x-axis) component of the vector
136+
// that defines the direction of movement for the upper half, based on the angle of the slice.
137+
// It determines how far to move the upper half horizontally.
138+
139+
// 3.3. `sin(angle)`: This gives the vertical (y-axis) component of the vector
140+
// that defines the direction of movement for the upper half, based on the angle of the slice.
141+
// It determines how far to move the upper half vertically.
142+
143+
// 3.4. `center - Vector2(...)`: By subtracting the calculated vector from the center,
144+
// we adjust the position of the upper half of the fruit, moving it in the correct direction
145+
// after the slice, according to the calculated angle.
117146
FruitComponent(
118147
parentComponent,
119148
center - Vector2(size.x / 2 * cos(angle), size.x / 2 * sin(angle)), // Adjust position for upper half.
@@ -127,6 +156,22 @@ class FruitComponent extends SpriteComponent with HasGameReference<MainRouterGam
127156
angle: angle,
128157
anchor: Anchor.topLeft,
129158
),
159+
160+
// Adjust position for the lower half of the fruit after slicing
161+
// Similar to the upper half, but with adjustments for the lower side:
162+
163+
// 3.5. `size.x / 4`: This is a quarter of the width of the fruit, determining the distance
164+
// from the center to the edge of the fruit for the lower half after slicing.
165+
166+
// 3.6. `cos(angle + 3 * pi / 2)`: This gives the horizontal (x-axis) component for the lower half's direction.
167+
// By adding `3 * pi / 2` to the angle, we rotate the vector to move the lower half in the opposite direction.
168+
169+
// 3.7. `sin(angle + 3 * pi / 2)`: This gives the vertical (y-axis) component for the lower half's direction.
170+
// Adding `3 * pi / 2` rotates the vertical direction to move the lower half.
171+
172+
// 3.8. `center + Vector2(...)`: By adding the calculated vector to the center,
173+
// we adjust the position of the lower half of the fruit, moving it in the correct direction
174+
// after the slice, according to the modified angle.
130175
FruitComponent(
131176
parentComponent,
132177
center + Vector2(size.x / 4 * cos(angle + 3 * pi / 2), size.x / 4 * sin(angle + 3 * pi / 2)), // Adjust position for lower half.
@@ -142,12 +187,29 @@ class FruitComponent extends SpriteComponent with HasGameReference<MainRouterGam
142187
),
143188
]);
144189
} else {
145-
// Create two halves of the fruit for a horizontal slice.
190+
// Horizontal slice: Create two halves of the fruit.
146191
final dividedImage1 = await createDividedImage(image, Rect.fromLTWH(0, 0, image.width / 2, image.height.toDouble()));
147192
final dividedImage2 = await createDividedImage(image, Rect.fromLTWH(image.width / 2, 0, image.width / 2, image.height.toDouble()));
148193

149-
// Add both halves of the fruit to the game after slicing.
194+
// 4. Position adjustments for horizontal slice:
195+
// The formulas for adjusting the position are similar to the vertical slice, but with a different factor for horizontal separation.
150196
parentComponent.addAll([
197+
// Adjust position for the left half of the fruit after slicing
198+
// The formula used here is based on vector rotation:
199+
// 4.1. `size.x / 4`: This is a quarter of the width of the fruit, determining the distance
200+
// from the center to the edge of the fruit for the left half after slicing.
201+
202+
// 4.2. `cos(angle)`: This gives the horizontal (x-axis) component of the vector
203+
// that defines the direction of movement for the left half, based on the angle of the slice.
204+
// It determines how far to move the left half horizontally.
205+
206+
// 4.3. `sin(angle)`: This gives the vertical (y-axis) component of the vector
207+
// that defines the direction of movement for the left half, based on the angle of the slice.
208+
// It determines how far to move the left half vertically.
209+
210+
// 4.4. `center - Vector2(...)`: By subtracting the calculated vector from the center,
211+
// we adjust the position of the left half of the fruit, moving it in the correct direction
212+
// after the slice, according to the calculated angle.
151213
FruitComponent(
152214
parentComponent,
153215
center - Vector2(size.x / 4 * cos(angle), size.x / 4 * sin(angle)), // Adjust position for left half.
@@ -161,6 +223,22 @@ class FruitComponent extends SpriteComponent with HasGameReference<MainRouterGam
161223
pageSize: pageSize,
162224
divided: true, // Mark as divided.
163225
),
226+
227+
// Adjust position for the right half of the fruit after slicing
228+
// Similar to the left half, but with adjustments for the right side:
229+
230+
// 4.5. `size.x / 2`: This is half the width of the fruit, determining the distance
231+
// from the center to the edge of the fruit for the right half after slicing.
232+
233+
// 4.6. `cos(angle + 3 * pi / 2)`: This gives the horizontal (x-axis) component for the right half's direction.
234+
// By adding `3 * pi / 2` to the angle, we effectively rotate the movement in the opposite direction.
235+
236+
// 4.7. `sin(angle + 3 * pi / 2)`: This gives the vertical (y-axis) component for the right half's direction.
237+
// Similar to `cos`, adding `3 * pi / 2` rotates the vertical direction to move the right half.
238+
239+
// 4.8. `center + Vector2(...)`: By adding the calculated vector to the center,
240+
// we adjust the position of the right half of the fruit, moving it in the correct direction
241+
// after the slice, based on the modified angle.
164242
FruitComponent(
165243
parentComponent,
166244
center + Vector2(size.x / 2 * cos(angle + 3 * pi / 2), size.x / 2 * sin(angle + 3 * pi / 2)), // Adjust position for right half.
@@ -188,16 +266,27 @@ class FruitComponent extends SpriteComponent with HasGameReference<MainRouterGam
188266
}
189267

190268
Future<Image> createDividedImage(Image originalImage, Rect sourceRect) async {
269+
// Create a PictureRecorder to record drawing operations on the Canvas
191270
final recorder = PictureRecorder();
271+
272+
// Create a Canvas to draw the image, linked to the recorder
192273
final canvas = Canvas(recorder);
274+
275+
// Create a Paint (no special settings) to use for drawing
193276
final paint = Paint();
277+
278+
// Draw the specified portion of the original image (sourceRect) onto the Canvas
194279
canvas.drawImageRect(
195-
originalImage,
196-
sourceRect,
197-
Rect.fromLTWH(0, 0, sourceRect.width, sourceRect.height),
198-
paint,
280+
originalImage, // The original image
281+
sourceRect, // The region to cut from the original image
282+
Rect.fromLTWH(0, 0, sourceRect.width, sourceRect.height), // Destination rect on the Canvas
283+
paint, // Default paint
199284
);
285+
286+
// End recording and get the Picture object
200287
final picture = recorder.endRecording();
288+
289+
// Convert the Picture to an Image with the specified width and height
201290
return await picture.toImage(sourceRect.width.toInt(), sourceRect.height.toInt());
202291
}
203292
}

0 commit comments

Comments
 (0)