flame 0.25.0 flame: ^0.25.0 copied to clipboard
A minimalist Flutter game engine, provides a nice set of somewhat independent modules you can choose from.
import 'dart:math' as math;
import 'dart:ui';
import 'package:flame/anchor.dart';
import 'package:flame/gestures.dart';
import 'package:flame/components/component.dart';
import 'package:flame/components/mixins/has_game_ref.dart';
import 'package:flame/game.dart';
import 'package:flame/palette.dart';
import 'package:flutter/material.dart';
void main() {
final game = MyGame();
runApp(game.widget);
}
class Palette {
static const PaletteEntry white = BasicPalette.white;
static const PaletteEntry red = PaletteEntry(Color(0xFFFF0000));
static const PaletteEntry blue = PaletteEntry(Color(0xFF0000FF));
}
class Square extends PositionComponent with HasGameRef<MyGame> {
static const SPEED = 0.25;
@override
void render(Canvas c) {
prepareCanvas(c);
c.drawRect(Rect.fromLTWH(0, 0, width, height), Palette.white.paint);
c.drawRect(const Rect.fromLTWH(0, 0, 3, 3), Palette.red.paint);
c.drawRect(Rect.fromLTWH(width / 2, height / 2, 3, 3), Palette.blue.paint);
}
@override
void update(double t) {
super.update(t);
angle += SPEED * t;
angle %= 2 * math.pi;
}
@override
void onMount() {
width = height = gameRef.squareSize;
anchor = Anchor.center;
}
}
class MyGame extends BaseGame with DoubleTapDetector, TapDetector {
final double squareSize = 128;
bool running = true;
MyGame() {
add(Square()
..x = 100
..y = 100);
}
@override
void onTapUp(details) {
final touchArea = Rect.fromCenter(
center: details.localPosition,
width: 20,
height: 20,
);
bool handled = false;
components.forEach((c) {
if (c is PositionComponent && c.toRect().overlaps(touchArea)) {
handled = true;
markToRemove(c);
}
});
if (!handled) {
addLater(Square()
..x = touchArea.left
..y = touchArea.top);
}
}
@override
void onDoubleTap() {
if (running) {
pauseEngine();
} else {
resumeEngine();
}
running = !running;
}
}