From b86cf57e1bcbdf596a00c130094ae347a8de8082 Mon Sep 17 00:00:00 2001 From: feroult Date: Mon, 4 Dec 2017 23:15:42 -0200 Subject: [PATCH] resize methods --- lib/box2d/box2d_component.dart | 7 +++-- lib/box2d/viewport.dart | 39 +++++++++++++++----------- lib/components/component.dart | 9 +++--- lib/components/parallax_component.dart | 34 +++++++++++----------- lib/game.dart | 8 ++++++ 5 files changed, 56 insertions(+), 41 deletions(-) diff --git a/lib/box2d/box2d_component.dart b/lib/box2d/box2d_component.dart index 1b8cccde1..a9f76d20a 100644 --- a/lib/box2d/box2d_component.dart +++ b/lib/box2d/box2d_component.dart @@ -54,8 +54,10 @@ abstract class Box2DComponent extends Component { void initializeWorld(); - void cameraFollow(BodyComponent component, {double horizontal, double vertical}) { - viewport.cameraFollow(component, horizontal: horizontal, vertical: vertical); + void cameraFollow(BodyComponent component, + {double horizontal, double vertical}) { + viewport.cameraFollow(component, + horizontal: horizontal, vertical: vertical); } } @@ -139,7 +141,6 @@ abstract class BodyComponent extends Component { final path = new Path()..addPolygon(points, true); final Paint paint = new Paint() ..color = new Color.fromARGB(255, 255, 255, 255); -// ..style = PaintingStyle.stroke; canvas.drawPath(path, paint); } } diff --git a/lib/box2d/viewport.dart b/lib/box2d/viewport.dart index 47ac94a2c..fbce60b9b 100644 --- a/lib/box2d/viewport.dart +++ b/lib/box2d/viewport.dart @@ -4,16 +4,23 @@ import 'package:box2d/box2d.dart'; import 'package:flame/box2d/box2d_component.dart'; class Viewport extends ViewportTransform { - Size dimensions; + Size size; double scale; - Viewport(this.dimensions, this.scale) - : super(new Vector2(dimensions.width / 2, dimensions.height / 2), - new Vector2(dimensions.width / 2, dimensions.height / 2), scale); + Viewport(this.size, this.scale) + : super(new Vector2(size.width / 2, size.height / 2), + new Vector2(size.width / 2, size.height / 2), scale); - double worldAlignBottom(double height) => - -(dimensions.height / 2 / scale) + height; + double worldAlignBottom(double height) => -(size.height / 2 / scale) + height; + + /** + * Resizes the current view port. + */ + void resize(Size size) { + extents = new Vector2.copy(new Vector2(size.width / 2, size.height / 2)); + center = new Vector2.copy(new Vector2(size.width / 2, size.height / 2)); + } /** * Computes the number of horizontal world meters of this viewport considering a @@ -22,7 +29,7 @@ class Viewport extends ViewportTransform { * @param percent percetage of the width in [0, 1] range */ double worldWidth(double percent) { - return percent * (dimensions.width / scale); + return percent * (size.width / scale); } /** @@ -34,9 +41,8 @@ class Viewport extends ViewportTransform { * @return the percentage in the range of [0, 1] */ double getCenterHorizontalScreenPercentage({double screens: 1.0}) { - var width = dimensions.width * screens; -// print("width: $width"); - var x = center.x + ((screens - 1) * dimensions.width / 2); + var width = size.width * screens; + var x = center.x + ((screens - 1) * size.width / 2); double rest = x.abs() % width; double scroll = rest / width; return x > 0 ? scroll : 1 - scroll; @@ -61,11 +67,11 @@ class Viewport extends ViewportTransform { Vector2 temp = new Vector2.zero(); getWorldToScreen(position, temp); - var margin = horizontal / 2 * dimensions.width / 2; - var focus = dimensions.width / 2 - temp.x; + var margin = horizontal / 2 * size.width / 2; + var focus = size.width / 2 - temp.x; if (focus.abs() > margin) { - x = dimensions.width / 2 + + x = size.width / 2 + (position.x * scale) + (focus > 0 ? margin : -margin); } @@ -75,11 +81,11 @@ class Viewport extends ViewportTransform { Vector2 temp = new Vector2.zero(); getWorldToScreen(position, temp); - var margin = vertical / 2 * dimensions.height / 2; - var focus = dimensions.height / 2 - temp.y; + var margin = vertical / 2 * size.height / 2; + var focus = size.height / 2 - temp.y; if (focus.abs() > margin) { - y = dimensions.height / 2 + + y = size.height / 2 + (position.y * scale) + (focus < 0 ? margin : -margin); } @@ -89,5 +95,4 @@ class Viewport extends ViewportTransform { setCamera(x, y, scale); } } - } diff --git a/lib/components/component.dart b/lib/components/component.dart index ab091081c..873b17650 100644 --- a/lib/components/component.dart +++ b/lib/components/component.dart @@ -9,6 +9,8 @@ abstract class Component { void render(Canvas c); + void resize(Size size) {} + bool loaded() { return true; } @@ -41,7 +43,8 @@ class SpriteComponent extends PositionComponent { final Paint paint = new Paint()..color = new Color(0xffffffff); - SpriteComponent.square(double size, String imagePath) : this.rectangle(size, size, imagePath); + SpriteComponent.square(double size, String imagePath) + : this.rectangle(size, size, imagePath); SpriteComponent.rectangle(this.width, this.height, String imagePath) { this.sprite = new Sprite(imagePath); @@ -63,7 +66,5 @@ class SpriteComponent extends PositionComponent { } @override - void update(double t) { - } + void update(double t) {} } - diff --git a/lib/components/parallax_component.dart b/lib/components/parallax_component.dart index 05282596c..c8010e0f8 100644 --- a/lib/components/parallax_component.dart +++ b/lib/components/parallax_component.dart @@ -1,11 +1,11 @@ import 'dart:async'; import 'dart:ui'; -import 'component.dart'; -import '../flame.dart'; - import 'package:flutter/src/painting/images.dart'; +import '../flame.dart'; +import 'component.dart'; + class ParallaxRenderer { String filename; Future future; @@ -23,7 +23,7 @@ class ParallaxRenderer { }); } - bool get loaded => image != null; + bool loaded() => image != null; void render(Canvas canvas, Rect rect) { if (image == null) { @@ -50,11 +50,11 @@ class ParallaxComponent extends PositionComponent { final BASE_SPEED = 30; final LAYER_DELTA = 40; - List layers = new List(); - Size size; + List _layers = new List(); + Size _size; bool _loaded = false; - ParallaxComponent(this.size); + ParallaxComponent(this._size); /** * Loads the images defined by this list of filenames. All images @@ -64,9 +64,9 @@ class ParallaxComponent extends PositionComponent { */ void load(List filenames) { var futures = - filenames.fold(new List(), (List result, filename) { + filenames.fold(new List(), (List result, filename) { var layer = new ParallaxRenderer(filename); - layers.add(layer); + _layers.add(layer); result.add(layer.future); return result; }); @@ -76,7 +76,7 @@ class ParallaxComponent extends PositionComponent { } void updateScroll(int layerIndex, scroll) { - layers[layerIndex].scroll = scroll; + _layers[layerIndex].scroll = scroll; } @override @@ -98,8 +98,8 @@ class ParallaxComponent extends PositionComponent { void _drawLayers(Canvas canvas) { Rect rect = new Rect.fromPoints( - new Offset(0.0, 0.0), new Offset(size.width, size.height)); - layers.forEach((layer) { + new Offset(0.0, 0.0), new Offset(_size.width, _size.height)); + _layers.forEach((layer) { layer.render(canvas, rect); }); } @@ -109,13 +109,13 @@ class ParallaxComponent extends PositionComponent { if (!this.loaded()) { return; } - for (var i = 0; i < layers.length; i++) { - var scroll = layers[i].scroll; - scroll += (BASE_SPEED + i * LAYER_DELTA) * delta / size.width; + for (var i = 0; i < _layers.length; i++) { + var scroll = _layers[i].scroll; + scroll += (BASE_SPEED + i * LAYER_DELTA) * delta / _size.width; if (scroll > 1) { scroll = scroll % 1; } - layers[i].scroll = scroll; + _layers[i].scroll = scroll; } } -} \ No newline at end of file +} diff --git a/lib/game.dart b/lib/game.dart index 714b699ea..8ecafeef1 100644 --- a/lib/game.dart +++ b/lib/game.dart @@ -12,6 +12,8 @@ abstract class Game { void render(Canvas canvas); + void resize(Size size); + Widget _widget; Widget get widget { @@ -54,6 +56,12 @@ class GameRenderBox extends RenderBox { // TODO: notify game? } + @override + void performResize() { + super.performResize(); + game.resize(constraints.biggest); + } + @override void attach(PipelineOwner owner) { super.attach(owner);