182 lines
3.9 KiB
Dart
182 lines
3.9 KiB
Dart
import 'dart:math';
|
|
|
|
import 'package:flame/components.dart';
|
|
import 'package:flame/effects.dart';
|
|
import 'package:flame/events.dart';
|
|
import 'package:flame/flame.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,
|
|
) {
|
|
// final randomSpriteIndex = _random.nextInt(crystals.length);
|
|
// tile = isMagicCube ? crystals[randomSpriteIndex] : magicCube();
|
|
}
|
|
|
|
// static final Random _random = Random();
|
|
late Sprite tile;
|
|
// @override
|
|
// bool onTapDown(TapDownEvent event) {
|
|
// if (isAnimating || (parent is Board && (parent as Board).animating)) {
|
|
// return false;
|
|
// }
|
|
// onTileTap(this);
|
|
// return true;
|
|
// }
|
|
|
|
static List<Sprite> crystals = [
|
|
crystal1(),
|
|
crystal2(),
|
|
crystal3(),
|
|
crystal4(),
|
|
crystal5(),
|
|
crystal6(),
|
|
crystal7(),
|
|
];
|
|
|
|
static Sprite magicCubeSprite = magicCube();
|
|
|
|
@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 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);
|
|
}
|
|
}
|
|
|
|
void animateMoveTo(Vector2 newPosition, VoidCallback onComplete) {
|
|
isAnimating = true;
|
|
add(MoveEffect.to(
|
|
newPosition,
|
|
EffectController(duration: 0.3),
|
|
onComplete: () {
|
|
isAnimating = false;
|
|
onComplete();
|
|
},
|
|
));
|
|
}
|
|
|
|
// void animateRemove(VoidCallback onComplete) {
|
|
// isAnimating = true;
|
|
// add(RemoveEffect(
|
|
// delay: 0.5,
|
|
// onComplete: () {
|
|
// isAnimating = false;
|
|
// onComplete();
|
|
// },
|
|
// ));
|
|
// }
|
|
}
|
|
|
|
Sprite crystal1() {
|
|
return Sprite(Flame.images.fromCache('crystal1.png'));
|
|
}
|
|
|
|
Sprite crystal2() {
|
|
return Sprite(Flame.images.fromCache('crystal2.png'));
|
|
}
|
|
|
|
Sprite crystal3() {
|
|
return Sprite(Flame.images.fromCache('crystal3.png'));
|
|
}
|
|
|
|
Sprite crystal4() {
|
|
return Sprite(Flame.images.fromCache('crystal4.png'));
|
|
}
|
|
|
|
Sprite crystal5() {
|
|
return Sprite(Flame.images.fromCache('crystal5.png'));
|
|
}
|
|
|
|
Sprite crystal6() {
|
|
return Sprite(Flame.images.fromCache('crystal6.png'));
|
|
}
|
|
|
|
Sprite crystal7() {
|
|
return Sprite(Flame.images.fromCache('crystal7.png'));
|
|
}
|
|
|
|
Sprite magicCube() {
|
|
return Sprite(Flame.images.fromCache('magic_cube.png'));
|
|
}
|