mirror of
				https://github.com/flame-engine/flame.git
				synced 2025-10-31 08:56:01 +08:00 
			
		
		
		
	 b283b82f6c
			
		
	
	b283b82f6c
	
	
	
		
			
			Replaces the switch cases that can be replaces with switch expressions and removes `break;` where it isn't needed. https://dart.dev/language/branches#switch-statements
		
			
				
	
	
		
			106 lines
		
	
	
		
			2.6 KiB
		
	
	
	
		
			Dart
		
	
	
	
	
	
			
		
		
	
	
			106 lines
		
	
	
		
			2.6 KiB
		
	
	
	
		
			Dart
		
	
	
	
	
	
| import 'package:flame/components.dart';
 | |
| import 'package:flame/events.dart';
 | |
| import 'package:flame/game.dart';
 | |
| import 'package:flutter/material.dart';
 | |
| 
 | |
| import 'actors/ember.dart';
 | |
| import 'actors/water_enemy.dart';
 | |
| import 'managers/segment_manager.dart';
 | |
| import 'objects/ground_block.dart';
 | |
| import 'objects/platform_block.dart';
 | |
| import 'objects/star.dart';
 | |
| import 'overlays/hud.dart';
 | |
| 
 | |
| class EmberQuestGame extends FlameGame
 | |
|     with HasCollisionDetection, HasKeyboardHandlerComponents {
 | |
|   EmberQuestGame();
 | |
| 
 | |
|   late EmberPlayer _ember;
 | |
|   late double lastBlockXPosition = 0.0;
 | |
|   late UniqueKey lastBlockKey;
 | |
| 
 | |
|   int starsCollected = 0;
 | |
|   int health = 3;
 | |
|   double cloudSpeed = 0.0;
 | |
|   double objectSpeed = 0.0;
 | |
| 
 | |
|   @override
 | |
|   Future<void> onLoad() async {
 | |
|     //debugMode = true; // Uncomment to see the bounding boxes
 | |
|     await images.loadAll([
 | |
|       'block.png',
 | |
|       'ember.png',
 | |
|       'ground.png',
 | |
|       'heart_half.png',
 | |
|       'heart.png',
 | |
|       'star.png',
 | |
|       'water_enemy.png',
 | |
|     ]);
 | |
|     camera.viewfinder.anchor = Anchor.topLeft;
 | |
| 
 | |
|     initializeGame(loadHud: true);
 | |
|   }
 | |
| 
 | |
|   @override
 | |
|   void update(double dt) {
 | |
|     if (health <= 0) {
 | |
|       overlays.add('GameOver');
 | |
|     }
 | |
|     super.update(dt);
 | |
|   }
 | |
| 
 | |
|   @override
 | |
|   Color backgroundColor() {
 | |
|     return const Color.fromARGB(255, 173, 223, 247);
 | |
|   }
 | |
| 
 | |
|   void loadGameSegments(int segmentIndex, double xPositionOffset) {
 | |
|     for (final block in segments[segmentIndex]) {
 | |
|       final component = switch (block.blockType) {
 | |
|         GroundBlock => GroundBlock(
 | |
|             gridPosition: block.gridPosition,
 | |
|             xOffset: xPositionOffset,
 | |
|           ),
 | |
|         PlatformBlock => PlatformBlock(
 | |
|             gridPosition: block.gridPosition,
 | |
|             xOffset: xPositionOffset,
 | |
|           ),
 | |
|         Star => Star(
 | |
|             gridPosition: block.gridPosition,
 | |
|             xOffset: xPositionOffset,
 | |
|           ),
 | |
|         WaterEnemy => WaterEnemy(
 | |
|             gridPosition: block.gridPosition,
 | |
|             xOffset: xPositionOffset,
 | |
|           ),
 | |
|         _ => throw UnimplementedError(),
 | |
|       };
 | |
|       world.add(component);
 | |
|     }
 | |
|   }
 | |
| 
 | |
|   void initializeGame({required bool loadHud}) {
 | |
|     // Assume that size.x < 3200
 | |
|     final segmentsToLoad = (size.x / 640).ceil();
 | |
|     segmentsToLoad.clamp(0, segments.length);
 | |
| 
 | |
|     for (var i = 0; i <= segmentsToLoad; i++) {
 | |
|       loadGameSegments(i, (640 * i).toDouble());
 | |
|     }
 | |
| 
 | |
|     _ember = EmberPlayer(
 | |
|       position: Vector2(128, canvasSize.y - 128),
 | |
|     );
 | |
|     world.add(_ember);
 | |
|     if (loadHud) {
 | |
|       camera.viewport.add(Hud());
 | |
|     }
 | |
|   }
 | |
| 
 | |
|   void reset() {
 | |
|     starsCollected = 0;
 | |
|     health = 3;
 | |
|     initializeGame(loadHud: false);
 | |
|   }
 | |
| }
 |