Commit acccd438 authored by Viktor Lidholt's avatar Viktor Lidholt

Merge pull request #785 from vlidholt/new_game

New demo game
parents 97b432db 840dfae9
part of sprites;
Point _cardinalSplineAt(Point p0, Point p1, Point p2, Point p3, double tension, double t) {
double t2 = t * t;
double t3 = t2 * t;
double s = (1.0 - tension) / 2.0;
double b1 = s * ((-t3 + (2.0 * t2)) - t);
double b2 = s * (-t3 + t2) + (2.0 * t3 - 3.0 * t2 + 1.0);
double b3 = s * (t3 - 2.0 * t2 + t) + (-2.0 * t3 + 3.0 * t2);
double b4 = s * (t3 - t2);
double x = p0.x * b1 + p1.x * b2 + p2.x * b3 + p3.x * b4;
double y = p0.y * b1 + p1.y * b2 + p2.y * b3 + p3.y * b4;
return new Point(x, y);
}
class ActionSpline extends ActionInterval {
ActionSpline(this.setter, this.points, double duration, [Curve curve]) : super(duration, curve) {
_dt = 1.0 / (points.length - 1.0);
}
final Function setter;
final List<Point> points;
double _dt;
void update(double t) {
double tension = 0.5;
int p;
double lt;
if (t < 0.0) t = 0.0;
if (t >= 1.0) {
p = points.length - 1;
lt = 1.0;
} else {
p = (t / _dt).floor();
lt = (t - _dt * p) / _dt;
}
Point p0 = points[(p - 1).clamp(0, points.length - 1)];
Point p1 = points[(p + 0).clamp(0, points.length - 1)];
Point p2 = points[(p + 1).clamp(0, points.length - 1)];
Point p3 = points[(p + 2).clamp(0, points.length - 1)];
Point newPos = _cardinalSplineAt(p0, p1, p2, p3, tension, lt);
//print("newPos: $newPos");
setter(newPos);
}
}
part of sprites;
abstract class Constraint {
void preUpdate(Node node, double dt) {
}
void constrain(Node node, double dt);
}
double _dampenRotation(double src, double dst, double dampening) {
double delta = dst - src;
while (delta > 180.0) delta -= 360;
while (delta < -180) delta += 360;
delta *= dampening;
return src + delta;
}
class ConstraintRotationToMovement {
ConstraintRotationToMovement([this.dampening]);
final double dampening;
Point _lastPosition;
void preUpdate(Node node, double dt) {
_lastPosition = node.position;
}
void constrain(Node node, double dt) {
assert(_lastPosition != null);
if (_lastPosition == node.position) return;
// Get the target angle
Offset offset = node.position - _lastPosition;
double target = degrees(GameMath.atan2(offset.dy, offset.dx));
if (dampening == null)
node.rotation = target;
else
node.rotation = _dampenRotation(node.rotation, target, dampening);
}
}
library game; library game;
import 'dart:async';
import 'dart:sky' as sky; import 'dart:sky' as sky;
import 'dart:math' as Math; import 'dart:math' as Math;
import 'sprites.dart'; import 'sprites.dart';
...@@ -10,4 +11,4 @@ import 'package:sky/widgets/navigator.dart'; ...@@ -10,4 +11,4 @@ import 'package:sky/widgets/navigator.dart';
import 'package:sky/animation/curves.dart'; import 'package:sky/animation/curves.dart';
import 'package:vector_math/vector_math_64.dart'; import 'package:vector_math/vector_math_64.dart';
part 'game_demo_world.dart'; part 'game_demo_node.dart';
This diff is collapsed.
This diff is collapsed.
...@@ -77,10 +77,19 @@ main() async { ...@@ -77,10 +77,19 @@ main() async {
class GameDemoApp extends App { class GameDemoApp extends App {
NavigationState _navigationState; NavigationState _navigationState;
GameDemoWorld _game; NodeWithSize _game;
int _lastScore = 0; int _lastScore = 0;
void initState() { void initState() {
// _game = new GameDemoNode(
// _imageMap,
// _spriteSheet,
// _spriteSheetUI,
// _sounds,
// (lastScore) {
// setState(() {_lastScore = lastScore;});
// });
_navigationState = new NavigationState([ _navigationState = new NavigationState([
new Route( new Route(
name: '/', name: '/',
...@@ -112,24 +121,23 @@ class GameDemoApp extends App { ...@@ -112,24 +121,23 @@ class GameDemoApp extends App {
} }
Widget _buildGameScene(navigator, route) { Widget _buildGameScene(navigator, route) {
return new SpriteWidget(_game); return new SpriteWidget(_game, SpriteBoxTransformMode.fixedWidth);
} }
Widget _buildMainScene(navigator, route) { Widget _buildMainScene(navigator, route) {
return new Stack([ return new Stack([
new SpriteWidget(new MainScreenBackground()), new SpriteWidget(new MainScreenBackground(), SpriteBoxTransformMode.fixedWidth),
new Flex([ new Flex([
new TextureButton( new TextureButton(
onPressed: () { onPressed: () {
_game = new GameDemoWorld( _game = new GameDemoNode(
_app,
navigator,
_imageMap, _imageMap,
_spriteSheet, _spriteSheet,
_spriteSheetUI, _spriteSheetUI,
_sounds, _sounds,
(lastScore) { (lastScore) {
setState(() {_lastScore = lastScore;}); setState(() {_lastScore = lastScore;});
navigator.pop();
} }
); );
navigator.pushNamed('/game'); navigator.pushNamed('/game');
...@@ -243,14 +251,19 @@ class _TextureButtonToken { ...@@ -243,14 +251,19 @@ class _TextureButtonToken {
} }
class MainScreenBackground extends NodeWithSize { class MainScreenBackground extends NodeWithSize {
MainScreenBackground() : super(new Size(1024.0, 1024.0)) { MainScreenBackground() : super(new Size(320.0, 320.0)) {
Sprite sprtBackground = new Sprite.fromImage(_imageMap['assets/starfield.png']); // Sprite sprtBackground = new Sprite.fromImage(_imageMap['assets/starfield.png']);
sprtBackground.position = new Point(512.0, 512.0); // sprtBackground.position = new Point(160.0, 160.0);
addChild(sprtBackground); // addChild(sprtBackground);
assert(_spriteSheet.image != null); assert(_spriteSheet.image != null);
StarField starField = new StarField(_spriteSheet, 200, true); StarField starField = new StarField(_spriteSheet, 200, true);
addChild(starField); addChild(starField);
} }
void paint(PaintingCanvas canvas) {
canvas.drawRect(new Rect.fromLTWH(0.0, 0.0, 320.0, 320.0), new Paint()..color=new Color(0xff000000));
super.paint(canvas);
}
} }
...@@ -76,6 +76,25 @@ class Node { ...@@ -76,6 +76,25 @@ class Node {
return _actions; return _actions;
} }
List<Constraint> _constraints;
List<Constraint> get constraints {
return _constraints;
}
set constraints(List<Constraint> constraints) {
_constraints = constraints;
if (_spriteBox != null) _spriteBox._constrainedNodes = null;
}
void applyConstraints(double dt) {
if (_constraints == null) return;
for (Constraint constraint in _constraints) {
constraint.constrain(this, dt);
}
}
// Constructors // Constructors
/// Creates a new [Node] without any transformation. /// Creates a new [Node] without any transformation.
......
...@@ -74,6 +74,8 @@ class SpriteBox extends RenderBox { ...@@ -74,6 +74,8 @@ class SpriteBox extends RenderBox {
List<ActionController> _actionControllers; List<ActionController> _actionControllers;
List<Node> _constrainedNodes;
Rect _visibleArea; Rect _visibleArea;
Rect get visibleArea { Rect get visibleArea {
...@@ -139,11 +141,13 @@ class SpriteBox extends RenderBox { ...@@ -139,11 +141,13 @@ class SpriteBox extends RenderBox {
_registerNode(Node node) { _registerNode(Node node) {
_actionControllers = null; _actionControllers = null;
_eventTargets = null; _eventTargets = null;
if (node == null || node.constraints != null) _constrainedNodes = null;
} }
_deregisterNode(Node node) { _deregisterNode(Node node) {
_actionControllers = null; _actionControllers = null;
_eventTargets = null; _eventTargets = null;
if (node == null || node.constraints != null) _constrainedNodes = null;
} }
// Event handling // Event handling
...@@ -353,8 +357,10 @@ class SpriteBox extends RenderBox { ...@@ -353,8 +357,10 @@ class SpriteBox extends RenderBox {
_frameRate = 1.0/delta; _frameRate = 1.0/delta;
_callConstraintsPreUpdate(delta);
_runActions(delta); _runActions(delta);
_callUpdate(_rootNode, delta); _callUpdate(_rootNode, delta);
_callConstraintsConstrain(delta);
// Schedule next update // Schedule next update
_scheduleTick(); _scheduleTick();
...@@ -392,6 +398,42 @@ class SpriteBox extends RenderBox { ...@@ -392,6 +398,42 @@ class SpriteBox extends RenderBox {
} }
} }
void _callConstraintsPreUpdate(double dt) {
if (_constrainedNodes == null) {
_constrainedNodes = [];
_addConstrainedNodes(_rootNode, _constrainedNodes);
}
for (Node node in _constrainedNodes) {
for (Constraint constraint in node.constraints) {
constraint.preUpdate(node, dt);
}
}
}
void _callConstraintsConstrain(double dt) {
if (_constrainedNodes == null) {
_constrainedNodes = [];
_addConstrainedNodes(_rootNode, _constrainedNodes);
}
for (Node node in _constrainedNodes) {
for (Constraint constraint in node.constraints) {
constraint.constrain(node, dt);
}
}
}
void _addConstrainedNodes(Node node, List<Node> nodes) {
if (node._constraints != null && node._constraints.length > 0) {
nodes.add(node);
}
for (Node child in node.children) {
_addConstrainedNodes(child, nodes);
}
}
void _callSpriteBoxPerformedLayout(Node node) { void _callSpriteBoxPerformedLayout(Node node) {
node.spriteBoxPerformedLayout(); node.spriteBoxPerformedLayout();
for (Node child in node.children) { for (Node child in node.children) {
......
...@@ -22,6 +22,8 @@ import 'package:sky_services/media/media.mojom.dart'; ...@@ -22,6 +22,8 @@ import 'package:sky_services/media/media.mojom.dart';
import 'package:vector_math/vector_math.dart'; import 'package:vector_math/vector_math.dart';
part 'action.dart'; part 'action.dart';
part 'constraint.dart';
part 'action_spline.dart';
part 'color_secuence.dart'; part 'color_secuence.dart';
part 'image_map.dart'; part 'image_map.dart';
part 'layer.dart'; part 'layer.dart';
......
...@@ -80,4 +80,21 @@ class GameMath { ...@@ -80,4 +80,21 @@ class GameMath {
} }
} }
} }
static double pointQuickDist(Point a, Point b) {
double dx = a.x - b.x;
double dy = a.y - b.y;
if (dx < 0.0) dx = -dx;
if (dy < 0.0) dy = -dy;
if (dx > dy) {
return dx + dy/2.0;
}
else {
return dy + dx/2.0;
}
}
static double filter (double a, double b, double filterFactor) {
return (a * (1-filterFactor)) + b * filterFactor;
}
} }
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