mirror of
https://github.com/flame-engine/flame.git
synced 2025-11-02 20:13:50 +08:00
resize methods
This commit is contained in:
@ -54,8 +54,10 @@ abstract class Box2DComponent extends Component {
|
|||||||
|
|
||||||
void initializeWorld();
|
void initializeWorld();
|
||||||
|
|
||||||
void cameraFollow(BodyComponent component, {double horizontal, double vertical}) {
|
void cameraFollow(BodyComponent component,
|
||||||
viewport.cameraFollow(component, horizontal: horizontal, vertical: vertical);
|
{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 path = new Path()..addPolygon(points, true);
|
||||||
final Paint paint = new Paint()
|
final Paint paint = new Paint()
|
||||||
..color = new Color.fromARGB(255, 255, 255, 255);
|
..color = new Color.fromARGB(255, 255, 255, 255);
|
||||||
// ..style = PaintingStyle.stroke;
|
|
||||||
canvas.drawPath(path, paint);
|
canvas.drawPath(path, paint);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,16 +4,23 @@ import 'package:box2d/box2d.dart';
|
|||||||
import 'package:flame/box2d/box2d_component.dart';
|
import 'package:flame/box2d/box2d_component.dart';
|
||||||
|
|
||||||
class Viewport extends ViewportTransform {
|
class Viewport extends ViewportTransform {
|
||||||
Size dimensions;
|
Size size;
|
||||||
|
|
||||||
double scale;
|
double scale;
|
||||||
|
|
||||||
Viewport(this.dimensions, this.scale)
|
Viewport(this.size, this.scale)
|
||||||
: super(new Vector2(dimensions.width / 2, dimensions.height / 2),
|
: super(new Vector2(size.width / 2, size.height / 2),
|
||||||
new Vector2(dimensions.width / 2, dimensions.height / 2), scale);
|
new Vector2(size.width / 2, size.height / 2), scale);
|
||||||
|
|
||||||
double worldAlignBottom(double height) =>
|
double worldAlignBottom(double height) => -(size.height / 2 / scale) + height;
|
||||||
-(dimensions.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
|
* 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
|
* @param percent percetage of the width in [0, 1] range
|
||||||
*/
|
*/
|
||||||
double worldWidth(double percent) {
|
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]
|
* @return the percentage in the range of [0, 1]
|
||||||
*/
|
*/
|
||||||
double getCenterHorizontalScreenPercentage({double screens: 1.0}) {
|
double getCenterHorizontalScreenPercentage({double screens: 1.0}) {
|
||||||
var width = dimensions.width * screens;
|
var width = size.width * screens;
|
||||||
// print("width: $width");
|
var x = center.x + ((screens - 1) * size.width / 2);
|
||||||
var x = center.x + ((screens - 1) * dimensions.width / 2);
|
|
||||||
double rest = x.abs() % width;
|
double rest = x.abs() % width;
|
||||||
double scroll = rest / width;
|
double scroll = rest / width;
|
||||||
return x > 0 ? scroll : 1 - scroll;
|
return x > 0 ? scroll : 1 - scroll;
|
||||||
@ -61,11 +67,11 @@ class Viewport extends ViewportTransform {
|
|||||||
Vector2 temp = new Vector2.zero();
|
Vector2 temp = new Vector2.zero();
|
||||||
getWorldToScreen(position, temp);
|
getWorldToScreen(position, temp);
|
||||||
|
|
||||||
var margin = horizontal / 2 * dimensions.width / 2;
|
var margin = horizontal / 2 * size.width / 2;
|
||||||
var focus = dimensions.width / 2 - temp.x;
|
var focus = size.width / 2 - temp.x;
|
||||||
|
|
||||||
if (focus.abs() > margin) {
|
if (focus.abs() > margin) {
|
||||||
x = dimensions.width / 2 +
|
x = size.width / 2 +
|
||||||
(position.x * scale) +
|
(position.x * scale) +
|
||||||
(focus > 0 ? margin : -margin);
|
(focus > 0 ? margin : -margin);
|
||||||
}
|
}
|
||||||
@ -75,11 +81,11 @@ class Viewport extends ViewportTransform {
|
|||||||
Vector2 temp = new Vector2.zero();
|
Vector2 temp = new Vector2.zero();
|
||||||
getWorldToScreen(position, temp);
|
getWorldToScreen(position, temp);
|
||||||
|
|
||||||
var margin = vertical / 2 * dimensions.height / 2;
|
var margin = vertical / 2 * size.height / 2;
|
||||||
var focus = dimensions.height / 2 - temp.y;
|
var focus = size.height / 2 - temp.y;
|
||||||
|
|
||||||
if (focus.abs() > margin) {
|
if (focus.abs() > margin) {
|
||||||
y = dimensions.height / 2 +
|
y = size.height / 2 +
|
||||||
(position.y * scale) +
|
(position.y * scale) +
|
||||||
(focus < 0 ? margin : -margin);
|
(focus < 0 ? margin : -margin);
|
||||||
}
|
}
|
||||||
@ -89,5 +95,4 @@ class Viewport extends ViewportTransform {
|
|||||||
setCamera(x, y, scale);
|
setCamera(x, y, scale);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -9,6 +9,8 @@ abstract class Component {
|
|||||||
|
|
||||||
void render(Canvas c);
|
void render(Canvas c);
|
||||||
|
|
||||||
|
void resize(Size size) {}
|
||||||
|
|
||||||
bool loaded() {
|
bool loaded() {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -41,7 +43,8 @@ class SpriteComponent extends PositionComponent {
|
|||||||
|
|
||||||
final Paint paint = new Paint()..color = new Color(0xffffffff);
|
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) {
|
SpriteComponent.rectangle(this.width, this.height, String imagePath) {
|
||||||
this.sprite = new Sprite(imagePath);
|
this.sprite = new Sprite(imagePath);
|
||||||
@ -63,7 +66,5 @@ class SpriteComponent extends PositionComponent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void update(double t) {
|
void update(double t) {}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,11 +1,11 @@
|
|||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'dart:ui';
|
import 'dart:ui';
|
||||||
|
|
||||||
import 'component.dart';
|
|
||||||
import '../flame.dart';
|
|
||||||
|
|
||||||
import 'package:flutter/src/painting/images.dart';
|
import 'package:flutter/src/painting/images.dart';
|
||||||
|
|
||||||
|
import '../flame.dart';
|
||||||
|
import 'component.dart';
|
||||||
|
|
||||||
class ParallaxRenderer {
|
class ParallaxRenderer {
|
||||||
String filename;
|
String filename;
|
||||||
Future future;
|
Future future;
|
||||||
@ -23,7 +23,7 @@ class ParallaxRenderer {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
bool get loaded => image != null;
|
bool loaded() => image != null;
|
||||||
|
|
||||||
void render(Canvas canvas, Rect rect) {
|
void render(Canvas canvas, Rect rect) {
|
||||||
if (image == null) {
|
if (image == null) {
|
||||||
@ -50,11 +50,11 @@ class ParallaxComponent extends PositionComponent {
|
|||||||
final BASE_SPEED = 30;
|
final BASE_SPEED = 30;
|
||||||
final LAYER_DELTA = 40;
|
final LAYER_DELTA = 40;
|
||||||
|
|
||||||
List<ParallaxRenderer> layers = new List();
|
List<ParallaxRenderer> _layers = new List();
|
||||||
Size size;
|
Size _size;
|
||||||
bool _loaded = false;
|
bool _loaded = false;
|
||||||
|
|
||||||
ParallaxComponent(this.size);
|
ParallaxComponent(this._size);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Loads the images defined by this list of filenames. All images
|
* Loads the images defined by this list of filenames. All images
|
||||||
@ -64,9 +64,9 @@ class ParallaxComponent extends PositionComponent {
|
|||||||
*/
|
*/
|
||||||
void load(List<String> filenames) {
|
void load(List<String> filenames) {
|
||||||
var futures =
|
var futures =
|
||||||
filenames.fold(new List<Future>(), (List<Future> result, filename) {
|
filenames.fold(new List<Future>(), (List<Future> result, filename) {
|
||||||
var layer = new ParallaxRenderer(filename);
|
var layer = new ParallaxRenderer(filename);
|
||||||
layers.add(layer);
|
_layers.add(layer);
|
||||||
result.add(layer.future);
|
result.add(layer.future);
|
||||||
return result;
|
return result;
|
||||||
});
|
});
|
||||||
@ -76,7 +76,7 @@ class ParallaxComponent extends PositionComponent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void updateScroll(int layerIndex, scroll) {
|
void updateScroll(int layerIndex, scroll) {
|
||||||
layers[layerIndex].scroll = scroll;
|
_layers[layerIndex].scroll = scroll;
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@ -98,8 +98,8 @@ class ParallaxComponent extends PositionComponent {
|
|||||||
|
|
||||||
void _drawLayers(Canvas canvas) {
|
void _drawLayers(Canvas canvas) {
|
||||||
Rect rect = new Rect.fromPoints(
|
Rect rect = new Rect.fromPoints(
|
||||||
new Offset(0.0, 0.0), new Offset(size.width, size.height));
|
new Offset(0.0, 0.0), new Offset(_size.width, _size.height));
|
||||||
layers.forEach((layer) {
|
_layers.forEach((layer) {
|
||||||
layer.render(canvas, rect);
|
layer.render(canvas, rect);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -109,13 +109,13 @@ class ParallaxComponent extends PositionComponent {
|
|||||||
if (!this.loaded()) {
|
if (!this.loaded()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for (var i = 0; i < layers.length; i++) {
|
for (var i = 0; i < _layers.length; i++) {
|
||||||
var scroll = layers[i].scroll;
|
var scroll = _layers[i].scroll;
|
||||||
scroll += (BASE_SPEED + i * LAYER_DELTA) * delta / size.width;
|
scroll += (BASE_SPEED + i * LAYER_DELTA) * delta / _size.width;
|
||||||
if (scroll > 1) {
|
if (scroll > 1) {
|
||||||
scroll = scroll % 1;
|
scroll = scroll % 1;
|
||||||
}
|
}
|
||||||
layers[i].scroll = scroll;
|
_layers[i].scroll = scroll;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -12,6 +12,8 @@ abstract class Game {
|
|||||||
|
|
||||||
void render(Canvas canvas);
|
void render(Canvas canvas);
|
||||||
|
|
||||||
|
void resize(Size size);
|
||||||
|
|
||||||
Widget _widget;
|
Widget _widget;
|
||||||
|
|
||||||
Widget get widget {
|
Widget get widget {
|
||||||
@ -54,6 +56,12 @@ class GameRenderBox extends RenderBox {
|
|||||||
// TODO: notify game?
|
// TODO: notify game?
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void performResize() {
|
||||||
|
super.performResize();
|
||||||
|
game.resize(constraints.biggest);
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void attach(PipelineOwner owner) {
|
void attach(PipelineOwner owner) {
|
||||||
super.attach(owner);
|
super.attach(owner);
|
||||||
|
|||||||
Reference in New Issue
Block a user