Skip to content

Commit

Permalink
restructuring maps and using doors to move around
Browse files Browse the repository at this point in the history
  • Loading branch information
kurtome committed Nov 20, 2023
1 parent 3194dd4 commit bcdccf5
Show file tree
Hide file tree
Showing 9 changed files with 330 additions and 14 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

Large diffs are not rendered by default.

167 changes: 167 additions & 0 deletions examples/standard_platformer/assets/tiles/map_menu.tmx

Large diffs are not rendered by default.

41 changes: 41 additions & 0 deletions examples/standard_platformer/lib/door.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import 'package:flame/components.dart';
import 'package:leap/leap.dart';
import 'package:leap_standard_platformer/main.dart';
import 'package:tiled/tiled.dart';

class Door extends PhysicalEntity<ExamplePlatformerLeapGame> {
Door(TiledObject object, ObjectGroup layer)
: super(
position: Vector2(object.x, object.y),
size: Vector2(object.width, object.height),
collisionType: CollisionType.standard,
) {
destinationMap = object.properties.getValue<String>('DestinationMap');
final destinationObjectId =
object.properties.getValue<int>('DestinationObject');
if (destinationObjectId != null) {
destinationObject =
layer.objects.firstWhere((obj) => obj.id == destinationObjectId);
}
}

late final String? destinationMap;
late final TiledObject? destinationObject;

void enter(PhysicalEntity other) {
if (destinationMap != null) {
game.goToLevel(destinationMap!);
} else if (destinationObject != null) {
other.x = destinationObject!.x;
other.y = destinationObject!.y;
}
}
}

class DoorFactory implements TiledObjectHandler {
@override
void handleObject(TiledObject object, Layer layer, LeapMap map) {
final component = Door(object, layer as ObjectGroup);
map.add(component);
}
}
59 changes: 59 additions & 0 deletions examples/standard_platformer/lib/info_text.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import 'package:flame/components.dart';
import 'package:flutter/material.dart';
import 'package:leap/leap.dart';
import 'package:leap_standard_platformer/main.dart';
import 'package:tiled/tiled.dart';

class InfoText extends PhysicalEntity<ExamplePlatformerLeapGame> {
InfoText(TiledObject object)
: super(
position: Vector2(object.x, object.y),
size: Vector2(object.width, object.height),
collisionType: CollisionType.standard,
) {
text = object.properties.getValue<String>('Text') ??
'Lorem ipsum mising text.';
}

late final String text;
TextBoxComponent? textBoxComponent;

TextBoxComponent _buildTextBox() {
return TextBoxComponent(
text: text,
// size: Vector2(160, 32),
position: Vector2(-16, -48),
boxConfig:
TextBoxConfig(dismissDelay: 3, margins: const EdgeInsets.all(4)),
textRenderer: TextPaint(
style: TextStyle(
fontSize: 8,
backgroundColor: Colors.black.withOpacity(0.4),
),
),
);
}

void activateText() {
if (textBoxComponent == null) {
textBoxComponent = _buildTextBox();
add(textBoxComponent!);
}
}

@override
void update(double dt) {
if (textBoxComponent?.finished ?? false) {
textBoxComponent!.removeFromParent();
textBoxComponent = null;
}
}
}

class InfoTextFactory implements TiledObjectHandler {
@override
void handleObject(TiledObject object, Layer layer, LeapMap map) {
final component = InfoText(object);
map.add(component);
}
}
19 changes: 15 additions & 4 deletions examples/standard_platformer/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import 'package:flutter/widgets.dart' hide Animation, Image;
import 'package:leap/leap.dart';
import 'package:leap_standard_platformer/basic_ladder.dart';
import 'package:leap_standard_platformer/coin.dart';
import 'package:leap_standard_platformer/door.dart';
import 'package:leap_standard_platformer/hud.dart';
import 'package:leap_standard_platformer/info_text.dart';
import 'package:leap_standard_platformer/player.dart';
import 'package:leap_standard_platformer/snowy_moving_platform.dart';
import 'package:leap_standard_platformer/welcome_dialog.dart';
Expand All @@ -33,15 +35,16 @@ class ExamplePlatformerLeapGame extends LeapGame
late final Map<String, TiledObjectHandler> tiledObjectHandlers;

static const _levels = [
'map.tmx',
'map_menu.tmx',
'map_everything.tmx',
'map_2.tmx',
];

var _currentLevel = 0;
var _currentLevel = 'map_menu.tmx';

Future<void> _loadLevel() {
return loadWorldAndMap(
tiledMapPath: _levels[_currentLevel],
tiledMapPath: _currentLevel,
tiledObjectHandlers: tiledObjectHandlers,
);
}
Expand All @@ -54,6 +57,8 @@ class ExamplePlatformerLeapGame extends LeapGame
'Coin': await CoinFactory.createFactory(),
'SnowyMovingPlatform': await SnowyMovingPlatformFactory.createFactory(),
'BasicLadder': await BasicLadderFactory.createFactory(),
'InfoText': InfoTextFactory(),
'Door': DoorFactory(),
};

// Default the camera size to the bounds of the Tiled map.
Expand Down Expand Up @@ -115,11 +120,17 @@ class ExamplePlatformerLeapGame extends LeapGame
}

Future<void> levelCleared() async {
_currentLevel = (_currentLevel + 1) % _levels.length;
final i = _levels.indexOf(_currentLevel);
_currentLevel = _levels[(i + 1) % _levels.length];

await _loadLevel();
}

Future<void> goToLevel(String mapName) async {
_currentLevel = mapName;
await _loadLevel();
}

@override
void update(double dt) {
super.update(dt);
Expand Down
10 changes: 10 additions & 0 deletions examples/standard_platformer/lib/player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import 'package:flame/sprite.dart';
import 'package:flame_audio/flame_audio.dart';
import 'package:leap/leap.dart';
import 'package:leap_standard_platformer/coin.dart';
import 'package:leap_standard_platformer/door.dart';
import 'package:leap_standard_platformer/info_text.dart';
import 'package:leap_standard_platformer/main.dart';

class Player extends JumperCharacter<ExamplePlatformerLeapGame> {
Expand Down Expand Up @@ -239,6 +241,14 @@ class Player extends JumperCharacter<ExamplePlatformerLeapGame> {
coins++;
_checkForLevelCompletion();
}

if (other is InfoText) {
other.activateText();
}

if (other is Door && _input.justPressed && _input.isPressedCenter) {
other.enter(this);
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions examples/standard_platformer/lib/snowy_moving_platform.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ class SnowyMovingPlatformFactory implements TiledObjectHandler {

@override
void handleObject(TiledObject object, Layer layer, LeapMap map) {
final coin = SnowyMovingPlatform(object, sprite, map.tileSize);
map.add(coin);
final platform = SnowyMovingPlatform(object, sprite, map.tileSize);
map.add(platform);
}

static Future<SnowyMovingPlatformFactory> createFactory() async {
Expand Down
3 changes: 2 additions & 1 deletion examples/standard_platformer/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@ flutter:
- assets/images/level_standard_tileset.png
- assets/images/repeatable_background.png
- assets/images/player_spritesheet.png
- assets/tiles/map.tmx
- assets/tiles/map_everything.tmx
- assets/tiles/map_2.tmx
- assets/tiles/map_menu.tmx

0 comments on commit bcdccf5

Please sign in to comment.