Allow Setting Color Change Speed for Variable Timestep
This commit is contained in:
parent
08c7e8ac1d
commit
46150e3c34
3 changed files with 23 additions and 12 deletions
|
@ -21,8 +21,7 @@ class Game {
|
||||||
}
|
}
|
||||||
|
|
||||||
// In-World Logic Updates
|
// In-World Logic Updates
|
||||||
void update([num speed]) {
|
void update([int speed]) {
|
||||||
print(speed);
|
|
||||||
Point next = isRandom ? nextPosRandom() : nextPosOrdered(_curPos);
|
Point next = isRandom ? nextPosRandom() : nextPosOrdered(_curPos);
|
||||||
if (_curPos.x < 0) _curPos = Point(0, _curPos.y);
|
if (_curPos.x < 0) _curPos = Point(0, _curPos.y);
|
||||||
Color newColor = isRandom ? randomColor() : nextColor(
|
Color newColor = isRandom ? randomColor() : nextColor(
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import 'dart:html';
|
import 'dart:html';
|
||||||
|
|
||||||
import 'package:browserloop/game/Game.dart';
|
import 'package:browserloop/game/Game.dart';
|
||||||
import 'package:browserloop/game/LoopExample.dart';
|
import 'package:browserloop/game/LoopExample.dart';
|
||||||
|
|
||||||
|
@ -7,7 +8,8 @@ import 'package:browserloop/game/LoopExample.dart';
|
||||||
/// Perhaps one of the most widely used loops
|
/// Perhaps one of the most widely used loops
|
||||||
/// in games for many years. Many game frameworks
|
/// in games for many years. Many game frameworks
|
||||||
/// make use of a loop similar to this.
|
/// make use of a loop similar to this.
|
||||||
class VariableTimestep implements LoopExample {
|
class VariableTimestep implements LoopExample, VariableUpdates {
|
||||||
|
double _COLORSWITCHSPEED = 1.0;
|
||||||
Game game;
|
Game game;
|
||||||
num id;
|
num id;
|
||||||
Stopwatch elapsed = new Stopwatch();
|
Stopwatch elapsed = new Stopwatch();
|
||||||
|
@ -16,14 +18,21 @@ class VariableTimestep implements LoopExample {
|
||||||
|
|
||||||
void eventloop(num time) {
|
void eventloop(num time) {
|
||||||
int dt = elapsed.elapsedMilliseconds;
|
int dt = elapsed.elapsedMilliseconds;
|
||||||
game.update(dt);
|
|
||||||
|
// Multiply by dt, so that the color switching speed is the same,
|
||||||
|
// regardless of the time it took to compute this update
|
||||||
|
int colorSwitchspeed = (_COLORSWITCHSPEED / 10 * dt).toInt();
|
||||||
|
|
||||||
|
game.update(colorSwitchspeed);
|
||||||
game.draw();
|
game.draw();
|
||||||
elapsed.reset();
|
elapsed.reset();
|
||||||
|
|
||||||
id = window.requestAnimationFrame(eventloop);
|
id = window.requestAnimationFrame(eventloop);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Starting and stopping the loop for the example page
|
|
||||||
|
// CONTROLS ON THE EXAMPLE PAGE
|
||||||
|
// NOT NECESSARY FOR LOOP ITSELF
|
||||||
void stop() {
|
void stop() {
|
||||||
elapsed.stop();
|
elapsed.stop();
|
||||||
window.cancelAnimationFrame(id);
|
window.cancelAnimationFrame(id);
|
||||||
|
@ -34,4 +43,9 @@ class VariableTimestep implements LoopExample {
|
||||||
elapsed.reset();
|
elapsed.reset();
|
||||||
window.requestAnimationFrame(eventloop);
|
window.requestAnimationFrame(eventloop);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void setUpdates(double updateRate) {
|
||||||
|
_COLORSWITCHSPEED = 1000 / updateRate;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,10 @@
|
||||||
import 'dart:html';
|
import 'dart:html';
|
||||||
|
|
||||||
import 'package:browserloop/game/Game.dart';
|
import 'package:browserloop/game/Game.dart';
|
||||||
import 'package:browserloop/game/LoopExample.dart';
|
import 'package:browserloop/game/LoopExample.dart';
|
||||||
|
import 'package:browserloop/src/02-AnimationFrameWhile.dart';
|
||||||
import 'package:browserloop/src/03-VariableTimestep.dart';
|
import 'package:browserloop/src/03-VariableTimestep.dart';
|
||||||
import 'package:browserloop/src/04-FixedLoopVariableRender.dart';
|
import 'package:browserloop/src/04-FixedLoopVariableRender.dart';
|
||||||
import 'package:browserloop/src/02-AnimationFrameWhile.dart';
|
|
||||||
import 'package:browserloop/src/05_DirtyFlagRendering.dart';
|
import 'package:browserloop/src/05_DirtyFlagRendering.dart';
|
||||||
|
|
||||||
CanvasElement baseCanvas = new CanvasElement(width: 480, height: 480);
|
CanvasElement baseCanvas = new CanvasElement(width: 480, height: 480);
|
||||||
|
@ -135,7 +136,7 @@ void addControls(Example ex) {
|
||||||
})));
|
})));
|
||||||
|
|
||||||
// Don't add controls which don't work anyways (for simple examples)
|
// Don't add controls which don't work anyways (for simple examples)
|
||||||
if (examples.indexOf(ex) <= 1) return;
|
if (examples.indexOf(ex) <= 0) return;
|
||||||
VariableUpdates loop = (ex.loop as VariableUpdates);
|
VariableUpdates loop = (ex.loop as VariableUpdates);
|
||||||
|
|
||||||
// Update Speed Slider
|
// Update Speed Slider
|
||||||
|
@ -147,13 +148,10 @@ void addControls(Example ex) {
|
||||||
..id = "update_speed"
|
..id = "update_speed"
|
||||||
..min = "1"
|
..min = "1"
|
||||||
..max = "50"
|
..max = "50"
|
||||||
..value = "3"
|
..value = "1"
|
||||||
..step = "1"
|
..step = "1"
|
||||||
..onInput.listen((Event e) {
|
..onInput.listen((Event e) {
|
||||||
loop.MS_PER_UPDATE =
|
loop.setUpdates(1000 / int.parse((e.target as InputElement).value));
|
||||||
(1000 / int.parse((e.target as InputElement).value));
|
|
||||||
}));
|
}));
|
||||||
// querySelector('#reset').onClick.listen((e) => ex.loop.game.reset());
|
// querySelector('#reset').onClick.listen((e) => ex.loop.game.reset());
|
||||||
// querySelector('#plus').onClick.listen((e) => _changeGrid(-5));
|
|
||||||
// querySelector('#minus').onClick.listen((e) => _changeGrid(5));
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue