Skip to content

Commit

Permalink
Merge pull request #527 from RafaelBarbosatec/develop
Browse files Browse the repository at this point in the history
Version 3.9.5
  • Loading branch information
RafaelBarbosatec authored Jun 10, 2024
2 parents 46ace7f + 4860bb4 commit aea8c40
Show file tree
Hide file tree
Showing 20 changed files with 209 additions and 115 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 3.9.5

- Fix Joystick bug when viewport is fixed resolution. [#526](https://github.com/RafaelBarbosatec/bonfire/issues/526)

# 3.9.4
- Fix bug in `FollowerWidget`.
- Fix bug in `SimpleDirectionAnimation` where fast animation direction did not change when character direction changed. Thanks [tkshnwesper](https://github.com/tkshnwesper)
Expand Down
36 changes: 29 additions & 7 deletions lib/base/bonfire_game.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import 'package:bonfire/camera/bonfire_camera.dart';
import 'package:bonfire/color_filter/color_filter_component.dart';
import 'package:bonfire/joystick/joystick_map_explorer.dart';
import 'package:bonfire/lighting/lighting_component.dart';
// ignore: implementation_imports
import 'package:flame/src/camera/viewports/fixed_resolution_viewport.dart';
import 'package:flame/camera.dart';
// ignore: implementation_imports
import 'package:flutter/widgets.dart';

Expand Down Expand Up @@ -117,9 +116,7 @@ class BonfireGame extends BaseGame implements BonfireGameInterface {
super(
camera: BonfireCamera(
config: cameraConfig,
viewport: cameraConfig?.resolution != null
? FixedResolutionViewport(resolution: cameraConfig!.resolution!)
: null,
viewport: _getViewPort(cameraConfig),
backdrop: background,
hudComponents: [
LightingComponent(
Expand Down Expand Up @@ -258,12 +255,28 @@ class BonfireGame extends BaseGame implements BonfireGameInterface {

@override
Vector2 worldToScreen(Vector2 position) {
return camera.worldToScreen(position);
final worldPosition = camera.worldToScreen(position);
return viewportPositionToGlobal(
worldPosition,
);
}

@override
Vector2 screenToWorld(Vector2 position) {
return camera.screenToWorld(position);
final viewportPosition = globalToViewportPosition(
position,
);
return camera.screenToWorld(viewportPosition);
}

@override
Vector2 globalToViewportPosition(Vector2 position) {
return camera.viewport.globalToLocal(position);
}

@override
Vector2 viewportPositionToGlobal(Vector2 position) {
return camera.viewport.localToGlobal(position);
}

@override
Expand Down Expand Up @@ -497,4 +510,13 @@ class BonfireGame extends BaseGame implements BonfireGameInterface {
FutureOr<void> addHud(Component component) {
return camera.viewport.add(component);
}

static _getViewPort(CameraConfig? cameraConfig) {
if (cameraConfig?.resolution != null) {
return FixedResolutionViewport(
resolution: cameraConfig!.resolution!,
);
}
return null;
}
}
6 changes: 6 additions & 0 deletions lib/base/bonfire_game_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ abstract class BonfireGameInterface {
/// This method convert screen position to word position
Vector2 screenToWorld(Vector2 screenPosition);

/// This method convert viewport position to word position
Vector2 globalToViewportPosition(Vector2 position);

/// This method convert viewport position to screen position
Vector2 viewportPositionToGlobal(Vector2 position);

/// Used to check if a component is visible in the camera.
bool isVisibleInCamera(GameComponent c);

Expand Down
4 changes: 4 additions & 0 deletions lib/input/gestures/drag_gesture.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ mixin DragGesture on GameComponent {
final gEvent = GestureEvent.fromPointerEvent(
event,
screenToWorld: gameRef.screenToWorld,
globalToViewportPosition: gameRef.globalToViewportPosition,
);
bool handler = false;

Expand Down Expand Up @@ -46,6 +47,7 @@ mixin DragGesture on GameComponent {
final gEvent = GestureEvent.fromPointerEvent(
event,
screenToWorld: gameRef.screenToWorld,
globalToViewportPosition: gameRef.globalToViewportPosition,
);
bool canMove = hasGameRef &&
_startDragPosition != null &&
Expand Down Expand Up @@ -79,6 +81,7 @@ mixin DragGesture on GameComponent {
final gEvent = GestureEvent.fromPointerEvent(
event,
screenToWorld: gameRef.screenToWorld,
globalToViewportPosition: gameRef.globalToViewportPosition,
);
if (gEvent.pointer == _pointer && _inMoving) {
_startDragPosition = null;
Expand All @@ -96,6 +99,7 @@ mixin DragGesture on GameComponent {
final gEvent = GestureEvent.fromPointerEvent(
event,
screenToWorld: gameRef.screenToWorld,
globalToViewportPosition: gameRef.globalToViewportPosition,
);
if (gEvent.pointer == _pointer && _inMoving) {
_startDragPosition = null;
Expand Down
8 changes: 5 additions & 3 deletions lib/input/gestures/gesture_event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ class GestureEvent {
factory GestureEvent.fromPointerEvent(
PointerEvent event, {
required Vector2 Function(Vector2 position) screenToWorld,
required Vector2 Function(Vector2 position) globalToViewportPosition,
}) {
final position = event.localPosition.toVector2();
final eventPosition = event.localPosition.toVector2();
final screenPosition = globalToViewportPosition(eventPosition);
return GestureEvent(
pointer: event.pointer,
kind: event.kind,
screenPosition: position,
worldPosition: screenToWorld(position),
screenPosition: screenPosition,
worldPosition: screenToWorld(eventPosition),
);
}
}
2 changes: 2 additions & 0 deletions lib/input/gestures/pinch_gesture.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ mixin PinchGesture on GameComponent {
final gEvent = GestureEvent.fromPointerEvent(
event,
screenToWorld: gameRef.screenToWorld,
globalToViewportPosition: gameRef.globalToViewportPosition,
);
if (!_fingers.contains(event.pointer)) {
_fingers.add(
Expand Down Expand Up @@ -62,6 +63,7 @@ mixin PinchGesture on GameComponent {
final gEvent = GestureEvent.fromPointerEvent(
event,
screenToWorld: gameRef.screenToWorld,
globalToViewportPosition: gameRef.globalToViewportPosition,
);
_updateFingers(gEvent);
_handleMove();
Expand Down
2 changes: 2 additions & 0 deletions lib/input/gestures/tap_gesture.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mixin TapGesture on GameComponent {
final tapEvent = GestureEvent.fromPointerEvent(
event,
screenToWorld: gameRef.screenToWorld,
globalToViewportPosition: gameRef.globalToViewportPosition,
);
bool handler = false;

Expand All @@ -34,6 +35,7 @@ mixin TapGesture on GameComponent {
final tapEvent = GestureEvent.fromPointerEvent(
event,
screenToWorld: gameRef.screenToWorld,
globalToViewportPosition: gameRef.globalToViewportPosition,
);

if (enableTab && tapEvent.pointer == _pointer && hasGameRef) {
Expand Down
34 changes: 18 additions & 16 deletions lib/input/keyboard/keyboard.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Keyboard extends PlayerController with KeyboardEventListener {
!event.synthesized &&
!_directionalIsIdle) {
_directionalIsIdle = true;
joystickChangeDirectional(
onJoystickChangeDirectional(
JoystickDirectionalEvent(
directional: JoystickMoveDirectional.IDLE,
intensity: 0.0,
Expand All @@ -64,14 +64,14 @@ class Keyboard extends PlayerController with KeyboardEventListener {
} else {
/// Process action events
if (event is KeyDownEvent) {
joystickAction(
onJoystickAction(
JoystickActionEvent(
id: event.logicalKey,
event: ActionEvent.DOWN,
),
);
} else if (event is KeyUpEvent) {
joystickAction(
onJoystickAction(
JoystickActionEvent(
id: event.logicalKey,
event: ActionEvent.UP,
Expand Down Expand Up @@ -109,15 +109,17 @@ class Keyboard extends PlayerController with KeyboardEventListener {

void _sendOneDirection(LogicalKeyboardKey key) {
if (isUpPressed(key)) {
joystickChangeDirectional(JoystickDirectionalEvent(
directional: JoystickMoveDirectional.MOVE_UP,
intensity: 1.0,
radAngle: 0.0,
isKeyboard: true,
));
onJoystickChangeDirectional(
JoystickDirectionalEvent(
directional: JoystickMoveDirectional.MOVE_UP,
intensity: 1.0,
radAngle: 0.0,
isKeyboard: true,
),
);
}
if (isDownPressed(key)) {
joystickChangeDirectional(JoystickDirectionalEvent(
onJoystickChangeDirectional(JoystickDirectionalEvent(
directional: JoystickMoveDirectional.MOVE_DOWN,
intensity: 1.0,
radAngle: 0.0,
Expand All @@ -126,7 +128,7 @@ class Keyboard extends PlayerController with KeyboardEventListener {
}

if (isLeftPressed(key)) {
joystickChangeDirectional(JoystickDirectionalEvent(
onJoystickChangeDirectional(JoystickDirectionalEvent(
directional: JoystickMoveDirectional.MOVE_LEFT,
intensity: 1.0,
radAngle: 0.0,
Expand All @@ -135,7 +137,7 @@ class Keyboard extends PlayerController with KeyboardEventListener {
}

if (isRightPressed(key)) {
joystickChangeDirectional(JoystickDirectionalEvent(
onJoystickChangeDirectional(JoystickDirectionalEvent(
directional: JoystickMoveDirectional.MOVE_RIGHT,
intensity: 1.0,
radAngle: 0.0,
Expand All @@ -147,7 +149,7 @@ class Keyboard extends PlayerController with KeyboardEventListener {
void _sendTwoDirection(LogicalKeyboardKey key1, LogicalKeyboardKey key2) {
if (isRightPressed(key1) && isDownPressed(key2) ||
isDownPressed(key1) && isRightPressed(key2)) {
joystickChangeDirectional(JoystickDirectionalEvent(
onJoystickChangeDirectional(JoystickDirectionalEvent(
directional: JoystickMoveDirectional.MOVE_DOWN_RIGHT,
intensity: 1.0,
radAngle: 0.0,
Expand All @@ -157,7 +159,7 @@ class Keyboard extends PlayerController with KeyboardEventListener {

if (isLeftPressed(key1) && isDownPressed(key2) ||
isDownPressed(key1) && isLeftPressed(key2)) {
joystickChangeDirectional(JoystickDirectionalEvent(
onJoystickChangeDirectional(JoystickDirectionalEvent(
directional: JoystickMoveDirectional.MOVE_DOWN_LEFT,
intensity: 1.0,
radAngle: 0.0,
Expand All @@ -167,7 +169,7 @@ class Keyboard extends PlayerController with KeyboardEventListener {

if (isLeftPressed(key1) && isUpPressed(key2) ||
isUpPressed(key1) && isLeftPressed(key2)) {
joystickChangeDirectional(JoystickDirectionalEvent(
onJoystickChangeDirectional(JoystickDirectionalEvent(
directional: JoystickMoveDirectional.MOVE_UP_LEFT,
intensity: 1.0,
radAngle: 0.0,
Expand All @@ -177,7 +179,7 @@ class Keyboard extends PlayerController with KeyboardEventListener {

if (isRightPressed(key1) && isUpPressed(key2) ||
isUpPressed(key1) && isRightPressed(key2)) {
joystickChangeDirectional(JoystickDirectionalEvent(
onJoystickChangeDirectional(JoystickDirectionalEvent(
directional: JoystickMoveDirectional.MOVE_UP_RIGHT,
intensity: 1.0,
radAngle: 0.0,
Expand Down
9 changes: 6 additions & 3 deletions lib/input/player_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ mixin PlayerControllerListener {
void onJoystickAction(JoystickActionEvent event) {}
}

abstract class PlayerController extends GameComponent {
abstract class PlayerController extends GameComponent
with PlayerControllerListener {
final dynamic id;
final List<PlayerControllerListener> _observers = [];

Expand All @@ -80,13 +81,15 @@ abstract class PlayerController extends GameComponent {
}
}

void joystickChangeDirectional(JoystickDirectionalEvent event) {
@override
void onJoystickChangeDirectional(JoystickDirectionalEvent event) {
for (var o in _observers) {
o.onJoystickChangeDirectional(event);
}
}

void joystickAction(JoystickActionEvent event) {
@override
void onJoystickAction(JoystickActionEvent event) {
for (var o in _observers) {
o.onJoystickAction(event);
}
Expand Down
15 changes: 11 additions & 4 deletions lib/joystick/joystick.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,21 @@ class Joystick extends PlayerController {
}

void initialize(Vector2 size) async {
directional?.initialize(size, this);
if (!hasGameRef) return;
directional?.initialize(this, gameRef.camera.viewport);
for (var action in actions) {
action.initialize(size, this);
action.initialize(this, gameRef.camera.viewport);
}
}

Future updateDirectional(JoystickDirectional? directional) async {
directional?.initialize(gameRef.size, this);
directional?.initialize(this, gameRef.camera.viewport);
await directional?.onLoad();
_directional = directional;
}

Future addAction(JoystickAction action) async {
action.initialize(gameRef.size, this);
action.initialize(this, gameRef.camera.viewport);
await action.onLoad();
actions.add(action);
}
Expand Down Expand Up @@ -104,6 +105,12 @@ class Joystick extends PlayerController {
super.onGameResize(size);
}

@override
void onMount() {
initialize(size);
super.onMount();
}

@override
Future<void> onLoad() async {
await super.onLoad();
Expand Down
Loading

0 comments on commit aea8c40

Please sign in to comment.