Advertisement
DeadlyAlive

Untitled

Jun 29th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <style>
  2.     .qqstick {
  3.         background: url("img/sticksprite.png");
  4.         width: 256px;
  5.         height: 256px;
  6.         transform: scale(0.5, 0.5);
  7.         animation: none;
  8.         position: absolute;
  9.         left: -120px;
  10.     }
  11.    
  12.     .borderstick {
  13.         width: 64px;
  14.         height: 64px;
  15.     }
  16.    
  17.     @keyframes avoid {
  18.         from {
  19.             background-position: -512px 0px
  20.         }
  21.         to {
  22.             background-position: -512px -768px
  23.         }
  24.     }
  25.    
  26.     @keyframes walk {
  27.         from {
  28.             background-position: 0px 0px
  29.         }
  30.         to {
  31.             background-position: 0px -768px
  32.         }
  33.     }
  34.    
  35.     @keyframes attack {
  36.         from {
  37.             background-position: -256px 0px
  38.         }
  39.         to {
  40.             background-position: -256px -768px
  41.         }
  42.     }
  43. </style>
  44. <script>
  45.     var look = 20,
  46.         brake = 0.1,
  47.         mouseX = 0,
  48.         mouseY = 0,
  49.         attackDistance = 32,
  50.         walking = false,
  51.         avoiding = false,
  52.         height, distance;
  53.  
  54.     function updateMouse(e) {
  55.         mouseX = e.pageX;
  56.         mouseY = e.pageY;
  57.         touchMe();
  58.     }
  59.  
  60.     function touchMe() {
  61.         lookForMouse();
  62.         if (Math.abs(distance) < 8 && Math.abs(height) < 64) avoid();
  63.     }
  64.  
  65.     function lookForMouse() {
  66.         height = mouseY - $(".qqstick").offset().top - 32;
  67.         distance = mouseX - ($(".qqstick").offset().left + 64);
  68.     }
  69.  
  70.     function whereTo() {
  71.         var howFar = Math.abs(distance) * brake;
  72.         if (howFar > 10) howFar = 10;
  73.         lookForMouse();
  74.         if (!avoiding) {
  75.             if (Math.abs(distance) < attackDistance) {
  76.                 if (Math.abs(height) < 64) attack();
  77.                 else wait();
  78.             } else {
  79.                 walk();
  80.                 if (distance < 0) {
  81.                     turnLeft();
  82.                     moveLeft(howFar, look);
  83.                 } else {
  84.                     turnRight();
  85.                     moveRight(howFar, look);
  86.                 }
  87.             }
  88.             setTimeout(whereTo, look * 2);
  89.         }
  90.         $(".log").text(mouseX + " " + mouseY + " " + ($(".qqstick").offset().left + 64));
  91.     }
  92.  
  93.     function avoid() {
  94.         if (!avoiding) {
  95.             $(".qqstick").css("animation", "avoid 0.3s steps(3) infinite");
  96.             attackDistance = 64;
  97.             if ($(".qqstick").offset().left + 64 > mouseX) {
  98.                 turnLeft();
  99.                 moveRight(150, 300);
  100.             } else {
  101.                 turnRight();
  102.                 moveLeft(150, 300);
  103.             }
  104.             avoiding = true;
  105.             setTimeout(function () {
  106.                 avoiding = false;
  107.                 whereTo();
  108.             }, 300);
  109.             walking = false;
  110.         }
  111.     }
  112.  
  113.     function attack() {
  114.         $(".qqstick").css("animation", "attack 0.4s steps(3) infinite");
  115.         attackDistance = 64;
  116.         walking = false;
  117.     }
  118.  
  119.     function wait() {
  120.         $(".qqstick").css("animation", "none");
  121.         attackDistance = 64;
  122.         walking = false;
  123.     }
  124.  
  125.     function turnLeft() {
  126.         $(".qqstick").css("transform", "scale(-0.5,0.5)");
  127.     }
  128.  
  129.     function turnRight() {
  130.         $(".qqstick").css("transform", "scale(0.5,0.5)");
  131.     }
  132.  
  133.     function moveLeft(x, t) {
  134.         var left = {
  135.             'left': "-=" + x + "px"
  136.         };
  137.         $(".qqstick").animate(left, t, "linear");
  138.     }
  139.  
  140.     function moveRight(x, t) {
  141.         var right = {
  142.             'left': "+=" + x + "px"
  143.         };
  144.         $(".qqstick").animate(right, t, "linear");
  145.     }
  146.  
  147.     function walk() {
  148.         if (!walking) {
  149.             $(".qqstick").css("animation", "walk 0.5s steps(3) infinite");
  150.             attackDistance = 32;
  151.             walking = true;
  152.         }
  153.     }
  154.  
  155.     function watchMouse() {
  156.         look = true;
  157.         $(".log").text("true?");
  158.     }
  159.     whereTo();
  160. </script>
  161.  
  162.  
  163. <div class="container">
  164.     <div class="row">
  165.         <div class="col-sm-9">
  166.             <div class="qqstick" />
  167.             <div class="borderstick"></div>
  168.             <div class="log" />
  169.         </div>
  170.     </div>
  171. </div>
  172. <script>
  173.     $(document).mousemove(function (event) {
  174.         updateMouse(event);
  175.     });
  176. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement