Advertisement
Guest User

Untitled

a guest
Oct 24th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. export default class Game {
  2. constructor(scenes) {
  3. // check if the browser supports requestAnimationFrame
  4. if(!(typeof requestAnimationFrame)) {
  5. throw new Error('Your browser doesn\'t support requestAnimationFrame :(');
  6. }
  7.  
  8. this.scenes = [];
  9. this.addScenes(scenes);
  10. this.play();
  11. this.loop();
  12. }
  13. addScenes(elem) {
  14. if(elem.constructor === Array) {
  15. this.scenes = this.scenes.concat(elem);
  16. } else {
  17. this.scenes.push(elem);
  18. }
  19. }
  20. removeScene(elem) {
  21. let idx = this.scenes.indexOf(elem);
  22. if(idx > -1) {
  23. this.scenes.splice(idx, 1);
  24. }
  25. }
  26. pause() {
  27. this.running = false;
  28. }
  29. play() {
  30. this.running = true;
  31. }
  32. togglePause() {
  33. this.running = !this.running;
  34. }
  35. loop() {
  36. if(this.running) {
  37. for(let scene of this.scenes) {
  38. scene.update();
  39. }
  40. }
  41.  
  42. requestAnimationFrame(this.loop.bind(this));
  43. }
  44. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement