87 lines
1.9 KiB
Dart
87 lines
1.9 KiB
Dart
import 'package:flame/components.dart';
|
|
import 'package:flame/effects.dart';
|
|
import 'package:flame/events.dart';
|
|
import 'package:flutter/material.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;
|
|
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) {
|
|
onTileTap(this);
|
|
return true;
|
|
}
|
|
|
|
@override
|
|
bool onDragStart(DragStartEvent event) {
|
|
startDragPosition = event.localPosition;
|
|
return true;
|
|
}
|
|
|
|
@override
|
|
bool onDragEnd(DragEndEvent 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,
|
|
));
|
|
}
|
|
}
|