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

Version 3.8.4 #508

Merged
merged 6 commits into from
May 2, 2024
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
5 changes: 4 additions & 1 deletion example/lib/pages/mini_games/tiled_map/game_tiled_map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
],
Expand Down
53 changes: 32 additions & 21 deletions lib/input/keyboard/control_by_keyboard.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,38 @@ 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,
radAngle: 0.0,
isKeyboard: true,
));
}
if (keyboardConfig.directionalKeys.down == key) {
if (isDownPressed(key)) {
joystickChangeDirectional(JoystickDirectionalEvent(
directional: JoystickMoveDirectional.MOVE_DOWN,
intensity: 1.0,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
10 changes: 6 additions & 4 deletions lib/input/keyboard/keyboard_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class KeyboardConfig {
final bool enable;

/// Type of the directional (arrows, wasd or wasdAndArrows)
final KeyboardDirectionalKeys directionalKeys;
final List<KeyboardDirectionalKeys> directionalKeys;

/// You can pass specific Keys accepted. If null accept all keys
final List<LogicalKeyboardKey>? acceptedKeys;
Expand All @@ -51,10 +51,12 @@ class KeyboardConfig {

KeyboardConfig({
this.enable = true,
KeyboardDirectionalKeys? directionalKeys,
List<KeyboardDirectionalKeys>? 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),
);
}
}
7 changes: 7 additions & 0 deletions lib/npc/enemy/platform_enemy.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,11 @@ class PlatformEnemy extends SimpleEnemy
) {
setupJumper(maxJump: countJumps);
}

@override
void idle() {
if (lastDirection.isHorizontal) {
super.idle();
}
}
}
63 changes: 1 addition & 62 deletions lib/util/extensions/extensions.dart
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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';
Expand All @@ -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<Image> 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<Image> overlapList(List<Image> 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);
Expand Down
90 changes: 90 additions & 0 deletions lib/util/extensions/image_extensions.dart
Original file line number Diff line number Diff line change
@@ -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<Image> 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<Image> overlapList(List<Image> 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<Image> 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);
}
}
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.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
Expand Down
Loading