game2.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. var game = new Phaser.Game (
  2. 1024, 768,
  3. Phaser.AUTO,
  4. 'game',
  5. {
  6. preload: preload,
  7. create: create,
  8. update: update,
  9. render: render
  10. }
  11. );
  12. /*-----ПРОТОТИП ИГРОКА-----*/
  13. function Player(name) {
  14. this.name = name;
  15. this.hp = 100;
  16. };
  17. Player.prototype.spawn = function() {
  18. /*-----СОЗДАНИЕ АНИМАЦИИ-----*/
  19. this.name = game.add.sprite(480, 350, 'humantile');
  20. this.name.animations.add('left', [14, 15, 16], 10, true);
  21. this.name.animations.add('right', [17, 18, 19], 10, true);
  22. this.name.animations.add('up', [20, 21, 22], 10, true);
  23. this.name.animations.add('down', [23, 24, 25], 10, true);
  24. this.name.animations.add('attack', [1, 2, 3], 10, true);
  25. game.physics.enable(this.name);
  26. };
  27. Player.prototype.animation = function() {
  28. game.physics.arcade.collide(this.name, platforms);
  29. this.name.body.velocity.x = 0;
  30. this.name.body.velocity.y = 0;
  31. this.name.body.angularVelocity = 0;
  32. /*-----ДВИЖЕНИЕ ИГРОКА-----*/
  33. if (cursors.left.isDown)
  34. {
  35. // Движение влево
  36. this.name.body.velocity.x = -250;
  37. if (!attackbut.isDown){this.name.animations.play('left');}
  38. // Движение влево вверх
  39. if (cursors.up.isDown){
  40. this.name.body.velocity.x = -175;
  41. this.name.body.velocity.y = -175;
  42. if (!attackbut.isDown){this.name.animations.play('left');}
  43. }
  44. // Движение влево вниз
  45. if (cursors.down.isDown){
  46. this.name.body.velocity.x = -175;
  47. this.name.body.velocity.y = 175;
  48. if (!attackbut.isDown){this.name.animations.play('left');}
  49. }
  50. }
  51. else if (cursors.right.isDown)
  52. {
  53. // Движение вправо
  54. this.name.body.velocity.x = 250;
  55. if (!attackbut.isDown){this.name.animations.play('right');}
  56. // Движение вправо вниз
  57. if (cursors.up.isDown){
  58. this.name.body.velocity.x = 175;
  59. this.name.body.velocity.y = -175;
  60. if (!attackbut.isDown){this.name.animations.play('right');}
  61. }
  62. // Движение вправо вверх
  63. if (cursors.down.isDown){
  64. this.name.body.velocity.x = 175;
  65. this.name.body.velocity.y = 175;
  66. if (!attackbut.isDown){this.name.animations.play('right');}
  67. }
  68. }
  69. else if (cursors.up.isDown)
  70. {
  71. // Движение вверх
  72. this.name.body.velocity.y = -250;
  73. if (!attackbut.isDown){this.name.animations.play('up');}
  74. }
  75. else if (cursors.down.isDown)
  76. {
  77. // Движение вниз
  78. this.name.body.velocity.y = 250;
  79. if (!attackbut.isDown){this.name.animations.play('down');}
  80. }
  81. else
  82. {
  83. // Неподвижно
  84. if (!attackbut.isDown){this.name.animations.stop();}
  85. this.name.frame = 0;
  86. }
  87. if (attackbut.isDown){
  88. this.name.animations.play('attack');
  89. }
  90. };
  91. /*-----ПРОТОТИП МОНСТРОВ-----*/
  92. function Monster(name, line, timespawn) {
  93. this.name = name;
  94. this.monsterhp = 50; //количество жизней
  95. this.damage = 2; //наносимый им урон
  96. this.range = 50; //радиус атаки игрока
  97. this.arange = -50;
  98. this.monster_count = 0; //счетчик кадров (60 в секунду)
  99. this.line = line; //сторона появления (35 или 940)
  100. this.timespawn = timespawn; //время появления, указывается в секундах
  101. this.ymelee = 200; //расстояние от игрока до врага
  102. this.xmelee = 200;
  103. };
  104. /*-----СПАВН МОНСТРОВ-----*/
  105. Monster.prototype.spawn = function() {
  106. this.monster_count++;
  107. if(this.monster_count == (this.timespawn * 60)) {
  108. this.name = game.add.sprite(this.line, game.world.randomY, 'monsterstile');
  109. this.name.animations.add('left', [4, 5, 6], 10, true);
  110. this.name.animations.add('right', [1, 2, 3], 10, true);
  111. this.name.animations.add('up', [7, 8, 9], 10, true);
  112. this.name.animations.add('down', [10, 11, 12], 10, true);
  113. this.name.animations.add('dmg', [13, 14, 15], 10, true);
  114. }
  115. };
  116. Monster.prototype.move = function() {
  117. /*-----ДВИЖЕНИЕ МОНСТРОВ-----*/
  118. game.physics.enable(this.name);
  119. if (this.name.x > poz.name.x)
  120. {
  121. // Движение влево
  122. this.name.body.velocity.x = -80;
  123. this.name.animations.play('left');
  124. // Движение влево вверх
  125. if (this.name.y > poz.name.y){
  126. this.name.body.velocity.x = -50;
  127. this.name.body.velocity.y = -50;
  128. this.name.animations.play('left');
  129. }
  130. // Движение влево вниз
  131. if (this.name.y < poz.name.y){
  132. this.name.body.velocity.x = -50;
  133. this.name.body.velocity.y = 50;
  134. this.name.animations.play('left');
  135. }
  136. }
  137. else if (this.name.x < poz.name.x)
  138. {
  139. // Движение вправо
  140. this.name.body.velocity.x = 80;
  141. this.name.animations.play('right');
  142. // Движение вправо вниз
  143. if (this.name.y > poz.name.y){
  144. this.name.body.velocity.x = 50;
  145. this.name.body.velocity.y = -50;
  146. this.name.animations.play('right');
  147. }
  148. // Движение вправо вверх
  149. if (this.name.y < poz.name.y){
  150. this.name.body.velocity.x = 50;
  151. this.name.body.velocity.y = 50;
  152. this.name.animations.play('right');
  153. }
  154. }
  155. else if (this.name.y < poz.name.y)
  156. {
  157. // Движение вверх
  158. this.name.body.velocity.y = -80;
  159. this.name.animations.play('up');
  160. }
  161. else if (this.name.y > poz.name.y)
  162. {
  163. // Движение вниз
  164. this.name.body.velocity.y = 80;
  165. this.name.animations.play('down');
  166. }
  167. };
  168. /*-----БОЙ МОНСТРОВ-----*/
  169. Monster.prototype.death = function() {
  170. this.ymelee = this.name.y - poz.name.y;
  171. this.xmelee = this.name.x - poz.name.x;
  172. if (this.ymelee < this.range && this.ymelee > this.arange && this.xmelee < this.range && this.xmelee > this.arange && attackbut.isDown) {
  173. this.monsterhp -= this.damage;
  174. }
  175. if (this.monsterhp <= 0) {
  176. if (poz.hp > 0) {
  177. xp += 5;
  178. }
  179. this.monsterhp = 50;
  180. if (this.line == 35) {
  181. this.name.x = this.line - 300;
  182. this.name.y = game.world.randomY;
  183. }
  184. if (this.line == 940) {
  185. this.name.x = this.line + 300;
  186. this.name.y = game.world.randomY;
  187. }
  188. }
  189. if (this.ymelee < this.range - 20 && this.ymelee > this.arange + 20 && this.xmelee < this.range - 20 && this.xmelee > this.arange + 20) {
  190. this.name.animations.play('dmg');
  191. poz.hp -= 1;
  192. }
  193. return poz.hp;
  194. };
  195. function gameover() {
  196. if (poz.hp <= 0) {
  197. endString = 'Game Over. Your Score - ';
  198. endText = game.add.text(320, 200, endString + xp, { font: '34px Arial', fill: '#fff' });
  199. poz.name.kill();
  200. }
  201. }
  202. function preload() {
  203. game.load.image('map', 'assets/mapp2.png');
  204. game.load.spritesheet('humantile', 'assets/humantile.png', 64, 64);
  205. game.load.spritesheet('monsterstile', 'assets/monsterstile.png', 64, 64);
  206. game.load.image('board_left', 'assets/board_left.png');
  207. game.load.image('board_right', 'assets/board_right.png');
  208. game.load.image('board_up', 'assets/board_up.png');
  209. game.load.image('board_down', 'assets/board_down.png');
  210. }
  211. var cursors;
  212. var attackbut;
  213. var layer;
  214. var map;
  215. var boardl, boardr, boardd, boardu;
  216. var platforms;
  217. var xp = 0;
  218. var skeleton11 = new Monster('sk11', 35, 1);
  219. var skeleton12 = new Monster('sk12', 35, 1);
  220. var skeleton13 = new Monster('sk13', 35, 5);
  221. var skeleton14 = new Monster('sk14', 35, 5);
  222. var skeleton15 = new Monster('sk15', 35, 5);
  223. var skeleton16 = new Monster('sk16', 35, 12);
  224. var skeleton17 = new Monster('sk17', 35, 12);
  225. var skeleton18 = new Monster('sk18', 35, 12);
  226. var skeleton19 = new Monster('sk19', 35, 12);
  227. var skeleton10 = new Monster('sk10', 35, 25);
  228. var skeleton111 = new Monster('sk111', 35, 25);
  229. var skeleton112 = new Monster('sk112', 35, 25);
  230. var skeleton113 = new Monster('sk113', 35, 25);
  231. var skeleton114 = new Monster('sk114', 35, 25);
  232. var skeleton21 = new Monster('sk21', 940, 1);
  233. var skeleton22 = new Monster('sk22', 940, 1);
  234. var skeleton23 = new Monster('sk23', 940, 5);
  235. var skeleton24 = new Monster('sk24', 940, 5);
  236. var skeleton25 = new Monster('sk25', 940, 5);
  237. var skeleton26 = new Monster('sk26', 940, 12);
  238. var skeleton27 = new Monster('sk27', 940, 12);
  239. var skeleton28 = new Monster('sk28', 940, 12);
  240. var skeleton29 = new Monster('sk29', 940, 12);
  241. var skeleton20 = new Monster('sk20', 940, 25);
  242. var skeleton211 = new Monster('sk211', 940, 25);
  243. var skeleton212 = new Monster('sk212', 940, 25);
  244. var skeleton213 = new Monster('sk213', 940, 25);
  245. var skeleton214 = new Monster('sk214', 940, 25);
  246. var poz = new Player('pozh');
  247. function create() {
  248. game.physics.startSystem(Phaser.Physics.ARCADE);
  249. /*-----ГЕНЕРАЦИЯ УРОВНЯ-----*/
  250. platforms = game.add.group();
  251. platforms.enableBody = true;
  252. boardl = platforms.create(0, 0, 'board_left');
  253. boardr = platforms.create(1000, 0, 'board_right');
  254. boardd = platforms.create(0, 750, 'board_down');
  255. boardu = platforms.create(0, 0, 'board_up');
  256. boardl.body.immovable = true;
  257. boardr.body.immovable = true;
  258. boardd.body.immovable = true;
  259. boardu.body.immovable = true;
  260. game.add.sprite(0, 0, 'map');
  261. poz.spawn();
  262. hpString = 'Health: '; //Здоровье
  263. hpText = game.add.text(10, 10, hpString + poz.hp, { font: '24px Arial', fill: '#fff' });
  264. xpString = 'Score: '; //Очки
  265. xpText = game.add.text(850, 10, xpString + xp, { font: '24px Arial', fill: '#fff' });
  266. cursors = game.input.keyboard.createCursorKeys();
  267. attackbut = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
  268. }
  269. /*-----ИГРОВОЙ ПРОЦЕСС-----*/
  270. function update() {
  271. poz.animation();
  272. gameover();
  273. if (poz.hp >= 0) {
  274. hpText.text = hpString + poz.hp;
  275. }
  276. xpText.text = xpString + xp;
  277. monsterspawn();
  278. }
  279. function render () {
  280. }