mirror of
				https://github.com/flame-engine/flame.git
				synced 2025-11-01 01:18:38 +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>
		
			
				
	
	
		
			93 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			Dart
		
	
	
	
	
	
			
		
		
	
	
			93 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			Dart
		
	
	
	
	
	
| import 'dart:math';
 | |
| import 'dart:ui';
 | |
| 
 | |
| import 'package:flame/components.dart';
 | |
| import 'package:flame/joystick.dart';
 | |
| import 'package:flame/palette.dart';
 | |
| 
 | |
| class Player extends Component implements JoystickListener {
 | |
|   final _whitePaint = BasicPalette.white.paint;
 | |
|   final _bluePaint = Paint()..color = const Color(0xFF0000FF);
 | |
|   final _greenPaint = Paint()..color = const Color(0xFF00FF00);
 | |
|   final double speed = 159;
 | |
|   double currentSpeed = 0;
 | |
|   double radAngle = 0;
 | |
|   bool _move = false;
 | |
|   Paint _paint;
 | |
| 
 | |
|   Rect _rect;
 | |
| 
 | |
|   Player() {
 | |
|     _paint = _whitePaint;
 | |
|   }
 | |
| 
 | |
|   @override
 | |
|   void render(Canvas canvas) {
 | |
|     if (_rect == null) {
 | |
|       return;
 | |
|     }
 | |
|     canvas.save();
 | |
|     canvas.translate(_rect.center.dx, _rect.center.dy);
 | |
|     canvas.rotate(radAngle == 0.0 ? 0.0 : radAngle + (pi / 2));
 | |
|     canvas.translate(-_rect.center.dx, -_rect.center.dy);
 | |
|     canvas.drawRect(_rect, _paint);
 | |
|     canvas.restore();
 | |
|   }
 | |
| 
 | |
|   @override
 | |
|   void update(double dt) {
 | |
|     super.update(dt);
 | |
|     if (_move) {
 | |
|       moveFromAngle(dt);
 | |
|     }
 | |
|   }
 | |
| 
 | |
|   @override
 | |
|   void onGameResize(Vector2 size) {
 | |
|     _rect = Rect.fromLTWH(
 | |
|       (size.x / 2) - 25,
 | |
|       (size.y / 2) - 25,
 | |
|       50,
 | |
|       50,
 | |
|     );
 | |
|     super.onGameResize(size);
 | |
|   }
 | |
| 
 | |
|   @override
 | |
|   void joystickAction(JoystickActionEvent event) {
 | |
|     if (event.event == ActionEvent.DOWN) {
 | |
|       if (event.id == 1) {
 | |
|         _paint = _paint == _whitePaint ? _bluePaint : _whitePaint;
 | |
|       }
 | |
|       if (event.id == 2) {
 | |
|         _paint = _paint == _whitePaint ? _greenPaint : _whitePaint;
 | |
|       }
 | |
|     }
 | |
|   }
 | |
| 
 | |
|   @override
 | |
|   void joystickChangeDirectional(JoystickDirectionalEvent event) {
 | |
|     _move = event.directional != JoystickMoveDirectional.IDLE;
 | |
|     if (_move) {
 | |
|       radAngle = event.radAngle;
 | |
|       currentSpeed = speed * event.intensity;
 | |
|     }
 | |
|   }
 | |
| 
 | |
|   void moveFromAngle(double dtUpdate) {
 | |
|     final double nextX = (currentSpeed * dtUpdate) * cos(radAngle);
 | |
|     final double nextY = (currentSpeed * dtUpdate) * sin(radAngle);
 | |
| 
 | |
|     if (_rect == null) {
 | |
|       return;
 | |
|     }
 | |
| 
 | |
|     final Offset diffBase = Offset(
 | |
|           _rect.center.dx + nextX,
 | |
|           _rect.center.dy + nextY,
 | |
|         ) -
 | |
|         _rect.center;
 | |
|     _rect = _rect.shift(diffBase);
 | |
|   }
 | |
| }
 |