Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Fixes issues with invalidating matrix for SpriteBox & adds HUD to demo game #202

Merged
merged 4 commits into from
Jul 22, 2015
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 51 additions & 1 deletion sky/sdk/example/game/lib/game_demo_world.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class GameDemoWorld extends NodeWithSize {
sky.Image _imgNebula;

SpriteSheet _spriteSheet;
SpriteSheet _spriteSheetUI;
Navigator _navigator;

// Inputs
Expand All @@ -46,8 +47,10 @@ class GameDemoWorld extends NodeWithSize {
int _numFrames = 0;
bool _isGameOver = false;

GameDemoWorld(App app, this._navigator, ImageMap images, this._spriteSheet) : super(new Size(_gameSizeWidth, _gameSizeHeight)) {
// Heads up display
Hud _hud;

GameDemoWorld(App app, this._navigator, ImageMap images, this._spriteSheet, this._spriteSheetUI) : super(new Size(_gameSizeWidth, _gameSizeHeight)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

constructors should come first, then fields

// Fetch images
_imgNebula = images["assets/nebula.png"];

Expand Down Expand Up @@ -81,6 +84,10 @@ class GameDemoWorld extends NodeWithSize {

userInteractionEnabled = true;
handleMultiplePointers = true;

_hud = new Hud(_spriteSheetUI);
_hud.zPosition = 1000.0;
addChild(_hud);
}

// Methods for adding game objects
Expand Down Expand Up @@ -585,6 +592,49 @@ class StarField extends Node {
}
}

class Hud extends NodeWithSize {
SpriteSheet spriteSheetUI;
Sprite sprtBgScore;
Sprite sprtBgShield;

int _score = 0;

int get score => _score;

set score(int score) {
_score = score;
_updateHud();
}

Hud(this.spriteSheetUI) {
pivot = Point.origin;

sprtBgScore = new Sprite(spriteSheetUI["scoreboard.png"]);
sprtBgScore.pivot = Point.origin;
sprtBgScore.scale = 0.6;
addChild(sprtBgScore);

sprtBgShield = new Sprite(spriteSheetUI["bar_shield.png"]);
sprtBgShield.pivot = new Point(1.0, 0.0);
sprtBgShield.scale = 0.6;
addChild(sprtBgShield);
}

void spriteBoxPerformedLayout() {
// Set the size and position of HUD display
position = spriteBox.visibleArea.topLeft;
size = spriteBox.visibleArea.size;

// Position hud objects
sprtBgScore.position = new Point(20.0, 20.0);
sprtBgShield.position = new Point(size.width - 20.0, 20.0);
}

void _updateHud() {

}
}

class Nebula extends Node {

Nebula.withImage(sky.Image img) {
Expand Down
25 changes: 19 additions & 6 deletions sky/sdk/example/game/lib/sprite_box.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class SpriteBox extends RenderBox {

// Add new references
_addSpriteBoxReference(_rootNode);
markNeedsLayout();
}

// Tracking of frame rate and updates
Expand All @@ -55,7 +56,7 @@ class SpriteBox extends RenderBox {
_transformMode = value;

// Invalidate stuff
if (attached) performLayout();
markNeedsLayout();
}

/// The transform mode used by the [SpriteBox].
Expand All @@ -68,7 +69,10 @@ class SpriteBox extends RenderBox {

Rect _visibleArea;

Rect get visibleArea => _visibleArea;
Rect get visibleArea {
if (_visibleArea == null) _calcTransformMatrix();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

split if statement onto two lines

return _visibleArea;
}

// Setup

Expand Down Expand Up @@ -150,6 +154,8 @@ class SpriteBox extends RenderBox {
}

void handleEvent(Event event, _SpriteBoxHitTestEntry entry) {
if (!attached) return;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please put if statement bodies, especially control flow statements like 'return', clearly on their own line so that they are obvious when scanning the code.


if (event is PointerEvent) {

if (event.type == 'pointerdown') {
Expand Down Expand Up @@ -212,10 +218,13 @@ class SpriteBox extends RenderBox {
/// var matrix = mySpriteBox.transformMatrix;
Matrix4 get transformMatrix {
// Get cached matrix if available
if (_transformMatrix != null) {
return _transformMatrix;
if (_transformMatrix == null) {
_calcTransformMatrix();
}
return _transformMatrix;
}

void _calcTransformMatrix() {
_transformMatrix = new Matrix4.identity();

// Calculate matrix
Expand Down Expand Up @@ -273,13 +282,17 @@ class SpriteBox extends RenderBox {
break;
}

_visibleArea = new Rect.fromLTRB(-offsetX / scaleX,
-offsetY / scaleY,
systemWidth + offsetX / scaleX,
systemHeight + offsetY / scaleY);

_transformMatrix.translate(offsetX, offsetY);
_transformMatrix.scale(scaleX, scaleY);

return _transformMatrix;
}

void _invalidateTransformMatrix() {
_visibleArea = null;
_transformMatrix = null;
_rootNode._invalidateToBoxTransformMatrix();
}
Expand Down
11 changes: 7 additions & 4 deletions sky/sdk/example/game/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ main() async {
class GameDemoApp extends App {

NavigationState _navigationState;
GameDemoWorld _game;

void initState() {
_navigationState = new NavigationState([
Expand Down Expand Up @@ -84,16 +85,18 @@ class GameDemoApp extends App {
}

Widget _buildGameScene(navigator, route) {
return new SpriteWidget(
new GameDemoWorld(_app, navigator, _loader, _spriteSheet)
);
if (_game == null) _game = new GameDemoWorld(_app, navigator, _loader, _spriteSheet);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please split if statements over two lines

return new SpriteWidget(_game);
}

Widget _buildMainScene(navigator, route) {
return new Center(
child: new RaisedButton(
child: new Text("Play"),
onPressed: () => navigator.pushNamed('/game')
onPressed: () {
_game = new GameDemoWorld(_app, navigator, _loader, _spriteSheet, _spriteSheetUI);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't this redundant with _buildGameScene?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, generally, build functions should be stateless and idempotent, and state setting should happen in event callbacks within setState() blocks. Might be worth refactoring this a bit.

navigator.pushNamed('/game');
}
)
);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resetGame seems to be dead code (nothing calls it).

Expand Down