bubble.dart 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. import 'dart:math';
  2. import 'dart:async';
  3. import 'package:flame/components.dart';
  4. import 'package:flame/effects.dart';
  5. import 'package:flame/events.dart';
  6. import 'package:flame/collisions.dart';
  7. import 'package:flutter/material.dart';
  8. import '../../utils/colors.dart';
  9. class Bubble extends SpriteComponent with HasGameReference, TapCallbacks, CollisionCallbacks {
  10. static const double startSize = 40.0; // Small starting size for all bubbles
  11. static const double maxSize = 50.0; // Same final size for all bubbles
  12. static const double lifecycleDuration = 12.0; // Same lifecycle duration for all bubbles
  13. // Triple collision rule constants
  14. static const int maxCollisions = 3;
  15. static const double collisionGracePeriod = 2.0; // seconds
  16. late String bubbleImagePath;
  17. late Color bubbleColor;
  18. bool isPopping = false;
  19. bool poppedByUser = false;
  20. bool isAutoSpawned = true; // Whether bubble was auto-spawned or user-triggered
  21. Function(Bubble, bool)? onPop;
  22. // Collision tracking for triple collision rule
  23. int _collisionCount = 0;
  24. double _lastCollisionTime = 0.0;
  25. double _age = 0.0;
  26. double _lifecycleProgress = 0.0;
  27. late double _targetSize;
  28. // Physics properties for collision and movement
  29. Vector2 velocity = Vector2.zero();
  30. static const double maxSpeed = 50.0;
  31. static const double friction = 0.98;
  32. static const double collisionDamping = 0.7;
  33. double _floatTimer = 0.0;
  34. Bubble({
  35. required Vector2 position,
  36. this.onPop,
  37. this.isAutoSpawned = true,
  38. }) : super(
  39. size: Vector2.all(startSize),
  40. position: position,
  41. anchor: Anchor.center,
  42. ) {
  43. // All bubbles have the same target size regardless of type
  44. _targetSize = maxSize;
  45. }
  46. @override
  47. Future<void> onLoad() async {
  48. // Choose random bubble image and set corresponding color
  49. final bubbleData = _getRandomBubbleData();
  50. bubbleImagePath = bubbleData['path'];
  51. bubbleColor = bubbleData['color'];
  52. // Load the sprite
  53. sprite = await Sprite.load(bubbleImagePath);
  54. await super.onLoad();
  55. // Add circular collision hitbox - will be updated dynamically
  56. add(CircleHitbox(
  57. radius: startSize / 2, // Start with initial size
  58. anchor: Anchor.center,
  59. ));
  60. // Start with zero scale for smooth entrance animation
  61. scale = Vector2.zero();
  62. // Add smooth entrance animation
  63. _addEntranceAnimation();
  64. // Add varied, organic floating animations
  65. _addRandomFloatingAnimation();
  66. _addRandomRotationAnimation();
  67. // Initialize random velocity for natural movement
  68. final random = Random();
  69. velocity = Vector2(
  70. (random.nextDouble() - 0.5) * 20, // -10 to +10
  71. (random.nextDouble() - 0.5) * 20, // -10 to +10
  72. );
  73. }
  74. @override
  75. void update(double dt) {
  76. super.update(dt);
  77. if (isPopping) return;
  78. // Update age and lifecycle
  79. _age += dt;
  80. _lifecycleProgress = (_age / lifecycleDuration).clamp(0.0, 1.0);
  81. // Update scale based on lifecycle progress - grow from startSize to maxSize
  82. final currentScale = _lifecycleProgress * (_targetSize / startSize);
  83. scale = Vector2.all(1.0 + currentScale * 3.0); // Scale from 1.0 to 4.0 over lifetime
  84. // Update collision hitbox radius to match current visual size
  85. final currentRadius = (startSize / 2) * scale.x; // Use scale.x since it's uniform scaling
  86. final hitbox = children.whereType<CircleHitbox>().firstOrNull;
  87. if (hitbox != null) {
  88. hitbox.radius = currentRadius;
  89. }
  90. // Update opacity based on lifecycle (fade out towards the end)
  91. final opacityFactor = 1.0 - (_lifecycleProgress * 0.6); // Fade to 40% opacity
  92. opacity = opacityFactor.clamp(0.4, 1.0);
  93. // Physics-based movement
  94. _updatePhysics(dt);
  95. // Keep bubble within screen bounds
  96. _keepInBounds();
  97. // Update collision tracking
  98. _updateCollisionTracking(dt);
  99. // Auto-pop when lifecycle is complete
  100. if (_lifecycleProgress >= 1.0) {
  101. pop(userTriggered: false);
  102. }
  103. }
  104. void _updatePhysics(double dt) {
  105. // Apply velocity to position
  106. position.add(velocity * dt);
  107. // Apply friction
  108. velocity.scale(friction);
  109. // Add periodic floating effects
  110. _floatTimer += dt;
  111. if (_floatTimer > 2.0) {
  112. _floatTimer = 0.0;
  113. final random = Random();
  114. final floatForce = Vector2(
  115. (random.nextDouble() - 0.5) * 8, // -4 to +4 horizontal
  116. (random.nextDouble() - 0.5) * 8, // -4 to +4 vertical
  117. );
  118. velocity.add(floatForce);
  119. }
  120. // Clamp velocity to max speed
  121. if (velocity.length > maxSpeed) {
  122. velocity.normalize();
  123. velocity.scale(maxSpeed);
  124. }
  125. }
  126. void _keepInBounds() {
  127. const margin = 60.0;
  128. final gameSize = game.size;
  129. // Bounce off screen edges
  130. if (position.x < margin) {
  131. position.x = margin;
  132. velocity.x = velocity.x.abs(); // Bounce right
  133. } else if (position.x > gameSize.x - margin) {
  134. position.x = gameSize.x - margin;
  135. velocity.x = -velocity.x.abs(); // Bounce left
  136. }
  137. if (position.y < margin + 100) { // Account for UI at top
  138. position.y = margin + 100;
  139. velocity.y = velocity.y.abs(); // Bounce down
  140. } else if (position.y > gameSize.y - margin) {
  141. position.y = gameSize.y - margin;
  142. velocity.y = -velocity.y.abs(); // Bounce up
  143. }
  144. }
  145. Map<String, dynamic> _getRandomBubbleData() {
  146. final random = Random();
  147. final bubbleTypes = [
  148. {
  149. 'path': 'bubble_blue.png',
  150. 'color': ZenColors.defaultLink,
  151. },
  152. {
  153. 'path': 'bubble_cyan.png',
  154. 'color': ZenColors.buttonBackground,
  155. },
  156. {
  157. 'path': 'bubble_green.png',
  158. 'color': const Color(0xFF00FF80),
  159. },
  160. {
  161. 'path': 'bubble_purple.png',
  162. 'color': ZenColors.lightModeHover,
  163. },
  164. ];
  165. return bubbleTypes[random.nextInt(bubbleTypes.length)];
  166. }
  167. void _addRandomFloatingAnimation() {
  168. final random = Random();
  169. // Add a small upward bias to the initial velocity
  170. velocity.y += -5 - random.nextDouble() * 10; // -5 to -15 upward bias
  171. // The floating effect will now be handled by the physics system
  172. // and periodic velocity changes in the update method
  173. }
  174. void _addEntranceAnimation() {
  175. // Start at scale 1.0 (startSize) and grow gradually throughout lifecycle
  176. // The growth will be handled in the update method based on lifecycle progress
  177. // Simple fade-in animation only
  178. opacity = 0.0;
  179. add(
  180. OpacityEffect.to(
  181. 1.0,
  182. EffectController(
  183. duration: 0.4,
  184. curve: Curves.easeOut,
  185. ),
  186. ),
  187. );
  188. }
  189. void _addRandomRotationAnimation() {
  190. final random = Random();
  191. // 30% chance for rotation animation
  192. if (random.nextDouble() < 0.3) {
  193. // Small random rotation between -0.2 and 0.2 radians
  194. final rotationAngle = (random.nextDouble() - 0.5) * 0.4;
  195. // Random rotation duration between 3 and 8 seconds
  196. final rotationDuration = 3.0 + random.nextDouble() * 5.0;
  197. // Random delay
  198. final delay = random.nextDouble() * 3.0;
  199. Future.delayed(Duration(milliseconds: (delay * 1000).round()), () {
  200. if (isMounted) {
  201. add(
  202. RotateEffect.by(
  203. rotationAngle,
  204. EffectController(
  205. duration: rotationDuration,
  206. alternate: true,
  207. infinite: true,
  208. curve: Curves.easeInOut,
  209. ),
  210. ),
  211. );
  212. }
  213. });
  214. }
  215. }
  216. @override
  217. bool onTapDown(TapDownEvent event) {
  218. if (!isPopping) {
  219. pop(userTriggered: true);
  220. }
  221. return true;
  222. }
  223. void pop({bool userTriggered = false}) {
  224. if (isPopping) return;
  225. isPopping = true;
  226. poppedByUser = userTriggered;
  227. // Create exciting multi-stage pop animation
  228. // Stage 1: Quick expand (0.1s)
  229. final expandEffect = ScaleEffect.to(
  230. Vector2.all(1.8),
  231. EffectController(duration: 0.1, curve: Curves.easeOut),
  232. );
  233. // Stage 2: Slight compression (0.05s)
  234. final compressEffect = ScaleEffect.to(
  235. Vector2.all(0.8),
  236. EffectController(duration: 0.05, curve: Curves.easeIn),
  237. );
  238. // Stage 3: Final explosion (0.15s)
  239. final explodeEffect = ScaleEffect.to(
  240. Vector2.all(2.5),
  241. EffectController(duration: 0.15, curve: Curves.easeOut),
  242. );
  243. // Fade out effect
  244. final fadeEffect = OpacityEffect.to(
  245. 0.0,
  246. EffectController(duration: 0.3, curve: Curves.easeIn),
  247. );
  248. // Rotation effect for more dynamic feel
  249. final rotateEffect = RotateEffect.by(
  250. 0.5, // Half rotation
  251. EffectController(duration: 0.3),
  252. );
  253. // Chain the scale effects
  254. add(expandEffect);
  255. expandEffect.onComplete = () {
  256. if (isMounted) {
  257. add(compressEffect);
  258. compressEffect.onComplete = () {
  259. if (isMounted) {
  260. add(explodeEffect);
  261. }
  262. };
  263. }
  264. };
  265. add(fadeEffect);
  266. add(rotateEffect);
  267. // Create enhanced particle effects
  268. _createExcitingPopParticles();
  269. // Notify parent and remove bubble
  270. onPop?.call(this, userTriggered);
  271. Future.delayed(const Duration(milliseconds: 300), () {
  272. if (isMounted) {
  273. removeFromParent();
  274. }
  275. });
  276. }
  277. void _createExcitingPopParticles() {
  278. final random = Random();
  279. const particleCount = 12; // Further reduced for better performance
  280. // Use object pooling approach - create fewer, simpler particles
  281. for (int i = 0; i < particleCount; i++) {
  282. final angle = (i / particleCount) * 2 * pi;
  283. // Simplified particle creation
  284. final baseSpeed = 50 + random.nextDouble() * 30; // 50-80 speed
  285. final particleVelocity = Vector2(
  286. cos(angle) * baseSpeed,
  287. sin(angle) * baseSpeed,
  288. );
  289. // Fixed particle size for performance
  290. const particleSize = 3.0;
  291. // Simplified particle color
  292. final particleColor = bubbleColor.withValues(alpha: 0.8);
  293. final particle = CircleComponent(
  294. radius: particleSize,
  295. position: position.clone(),
  296. paint: Paint()
  297. ..color = particleColor
  298. ..style = PaintingStyle.fill,
  299. );
  300. parent?.add(particle);
  301. // Combine effects for better performance
  302. final combinedEffect = MoveEffect.by(
  303. particleVelocity + Vector2(0, 40), // Combined movement + gravity
  304. EffectController(duration: 0.3, curve: Curves.easeOut), // Faster animation
  305. );
  306. particle.add(combinedEffect);
  307. // Single fade effect instead of multiple effects
  308. particle.add(
  309. OpacityEffect.to(
  310. 0.0,
  311. EffectController(duration: 0.3, curve: Curves.easeIn),
  312. ),
  313. );
  314. // Faster cleanup
  315. Future.delayed(const Duration(milliseconds: 300), () {
  316. if (particle.isMounted) {
  317. particle.removeFromParent();
  318. }
  319. });
  320. }
  321. }
  322. // Track if shake effect is currently active to prevent overlapping effects
  323. bool _isShakeEffectActive = false;
  324. /// Add shake effect to this bubble (called when phone is shaken)
  325. /// Fixed version that only adds visual effects without movement or size flickering
  326. void addShakeEffect() {
  327. if (isPopping || _isShakeEffectActive) return;
  328. _isShakeEffectActive = true;
  329. final random = Random();
  330. // Store current scale to return to it after effect
  331. final currentScale = scale.clone();
  332. final targetScale = currentScale * 1.1; // Only 10% bigger than current size
  333. // Use absolute scaling to prevent flickering with lifecycle scaling
  334. final scaleUpEffect = ScaleEffect.to(
  335. targetScale,
  336. EffectController(
  337. duration: 0.1, // Very short duration
  338. curve: Curves.easeOut,
  339. ),
  340. );
  341. final scaleDownEffect = ScaleEffect.to(
  342. currentScale,
  343. EffectController(
  344. duration: 0.1, // Very short duration
  345. curve: Curves.easeIn,
  346. ),
  347. );
  348. // Add slight rotation effect - reduced intensity
  349. final rotateEffect = RotateEffect.by(
  350. (random.nextDouble() - 0.5) * 0.15, // Further reduced rotation
  351. EffectController(
  352. duration: 0.15,
  353. alternate: true,
  354. curve: Curves.easeInOut,
  355. ),
  356. );
  357. // Chain the scale effects
  358. add(scaleUpEffect);
  359. scaleUpEffect.onComplete = () {
  360. if (isMounted && !isPopping) {
  361. add(scaleDownEffect);
  362. }
  363. };
  364. add(rotateEffect);
  365. // Set completion callback to reset shake effect flag
  366. Future.delayed(const Duration(milliseconds: 200), () {
  367. _isShakeEffectActive = false;
  368. });
  369. }
  370. // Collision detection methods
  371. @override
  372. bool onCollision(Set<Vector2> intersectionPoints, PositionComponent other) {
  373. super.onCollision(intersectionPoints, other);
  374. if (other is Bubble && !isPopping && !other.isPopping) {
  375. _handleBubbleCollision(other, intersectionPoints);
  376. }
  377. return true;
  378. }
  379. void _handleBubbleCollision(Bubble other, Set<Vector2> intersectionPoints) {
  380. if (intersectionPoints.isEmpty) return;
  381. // Track collision for triple collision rule
  382. _incrementCollisionCount();
  383. other._incrementCollisionCount();
  384. // Calculate collision direction
  385. final direction = (position - other.position);
  386. final distance = direction.length;
  387. // Prevent division by zero
  388. if (distance < 0.1) {
  389. // If bubbles are too close, separate them with a random direction
  390. final random = Random();
  391. direction.setFrom(Vector2(
  392. (random.nextDouble() - 0.5) * 2,
  393. (random.nextDouble() - 0.5) * 2,
  394. ));
  395. }
  396. direction.normalize();
  397. // Calculate required separation based on current bubble sizes
  398. final thisRadius = (startSize / 2) * scale.x;
  399. final otherRadius = (startSize / 2) * other.scale.x;
  400. final requiredDistance = thisRadius + otherRadius + 5.0; // 5px padding
  401. // If bubbles are overlapping, separate them immediately
  402. if (distance < requiredDistance) {
  403. final separationNeeded = requiredDistance - distance;
  404. final separationVector = direction * (separationNeeded / 2);
  405. // Move both bubbles away from each other
  406. position.add(separationVector);
  407. other.position.sub(separationVector);
  408. // Ensure bubbles stay within bounds after separation
  409. _keepInBounds();
  410. other._keepInBounds();
  411. }
  412. // Apply collision response - bubbles bounce off each other
  413. final collisionForce = 25.0; // Reduced from 30.0 for gentler bouncing
  414. velocity.add(direction * collisionForce);
  415. other.velocity.add(direction * -collisionForce);
  416. // Apply stronger damping to prevent infinite acceleration
  417. velocity.scale(0.6); // Increased damping from 0.7 to 0.6
  418. other.velocity.scale(0.6);
  419. }
  420. /// Update collision tracking and handle grace period reset
  421. void _updateCollisionTracking(double dt) {
  422. final currentTime = DateTime.now().millisecondsSinceEpoch / 1000.0;
  423. // Reset collision count if grace period has passed
  424. if (currentTime - _lastCollisionTime > collisionGracePeriod) {
  425. _collisionCount = 0;
  426. }
  427. }
  428. /// Increment collision count and check for auto-pop
  429. void _incrementCollisionCount() {
  430. if (isPopping) return;
  431. _collisionCount++;
  432. _lastCollisionTime = DateTime.now().millisecondsSinceEpoch / 1000.0;
  433. // Auto-pop after max collisions
  434. if (_collisionCount >= maxCollisions) {
  435. pop(userTriggered: false); // Auto-pop due to collision rule
  436. }
  437. }
  438. /// Get current collision count (for debugging/testing)
  439. int get collisionCount => _collisionCount;
  440. /// Apply tilt force to bubble based on device orientation
  441. void applyTiltForce(double tiltStrength) {
  442. if (isPopping) return;
  443. // Reduced tilt force to prevent excessive movement during shaking
  444. // Apply force opposite to tilt direction
  445. // If device tilts right, bubbles move left and vice versa
  446. final tiltForce = -tiltStrength * 8.0; // Reduced from 15.0 to 8.0
  447. velocity.x += tiltForce;
  448. // Add slight vertical component for more natural movement
  449. velocity.y += tiltForce * 0.2; // Reduced from 0.3 to 0.2
  450. }
  451. }