Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use provided map path #9

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion example/README.md

This file was deleted.

16 changes: 9 additions & 7 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 SpriteAnimation animation,
}) : super(static: true, collisionType: CollisionType.standard) {
tiledObject.width = 16;
tiledObject.height = 16;
priority = 2;

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

final TiledObject tiledObject;

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

FlameAudio.play('coin.wav');
}

Expand All @@ -47,7 +49,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
36 changes: 26 additions & 10 deletions examples/standard_platformer/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
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 +9,56 @@ 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(),
),
);
}

class ExamplePlatformerLeapGame extends LeapGame
with HasTappables, HasKeyboardHandlerComponents {
with TapCallbacks, HasKeyboardHandlerComponents {
late final Player player;
late final SimpleCombinedInput input;

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

await loadWorldAndMap('map.tmx', 16);
setFixedViewportInTiles(32, 16);
await loadWorldAndMap(
tileSize: 16,
tiledMapPath: 'map.tmx',
);

input = SimpleCombinedInput();
add(input);

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

cameraComponent.follow(player);

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

add(Hud());
add(WelcomeDialog(camera));
cameraComponent.viewport.add(Hud());
cameraComponent.viewport.add(
WelcomeDialog(
position: Vector2(
cameraComponent.viewport.size.x * 0.5,
cameraComponent.viewport.size.y * 0.9,
),
),
);

await Coin.loadAllInMap(map);
}

@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