Guest User

Untitled

a guest
Jul 17th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.70 KB | None | 0 0
  1. var gamejs = require('gamejs');
  2. var $v = require('gamejs/utils/vectors');
  3.  
  4. function main() {
  5. gamejs.display.setCaption("Der Soldat");
  6.  
  7. // create background
  8. var screen = gamejs.display.setMode([850, 500]);
  9. var displayRect = screen.rect;
  10. var bg = gamejs.image.load('images/ground.png');
  11. var clouds = gamejs.image.load('images/clouds.png');
  12. var heliGui = gamejs.image.load('images/heligui.png');
  13.  
  14. // test
  15. var combatants = new gamejs.sprite.Group();
  16. var bombs = new gamejs.sprite.Group();
  17.  
  18. var getCombatant = function(type) {
  19. var tank = new gamejs.scene.MovingSprite();
  20. var pos = [0,0];
  21. if (Math.random() > 0.5) {
  22. pos[0] = parseInt(Math.random() * displayRect.width);
  23. } else {
  24. pos[1] = parseInt(Math.random() * displayRect.height);
  25. }
  26. tank.setPosition(pos);
  27. tank.setImage('images/' + type + '.png');
  28. tank.setSpeed(type === 'tank' ? 5 : 2);
  29. tank.goTo([Math.random() * displayRect.width, Math.random() * displayRect.height]);
  30. return tank;
  31. }
  32.  
  33. // sounds
  34. /*
  35. var pulse = new gamejs.mixer.Sound('sounds/lowpulse.ogg');
  36. var explosion = new gamejs.mixer.Sound('sounds/explosion.ogg');
  37. var kick =
  38. */
  39.  
  40. var craters = [];
  41.  
  42. /**
  43. * bomb
  44. */
  45. var Bomb = function(pos) {
  46. var EXP_SIZE = [60, 60];
  47. Bomb.superConstructor.apply(this, arguments);
  48. this.rect = new gamejs.Rect([0,0], EXP_SIZE);
  49. this.rect.center = pos;
  50. this.image = null;
  51.  
  52. this.flightDuration = 0;
  53. this.explosionDuration = 0;
  54. this.isOnGround = false;
  55.  
  56. this.frameTime = 0;
  57. this.currentFrame = 0;
  58. this.frameDuration = 80;
  59. this.frameDirection = 1;
  60.  
  61. (new gamejs.mixer.Sound('sounds/kick.ogg')).play();
  62. (new gamejs.mixer.Sound('sounds/lowpulse.ogg')).play();
  63. return this;
  64. };
  65. gamejs.utils.objects.extend(Bomb, gamejs.sprite.Sprite)
  66. Bomb.prototype.draw = function(screen) {
  67. if (!this.image) return;
  68. screen.blit(this.image, this.rect);
  69. return;
  70. };
  71. Bomb.prototype.update = function(msDuration) {
  72. if (this.isOnGround) {
  73. this.frameTime += msDuration;
  74.  
  75. if (!this.image || this.frameTime > this.frameDuration) {
  76. if (this.currentFrame < 0) {
  77. this.kill();
  78. return;
  79. }
  80.  
  81. this.image = gamejs.image.load('images/explosion_' + this.currentFrame +'.png');
  82. this.frameTime = 0;
  83. if (this.currentFrame == 11) {
  84. var craterRect = new gamejs.Rect(this.rect);
  85. craterRect.width = 15
  86. craterRect.height = 15;
  87. craterRect.center = this.rect.center;
  88. craters.push({
  89. image: gamejs.image.load('images/crater_' + parseInt(Math.random() * 6, 10) + '.png'),
  90. rect: craterRect,
  91. });
  92. this.frameDirection = -1;
  93. }
  94. this.currentFrame += this.frameDirection;
  95. }
  96. } else {
  97. this.flightDuration += msDuration;
  98. if (this.flightDuration > 3500) {
  99. (new gamejs.mixer.Sound('sounds/explosion.ogg')).play();
  100. this.isOnGround = true;
  101. };
  102. }
  103. return;
  104. };
  105.  
  106. /**
  107. * Main Game 'loop'
  108. *
  109. */
  110. var jitterPause = 300;
  111. var jitterCumulated = null;
  112. jitterPos = displayRect;
  113.  
  114. var spawnPause = 3500;
  115. var spawnCumulated = null;
  116.  
  117. var shootPause = 1500;
  118. var shootCumulated = shootPause * 2;
  119.  
  120.  
  121. function tick(msDuration) {
  122. // event handling
  123. gamejs.event.get().forEach(function(event) {
  124.  
  125. if (event.type === gamejs.event.MOUSE_UP) {
  126. if (event.button === 0) {
  127. if (shootCumulated > shootPause) {
  128. bombs.add(new Bomb(event.pos));
  129. shootCumulated = 0;
  130. }
  131. }
  132. };
  133. });
  134. shootCumulated += msDuration;
  135.  
  136. // update models
  137. combatants.update(msDuration);
  138. bombs.update(msDuration);
  139.  
  140. // bomb combat collision detection
  141. bombs.sprites().filter(function(bomb) {
  142. if (bomb.currentFrame >= 8) {
  143. gamejs.sprite.spriteCollide(bomb, combatants, true);
  144. }
  145. }, this);
  146.  
  147. // combatent spawn
  148. if (spawnCumulated === null || spawnCumulated > spawnPause) {
  149. if (Math.random() > 0.5) {
  150. combatants.add(getCombatant('tank'));
  151. combatants.add(getCombatant('tank'));
  152. } else {
  153. combatants.add(getCombatant('infantry'));
  154. combatants.add(getCombatant('infantry'));
  155. }
  156. spawnCumulated = 0;
  157. }
  158. spawnCumulated += msDuration;
  159.  
  160. // draw
  161. screen.blit(bg, displayRect);
  162. craters.forEach(function(crater) {
  163. screen.blit(crater.image, crater.rect);
  164. });
  165. combatants.draw(screen);
  166. bombs.draw(screen);
  167. // screen jitter
  168. if (jitterCumulated === null | jitterCumulated >= jitterPause) {
  169. jitterPos = displayRect.move(-5 + (Math.random() * 5), -5 + (Math.random() * 5))
  170. jitterPos.width = displayRect.width * 1.3;
  171. jitterPos.height = displayRect.height * 1.3;
  172. jitterCumulated = 0;
  173. }
  174. jitterCumulated += msDuration;
  175. screen.blit(clouds, jitterPos);
  176. screen.blit(heliGui, displayRect);
  177. };
  178. gamejs.time.fpsCallback(tick, this, 30);
  179. return;
  180. }
  181.  
  182. var PRELOAD_LIST = [
  183. 'images/ground.png',
  184. 'images/clouds.png',
  185. 'images/heligui.png',
  186.  
  187. 'images/tank.png',
  188. 'images/infantry.png',
  189.  
  190. 'sounds/kick.ogg',
  191. 'sounds/explosion.ogg',
  192. 'sounds/lowpulse.ogg',
  193. ];
  194. for (var i=0;i<12;i++) {
  195. PRELOAD_LIST.push('images/explosion_' + i + '.png');
  196. };
  197. for (var i=0;i<6;i++) {
  198. PRELOAD_LIST.push('images/crater_' + i + '.png');
  199. };
  200. gamejs.preload(PRELOAD_LIST);
  201. gamejs.ready(main);
Add Comment
Please, Sign In to add comment