Commit b06190fb authored by Viktor Lidholt's avatar Viktor Lidholt

Adds a SpriteWidget and simplifies sample game setup

R=abarth@chromium.org

Review URL: https://codereview.chromium.org/1197493002.
parent 7c438ac2
...@@ -5,7 +5,5 @@ import 'dart:math' as Math; ...@@ -5,7 +5,5 @@ import 'dart:math' as Math;
import 'package:vector_math/vector_math_64.dart'; import 'package:vector_math/vector_math_64.dart';
import 'sprites.dart'; import 'sprites.dart';
import 'package:box2d/box2d.dart'; import 'package:box2d/box2d.dart';
import 'package:sky/rendering/box.dart';
part 'game_demo_world.dart'; part 'game_demo_world.dart';
part 'game_demo_box.dart';
part of game;
const double _steeringThreshold = 0.0;
const double _steeringMax = 150.0;
class GameDemoBox extends SpriteBox {
GameDemoBox(GameDemoWorld game) : super(game, SpriteBoxTransformMode.letterbox);
GameDemoWorld get _gameWorld => this.rootNode;
// Handle pointers
int _firstPointer = -1;
int _secondPointer = -1;
Point _firstPointerDownPos;
// void handleEvent(Event event, BoxHitTestEntry entry) {
// if (event is PointerEvent) {
// Point pointerPos = new Point(event.x, event.y);
// int pointer = event.pointer;
//
// switch (event.type) {
// case 'pointerdown':
// if (_firstPointer == -1) {
// // Assign the first pointer
// _firstPointer = pointer;
// _firstPointerDownPos = pointerPos;
// }
// else if (_secondPointer == -1) {
// // Assign second pointer
// _secondPointer = pointer;
// _gameWorld.controlFire();
// }
// else {
// // There is a pointer used for steering, let's fire instead
// _gameWorld.controlFire();
// }
// break;
// case 'pointermove':
// if (pointer == _firstPointer) {
// // Handle turning control
// double joystickX = 0.0;
// double deltaX = pointerPos.x - _firstPointerDownPos.x;
// if (deltaX > _steeringThreshold || deltaX < -_steeringThreshold) {
// joystickX = (deltaX - _steeringThreshold)/(_steeringMax - _steeringThreshold);
// if (joystickX > 1.0) joystickX = 1.0;
// if (joystickX < -1.0) joystickX = -1.0;
// }
//
// double joystickY = 0.0;
// double deltaY = pointerPos.y - _firstPointerDownPos.y;
// if (deltaY > _steeringThreshold || deltaY < -_steeringThreshold) {
// joystickY = (deltaY - _steeringThreshold)/(_steeringMax - _steeringThreshold);
// if (joystickY > 1.0) joystickY = 1.0;
// if (joystickY < -1.0) joystickY = -1.0;
// }
//
// _gameWorld.controlSteering(joystickX, joystickY);
// }
// break;
// case 'pointerup':
// case 'pointercancel':
// if (pointer == _firstPointer) {
// // Un-assign the first pointer
// _firstPointer = -1;
// _firstPointerDownPos = null;
// _gameWorld.controlSteering(0.0, 0.0);
// }
// else if (pointer == _secondPointer) {
// _secondPointer = -1;
// }
// break;
// default:
// break;
// }
// }
// }
}
part of game; part of game;
const double _steeringThreshold = 0.0;
const double _steeringMax = 150.0;
// Random generator // Random generator
Math.Random _rand = new Math.Random(); Math.Random _rand = new Math.Random();
...@@ -357,7 +360,7 @@ class Laser extends Sprite { ...@@ -357,7 +360,7 @@ class Laser extends Sprite {
_movementVector = new Point(_movementVector.x + ship._movementVector[0], _movementVector.y + ship._movementVector[1]); _movementVector = new Point(_movementVector.x + ship._movementVector[0], _movementVector.y + ship._movementVector[1]);
} }
bool move() { void move() {
position = pointAdd(position, _movementVector); position = pointAdd(position, _movementVector);
_frameCount++; _frameCount++;
} }
......
...@@ -14,7 +14,7 @@ class SpriteBox extends RenderBox { ...@@ -14,7 +14,7 @@ class SpriteBox extends RenderBox {
// Member variables // Member variables
// Root node for drawing // Root node for drawing
Node _rootNode; NodeWithSize _rootNode;
// Tracking of frame rate and updates // Tracking of frame rate and updates
double _lastTimeStamp; double _lastTimeStamp;
...@@ -23,8 +23,8 @@ class SpriteBox extends RenderBox { ...@@ -23,8 +23,8 @@ class SpriteBox extends RenderBox {
// Transformation mode // Transformation mode
SpriteBoxTransformMode transformMode; SpriteBoxTransformMode transformMode;
double _systemWidth; // double _systemWidth;
double _systemHeight; // double _systemHeight;
// Cached transformation matrix // Cached transformation matrix
Matrix4 _transformMatrix; Matrix4 _transformMatrix;
...@@ -33,7 +33,7 @@ class SpriteBox extends RenderBox { ...@@ -33,7 +33,7 @@ class SpriteBox extends RenderBox {
// Setup // Setup
SpriteBox(Node rootNode, [SpriteBoxTransformMode mode = SpriteBoxTransformMode.nativePoints, double width=1024.0, double height=1024.0]) { SpriteBox(NodeWithSize rootNode, [SpriteBoxTransformMode mode = SpriteBoxTransformMode.nativePoints]) {
assert(rootNode != null); assert(rootNode != null);
assert(rootNode._spriteBox == null); assert(rootNode._spriteBox == null);
...@@ -45,8 +45,8 @@ class SpriteBox extends RenderBox { ...@@ -45,8 +45,8 @@ class SpriteBox extends RenderBox {
// Setup transform mode // Setup transform mode
transformMode = mode; transformMode = mode;
_systemWidth = width; // _systemWidth = rootNode.size.width;
_systemHeight = height; // _systemHeight = rootNode.size.height;
_scheduleTick(); _scheduleTick();
} }
...@@ -60,10 +60,10 @@ class SpriteBox extends RenderBox { ...@@ -60,10 +60,10 @@ class SpriteBox extends RenderBox {
// Properties // Properties
double get systemWidth => _systemWidth; double get systemWidth => rootNode.size.width;
double get systemHeight => _systemHeight; double get systemHeight => rootNode.size.height;
Node get rootNode => _rootNode; NodeWithSize get rootNode => _rootNode;
void performLayout() { void performLayout() {
size = constraints.constrain(Size.infinite); size = constraints.constrain(Size.infinite);
...@@ -152,44 +152,49 @@ class SpriteBox extends RenderBox { ...@@ -152,44 +152,49 @@ class SpriteBox extends RenderBox {
double offsetX = 0.0; double offsetX = 0.0;
double offsetY = 0.0; double offsetY = 0.0;
double systemWidth = rootNode.size.width;
double systemHeight = rootNode.size.height;
switch(transformMode) { switch(transformMode) {
case SpriteBoxTransformMode.stretch: case SpriteBoxTransformMode.stretch:
scaleX = size.width/_systemWidth; scaleX = size.width/systemWidth;
scaleY = size.height/_systemHeight; scaleY = size.height/systemHeight;
break; break;
case SpriteBoxTransformMode.letterbox: case SpriteBoxTransformMode.letterbox:
scaleX = size.width/_systemWidth; scaleX = size.width/systemWidth;
scaleY = size.height/_systemHeight; scaleY = size.height/systemHeight;
if (scaleX > scaleY) { if (scaleX > scaleY) {
scaleY = scaleX; scaleY = scaleX;
offsetY = (size.height - scaleY * _systemHeight)/2.0; offsetY = (size.height - scaleY * systemHeight)/2.0;
} }
else { else {
scaleX = scaleY; scaleX = scaleY;
offsetX = (size.width - scaleX * _systemWidth)/2.0; offsetX = (size.width - scaleX * systemWidth)/2.0;
} }
break; break;
case SpriteBoxTransformMode.scaleToFit: case SpriteBoxTransformMode.scaleToFit:
scaleX = size.width/_systemWidth; scaleX = size.width/systemWidth;
scaleY = size.height/_systemHeight; scaleY = size.height/systemHeight;
if (scaleX < scaleY) { if (scaleX < scaleY) {
scaleY = scaleX; scaleY = scaleX;
offsetY = (size.height - scaleY * _systemHeight)/2.0; offsetY = (size.height - scaleY * systemHeight)/2.0;
} }
else { else {
scaleX = scaleY; scaleX = scaleY;
offsetX = (size.width - scaleX * _systemWidth)/2.0; offsetX = (size.width - scaleX * systemWidth)/2.0;
} }
break; break;
case SpriteBoxTransformMode.fixedWidth: case SpriteBoxTransformMode.fixedWidth:
scaleX = size.width/_systemWidth; scaleX = size.width/systemWidth;
scaleY = scaleX; scaleY = scaleX;
_systemHeight = size.height/scaleX; systemHeight = size.height/scaleX;
rootNode.size = new Size(systemWidth, systemHeight);
break; break;
case SpriteBoxTransformMode.fixedHeight: case SpriteBoxTransformMode.fixedHeight:
scaleY = size.height/_systemHeight; scaleY = size.height/systemHeight;
scaleX = scaleY; scaleX = scaleY;
_systemWidth = size.width/scaleY; systemWidth = size.width/scaleY;
rootNode.size = new Size(systemWidth, systemHeight);
break; break;
case SpriteBoxTransformMode.nativePoints: case SpriteBoxTransformMode.nativePoints:
break; break;
......
part of sprites;
class SpriteWidget extends OneChildRenderObjectWrapper {
final NodeWithSize rootNode;
final SpriteBoxTransformMode transformMode;
SpriteWidget(this.rootNode, [this.transformMode = SpriteBoxTransformMode.letterbox]);
SpriteBox get root => super.root;
SpriteBox createNode() => new SpriteBox(rootNode, transformMode);
void syncRenderObject(SpriteWidget old) {
super.syncRenderObject(old);
// SpriteBox doesn't allow mutation of these properties
assert(rootNode == root.rootNode);
assert(transformMode == root.transformMode);
}
}
\ No newline at end of file
...@@ -8,9 +8,11 @@ import 'package:sky/base/scheduler.dart' as scheduler; ...@@ -8,9 +8,11 @@ import 'package:sky/base/scheduler.dart' as scheduler;
import 'package:sky/mojo/net/image_cache.dart' as image_cache; import 'package:sky/mojo/net/image_cache.dart' as image_cache;
import 'package:sky/rendering/box.dart'; import 'package:sky/rendering/box.dart';
import 'package:sky/rendering/object.dart'; import 'package:sky/rendering/object.dart';
import 'package:sky/widgets/widget.dart';
import 'package:vector_math/vector_math.dart'; import 'package:vector_math/vector_math.dart';
part 'sprite_box.dart'; part 'sprite_box.dart';
part 'sprite_widget.dart';
part 'node.dart'; part 'node.dart';
part 'node_with_size.dart'; part 'node_with_size.dart';
part 'sprite.dart'; part 'sprite.dart';
......
...@@ -29,7 +29,7 @@ class GameDemoApp extends App { ...@@ -29,7 +29,7 @@ class GameDemoApp extends App {
Widget build() { Widget build() {
return new Stack([ return new Stack([
new GameDemo(), new SpriteWidget(new GameDemoWorld(_loader)),
// new StackPositionedChild( // new StackPositionedChild(
// new Flex([ // new Flex([
// new FlexExpandingChild( // new FlexExpandingChild(
...@@ -49,12 +49,3 @@ class GameDemoApp extends App { ...@@ -49,12 +49,3 @@ class GameDemoApp extends App {
} }
ImageMap _loader; ImageMap _loader;
class GameDemo extends OneChildRenderObjectWrapper {
GameDemo({ Widget child, Object key })
: super(child: child, key: key);
GameDemoBox get root { return super.root; }
GameDemoBox createNode() => new GameDemoBox(new GameDemoWorld(_loader));
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment