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: misc clean-up #40

Merged
merged 1 commit into from
Dec 26, 2023
Merged
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
33 changes: 17 additions & 16 deletions examples/standard_platformer/lib/snowy_moving_platform.dart
Original file line number Diff line number Diff line change
@@ -1,44 +1,45 @@
import 'package:flame/components.dart';
import 'package:flame/flame.dart';
import 'package:flutter/foundation.dart';
import 'package:leap/leap.dart';
import 'package:leap_standard_platformer/main.dart';
import 'package:tiled/tiled.dart';

class SnowyMovingPlatform extends MovingPlatform<ExamplePlatformerLeapGame> {
SnowyMovingPlatform(TiledObject tiledObject, this.sprite, double tileSize)
: super.fromTiledObject(tiledObject, tileSize) {
SnowyMovingPlatform(super.tiledObject) : super.fromTiledObject() {
width = 16 * 6;
height = 16 * 2;
priority = 2;
}

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

final tileset = await Flame.images.load('level_ice_tileset.png');
final sprite = Sprite(
tileset,
srcPosition: Vector2(97, 64),
srcSize: Vector2(16 * 6, 16 * 2),
);

add(
SpriteComponent(
sprite: sprite,
),
);
}

final Sprite sprite;
}

class SnowyMovingPlatformFactory implements TiledObjectHandler {
late final Sprite sprite;

SnowyMovingPlatformFactory(this.sprite);

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

static Future<SnowyMovingPlatformFactory> createFactory() async {
final tileset = await Flame.images.load('level_ice_tileset.png');
final sprite = Sprite(
tileset,
srcPosition: Vector2(97, 64),
srcSize: Vector2(16 * 6, 16 * 2),
);
return SnowyMovingPlatformFactory(sprite);
return SnowyMovingPlatformFactory();
}
}
15 changes: 7 additions & 8 deletions examples/standard_platformer/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,10 @@ packages:
dependency: "direct main"
description:
name: flame
sha256: "4418cf72c53059241a2e9ddf8e783a01ae84caec31d7f6d30a19122b2c602748"
sha256: "8703abbdb3aec264f91823f8af3de4f1a14656326aff0f344d90484be97ee8e7"
url: "https://pub.dev"
source: hosted
version: "1.11.0"
version: "1.12.0"
flame_audio:
dependency: "direct main"
description:
Expand Down Expand Up @@ -156,11 +156,10 @@ packages:
flame_tiled:
dependency: "direct main"
description:
name: flame_tiled
sha256: "47f14dc87b92c252fc4b357b194f583817e93b7fb62880da2e7fe5abc3942c3b"
url: "https://pub.dev"
source: hosted
version: "1.18.0"
path: "../../../flame/packages/flame_tiled"
relative: true
source: path
version: "1.18.1"
flutter:
dependency: "direct main"
description: flutter
Expand Down Expand Up @@ -201,7 +200,7 @@ packages:
path: "../../packages/leap"
relative: true
source: path
version: "0.5.0"
version: "0.5.2"
material_color_utilities:
dependency: transitive
description:
Expand Down
5 changes: 5 additions & 0 deletions packages/leap/lib/src/entities/ladder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ abstract class Ladder<T extends LeapGame> extends PhysicalEntity<T> {
super.position,
super.size,
this.topExtraHitbox = 0,
this.tiledObject,
}) : super(static: true) {
y = y - topExtraHitbox;
height = height + topExtraHitbox;
Expand All @@ -23,6 +24,7 @@ abstract class Ladder<T extends LeapGame> extends PhysicalEntity<T> {
tiledObject.height,
),
topExtraHitbox: topExtraHitbox,
tiledObject: tiledObject,
);

// Extra hitbox is to make it easier for the ladder hitbox to
Expand All @@ -32,6 +34,9 @@ abstract class Ladder<T extends LeapGame> extends PhysicalEntity<T> {

/// The actual top of the ladder
double get logicalTop => top + topExtraHitbox;

/// [TiledObject] this was built from
final TiledObject? tiledObject;
}

/// The possible ladder movement states.
Expand Down
27 changes: 17 additions & 10 deletions packages/leap/lib/src/entities/moving_platform.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:developer' as developer;

import 'package:flame/extensions.dart';
import 'package:flutter/foundation.dart';
import 'package:leap/leap.dart';
import 'package:tiled/tiled.dart';

Expand All @@ -9,10 +10,11 @@ import 'package:tiled/tiled.dart';
abstract class MovingPlatform<T extends LeapGame> extends PhysicalEntity<T> {
MovingPlatform({
required Vector2 initialPosition,
required double tileSize,
required this.tilePath,
required this.moveSpeed,
required this.tilePath,
this.loopMode = MovingPlatformLoopMode.reverseAndLoop,
this.tiledObject,
super.size,
// moving platforms should update before other entities so anything
// standing on top of it from the previous frame can be properly moved
// with the platform.
Expand All @@ -21,25 +23,22 @@ abstract class MovingPlatform<T extends LeapGame> extends PhysicalEntity<T> {
tags.add(CommonTags.ground);

position = initialPosition;

// Snap to the closes position on the tile grid
position.x = position.x - position.x % tileSize;
position.y = position.y - position.y % tileSize;

this.positionPath = _calculatePositionPath(position, tilePath, tileSize);
}

MovingPlatform.fromTiledObject(
TiledObject tiledObject,
double tileSize,
) : this(
initialPosition: Vector2(tiledObject.x, tiledObject.y),
size: Vector2(tiledObject.width, tiledObject.height),
moveSpeed: _parseMoveSpeed(tiledObject),
tilePath: _parseTilePath(tiledObject),
loopMode: _parseLoopMode(tiledObject),
tileSize: tileSize,
tiledObject: tiledObject,
);

/// [TiledObject] this was built from
final TiledObject? tiledObject;

/// Move speed in tiles per second
final Vector2 moveSpeed;

Expand All @@ -65,6 +64,14 @@ abstract class MovingPlatform<T extends LeapGame> extends PhysicalEntity<T> {
bool _stopped = false;

@override
@mustCallSuper
void onLoad() {
if (tiledObject != null) {}
this.positionPath = _calculatePositionPath(position, tilePath, tileSize);
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ class CollisionDetectionBehavior extends PhysicalBehavior {
prevCollisionInfo.downCollision!.gridX >= 0 &&
prevCollisionInfo.downCollision!.gridY >= 0) {
final prevDown = prevCollisionInfo.downCollision!;
if (velocity.x > 0) {
if (velocity.x > 0 && prevDown.gridX < map.groundTiles.length - 1) {
// Walking down slope to the right.
final nextSlopeYDelta = prevDown.rightTop == 0 ? 1 : 0;
final nextSlope = map.groundTiles[prevDown.gridX + 1]
Expand All @@ -195,7 +195,7 @@ class CollisionDetectionBehavior extends PhysicalBehavior {
} else if (nextSlope != null && nextSlope.isSlopeFromRight) {
collisionInfo.addDownCollision(nextSlope);
}
} else if (velocity.x < 0) {
} else if (velocity.x < 0 && prevDown.gridX >= 1) {
// Walking down slope to the left.
final nextSlopeYDelta = prevDown.leftTop == 0 ? 1 : 0;
final nextSlope = map.groundTiles[prevDown.gridX - 1]
Expand Down
Loading