import 'package:flame/components.dart'; import 'package:flame/effects.dart'; import 'package:flame/events.dart'; import 'package:flutter/material.dart'; import 'board.dart'; class Tile extends SpriteComponent with TapCallbacks, DragCallbacks { int row; int col; int spriteIndex; final void Function(Tile) onTileTap; final void Function(Tile, Vector2) onSwipe; bool isSelected = false; bool isBomb = false; bool isMagicCube = false; Vector2? startDragPosition; Tile({ required Sprite sprite, required this.spriteIndex, required Vector2 size, required Vector2 position, required this.row, required this.col, required this.onTileTap, required this.onSwipe, }) : super(sprite: sprite, size: size, position: position); @override bool onTapDown(TapDownEvent event) { if (parent is Board && (parent as Board).animating) { return false; } onTileTap(this); return true; } @override bool onDragStart(DragStartEvent event) { super.onDragStart(event); if (parent is Board && (parent as Board).animating) { return false; } startDragPosition = event.localPosition; return true; } @override bool onDragEnd(DragEndEvent event) { super.onDragEnd(event); if (startDragPosition != null) { final delta = event.velocity; onSwipe(this, delta); } startDragPosition = null; return true; } void select() { isSelected = true; updateBorder(); } void deselect() { isSelected = false; updateBorder(); } void updateBorder() { if (isSelected) { add(RectangleComponent( size: size, paint: Paint() ..color = Colors.yellow ..style = PaintingStyle.stroke ..strokeWidth = 3, )); } else { children.removeWhere((child) => child is RectangleComponent); } } void animateMoveTo(Vector2 newPosition, VoidCallback onComplete) { add(MoveEffect.to( newPosition, EffectController(duration: 0.3), onComplete: onComplete, )); } void animateRemove(VoidCallback onComplete) { add(RemoveEffect( delay: 0.5, onComplete: onComplete, )); } }