Guest User

Untitled

a guest
Nov 23rd, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var Player = {
  2.     walkspeed: 1,
  3.     playerId: '#player',
  4.     animLayer: '#animationLayer',
  5.     createPlayer: function() {
  6.         $(animLayer).append('<div id="' + playerId.substr(1) + '"></div>');
  7.     },
  8.     getPos: function() {
  9.         return this.offset;
  10.     },
  11.     getX: function() {
  12.         return this.offset.left;
  13.     },
  14.     getY: function() {
  15.         return this.offset.top;
  16.     },
  17.     path: function(id) {
  18.         var waypoints = paths[id - 1];
  19.  
  20.         //console.log(waypoints);
  21.  
  22.         $(waypoints).find('point').each(function () {
  23.             var x = $(this).find('x').text();
  24.             var y = $(this).find('y').text();
  25.  
  26.             player.move(x, y);
  27.         });
  28.  
  29.  
  30.         var amount = paths.length - 1; // 0 correction
  31.         for (i = 0; i <= amount; i++) {
  32.             var value = waypoints[i]; // get coords
  33.             //alert(value);
  34.         }
  35.     },
  36.     move: function(x, y){
  37.         console.log("moving");
  38.  
  39.         this.x = x;
  40.         this.y = y;
  41.  
  42.         utils = new Utils();
  43.  
  44.         var h = utils.hyp(x, y);
  45.         var dur = (h * 10) / walkspeed;
  46.  
  47.         $("#player").animate({
  48.                 left: x,
  49.                 top: y
  50.             }, dur, 'linear',
  51.             function() {
  52.                 $(playerId).html(player.direction(0, 0));
  53.             });
  54.  
  55.         //$(playerId).html(player.direction(x, y));
  56.     },
  57.     direction: function(x, y){
  58.         curPos = player.currentPos();
  59.  
  60.         var a = Math.atan2(-(curPos[1] - y), (curPos[0] - x));
  61.         var angle = a * (180 / Math.PI);
  62.  
  63.         return angle;
  64.     },
  65.     debug: function() {
  66.         console.log(player.getpos.x);
  67.  
  68.         $('body').append('<div id="debug">');
  69.         $('#debug').append('<ul>');
  70.         $('#debug ul').append('<li>playerID: ' + playerId + '</li>');
  71.         $('#debug ul').append('<li>currentPos: ' + player.getpos.x + '</li>');
  72.         $('#debug ul').append('<li>targetPos:</li>');
  73.         $('#debug ul').append('<li>playerSpeed:</li>');
  74.         $('#debug ul').append('<li>playerDirection:</li>');
  75.         $('#debug ul').append('<li>movementDuration:</li>');
  76.         $('#debug').append('<a href="#" id="create">Create</a>');
  77.         $('#debug').append('<a href="#" id="execute">Execute animation</a>');
  78.     }
  79. }
  80.  
  81. var Utils = {
  82.     hyp: function(x, y){
  83.         // Bereken hypotenuse - breedte*breedte + hoogte*hoogte
  84.         var hyp = Math.sqrt(x * x + y * y);
  85.         // Rond af
  86.         var r = Math.round(hyp);
  87.  
  88.         return r;
  89.     }
  90. }
  91.  
  92. //alert(waypoints);
  93.  
  94. $(document).ready(function() {
  95.     $('#animationLayer').live('click', function() {
  96.  
  97.         player = new Player();
  98.         player.debug();
  99.         $('#animationLayer').die('click');
  100.     });
  101.     $('#create').live('click', function() {
  102.  
  103.         player.createPlayer();
  104.         $('#debug a#create').die('click');
  105.     });
  106.     $('#execute').live('click', function() {
  107.  
  108.         console.log(player.currentPos());
  109.         player.path(1);
  110.     });
  111.  
  112.     $.ajax({
  113.         type: 'GET',
  114.         url: 'data/paths.xml',
  115.         dataType: 'xml',
  116.         error: (function() {
  117.             alert("error loading paths");
  118.         }),
  119.         success: function(xml) {
  120.  
  121.             paths = $(xml).find('path').each(function () {
  122.  
  123.                 i = $(this).find('id');
  124.  
  125.                 w = $(this).find('waypoints').each(function () {
  126.                     $(this).find('x');
  127.                     $(this).find('y');
  128.                 });
  129.             });
  130.             //console.log(paths);
  131.         }
  132.     });
  133. });
Add Comment
Please, Sign In to add comment