Skip to content

Commit

Permalink
Merge pull request #506 from RafaelBarbosatec/develop
Browse files Browse the repository at this point in the history
Version 3.8.3
  • Loading branch information
RafaelBarbosatec authored May 2, 2024
2 parents 0aa2b9c + c54a32b commit f3401b5
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 37 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 3.8.3
- Fix bug in the `PlatformPlayer` movements.
- Adds `objectsBuilder` in `WorldMapBySpritefusion`. You can select a layer to adds objects in the tile position.
- Now we use `DamageHitbox` in `FlyingAttackGameObject`.

# 3.8.2
- Adds param `centerAnchor` in `SimpleDirectionAnimation` and `PlatformAnimations`. It's useful to correct spritesheet not centered.
- Now only handle move left or right by joystick in `SimplePlayer`.
Expand Down
22 changes: 21 additions & 1 deletion lib/input/player_controller.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// ignore_for_file: public_member_api_docs, sort_constructors_first
// ignore_for_file: constant_identifier_names

import 'package:bonfire/base/game_component.dart';
Expand All @@ -12,7 +13,12 @@ enum JoystickMoveDirectional {
MOVE_DOWN_RIGHT,
MOVE_DOWN_LEFT,
MOVE_LEFT,
IDLE
IDLE;

bool get isLeft =>
this == MOVE_LEFT || this == MOVE_DOWN_LEFT || this == MOVE_UP_LEFT;
bool get isRight =>
this == MOVE_RIGHT || this == MOVE_DOWN_RIGHT || this == MOVE_UP_RIGHT;
}

class JoystickDirectionalEvent {
Expand All @@ -27,6 +33,20 @@ class JoystickDirectionalEvent {
this.radAngle = 0.0,
this.isKeyboard = false,
});

JoystickDirectionalEvent copyWith({
JoystickMoveDirectional? directional,
double? intensity,
double? radAngle,
bool? isKeyboard,
}) {
return JoystickDirectionalEvent(
directional: directional ?? this.directional,
intensity: intensity ?? this.intensity,
radAngle: radAngle ?? this.radAngle,
isKeyboard: isKeyboard ?? this.isKeyboard,
);
}
}

enum ActionEvent { DOWN, UP, MOVE }
Expand Down
70 changes: 52 additions & 18 deletions lib/map/spritefusion/builder/spritefusion_world_builder.dart
Original file line number Diff line number Diff line change
@@ -1,35 +1,47 @@
import 'package:bonfire/bonfire.dart';
import 'package:bonfire/map/base/layer.dart';
import 'package:bonfire/map/spritefusion/model/spritefucion_map.dart';
import 'package:bonfire/map/tiled/model/tiled_world_data.dart';
import 'package:bonfire/map/util/map_assets_manager.dart';
import 'package:flutter/material.dart';

typedef SpritefusionObjectBuilder = GameComponent Function(
Vector2 position,
);

class SpritefusionWorldBuilder {
final ValueChanged<Object>? onError;
final WorldMapReader<SpritefusionMap> reader;
final double sizeToUpdate;
final List<Layer> _layers = [];
final List<GameComponent> components = [];
final Map<String, SpritefusionObjectBuilder>? objectsBuilder;

SpritefusionWorldBuilder(
this.reader, {
required this.onError,
this.sizeToUpdate = 0,
this.objectsBuilder,
});

Future<WorldMap> build() async {
Future<WorldBuildData> build() async {
try {
final map = await reader.readMap();
await _load(map);
} catch (e) {
onError?.call(e);
// ignore: avoid_print
print('(SpritefusionWorldBuilder) Error: $e');
rethrow;
}

return Future.value(
WorldMap(
_layers,
tileSizeToUpdate: sizeToUpdate,
WorldBuildData(
map: WorldMap(
_layers,
tileSizeToUpdate: sizeToUpdate,
),
components: components,
),
);
}
Expand All @@ -39,24 +51,34 @@ class SpritefusionWorldBuilder {
final spritesheet = await MapAssetsManager.loadImage(map.imgPath);
final maxRow = spritesheet.width / map.tileSize;
for (var layer in map.layers.reversed) {
List<Tile> tiles = _loadTiles(
layer.tiles,
map.tileSize,
map.imgPath,
maxRow,
layer.collider,
);
_layers.add(
Layer(
id: index,
tiles: tiles,
priority: index,
),
);
final objectBuilder = objectsBuilder?[layer.name];
if (objectBuilder != null) {
_addObjects(layer, objectBuilder, map.tileSize);
} else {
_addTile(layer, map, maxRow, index);
}
index++;
}
}

void _addTile(SpritefusionMapLayer layer, SpritefusionMap map, double maxRow,
int index) {
List<Tile> tiles = _loadTiles(
layer.tiles,
map.tileSize,
map.imgPath,
maxRow,
layer.collider,
);
_layers.add(
Layer(
id: index,
tiles: tiles,
priority: index,
),
);
}

List<Tile> _loadTiles(
List<SpritefusionMapLayerTile> tiles,
double tileSize,
Expand Down Expand Up @@ -84,4 +106,16 @@ class SpritefusionWorldBuilder {
},
).toList();
}

void _addObjects(SpritefusionMapLayer layer,
SpritefusionObjectBuilder objectBuilder, double tileSize) {
for (var tile in layer.tiles) {
final position = Vector2(
tile.x.toDouble(),
tile.y.toDouble(),
) *
tileSize;
components.add(objectBuilder(position));
}
}
}
5 changes: 4 additions & 1 deletion lib/map/spritefusion/world_map_by_spritefusion.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,22 @@ class WorldMapBySpritefusion extends WorldMap {
WorldMapReader<SpritefusionMap> reader, {
ValueChanged<Object>? onError,
double sizeToUpdate = 0,
Map<String, SpritefusionObjectBuilder>? objectsBuilder,
}) : super(const []) {
this.sizeToUpdate = sizeToUpdate;
_builder = SpritefusionWorldBuilder(
reader,
onError: onError,
sizeToUpdate: sizeToUpdate,
objectsBuilder: objectsBuilder,
);
}

@override
Future<void> onLoad() async {
final map = await _builder.build();
layers = map.layers;
layers = map.map.layers;
gameRef.addAll(map.components ?? []);
return super.onLoad();
}
}
4 changes: 2 additions & 2 deletions lib/map/tiled/builder/tiled_world_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class TiledWorldBuilder {
_objectsBuilder[name] = builder;
}

Future<TiledWorldData> build() async {
Future<WorldBuildData> build() async {
try {
_tiledMap = await reader.readMap();
if (_tiledMap?.orientation != _mapOrientationSupported) {
Expand All @@ -89,7 +89,7 @@ class TiledWorldBuilder {
}

return Future.value(
TiledWorldData(
WorldBuildData(
map: WorldMap(
_layers,
tileSizeToUpdate: sizeToUpdate,
Expand Down
4 changes: 2 additions & 2 deletions lib/map/tiled/model/tiled_world_data.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'package:bonfire/bonfire.dart';

class TiledWorldData {
class WorldBuildData {
final WorldMap map;
final List<GameComponent>? components;

TiledWorldData({required this.map, this.components});
WorldBuildData({required this.map, this.components});
}
2 changes: 1 addition & 1 deletion lib/mixins/attackable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ mixin Attackable on GameComponent {

/// This method is used to check if this component can receive damage from any attacker.
bool checkCanReceiveDamage(AttackOriginEnum attacker) {
if (isDead) return false;
if (isDead || isRemoving) return false;
switch (receivesAttackFrom) {
case AcceptableAttackOriginEnum.ALL:
return true;
Expand Down
17 changes: 10 additions & 7 deletions lib/objects/flying_attack_game_object.dart
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class FlyingAttackGameObject extends AnimatedGameObject

@override
void onCollision(Set<Vector2> intersectionPoints, PositionComponent other) {
if (other is Attackable && !other.isRemoving) {
if (other is Attackable && animationDestroy == null) {
other.receiveDamage(attackFrom, damage, id);
}
if (other is GameComponent) {
Expand Down Expand Up @@ -283,12 +283,15 @@ class FlyingAttackGameObject extends AnimatedGameObject
}

void _applyDestroyDamage(Rect rectPosition, GameComponent component) {
gameRef.attackables(onlyVisible: true).forEach((element) {
if (element.rectAttackable().overlaps(rectPosition) &&
element != component) {
element.receiveDamage(attackFrom, damage, id);
}
});
gameRef.add(
DamageHitbox(
id: id,
position: rectPosition.positionVector2,
damage: damage,
origin: attackFrom,
size: rectPosition.size.toVector2(),
),
);
}

@override
Expand Down
16 changes: 12 additions & 4 deletions lib/player/platform_player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,19 @@ class PlatformPlayer extends SimplePlayer

@override
void onJoystickChangeDirectional(JoystickDirectionalEvent event) {
if (event.directional == JoystickMoveDirectional.MOVE_LEFT ||
event.directional == JoystickMoveDirectional.MOVE_RIGHT ||
event.directional == JoystickMoveDirectional.IDLE) {
super.onJoystickChangeDirectional(event);
JoystickMoveDirectional newDirectional = JoystickMoveDirectional.IDLE;

if (event.directional.isRight) {
newDirectional = JoystickMoveDirectional.MOVE_RIGHT;
} else if (event.directional.isLeft) {
newDirectional = JoystickMoveDirectional.MOVE_LEFT;
}

super.onJoystickChangeDirectional(
event.copyWith(
directional: newDirectional,
),
);
}

@override
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: bonfire
description: (RPG maker) Create RPG-style or similar games more simply with Flame.
version: 3.8.2
version: 3.8.3
homepage: https://bonfire-engine.github.io
repository: https://github.com/RafaelBarbosatec/bonfire
issue_tracker: https://github.com/RafaelBarbosatec/bonfire/issues
Expand Down

0 comments on commit f3401b5

Please sign in to comment.