Game
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

worldgen.js 950B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. function WorldGen(game) {
  2. this.game = game;
  3. this.prevX = 0;
  4. this.minY = -400;
  5. this.maxY = 400;
  6. this.powerupCounter = randInt(WorldGen.range[0], WorldGen.range[1]);
  7. // Spawn player and background
  8. game.spawn(new Player(game));
  9. game.spawn(new Background(game));
  10. }
  11. WorldGen.range = [5, 12];
  12. WorldGen.prototype.update = function() {
  13. while (this.game.camera.x + this.game.canvas.width > this.prevX)
  14. this.genNext();
  15. }
  16. WorldGen.prototype.genNext = function() {
  17. var x = this.prevX + 300;
  18. var y = Math.round((Math.random() - 0.5) * 300);
  19. if (y < this.minY) y = this.minY;
  20. if (y > this.maxY) y = this.maxY;
  21. var obstacle = new Obstacle(this.game, x, y);
  22. this.game.spawn(obstacle);
  23. this.prevX = x;
  24. if (--this.powerupCounter === 0) {
  25. var powerup = new PowerUp(
  26. this.game,
  27. x - 50,
  28. y + 80 + (this.game.canvas.height / 2));
  29. this.game.spawn(powerup);
  30. this.powerupCounter = randInt(WorldGen.range[0], WorldGen.range[1]);
  31. }
  32. }