match_magic/lib/game/tile.dart

126 lines
2.7 KiB
Dart
Raw Normal View History

2024-08-05 11:04:54 +03:00
import 'package:flame/components.dart';
import 'package:flame/effects.dart';
import 'package:flame/events.dart';
import 'package:flutter/material.dart';
2024-08-16 19:09:24 +03:00
import 'board.dart';
2024-08-05 11:04:54 +03:00
class Tile extends SpriteComponent with TapCallbacks, DragCallbacks {
2024-08-05 11:04:54 +03:00
int row;
int col;
int spriteIndex;
2024-08-29 16:44:44 +03:00
// final void Function(Tile) onTileTap;
final void Function(Tile, Vector2) onSwipe;
bool isSelected = false;
2024-08-16 19:09:24 +03:00
bool isBomb = false;
bool isMagicCube = false;
Vector2? startDragPosition;
2024-08-05 11:04:54 +03:00
2024-08-29 16:44:44 +03:00
bool isAnimating = false;
2024-08-05 11:04:54 +03:00
Tile({
required Sprite sprite,
required this.spriteIndex,
required Vector2 size,
required Vector2 position,
required this.row,
required this.col,
2024-08-29 16:44:44 +03:00
// required this.onTileTap,
required this.onSwipe,
2024-08-05 11:04:54 +03:00
}) : super(sprite: sprite, size: size, position: position);
2024-08-29 16:44:44 +03:00
// @override
// bool onTapDown(TapDownEvent event) {
// if (isAnimating || (parent is Board && (parent as Board).animating)) {
// return false;
// }
// onTileTap(this);
// return true;
// }
2024-08-05 11:04:54 +03:00
@override
bool onDragStart(DragStartEvent event) {
2024-08-29 16:44:44 +03:00
if (isAnimating || (parent as Board).animating) {
2024-08-16 19:09:24 +03:00
return false;
}
2024-08-29 16:44:44 +03:00
super.onDragStart(event);
startDragPosition = event.localPosition;
return true;
}
@override
bool onDragEnd(DragEndEvent event) {
2024-08-29 16:44:44 +03:00
if (isAnimating || (parent as Board).animating) {
return false;
}
2024-08-16 19:09:24 +03:00
super.onDragEnd(event);
if (startDragPosition != null) {
final delta = event.velocity;
onSwipe(this, delta);
}
startDragPosition = null;
return true;
}
// void select() {
// isSelected = true;
// updateBorder();
// }
void select() {
final effectController = EffectController(
duration: 0.3,
repeatCount: 2,
reverseDuration: 0.3,
);
final blinkEffect = OpacityEffect.to(
0.0,
effectController,
);
add(blinkEffect);
}
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 11:04:54 +03:00
void animateMoveTo(Vector2 newPosition, VoidCallback onComplete) {
2024-08-29 16:44:44 +03:00
isAnimating = true;
2024-08-05 11:04:54 +03:00
add(MoveEffect.to(
newPosition,
EffectController(duration: 0.3),
2024-08-29 16:44:44 +03:00
onComplete: () {
isAnimating = false;
onComplete();
},
));
}
2024-08-29 16:44:44 +03:00
// void animateRemove(VoidCallback onComplete) {
// isAnimating = true;
// add(RemoveEffect(
// delay: 0.5,
// onComplete: () {
// isAnimating = false;
// onComplete();
// },
// ));
// }
2024-08-05 11:04:54 +03:00
}