Advertisement
jenspauwels

Untitled

Dec 1st, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const socket = io.connect('localhost:3000');
  2. const numSnakeSections = 30;
  3. const snakeSpacer = 1;
  4.  
  5. let snakeHead;
  6. let snakeSection = new Array(30).fill('');
  7. let snakePath = new Array(numSnakeSections * snakeSpacer).fill('');
  8.  
  9. const preload = function () {
  10.   game.load.image('ball', 'assets/sprites/ball.png');
  11. };
  12.  
  13. const SnakeDot = function() {
  14.   const section = game.add.sprite(400, 300, 'ball');
  15.   section.scale.setTo(0.03, 0.03);
  16.   section.anchor.setTo(0.5, 0.5);
  17.   return section;
  18. };
  19.  
  20. const initSnakeSection = function() {
  21.   snakeSection = snakeSection.map(x => SnakeDot());
  22. };
  23.  
  24. const initSnakePath = function() {
  25.   snakePath = snakePath.map(x => new Phaser.Point(400, 300));
  26. };
  27.  
  28. const create = function() {
  29.   game.physics.startSystem(Phaser.Physics.ARCADE);
  30.   game.world.setBounds(0, 0, 800, 600);
  31.  
  32.   cursors = game.input.keyboard.createCursorKeys();
  33.   snakeHead = SnakeDot();
  34.  
  35.   game.physics.enable(snakeHead, Phaser.Physics.ARCADE);
  36.   game.time.events.loop(Phaser.Timer.SECOND / 20, updateSnake, this);
  37.  
  38.   initSnakeSection();
  39.   initSnakePath();
  40. };
  41.  
  42. const updateSnakePath = function () {
  43.   const part = snakePath.pop();
  44.   part.setTo(snakeHead.x, snakeHead.y);
  45.   snakePath.unshift(part);
  46. };
  47.  
  48. const moveSnake = function(angularVelocity) {
  49.   snakeHead.body.angularVelocity = angularVelocity;
  50.   snakeHead.body.velocity.copyFrom(game.physics.arcade.velocityFromAngle(snakeHead.angle, 300));
  51.  
  52.   updateSnakePath();
  53.  
  54.   snakeSection.forEach((position, index) => {
  55.     position.x = snakePath[index].x;
  56.     position.y = snakePath[index].y;
  57.   });
  58.  
  59. };
  60.  
  61. const updateSnake = function() {
  62.   snakeHead.body.velocity.setTo(0, 0);
  63.   moveSnake(0);
  64. };
  65.  
  66. const handleLeft = function() {
  67.   moveSnake(-300);
  68. };
  69.  
  70. const handleRight = function() {
  71.   moveSnake(300);
  72. };
  73.  
  74. const update = function() {
  75.   snakeHead.body.velocity.setTo(0, 0);
  76.   snakeHead.body.angularVelocity = 0;
  77.  
  78.   if (cursors.left.isDown) handleLeft()
  79.   else if (cursors.right.isDown) handleRight();
  80. };
  81.  
  82. const render = function() {
  83.   game.debug.spriteInfo(snakeHead, 32, 32);
  84. };
  85.  
  86. const options = {
  87.   preload,
  88.   create,
  89.   update,
  90.   render
  91. };
  92.  
  93. const game = new Phaser.Game(800, 600, Phaser.CANVAS, 'CurveFever', options);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement