mirror of
				https://github.com/flame-engine/flame.git
				synced 2025-10-31 17:06:50 +08:00 
			
		
		
		
	 ccee9a466b
			
		
	
	ccee9a466b
	
	
	
		
			
			* 👌 Use `Offset` type directly in `JoystickAction.update` calculations (#631) * Move files to src and comply with the dart package layout convention * Fixing widgets example Co-authored-by: Serge Matveenko <lig@countzero.co> Co-authored-by: Erick Zanardo <erickzanardoo@gmail.com>
		
			
				
	
	
		
			98 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			Dart
		
	
	
	
	
	
			
		
		
	
	
			98 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			Dart
		
	
	
	
	
	
| import 'dart:math' as math;
 | |
| import 'dart:ui';
 | |
| 
 | |
| import 'package:flame/components.dart';
 | |
| import 'package:flame/game.dart';
 | |
| import 'package:flame/gestures.dart';
 | |
| import 'package:flame/palette.dart';
 | |
| import 'package:flutter/material.dart';
 | |
| 
 | |
| void main() {
 | |
|   runApp(
 | |
|     GameWidget(
 | |
|       game: MyGame(),
 | |
|     ),
 | |
|   );
 | |
| }
 | |
| 
 | |
| 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;
 | |
|   static Paint white = Palette.white.paint;
 | |
|   static Paint red = Palette.red.paint;
 | |
|   static Paint blue = Palette.blue.paint;
 | |
| 
 | |
|   @override
 | |
|   void render(Canvas c) {
 | |
|     super.render(c);
 | |
| 
 | |
|     c.drawRect(size.toRect(), white);
 | |
|     c.drawRect(const Rect.fromLTWH(0, 0, 3, 3), red);
 | |
|     c.drawRect(Rect.fromLTWH(width / 2, height / 2, 3, 3), blue);
 | |
|   }
 | |
| 
 | |
|   @override
 | |
|   void update(double t) {
 | |
|     super.update(t);
 | |
|     angle += SPEED * t;
 | |
|     angle %= 2 * math.pi;
 | |
|   }
 | |
| 
 | |
|   @override
 | |
|   void onMount() {
 | |
|     super.onMount();
 | |
|     size = Vector2.all(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;
 | |
|         remove(c);
 | |
|       }
 | |
|     });
 | |
| 
 | |
|     if (!handled) {
 | |
|       add(Square()
 | |
|         ..x = touchArea.left
 | |
|         ..y = touchArea.top);
 | |
|     }
 | |
|   }
 | |
| 
 | |
|   @override
 | |
|   void onDoubleTap() {
 | |
|     if (running) {
 | |
|       pauseEngine();
 | |
|     } else {
 | |
|       resumeEngine();
 | |
|     }
 | |
| 
 | |
|     running = !running;
 | |
|   }
 | |
| }
 |