Skip to content

Commit

Permalink
feat!: upgrade to flame 1.9.1 (#7)
Browse files Browse the repository at this point in the history
Co-authored-by: Erick Zanardo <[email protected]>
  • Loading branch information
marcossevilla and erickzanardo authored Sep 27, 2023
1 parent e867c32 commit 7b59cdc
Show file tree
Hide file tree
Showing 36 changed files with 775 additions and 574 deletions.
1 change: 0 additions & 1 deletion example/README.md

This file was deleted.

20 changes: 12 additions & 8 deletions examples/standard_platformer/lib/coin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import 'package:leap/leap.dart';
import 'package:tiled/tiled.dart';

class Coin extends PhysicalEntity {
final TiledObject tiledObject;

Coin(this.tiledObject, SpriteAnimation animation)
: super(static: true, collisionType: CollisionType.standard) {
size = Vector2.all(16);
Coin({
required this.tiledObject,
required this.animation,
}) : super(static: true, collisionType: CollisionType.standard) {
width = 16;
height = 16;
priority = 2;

anchor = Anchor.center;
Expand All @@ -24,15 +25,18 @@ class Coin extends PhysicalEntity {
);
}

final TiledObject tiledObject;
final SpriteAnimation animation;

@override
void onRemove() {
super.onRemove();

animation.stepTime = 0.2;
FlameAudio.play('coin.wav');
}

static Future<void> loadAllInMap(LeapMap map) async {
final objGroup = map.getTileLayer<ObjectGroup>('AnimatedCoins')!;
final objGroup = map.getTileLayer<ObjectGroup>('AnimatedCoins');
final tileset = await Flame.images.load('level_ice_tileset.png');
final spriteAnimation = SpriteAnimation.fromFrameData(
tileset,
Expand All @@ -47,7 +51,7 @@ class Coin extends PhysicalEntity {
// We are 100% sure that an object layer named `AnimatedCoins`
// exists in the example `map.tmx`.
for (final obj in objGroup.objects) {
map.add(Coin(obj, spriteAnimation));
map.add(Coin(tiledObject: obj, animation: spriteAnimation));
}
}
}
22 changes: 9 additions & 13 deletions examples/standard_platformer/lib/hud.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,24 @@ import 'package:flutter/material.dart';
import 'package:leap_standard_platformer/main.dart';

class Hud extends PositionComponent with HasGameRef<ExamplePlatformerLeapGame> {
late final TextComponent textComponent;

static const textStyle = TextStyle(
fontSize: 10,
color: Colors.white,
shadows: [
Shadow(
blurRadius: 4,
),
],
);

Hud() {
final textPaint = TextPaint(style: textStyle);
textComponent = TextComponent(textRenderer: textPaint);
add(textComponent);

positionType = PositionType.viewport;
x = 16;
y = 4;
}

late final TextComponent textComponent;
late final CameraComponent cameraComponent;

static const textStyle = TextStyle(
fontSize: 10,
color: Colors.white,
shadows: [Shadow(blurRadius: 4)],
);

@override
void update(double dt) {
super.update(dt);
Expand Down
53 changes: 42 additions & 11 deletions examples/standard_platformer/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flame/camera.dart';
import 'package:flame/events.dart';
import 'package:flame/game.dart';
import 'package:flame/input.dart';
import 'package:flame_audio/flame_audio.dart';
import 'package:flutter/widgets.dart' hide Animation, Image;
import 'package:leap/leap.dart';
Expand All @@ -9,40 +10,70 @@ import 'package:leap_standard_platformer/player.dart';
import 'package:leap_standard_platformer/welcome_dialog.dart';

void main() {
runApp(GameWidget(game: ExamplePlatformerLeapGame()));
runApp(
GameWidget(
game: ExamplePlatformerLeapGame(
tileSize: 16,
),
),
);
}

class ExamplePlatformerLeapGame extends LeapGame
with HasTappables, HasKeyboardHandlerComponents {
with TapCallbacks, HasKeyboardHandlerComponents {
ExamplePlatformerLeapGame({
required super.tileSize,
}) : resolution = Vector2(32, 16);

final Vector2 resolution;
late final Player player;
late final SimpleCombinedInput input;

@override
Future<void> onLoad() async {
await super.onLoad();

await loadWorldAndMap('map.tmx', 16);
setFixedViewportInTiles(32, 16);
// Default the camera size to the bounds of the Tiled map.
camera = CameraComponent.withFixedResolution(
world: world,
width: tileSize * resolution.x.toInt(),
height: tileSize * resolution.y.toInt(),
);

await loadWorldAndMap(
camera: camera,
tiledMapPath: 'map.tmx',
);

input = SimpleCombinedInput();
add(input);

player = Player();
add(player);
camera.followComponent(player);
world.add(player);
camera.follow(player);

if (!FlameAudio.bgm.isPlaying) {
FlameAudio.bgm.play('village_music.mp3');
}

add(Hud());
add(WelcomeDialog(camera));
await Coin.loadAllInMap(map);
camera.viewport.add(Hud());
camera.viewport.add(
WelcomeDialog(
position: Vector2(
camera.viewport.size.x * 0.5,
camera.viewport.size.y * 0.9,
),
),
);

await Coin.loadAllInMap(leapMap);
}

@override
void update(double dt) {
super.update(dt);

// on web we need to wait for a user interaction before playing any sound
// On web, we need to wait for a user interaction before playing any sound.
if (input.justPressed && !FlameAudio.bgm.isPlaying) {
FlameAudio.bgm.play('village_music.mp3');
}
Expand Down
Loading

0 comments on commit 7b59cdc

Please sign in to comment.