Advertisement
Guest User

Untitled

a guest
May 2nd, 2019
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. SETUP NOTES: I have separate objects for the obj_body parts of the snake. In the create event,
  3. I made an array called "body", which stores the ID's of every object that forms the snake,
  4. INCLUDING (and starting from) the obj_head, which the player can control.
  5. */
  6.  
  7. //(This is in the STEP event of the obj_head object)
  8.  
  9. //HEAD MOVEMENT CODE
  10. //First part is just getting the head to move. I'm going to use the GML equivalent of vectors.
  11. mdir = point_direction(x,y,mouse_x,mouse_y); //gets the direction of the vector between the head & mouse
  12.  
  13. if(keyboard_check(vk_shift)) spd = 4; //allows us to change speeds by holding shift
  14. else spd = 2;
  15.  
  16. /* Our goal now is to move TOWARD the mouse by making a vector. A vector with a length of "spd".
  17. (I HIGHLY recommend looking up the documentation page for lengthdir_x if you are not familiar with it)
  18. GameMaker doesn't have "vector" movement, we have to manipulate x and y ourselves.
  19. So we are going to be translating the vector into an "x component" and "y component",
  20. ie. how far over we have to move in each direction to simulate the vector movement.
  21. */
  22. x += lengthdir_x(spd,mdir);
  23. y += lengthdir_y(spd,mdir);
  24.    
  25. //BODY MOVEMENT
  26. /*We are going to loop through the body array and move each head. We are going to start from the
  27. second entry (ie. entry 1) because the head is in the first spot in my array, which we already moved.
  28. Each body part will pull out the PREVIOUS entry (eg. if I am entry 2, my "chasePart" is entry 1),
  29. and move up right next to it.
  30. */
  31.  
  32. /*As I said, i is initialised at 1 because we are skipping the head entry.
  33. We want the number of times we loop to be the same as the number of body entries in the body array (MINUS the head).
  34. So we use array_length_1d(body) to get the array length, and -1 because we are skipping the head.*/
  35. var i = 1; repeat(array_length_1d(body)-1) {
  36.     var currentPart = body[i]; //the current body part we'll move
  37.     var chasePart = body[i-1]; //the part we're chasing; the one in front!
  38.  
  39.     with(currentPart){ //Note: a WITH statement means we are making "currentPart" run this code
  40.        
  41.         /*We are going to move in the same way the head does, but this time, there's no spd.
  42.         We just want to be right up next to our chasePart. So, the length of our vector will be
  43.         the distance between us, MINUS the size of the body parts. For me, I'm just using sprite_width
  44.         */
  45.         var dir = point_direction(x,y,chasePart.x,chasePart.y); //direction between currentPart and chasePart
  46.         var dist = point_distance(x,y,chasePart.x,chasePart.y) - sprite_width; //distance between currentPart and chasePart MINUS the size of the body parts
  47.            
  48.         x += lengthdir_x(dist,dir); //again we get the x and y components and move in the direction of our vector
  49.         y += lengthdir_y(dist,dir);
  50.     }
  51.            
  52.     i++; //we loop through the array by incrementing i
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement