Skip to content

Commit

Permalink
Merge pull request #567 from RafaelBarbosatec/develop
Browse files Browse the repository at this point in the history
Version 3.12.1
  • Loading branch information
RafaelBarbosatec authored Nov 16, 2024
2 parents 7485938 + 57574b1 commit bffb05a
Show file tree
Hide file tree
Showing 20 changed files with 175 additions and 112 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v1
- name: Intall dart
uses: dart-lang/setup-dart@v1
- name: Setup Flutter
uses: subosito/flutter-action@v2
with:
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 3.12.1
- Resolve the alignment and visibility issues when using the MiniMap widget with non-1.0 zoom values. Thanks [qulvmp6](https://github.com/qulvmp6)
- Adds `randomMovementArea` param in `RandomMovement` mixin.

# 3.12.0
- Adds `UseShader` mixin.

Expand Down
1 change: 0 additions & 1 deletion example/lib/pages/home/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,6 @@ class _HomePageState extends State<HomePage> {
),
],
),

SectionDrawer(
name: 'Mini games',
itens: [
Expand Down
4 changes: 2 additions & 2 deletions example/lib/pages/player/simple/human.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'dart:async';
import 'package:bonfire/bonfire.dart';
import 'package:example/shared/util/person_sprite_sheet.dart';

class HumanPlayer extends SimplePlayer with BlockMovementCollision, UseShader {
class HumanPlayer extends SimplePlayer with BlockMovementCollision {
HumanPlayer({
required Vector2 position,
}) : super(
Expand All @@ -14,7 +14,7 @@ class HumanPlayer extends SimplePlayer with BlockMovementCollision, UseShader {
);

@override
Future<void> onLoad() {
Future<void> onLoad() async {
/// Adds rectangle collision
add(
RectangleHitbox(
Expand Down
15 changes: 7 additions & 8 deletions example/lib/pages/shader/shader_layer_configuration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,19 @@ class ShaderConfiguration extends GameComponent {
return super.onLoad();
}

@override
void onMount() {
_loadShader();
super.onMount();
}

@override
void onRemove() {
super.onRemove();
controller.removeListener(_controllerListener);
}

Future<void> _loadShader() async {
await Future.delayed(Duration.zero);
@override
void onGameMounted() {
super.onGameMounted();
_setupShader();
}

void _setupShader() {
final layer = gameRef.map.layersComponent.elementAtOrNull(1);
layer?.shader = shader;
layer?.shaderComponentStatic = true;
Expand Down
6 changes: 5 additions & 1 deletion example/lib/pages/shader/shader_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class _ShaderPageState extends State<ShaderPage> {
controller = ShaderConfigController();
super.initState();
}

@override
Widget build(BuildContext context) {
return Row(
Expand All @@ -43,7 +44,10 @@ class _ShaderPageState extends State<ShaderPage> {
],
),
),
Expanded(child: ShaderConfigPanel(controller: controller,)),
Expanded(
child: ShaderConfigPanel(
controller: controller,
)),
],
);
}
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ packages:
path: ".."
relative: true
source: path
version: "3.11.0"
version: "3.12.0"
boolean_selector:
dependency: transitive
description:
Expand Down
1 change: 1 addition & 0 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ flutter:
uses-material-design: true

shaders:
- shaders/baseShader.frag
- shaders/waterShader.frag
- shaders/waterShaderV2.frag

Expand Down
2 changes: 1 addition & 1 deletion example/shaders/baseShader.frag
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ uniform sampler2D uTexture; // COMPONETE TEXTURE
out vec4 fragColor;

void main() {
vec2 uv = FlutterFragCoord().xy / uSize
vec2 uv = FlutterFragCoord().xy / uSize;
fragColor = texture(uTexture, uv);
}
20 changes: 7 additions & 13 deletions lib/base/bonfire_game.dart
Original file line number Diff line number Diff line change
Expand Up @@ -182,18 +182,16 @@ class BonfireGame extends BaseGame implements BonfireGameInterface {
);
}

bool _gameMounted = false;
@override
void update(double dt) {
super.update(dt);
_intervalUpdateOder.update(dt);
_intervalOprimizeTree.update(dt);
}

@override
void onMount() {
super.onMount();
_notifyGameMounted();
onReady?.call(this);
if (!_gameMounted && camera.world?.children.isNotEmpty == true) {
_gameMounted = true;
Future.delayed(Duration.zero, _notifyGameMounted);
}
}

@override
Expand Down Expand Up @@ -471,12 +469,8 @@ class BonfireGame extends BaseGame implements BonfireGameInterface {
void _notifyGameMounted() {
void gameMontedComp(GameComponent c) => c.onGameMounted();
query<GameComponent>().forEach(gameMontedComp);
for (var child in camera.children) {
if (child is GameComponent) {
child.onGameMounted();
}
child.children.query<GameComponent>().forEach(gameMontedComp);
}
camera.world?.children.query<GameComponent>().forEach(gameMontedComp);
onReady?.call(this);
}

void _notifyGameDetach() {
Expand Down
3 changes: 3 additions & 0 deletions lib/base/game_component.dart
Original file line number Diff line number Diff line change
Expand Up @@ -267,5 +267,8 @@ abstract class GameComponent extends PositionComponent

void onGameMounted() {
_gameMounted = true;
children.query<GameComponent>().forEach((c) {
c.onGameMounted();
});
}
}
2 changes: 1 addition & 1 deletion lib/bonfire.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export 'package:bonfire/map/tiled/world_map_by_tiled.dart';
export 'package:bonfire/map/util/world_map_reader.dart';
export 'package:bonfire/map/world_map.dart';
export 'package:bonfire/mixins/attackable.dart';
export 'package:bonfire/mixins/automatic_random_movement.dart';
export 'package:bonfire/mixins/random_movement.dart';
export 'package:bonfire/mixins/elastic_collision.dart';
export 'package:bonfire/mixins/flip_render.dart';
export 'package:bonfire/mixins/follower.dart';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,16 @@ mixin RandomMovement on Movement {

late Random _random;

double? distanceToArrived;
double? _distanceToArrived;
Direction _currentDirection = Direction.left;
Vector2 _originPosition = Vector2.zero();

double _lastMinDistance = 0;
double _travelledDistance = 0;

// Area where the random movement will be made
ShapeHitbox? randomMovementArea;

/// Method that bo used in [update] method.
void runRandomMovement(
double dt, {
Expand All @@ -68,15 +71,26 @@ mixin RandomMovement on Movement {
_onStartMove = onStartMove;
_onStopMove = onStopMove;

if (distanceToArrived == null) {
if (_distanceToArrived == null) {
if (checkInterval(_KEY_INTERVAL_KEEP_STOPPED, timeKeepStopped, dt)) {
final diffDistane = maxDistance - minDistance;
distanceToArrived = minDistance + _random.nextDouble() * diffDistane;
final randomInt = _random.nextInt(directions.length);
_currentDirection = directions.values[randomInt];
_distanceToArrived = _getDistance(minDistance, maxDistance);
_currentDirection = _getDirection(directions);
_originPosition = absoluteCenter.clone();
if (randomMovementArea != null) {
final targetPosition = _getTargetPosition(
_currentDirection,
_distanceToArrived,
);
final insideArea = randomMovementArea!.containsLocalPoint(
targetPosition,
);
if (!insideArea) {
_stop();
return;
}
}
if (checkDirectionWithRayCast) {
if (!canMove(_currentDirection, displacement: distanceToArrived)) {
if (!canMove(_currentDirection, displacement: _distanceToArrived)) {
_stop();
return;
}
Expand All @@ -85,7 +99,7 @@ mixin RandomMovement on Movement {
}
} else {
_travelledDistance = absoluteCenter.distanceTo(_originPosition);
if (_travelledDistance >= distanceToArrived!) {
if (_travelledDistance >= _distanceToArrived!) {
_stop();
return;
}
Expand Down Expand Up @@ -121,7 +135,7 @@ mixin RandomMovement on Movement {
}
_onStopMove = null;
_onStartMove = null;
distanceToArrived = null;
_distanceToArrived = null;
_originPosition = Vector2.zero();
stopMove();
}
Expand All @@ -131,4 +145,19 @@ mixin RandomMovement on Movement {
_random = Random(Random().nextInt(1000));
super.onMount();
}

double? _getDistance(double minDistance, double maxDistance) {
final diffDistane = maxDistance - minDistance;
return minDistance + _random.nextDouble() * diffDistane;
}

Direction _getDirection(RandomMovementDirections directions) {
final randomInt = _random.nextInt(directions.length);
return directions.values[randomInt];
}

Vector2 _getTargetPosition(
Direction currentDirection, double? distanceToArrived) {
return absoluteCenter + currentDirection.toVector2() * distanceToArrived!;
}
}
41 changes: 22 additions & 19 deletions lib/mixins/shader/shader_setter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,44 +32,47 @@ class ShaderSetter {
final List<ShaderSetterItem> values;
final int startFloatIndex;
final int startImageIndex;

int _indexFloat = 0;
int _indexImage = 0;
ShaderSetter({
required this.values,
this.startFloatIndex = 3,
this.startImageIndex = 1,
});

void apply(FragmentShader shader) {
int indexFloat = startFloatIndex;
int indexImage = startImageIndex;
_indexFloat = startFloatIndex;
_indexImage = startImageIndex;
for (var item in values) {
if (item is SetterDouble) {
shader.setFloat(indexFloat, item.value);
indexFloat++;
_setFloat(shader, item.value);
}
if (item is SetterImage) {
shader.setImageSampler(indexImage, item.value);
indexImage++;
_setSampler(shader, item.value);
}

if (item is SetterVector2) {
shader.setFloat(indexFloat, item.value.x);
indexFloat++;
shader.setFloat(indexFloat, item.value.y);
indexFloat++;
_setFloat(shader, item.value.x);
_setFloat(shader, item.value.y);
}

if (item is SetterColor) {
final color = item.value;
shader.setFloat(indexFloat, color.red / 255 * color.opacity);
indexFloat++;
shader.setFloat(indexFloat, color.green / 255 * color.opacity);
indexFloat++;
shader.setFloat(indexFloat, color.blue / 255 * color.opacity);
indexFloat++;
shader.setFloat(indexFloat, color.opacity);
indexFloat++;
_setFloat(shader, color.red / 255 * color.opacity);
_setFloat(shader, color.green / 255 * color.opacity);
_setFloat(shader, color.blue / 255 * color.opacity);
_setFloat(shader, color.opacity);
}
}
}

void _setFloat(FragmentShader shader, double value) {
shader.setFloat(_indexFloat, value);
_indexFloat++;
}

void _setSampler(FragmentShader shader, Image value) {
shader.setImageSampler(_indexImage, value);
_indexImage++;
}
}
48 changes: 48 additions & 0 deletions lib/mixins/shader/shader_util.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import 'dart:ui' as ui;

import 'package:bonfire/bonfire.dart';

abstract class ShaderUtils {
static ui.Image renderShader({
required ui.FragmentShader? shader,
required ui.Canvas canvas,
required Function(ui.Canvas canvas) record,
required Vector2 size,
required ui.Paint paint,
double shaderCanvasScale = 1.0,
bool shaderComponentStatic = false,
ui.Image? snapshot,
}) {
{
ui.Image? innerSnapshot = snapshot;
ui.PictureRecorder recorder = ui.PictureRecorder();
ui.Canvas canvasRecorder = ui.Canvas(recorder);
canvasRecorder.scale(shaderCanvasScale);
record(canvasRecorder);

if (shaderComponentStatic) {
if (innerSnapshot == null) {
innerSnapshot = recorder.endRecording().toImageSync(
(size.x * shaderCanvasScale).floor(),
(size.y * shaderCanvasScale).floor(),
);
shader!.setImageSampler(0, innerSnapshot);
}
} else {
innerSnapshot = recorder.endRecording().toImageSync(
(size.x * shaderCanvasScale).floor(),
(size.y * shaderCanvasScale).floor(),
);
shader!.setImageSampler(0, innerSnapshot);
}

paint.shader = shader;

canvas.drawRect(
ui.Rect.fromLTWH(0, 0, size.x, size.y),
paint,
);
return innerSnapshot;
}
}
}
Loading

0 comments on commit bffb05a

Please sign in to comment.