match_magic/lib/game/tile.dart

87 lines
1.9 KiB
Dart
Raw Normal View History

2024-08-05 08:04:54 +00:00
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 {
2024-08-05 08:04:54 +00:00
int row;
int col;
int spriteIndex;
final void Function(Tile) onTileTap;
final void Function(Tile, Vector2) onSwipe;
bool isSelected = false;
Vector2? startDragPosition;
2024-08-05 08:04:54 +00:00
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,
2024-08-05 08:04:54 +00:00
}) : 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);
}
}
2024-08-05 08:04:54 +00:00
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,
2024-08-05 08:04:54 +00:00
onComplete: onComplete,
));
}
}