Guest User

Untitled

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