Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- SETUP NOTES: I have separate objects for the obj_body parts of the snake. In the create event,
- I made an array called "body", which stores the ID's of every object that forms the snake,
- INCLUDING (and starting from) the obj_head, which the player can control.
- */
- //(This is in the STEP event of the obj_head object)
- //HEAD MOVEMENT CODE
- //First part is just getting the head to move. I'm going to use the GML equivalent of vectors.
- mdir = point_direction(x,y,mouse_x,mouse_y); //gets the direction of the vector between the head & mouse
- if(keyboard_check(vk_shift)) spd = 4; //allows us to change speeds by holding shift
- else spd = 2;
- /* Our goal now is to move TOWARD the mouse by making a vector. A vector with a length of "spd".
- (I HIGHLY recommend looking up the documentation page for lengthdir_x if you are not familiar with it)
- GameMaker doesn't have "vector" movement, we have to manipulate x and y ourselves.
- So we are going to be translating the vector into an "x component" and "y component",
- ie. how far over we have to move in each direction to simulate the vector movement.
- */
- x += lengthdir_x(spd,mdir);
- y += lengthdir_y(spd,mdir);
- //BODY MOVEMENT
- /*We are going to loop through the body array and move each head. We are going to start from the
- second entry (ie. entry 1) because the head is in the first spot in my array, which we already moved.
- Each body part will pull out the PREVIOUS entry (eg. if I am entry 2, my "chasePart" is entry 1),
- and move up right next to it.
- */
- /*As I said, i is initialised at 1 because we are skipping the head entry.
- 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).
- So we use array_length_1d(body) to get the array length, and -1 because we are skipping the head.*/
- var i = 1; repeat(array_length_1d(body)-1) {
- var currentPart = body[i]; //the current body part we'll move
- var chasePart = body[i-1]; //the part we're chasing; the one in front!
- with(currentPart){ //Note: a WITH statement means we are making "currentPart" run this code
- /*We are going to move in the same way the head does, but this time, there's no spd.
- We just want to be right up next to our chasePart. So, the length of our vector will be
- the distance between us, MINUS the size of the body parts. For me, I'm just using sprite_width
- */
- var dir = point_direction(x,y,chasePart.x,chasePart.y); //direction between currentPart and chasePart
- var dist = point_distance(x,y,chasePart.x,chasePart.y) - sprite_width; //distance between currentPart and chasePart MINUS the size of the body parts
- x += lengthdir_x(dist,dir); //again we get the x and y components and move in the direction of our vector
- y += lengthdir_y(dist,dir);
- }
- i++; //we loop through the array by incrementing i
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement