diff --git a/CHANGELOG.md b/CHANGELOG.md index 30e06a40f..aefbcb01a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +# 3.8.4 +- `KeyboardConfig` improvements. Now `directionalKeys` expect list of `KeyboardDirectionalKeys`. Fix [#507](https://github.com/RafaelBarbosatec/bonfire/issues/507) +- `PlatformEnemy` improvements. +- Adds `flipAnimation` method in `ui.Image`. + # 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. diff --git a/example/lib/pages/mini_games/tiled_map/game_tiled_map.dart b/example/lib/pages/mini_games/tiled_map/game_tiled_map.dart index ceeae1326..f1aec37d2 100644 --- a/example/lib/pages/mini_games/tiled_map/game_tiled_map.dart +++ b/example/lib/pages/mini_games/tiled_map/game_tiled_map.dart @@ -27,7 +27,10 @@ class GameTiledMap extends StatelessWidget { builder: (context, constraints) { return BonfireWidget( keyboardConfig: KeyboardConfig( - directionalKeys: KeyboardDirectionalKeys.arrows(), + directionalKeys: [ + KeyboardDirectionalKeys.arrows(), + KeyboardDirectionalKeys.wasd(), + ], acceptedKeys: [ LogicalKeyboardKey.space, ], diff --git a/lib/input/keyboard/control_by_keyboard.dart b/lib/input/keyboard/control_by_keyboard.dart index 5c63616d8..f41b1e8cc 100644 --- a/lib/input/keyboard/control_by_keyboard.dart +++ b/lib/input/keyboard/control_by_keyboard.dart @@ -79,11 +79,30 @@ class ControlByKeyboard extends PlayerController with KeyboardEventListener { /// Check if the key is for directional [arrows, wasd, or both] bool _isDirectional(LogicalKeyboardKey key) { - return keyboardConfig.directionalKeys.contain(key); + return keyboardConfig.directionalKeys.any( + (element) => element.contain(key), + ); + } + + bool isUpPressed(LogicalKeyboardKey key) { + return keyboardConfig.directionalKeys.any((element) => element.up == key); + } + + bool isDownPressed(LogicalKeyboardKey key) { + return keyboardConfig.directionalKeys.any((element) => element.down == key); + } + + bool isLeftPressed(LogicalKeyboardKey key) { + return keyboardConfig.directionalKeys.any((element) => element.left == key); + } + + bool isRightPressed(LogicalKeyboardKey key) { + return keyboardConfig.directionalKeys + .any((element) => element.right == key); } void _sendOneDirection(LogicalKeyboardKey key) { - if (keyboardConfig.directionalKeys.up == key) { + if (isUpPressed(key)) { joystickChangeDirectional(JoystickDirectionalEvent( directional: JoystickMoveDirectional.MOVE_UP, intensity: 1.0, @@ -91,7 +110,7 @@ class ControlByKeyboard extends PlayerController with KeyboardEventListener { isKeyboard: true, )); } - if (keyboardConfig.directionalKeys.down == key) { + if (isDownPressed(key)) { joystickChangeDirectional(JoystickDirectionalEvent( directional: JoystickMoveDirectional.MOVE_DOWN, intensity: 1.0, @@ -100,7 +119,7 @@ class ControlByKeyboard extends PlayerController with KeyboardEventListener { )); } - if (keyboardConfig.directionalKeys.left == key) { + if (isLeftPressed(key)) { joystickChangeDirectional(JoystickDirectionalEvent( directional: JoystickMoveDirectional.MOVE_LEFT, intensity: 1.0, @@ -109,7 +128,7 @@ class ControlByKeyboard extends PlayerController with KeyboardEventListener { )); } - if (keyboardConfig.directionalKeys.right == key) { + if (isRightPressed(key)) { joystickChangeDirectional(JoystickDirectionalEvent( directional: JoystickMoveDirectional.MOVE_RIGHT, intensity: 1.0, @@ -120,10 +139,8 @@ class ControlByKeyboard extends PlayerController with KeyboardEventListener { } void _sendTwoDirection(LogicalKeyboardKey key1, LogicalKeyboardKey key2) { - if (key1 == keyboardConfig.directionalKeys.right && - key2 == keyboardConfig.directionalKeys.down || - key1 == keyboardConfig.directionalKeys.down && - key2 == keyboardConfig.directionalKeys.right) { + if (isRightPressed(key1) && isDownPressed(key2) || + isDownPressed(key1) && isRightPressed(key2)) { joystickChangeDirectional(JoystickDirectionalEvent( directional: JoystickMoveDirectional.MOVE_DOWN_RIGHT, intensity: 1.0, @@ -132,10 +149,8 @@ class ControlByKeyboard extends PlayerController with KeyboardEventListener { )); } - if (key1 == keyboardConfig.directionalKeys.left && - key2 == keyboardConfig.directionalKeys.down || - key1 == keyboardConfig.directionalKeys.down && - key2 == keyboardConfig.directionalKeys.left) { + if (isLeftPressed(key1) && isDownPressed(key2) || + isDownPressed(key1) && isLeftPressed(key2)) { joystickChangeDirectional(JoystickDirectionalEvent( directional: JoystickMoveDirectional.MOVE_DOWN_LEFT, intensity: 1.0, @@ -144,10 +159,8 @@ class ControlByKeyboard extends PlayerController with KeyboardEventListener { )); } - if (key1 == keyboardConfig.directionalKeys.left && - key2 == keyboardConfig.directionalKeys.up || - key1 == keyboardConfig.directionalKeys.up && - key2 == keyboardConfig.directionalKeys.left) { + if (isLeftPressed(key1) && isUpPressed(key2) || + isUpPressed(key1) && isLeftPressed(key2)) { joystickChangeDirectional(JoystickDirectionalEvent( directional: JoystickMoveDirectional.MOVE_UP_LEFT, intensity: 1.0, @@ -156,10 +169,8 @@ class ControlByKeyboard extends PlayerController with KeyboardEventListener { )); } - if (key1 == keyboardConfig.directionalKeys.right && - key2 == keyboardConfig.directionalKeys.up || - key1 == keyboardConfig.directionalKeys.up && - key2 == keyboardConfig.directionalKeys.right) { + if (isRightPressed(key1) && isUpPressed(key2) || + isUpPressed(key1) && isRightPressed(key2)) { joystickChangeDirectional(JoystickDirectionalEvent( directional: JoystickMoveDirectional.MOVE_UP_RIGHT, intensity: 1.0, diff --git a/lib/input/keyboard/keyboard_config.dart b/lib/input/keyboard/keyboard_config.dart index a8b2728de..cac7dcd95 100644 --- a/lib/input/keyboard/keyboard_config.dart +++ b/lib/input/keyboard/keyboard_config.dart @@ -41,7 +41,7 @@ class KeyboardConfig { final bool enable; /// Type of the directional (arrows, wasd or wasdAndArrows) - final KeyboardDirectionalKeys directionalKeys; + final List directionalKeys; /// You can pass specific Keys accepted. If null accept all keys final List? acceptedKeys; @@ -51,10 +51,12 @@ class KeyboardConfig { KeyboardConfig({ this.enable = true, - KeyboardDirectionalKeys? directionalKeys, + List? directionalKeys, this.acceptedKeys, this.enableDiagonalInput = true, - }) : directionalKeys = directionalKeys ?? KeyboardDirectionalKeys.arrows() { - acceptedKeys?.addAll(this.directionalKeys.keys); + }) : directionalKeys = directionalKeys ?? [KeyboardDirectionalKeys.arrows()] { + acceptedKeys?.addAll( + this.directionalKeys.map((e) => e.keys).expand((e) => e), + ); } } diff --git a/lib/npc/enemy/platform_enemy.dart b/lib/npc/enemy/platform_enemy.dart index 17a9534df..d73dc771b 100644 --- a/lib/npc/enemy/platform_enemy.dart +++ b/lib/npc/enemy/platform_enemy.dart @@ -34,4 +34,11 @@ class PlatformEnemy extends SimpleEnemy ) { setupJumper(maxJump: countJumps); } + + @override + void idle() { + if (lastDirection.isHorizontal) { + super.idle(); + } + } } diff --git a/lib/util/extensions/extensions.dart b/lib/util/extensions/extensions.dart index c46be8a16..435cbbfea 100644 --- a/lib/util/extensions/extensions.dart +++ b/lib/util/extensions/extensions.dart @@ -1,6 +1,5 @@ import 'dart:async'; import 'dart:math'; -import 'dart:ui'; import 'package:bonfire/bonfire.dart'; import 'package:flutter/widgets.dart' as widget; @@ -9,6 +8,7 @@ export 'ally/ally_extensions.dart'; export 'enemy/enemy_extensions.dart'; export 'enemy/rotation_enemy_extensions.dart'; export 'game_component_extensions.dart'; +export 'image_extensions.dart'; export 'joystick_extensions.dart'; export 'movement_extensions.dart'; export 'npc/npc_extensions.dart'; @@ -17,67 +17,6 @@ export 'player/rotation_player_extensions.dart'; typedef BoolCallback = bool Function(); -extension BonfireImageExtension on Image { - SpriteAnimation getAnimation({ - required Vector2 size, - required int amount, - Vector2? position, - double stepTime = 0.1, - bool loop = true, - }) { - return SpriteAnimation.fromFrameData( - this, - SpriteAnimationData.sequenced( - amount: amount, - stepTime: stepTime, - textureSize: size, - loop: loop, - texturePosition: position, - ), - ); - } - - Sprite getSprite({ - Vector2? position, - Vector2? size, - }) { - return Sprite( - this, - srcPosition: position, - srcSize: size, - ); - } - - /// Do merge image. Overlaying the images - /// @deprecated Use [ImageComposition] - Future overlap(Image other) { - PictureRecorder recorder = PictureRecorder(); - final paint = Paint(); - Canvas canvas = Canvas(recorder); - final totalWidth = max(width, other.width); - final totalHeight = max(height, other.height); - canvas.drawImage(this, Offset.zero, paint); - canvas.drawImage(other, Offset.zero, paint); - return recorder.endRecording().toImage(totalWidth, totalHeight); - } - - /// Do merge image list. Overlaying the images - Future overlapList(List others) { - PictureRecorder recorder = PictureRecorder(); - final paint = Paint(); - Canvas canvas = Canvas(recorder); - int totalWidth = width; - int totalHeight = height; - canvas.drawImage(this, Offset.zero, paint); - for (var i in others) { - totalWidth = max(totalWidth, i.width); - totalHeight = max(totalHeight, i.height); - canvas.drawImage(i, Offset.zero, paint); - } - return recorder.endRecording().toImage(totalWidth, totalHeight); - } -} - extension OffSetExt on Offset { Offset copyWith({double? x, double? y}) { return Offset(x ?? dx, y ?? dy); diff --git a/lib/util/extensions/image_extensions.dart b/lib/util/extensions/image_extensions.dart new file mode 100644 index 000000000..729d9e09d --- /dev/null +++ b/lib/util/extensions/image_extensions.dart @@ -0,0 +1,90 @@ +import 'dart:math'; +import 'dart:ui'; + +import 'package:bonfire/bonfire.dart'; + +extension BonfireImageExtension on Image { + SpriteAnimation getAnimation({ + required Vector2 size, + required int amount, + Vector2? position, + double stepTime = 0.1, + bool loop = true, + }) { + return SpriteAnimation.fromFrameData( + this, + SpriteAnimationData.sequenced( + amount: amount, + stepTime: stepTime, + textureSize: size, + loop: loop, + texturePosition: position, + ), + ); + } + + Sprite getSprite({ + Vector2? position, + Vector2? size, + }) { + return Sprite( + this, + srcPosition: position, + srcSize: size, + ); + } + + /// Do merge image. Overlaying the images + /// @deprecated Use [ImageComposition] + Future overlap(Image other) { + PictureRecorder recorder = PictureRecorder(); + final paint = Paint(); + Canvas canvas = Canvas(recorder); + final totalWidth = max(width, other.width); + final totalHeight = max(height, other.height); + canvas.drawImage(this, Offset.zero, paint); + canvas.drawImage(other, Offset.zero, paint); + return recorder.endRecording().toImage(totalWidth, totalHeight); + } + + /// Do merge image list. Overlaying the images + Future overlapList(List others) { + PictureRecorder recorder = PictureRecorder(); + final paint = Paint(); + Canvas canvas = Canvas(recorder); + int totalWidth = width; + int totalHeight = height; + canvas.drawImage(this, Offset.zero, paint); + for (var i in others) { + totalWidth = max(totalWidth, i.width); + totalHeight = max(totalHeight, i.height); + canvas.drawImage(i, Offset.zero, paint); + } + return recorder.endRecording().toImage(totalWidth, totalHeight); + } + + // flip the frames horizontally + Future flipAnimation({required Vector2 size, required int count}) { + PictureRecorder recorder = PictureRecorder(); + final paint = Paint(); + Canvas canvas = Canvas(recorder); + + canvas.translate(width / 2, height / 2); + canvas.scale(-1, 1); + canvas.translate(-width / 2, -height / 2); + int indexAux = 0; + for (int i = count - 1; i >= 0; i--) { + final dstPosition = Vector2(size.x * i, 0); + final srcPosition = Vector2(size.x * indexAux, 0); + canvas.drawImageRect( + this, + Rect.fromLTWH(srcPosition.x, srcPosition.y, size.x, size.y), + Rect.fromLTWH(dstPosition.x, dstPosition.y, size.x, size.y), + paint, + ); + indexAux++; + } + + return recorder.endRecording().toImage(width, height); + } +} diff --git a/pubspec.yaml b/pubspec.yaml index 2e8a48102..c21956d76 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: bonfire description: (RPG maker) Create RPG-style or similar games more simply with Flame. -version: 3.8.3 +version: 3.8.4 homepage: https://bonfire-engine.github.io repository: https://github.com/RafaelBarbosatec/bonfire issue_tracker: https://github.com/RafaelBarbosatec/bonfire/issues