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