mirror of
https://github.com/flame-engine/flame.git
synced 2025-11-02 03:15:43 +08:00
docs: Update platformer tutorial to latest Flame (#2904)
Updates the platformer tutorial to Flame v1.11.0
This commit is contained in:
1
.github/.cspell/gamedev_dictionary.txt
vendored
1
.github/.cspell/gamedev_dictionary.txt
vendored
@ -45,7 +45,6 @@ raycasts # plural of raycast
|
|||||||
raytrace # act of raytracing
|
raytrace # act of raytracing
|
||||||
raytracing # rendering techniques that calculates light rays as straight lines
|
raytracing # rendering techniques that calculates light rays as straight lines
|
||||||
rects # plural of rect
|
rects # plural of rect
|
||||||
refactorings # plural of refactoring
|
|
||||||
respawned # past tense of respawn
|
respawned # past tense of respawn
|
||||||
respawn # when the player character dies and is brought back after some time and penalties
|
respawn # when the player character dies and is brought back after some time and penalties
|
||||||
RGBA # red green blue alpha
|
RGBA # red green blue alpha
|
||||||
|
|||||||
2
.github/.cspell/words_dictionary.txt
vendored
2
.github/.cspell/words_dictionary.txt
vendored
@ -2,8 +2,6 @@
|
|||||||
bloodlust
|
bloodlust
|
||||||
collidable
|
collidable
|
||||||
collidables
|
collidables
|
||||||
composability
|
|
||||||
discoverability
|
|
||||||
draggables
|
draggables
|
||||||
focusable
|
focusable
|
||||||
gamepad
|
gamepad
|
||||||
|
|||||||
@ -91,20 +91,15 @@ You can run this file and you should just have a blank screen now. Let's get Emb
|
|||||||
|
|
||||||
## CameraComponent and World
|
## CameraComponent and World
|
||||||
|
|
||||||
Since `FlameGame.camera` is deprecated, we want to add a `CameraComponent` that we can move around,
|
To move around in the world we are going the use the built-in `CameraComponent` and `World` that
|
||||||
and a world that we can add all our components to and move around our player in.
|
exists on the `FlameGame` class.
|
||||||
(The `CameraComponent` will be built-in in Flame v2).
|
We are going to add all our components to the `world` and follow the player with the `camera`.
|
||||||
|
|
||||||
```dart
|
```dart
|
||||||
import 'package:flame/components.dart';
|
import 'package:flame/components.dart';
|
||||||
import 'package:flame/game.dart';
|
import 'package:flame/game.dart';
|
||||||
|
|
||||||
class EmberQuestGame extends FlameGame {
|
class EmberQuestGame extends FlameGame {
|
||||||
EmberQuestGame();
|
|
||||||
|
|
||||||
final world = World();
|
|
||||||
late final CameraComponent cameraComponent;
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> onLoad() async {
|
Future<void> onLoad() async {
|
||||||
await images.loadAll([
|
await images.loadAll([
|
||||||
@ -117,12 +112,10 @@ class EmberQuestGame extends FlameGame {
|
|||||||
'water_enemy.png',
|
'water_enemy.png',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
cameraComponent = CameraComponent(world: world);
|
|
||||||
// Everything in this tutorial assumes that the position
|
// Everything in this tutorial assumes that the position
|
||||||
// of the `CameraComponent`s viewfinder (where the camera is looking)
|
// of the `CameraComponent`s viewfinder (where the camera is looking)
|
||||||
// is in the top left corner, that's why we set the anchor here.
|
// is in the top left corner, that's why we set the anchor here.
|
||||||
cameraComponent.viewfinder.anchor = Anchor.topLeft;
|
camera.viewfinder.anchor = Anchor.topLeft;
|
||||||
addAll([cameraComponent, world]);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@ -140,7 +133,7 @@ import 'package:flame/components.dart';
|
|||||||
import '../ember_quest.dart';
|
import '../ember_quest.dart';
|
||||||
|
|
||||||
class EmberPlayer extends SpriteAnimationComponent
|
class EmberPlayer extends SpriteAnimationComponent
|
||||||
with HasGameRef<EmberQuestGame> {
|
with HasGameReference<EmberQuestGame> {
|
||||||
EmberPlayer({
|
EmberPlayer({
|
||||||
required super.position,
|
required super.position,
|
||||||
}) : super(size: Vector2.all(64), anchor: Anchor.center);
|
}) : super(size: Vector2.all(64), anchor: Anchor.center);
|
||||||
@ -181,13 +174,8 @@ import 'package:flame/game.dart';
|
|||||||
import 'actors/ember.dart';
|
import 'actors/ember.dart';
|
||||||
|
|
||||||
class EmberQuestGame extends FlameGame {
|
class EmberQuestGame extends FlameGame {
|
||||||
EmberQuestGame();
|
|
||||||
|
|
||||||
late EmberPlayer _ember;
|
late EmberPlayer _ember;
|
||||||
|
|
||||||
final world = World();
|
|
||||||
late final CameraComponent cameraComponent;
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> onLoad() async {
|
Future<void> onLoad() async {
|
||||||
await images.loadAll([
|
await images.loadAll([
|
||||||
@ -200,9 +188,7 @@ class EmberQuestGame extends FlameGame {
|
|||||||
'water_enemy.png',
|
'water_enemy.png',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
cameraComponent = CameraComponent(world: world);
|
camera.viewfinder.anchor = Anchor.topLeft;
|
||||||
cameraComponent.viewfinder.anchor = Anchor.topLeft;
|
|
||||||
addAll([cameraComponent, world]);
|
|
||||||
|
|
||||||
_ember = EmberPlayer(
|
_ember = EmberPlayer(
|
||||||
position: Vector2(128, canvasSize.y - 70),
|
position: Vector2(128, canvasSize.y - 70),
|
||||||
|
|||||||
@ -300,10 +300,7 @@ as:
|
|||||||
'water_enemy.png',
|
'water_enemy.png',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
cameraComponent = CameraComponent(world: world);
|
camera.viewfinder.anchor = Anchor.topLeft;
|
||||||
cameraComponent.viewfinder.anchor = Anchor.topLeft;
|
|
||||||
addAll([cameraComponent, world]);
|
|
||||||
|
|
||||||
initializeGame();
|
initializeGame();
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@ -329,7 +326,7 @@ import 'package:flame/components.dart';
|
|||||||
import '../ember_quest.dart';
|
import '../ember_quest.dart';
|
||||||
|
|
||||||
class PlatformBlock extends SpriteComponent
|
class PlatformBlock extends SpriteComponent
|
||||||
with HasGameRef<EmberQuestGame> {
|
with HasGameReference<EmberQuestGame> {
|
||||||
final Vector2 gridPosition;
|
final Vector2 gridPosition;
|
||||||
double xOffset;
|
double xOffset;
|
||||||
|
|
||||||
|
|||||||
@ -17,7 +17,7 @@ import 'package:flutter/material.dart';
|
|||||||
import '../ember_quest.dart';
|
import '../ember_quest.dart';
|
||||||
|
|
||||||
class Star extends SpriteComponent
|
class Star extends SpriteComponent
|
||||||
with HasGameRef<EmberQuestGame> {
|
with HasGameReference<EmberQuestGame> {
|
||||||
final Vector2 gridPosition;
|
final Vector2 gridPosition;
|
||||||
double xOffset;
|
double xOffset;
|
||||||
|
|
||||||
@ -76,15 +76,15 @@ add(
|
|||||||
);
|
);
|
||||||
```
|
```
|
||||||
|
|
||||||
The `SizeEffect` is best explained by going to their [help
|
The `SizeEffect` is best explained by going to their
|
||||||
docs](../../flame/effects.md#sizeeffectby). In short, we simply reduce the size of the star
|
[docs](../../flame/effects.md#sizeeffectby). In short, we simply reduce the size of the star
|
||||||
by -24 pixels in both directions and we make it pulse infinitely using the `EffectController`.
|
by -24 pixels in both directions and we make it pulse infinitely using the `EffectController`.
|
||||||
|
|
||||||
Don't forget to add the star to your `lib/ember_quest.dart` file by doing:
|
Don't forget to add the star to your `lib/ember_quest.dart` file by doing:
|
||||||
|
|
||||||
```dart
|
```dart
|
||||||
case Star:
|
case Star:
|
||||||
add(
|
world.add(
|
||||||
Star(
|
Star(
|
||||||
gridPosition: block.gridPosition,
|
gridPosition: block.gridPosition,
|
||||||
xOffset: xPositionOffset,
|
xOffset: xPositionOffset,
|
||||||
@ -93,7 +93,7 @@ case Star:
|
|||||||
break;
|
break;
|
||||||
```
|
```
|
||||||
|
|
||||||
If you run your game, you should now see pulsing stars!
|
If you run your game, you should now see pulsating stars!
|
||||||
|
|
||||||
|
|
||||||
## Water Enemy
|
## Water Enemy
|
||||||
@ -109,7 +109,7 @@ import 'package:flame/effects.dart';
|
|||||||
import '../ember_quest.dart';
|
import '../ember_quest.dart';
|
||||||
|
|
||||||
class WaterEnemy extends SpriteAnimationComponent
|
class WaterEnemy extends SpriteAnimationComponent
|
||||||
with HasGameRef<EmberQuestGame> {
|
with HasGameReference<EmberQuestGame> {
|
||||||
final Vector2 gridPosition;
|
final Vector2 gridPosition;
|
||||||
double xOffset;
|
double xOffset;
|
||||||
|
|
||||||
@ -171,7 +171,7 @@ Don't forget to add the water enemy to your `lib/ember_quest.dart` file by doing
|
|||||||
|
|
||||||
```dart
|
```dart
|
||||||
case WaterEnemy:
|
case WaterEnemy:
|
||||||
add(
|
world.add(
|
||||||
WaterEnemy(
|
WaterEnemy(
|
||||||
gridPosition: block.gridPosition,
|
gridPosition: block.gridPosition,
|
||||||
xOffset: xPositionOffset,
|
xOffset: xPositionOffset,
|
||||||
@ -204,7 +204,7 @@ import 'package:flutter/material.dart';
|
|||||||
|
|
||||||
import '../ember_quest.dart';
|
import '../ember_quest.dart';
|
||||||
|
|
||||||
class GroundBlock extends SpriteComponent with HasGameRef<EmberQuestGame> {
|
class GroundBlock extends SpriteComponent with HasGameReference<EmberQuestGame> {
|
||||||
final Vector2 gridPosition;
|
final Vector2 gridPosition;
|
||||||
double xOffset;
|
double xOffset;
|
||||||
|
|
||||||
@ -334,7 +334,7 @@ import 'package:flutter/material.dart';
|
|||||||
import '../ember_quest.dart';
|
import '../ember_quest.dart';
|
||||||
import '../managers/segment_manager.dart';
|
import '../managers/segment_manager.dart';
|
||||||
|
|
||||||
class GroundBlock extends SpriteComponent with HasGameRef<EmberQuestGame> {
|
class GroundBlock extends SpriteComponent with HasGameReference<EmberQuestGame> {
|
||||||
final Vector2 gridPosition;
|
final Vector2 gridPosition;
|
||||||
double xOffset;
|
double xOffset;
|
||||||
|
|
||||||
@ -391,7 +391,7 @@ Finally, don't forget to add your Ground Block to `lib/ember_quest.dart` by addi
|
|||||||
|
|
||||||
```dart
|
```dart
|
||||||
case GroundBlock:
|
case GroundBlock:
|
||||||
add(
|
world.add(
|
||||||
GroundBlock(
|
GroundBlock(
|
||||||
gridPosition: block.gridPosition,
|
gridPosition: block.gridPosition,
|
||||||
xOffset: xPositionOffset,
|
xOffset: xPositionOffset,
|
||||||
|
|||||||
@ -20,7 +20,7 @@ class EmberQuestGame extends FlameGame with HasKeyboardHandlerComponents {
|
|||||||
|
|
||||||
```dart
|
```dart
|
||||||
class EmberPlayer extends SpriteAnimationComponent
|
class EmberPlayer extends SpriteAnimationComponent
|
||||||
with KeyboardHandler, HasGameRef<EmberQuestGame> {
|
with KeyboardHandler, HasGameReference<EmberQuestGame> {
|
||||||
```
|
```
|
||||||
|
|
||||||
Now we can add a new method:
|
Now we can add a new method:
|
||||||
@ -118,7 +118,7 @@ Next, add the `CollisionCallbacks` mixin to `lib/actors/ember.dart` like:
|
|||||||
|
|
||||||
```dart
|
```dart
|
||||||
class EmberPlayer extends SpriteAnimationComponent
|
class EmberPlayer extends SpriteAnimationComponent
|
||||||
with KeyboardHandler, CollisionCallbacks, HasGameRef<EmberQuestGame> {
|
with KeyboardHandler, CollisionCallbacks, HasGameReference<EmberQuestGame> {
|
||||||
```
|
```
|
||||||
|
|
||||||
If it did not auto-import, you will need the following:
|
If it did not auto-import, you will need the following:
|
||||||
@ -176,9 +176,7 @@ For the collisions to be activated for Ember, we need to add a `CircleHitbox`, s
|
|||||||
method, add the following:
|
method, add the following:
|
||||||
|
|
||||||
```dart
|
```dart
|
||||||
add(
|
add(CircleHitbox());
|
||||||
CircleHitbox(),
|
|
||||||
);
|
|
||||||
```
|
```
|
||||||
|
|
||||||
Now that we have the basic collisions created, we can add gravity so Ember exists in a game world
|
Now that we have the basic collisions created, we can add gravity so Ember exists in a game world
|
||||||
|
|||||||
@ -26,7 +26,7 @@ enum HeartState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class HeartHealthComponent extends SpriteGroupComponent<HeartState>
|
class HeartHealthComponent extends SpriteGroupComponent<HeartState>
|
||||||
with HasGameRef<EmberQuestGame> {
|
with HasGameReference<EmberQuestGame> {
|
||||||
final int heartNumber;
|
final int heartNumber;
|
||||||
|
|
||||||
HeartHealthComponent({
|
HeartHealthComponent({
|
||||||
@ -88,7 +88,7 @@ import 'package:flutter/material.dart';
|
|||||||
import '../ember_quest.dart';
|
import '../ember_quest.dart';
|
||||||
import 'heart.dart';
|
import 'heart.dart';
|
||||||
|
|
||||||
class Hud extends PositionComponent with HasGameRef<EmberQuestGame> {
|
class Hud extends PositionComponent with HasGameReference<EmberQuestGame> {
|
||||||
Hud({
|
Hud({
|
||||||
super.position,
|
super.position,
|
||||||
super.size,
|
super.size,
|
||||||
@ -152,7 +152,7 @@ the number of hearts necessary. The last step is to add the hud to the game.
|
|||||||
Go to `lib/ember_quest.dart` and add the following code in the `initializeGame` method:
|
Go to `lib/ember_quest.dart` and add the following code in the `initializeGame` method:
|
||||||
|
|
||||||
```dart
|
```dart
|
||||||
cameraComponent.viewport.add(Hud());
|
camera.viewport.add(Hud());
|
||||||
```
|
```
|
||||||
|
|
||||||
If the auto-import did not occur, you will need to add:
|
If the auto-import did not occur, you will need to add:
|
||||||
|
|||||||
@ -185,10 +185,8 @@ Future<void> onLoad() async {
|
|||||||
'star.png',
|
'star.png',
|
||||||
'water_enemy.png',
|
'water_enemy.png',
|
||||||
]);
|
]);
|
||||||
cameraComponent = CameraComponent(world: world);
|
|
||||||
cameraComponent.viewfinder.anchor = Anchor.topLeft;
|
|
||||||
addAll([cameraComponent, world]);
|
|
||||||
|
|
||||||
|
camera.viewfinder.anchor = Anchor.topLeft;
|
||||||
initializeGame(true);
|
initializeGame(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user