113 lines
2.5 KiB
Dart
113 lines
2.5 KiB
Dart
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;
|
|
|
|
bool isAnimating = false;
|
|
|
|
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 (isAnimating || (parent is Board && (parent as Board).animating)) {
|
|
// return false;
|
|
// }
|
|
// onTileTap(this);
|
|
// return true;
|
|
// }
|
|
|
|
@override
|
|
bool onDragStart(DragStartEvent event) {
|
|
if (isAnimating || (parent as Board).animating) {
|
|
return false;
|
|
}
|
|
super.onDragStart(event);
|
|
startDragPosition = event.localPosition;
|
|
return true;
|
|
}
|
|
|
|
@override
|
|
bool onDragEnd(DragEndEvent event) {
|
|
if (isAnimating || (parent as Board).animating) {
|
|
return false;
|
|
}
|
|
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) {
|
|
isAnimating = true;
|
|
print('animation start');
|
|
add(MoveEffect.to(
|
|
newPosition,
|
|
EffectController(duration: 0.3),
|
|
onComplete: () {
|
|
print('animation complete');
|
|
isAnimating = false;
|
|
onComplete();
|
|
},
|
|
));
|
|
}
|
|
|
|
// void animateRemove(VoidCallback onComplete) {
|
|
// isAnimating = true;
|
|
// add(RemoveEffect(
|
|
// delay: 0.5,
|
|
// onComplete: () {
|
|
// isAnimating = false;
|
|
// onComplete();
|
|
// },
|
|
// ));
|
|
// }
|
|
}
|