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
|
||||
void update([num speed]) {
|
||||
print(speed);
|
||||
void update([int speed]) {
|
||||
Point next = isRandom ? nextPosRandom() : nextPosOrdered(_curPos);
|
||||
if (_curPos.x < 0) _curPos = Point(0, _curPos.y);
|
||||
Color newColor = isRandom ? randomColor() : nextColor(
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
import 'dart:html';
|
||||
|
||||
import 'package:browserloop/game/Game.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
|
||||
/// in games for many years. Many game frameworks
|
||||
/// make use of a loop similar to this.
|
||||
class VariableTimestep implements LoopExample {
|
||||
class VariableTimestep implements LoopExample, VariableUpdates {
|
||||
double _COLORSWITCHSPEED = 1.0;
|
||||
Game game;
|
||||
num id;
|
||||
Stopwatch elapsed = new Stopwatch();
|
||||
|
|
@ -16,14 +18,21 @@ class VariableTimestep implements LoopExample {
|
|||
|
||||
void eventloop(num time) {
|
||||
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();
|
||||
elapsed.reset();
|
||||
|
||||
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() {
|
||||
elapsed.stop();
|
||||
window.cancelAnimationFrame(id);
|
||||
|
|
@ -34,4 +43,9 @@ class VariableTimestep implements LoopExample {
|
|||
elapsed.reset();
|
||||
window.requestAnimationFrame(eventloop);
|
||||
}
|
||||
|
||||
@override
|
||||
void setUpdates(double updateRate) {
|
||||
_COLORSWITCHSPEED = 1000 / updateRate;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue