Thursday, June 24, 2021

【GAMEMAKER】Chain Shot

 Information about object: objPlayer

Sprite: sprStand
Solid: false
Visible: true
Depth: 0
Persistent: false
Parent:
Children:
Mask:
No Physics Object
Create Event:
set variable ShotTimer to 0
set variable BulletType to 6
set variable sprite to sprStand
execute code:

chained_to = objBullet; //the ball object that the player is chained to
chain_length = 128; //how long the chain should be
movement_speed = 4; //how fast the player should currently move
max_movement_speed = 4; //how fast the player moves without being held back by the chain
chain_movement_speed = 1; //how fast the player moves when being held back by the chain

collision_padding = 4; //How close to the wall the chain can get - must be at least 1

chain_is_bent = false; //If there is a bend in the chain
chain_bend_x = 0; //where the bend is
chain_bend_y = 0;

hsp = 0
vsp = 0
Step Event:
execute code:

hspeed=0;
vspeed=0;

if keyboard_check(vk_left) hspeed=-4;
if keyboard_check(vk_right) hspeed=4;
if keyboard_check(vk_up) vspeed=-4;
if keyboard_check(vk_down) vspeed=4;
k_up = keyboard_check(vk_up)
k_down = keyboard_check(vk_down)
if hspeed<0 && vspeed=0 {
BulletType=4;
}
if hspeed<0 && vspeed<0 {
BulletType=3;
}

if hspeed<0 && vspeed>0 {
BulletType=5;
}

if hspeed>0 && vspeed=0 {
BulletType=0;
}
if hspeed>0 && vspeed<0 {
BulletType=1;
}
if hspeed>0 && vspeed>0 {
BulletType=7;
}

if hspeed=0 && vspeed<0 {
BulletType=2;
}
if hspeed=0 && vspeed>0 {
BulletType=6;
}

if mouse_x>x and mouse_y<y+10 and mouse_y>y-10 then sprite_index=sprEast;
if mouse_x<x and mouse_y<y+10 and mouse_y>y-10then sprite_index=sprWest;
if mouse_y>y and mouse_x<x+10 and mouse_x>x-10then sprite_index=sprSouth;
if mouse_y<y and mouse_x<x+10 and mouse_x>x-10then sprite_index=sprNorth;

if mouse_x>x+10 and mouse_y>y+10 then sprite_index=sprSouthEast;
if mouse_x>x+10 and mouse_y<y-10 then sprite_index=sprNorthEast;
if mouse_x<x-10 and mouse_y>y+10 then sprite_index=sprSouthWest;
if mouse_x<x-10 and mouse_y<y-10 then sprite_index=sprNorthWest;

if hspeed!=0 or vspeed!=0 {
image_single=-1;
} else {
image_single=1;
}

if mouse_button=mb_left {

if ShotTimer<0 {
axe = instance_create(x,y,objBullet);
ShotTimer=25;
if (k_up-k_down)!=0
{  
if (k_up-k_down)>0
{
axe.vsp=-12
axe.hsp=0
}

if (k_up-k_down)<0
{
axe.vsp=12
axe.hsp=0
}
}
else 
{
axe.vsp=0
axe.hsp=12*image_xscale
}
}
}
set variable ShotTimer relative to -1
execute code:

/*
if(instance_exists(chained_to)){
var ball_distance = point_distance(x,y,chained_to.x,chained_to.y); //Distance between the player and the ball
var ball_collision_distance = point_distance(chain_bend_x,chain_bend_y,chained_to.x,chained_to.y); //Distance between the ball and the chain bend
var player_collision_distance = point_distance(chain_bend_x,chain_bend_y,x,y); //Distance between the bend and the player

if chain_is_bent //If the chain is bent calculate the chain distance
{
ball_distance = ball_collision_distance + player_collision_distance;
}

if position_meeting(chained_to.x, chained_to.y, block_object) //If the ball is stuck on the wall just go straight towards the player
{
chain_bend_x = objPlayer.x;
chain_bend_y = objPlayer.y;
}

if ball_distance > chain_length //If the player is too far
{
movement_speed = lerp(movement_speed,chain_movement_speed,.2); //Change the movement speed to the slow speed
//I use lerp to keep the movement from looking jumpy

if chain_is_bent //If the chain is bent move the ball towards the bend
{
chained_to.x = lerp(chained_to.x,chain_bend_x,.02*(ball_distance/ball_collision_distance));
chained_to.y = lerp(chained_to.y,chain_bend_y,.02*(ball_distance/ball_collision_distance));
}
else //If the chain isn't bent move the ball toward the player
{
chained_to.x = lerp(chained_to.x,objPlayer.x,.02);
chained_to.y = lerp(chained_to.y,objPlayer.y,.02);
}
}
else //If the player is not too far away set the player's speed to the max speed
{
movement_speed = lerp(movement_speed,max_movement_speed,.2);
}

//Check the line to see if a bend should be added
collision_object = collision_line(x,y,chained_to.x,chained_to.y,block_object,2,false);

if collision_object != noone //If there' a collision
{
var xoff = (collision_object.sprite_width*.5)+collision_padding; //this assumes the axis is in the middle of the object
var yoff = (collision_object.sprite_height*.5)+collision_padding; //this assumes the axis is in the middle of the object 



if !chain_is_bent //If there is a bend, check wich corner of the wall the bend should be on
{
if check_chain_bend(collision_object.x+xoff,collision_object.y+yoff)
{
chain_bend_x = collision_object.x+xoff;
chain_bend_y = collision_object.y+yoff;     
}
if check_chain_bend(collision_object.x-xoff,collision_object.y+yoff)
{
chain_bend_x = collision_object.x-xoff;
chain_bend_y = collision_object.y+yoff;     
}
if check_chain_bend(collision_object.x+xoff,collision_object.y-yoff)
{
chain_bend_x = collision_object.x+xoff;
chain_bend_y = collision_object.y-yoff;     
}
if check_chain_bend(collision_object.x-xoff,collision_object.y-yoff)
{
chain_bend_x = collision_object.x-xoff;
chain_bend_y = collision_object.y-yoff;     
}                     
chain_is_bent = true;
}
}
else
{
chain_is_bent = false;
}
}

Draw Event:
execute code:

draw_self()


if(instance_exists(chained_to)){
if chain_is_bent
{
draw_cool_line(x,y,chain_bend_x,chain_bend_y,chain_sprite,5);
draw_cool_line(chain_bend_x,chain_bend_y,chained_to.x,chained_to.y,chain_sprite,5);
}
else
{
draw_cool_line(x,y,chained_to.x,chained_to.y,sprBullet,5);
}
}

Information about object: objOtherBullet
Sprite: sprBullet
Solid: false
Visible: true
Depth: 0
Persistent: false
Parent:
Children:
Mask:
No Physics Object
Create Event:
start moving in the direction of position (mouse_x,mouse_y) with speed 8


Information about object: objBullet
Sprite: sprBullet
Solid: false
Visible: true
Depth: 0
Persistent: false
Parent:
Children:
Mask:
No Physics Object
Create Event:
execute code:

axe_clock=25

forward = true
backward = false
canThrow = false
trackPlayer = false
move_towards_point(mouse_x, mouse_y, 30)
Step Event:
execute code:


if(distance_to_object(objPlayer) >200) {

trackPlayer = true
}

if(trackPlayer){
move_towards_point(objPlayer.x, objPlayer.y, 8)
if place_meeting(x, y, objPlayer) {
trackPlayer = false
instance_destroy()
}
}
Other Event: Outside Room:
destroy the instance

Information about object: objPlayer
Sprite: sprStand
Solid: false
Visible: true
Depth: 0
Persistent: false
Parent:
Children:
Mask:
No Physics Object
Create Event:
set variable ShotTimer to 0
set variable BulletType to 6
set variable sprite to sprStand
execute code:

chained_to = objBullet; //the ball object that the player is chained to
chain_length = 128; //how long the chain should be
movement_speed = 4; //how fast the player should currently move
max_movement_speed = 4; //how fast the player moves without being held back by the chain
chain_movement_speed = 1; //how fast the player moves when being held back by the chain

collision_padding = 4; //How close to the wall the chain can get - must be at least 1

chain_is_bent = false; //If there is a bend in the chain
chain_bend_x = 0; //where the bend is
chain_bend_y = 0;

hsp = 0
vsp = 0
Step Event:
execute code:

hspeed=0;
vspeed=0;

if keyboard_check(vk_left) hspeed=-4;
if keyboard_check(vk_right) hspeed=4;
if keyboard_check(vk_up) vspeed=-4;
if keyboard_check(vk_down) vspeed=4;
k_up = keyboard_check(vk_up)
k_down = keyboard_check(vk_down)
if hspeed<0 && vspeed=0 {
BulletType=4;
}
if hspeed<0 && vspeed<0 {
BulletType=3;
}

if hspeed<0 && vspeed>0 {
BulletType=5;
}

if hspeed>0 && vspeed=0 {
BulletType=0;
}
if hspeed>0 && vspeed<0 {
BulletType=1;
}
if hspeed>0 && vspeed>0 {
BulletType=7;
}

if hspeed=0 && vspeed<0 {
BulletType=2;
}
if hspeed=0 && vspeed>0 {
BulletType=6;
}

if mouse_x>x and mouse_y<y+10 and mouse_y>y-10 then sprite_index=sprEast;
if mouse_x<x and mouse_y<y+10 and mouse_y>y-10then sprite_index=sprWest;
if mouse_y>y and mouse_x<x+10 and mouse_x>x-10then sprite_index=sprSouth;
if mouse_y<y and mouse_x<x+10 and mouse_x>x-10then sprite_index=sprNorth;

if mouse_x>x+10 and mouse_y>y+10 then sprite_index=sprSouthEast;
if mouse_x>x+10 and mouse_y<y-10 then sprite_index=sprNorthEast;
if mouse_x<x-10 and mouse_y>y+10 then sprite_index=sprSouthWest;
if mouse_x<x-10 and mouse_y<y-10 then sprite_index=sprNorthWest;

if hspeed!=0 or vspeed!=0 {
image_single=-1;
} else {
image_single=1;
}

if mouse_button=mb_left {

if ShotTimer<0 {
axe = instance_create(x,y,objBullet);
ShotTimer=25;
if (k_up-k_down)!=0
{  
if (k_up-k_down)>0
{
axe.vsp=-12
axe.hsp=0
}

if (k_up-k_down)<0
{
axe.vsp=12
axe.hsp=0
}
}
else 
{
axe.vsp=0
axe.hsp=12*image_xscale
}
}
}
set variable ShotTimer relative to -1
execute code:

/*
if(instance_exists(chained_to)){
var ball_distance = point_distance(x,y,chained_to.x,chained_to.y); //Distance between the player and the ball
var ball_collision_distance = point_distance(chain_bend_x,chain_bend_y,chained_to.x,chained_to.y); //Distance between the ball and the chain bend
var player_collision_distance = point_distance(chain_bend_x,chain_bend_y,x,y); //Distance between the bend and the player

if chain_is_bent //If the chain is bent calculate the chain distance
{
ball_distance = ball_collision_distance + player_collision_distance;
}

if position_meeting(chained_to.x, chained_to.y, block_object) //If the ball is stuck on the wall just go straight towards the player
{
chain_bend_x = objPlayer.x;
chain_bend_y = objPlayer.y;
}

if ball_distance > chain_length //If the player is too far
{
movement_speed = lerp(movement_speed,chain_movement_speed,.2); //Change the movement speed to the slow speed
//I use lerp to keep the movement from looking jumpy

if chain_is_bent //If the chain is bent move the ball towards the bend
{
chained_to.x = lerp(chained_to.x,chain_bend_x,.02*(ball_distance/ball_collision_distance));
chained_to.y = lerp(chained_to.y,chain_bend_y,.02*(ball_distance/ball_collision_distance));
}
else //If the chain isn't bent move the ball toward the player
{
chained_to.x = lerp(chained_to.x,objPlayer.x,.02);
chained_to.y = lerp(chained_to.y,objPlayer.y,.02);
}
}
else //If the player is not too far away set the player's speed to the max speed
{
movement_speed = lerp(movement_speed,max_movement_speed,.2);
}

//Check the line to see if a bend should be added
collision_object = collision_line(x,y,chained_to.x,chained_to.y,block_object,2,false);

if collision_object != noone //If there' a collision
{
var xoff = (collision_object.sprite_width*.5)+collision_padding; //this assumes the axis is in the middle of the object
var yoff = (collision_object.sprite_height*.5)+collision_padding; //this assumes the axis is in the middle of the object 



if !chain_is_bent //If there is a bend, check wich corner of the wall the bend should be on
{
if check_chain_bend(collision_object.x+xoff,collision_object.y+yoff)
{
chain_bend_x = collision_object.x+xoff;
chain_bend_y = collision_object.y+yoff;     
}
if check_chain_bend(collision_object.x-xoff,collision_object.y+yoff)
{
chain_bend_x = collision_object.x-xoff;
chain_bend_y = collision_object.y+yoff;     
}
if check_chain_bend(collision_object.x+xoff,collision_object.y-yoff)
{
chain_bend_x = collision_object.x+xoff;
chain_bend_y = collision_object.y-yoff;     
}
if check_chain_bend(collision_object.x-xoff,collision_object.y-yoff)
{
chain_bend_x = collision_object.x-xoff;
chain_bend_y = collision_object.y-yoff;     
}                     
chain_is_bent = true;
}
}
else
{
chain_is_bent = false;
}
}

Draw Event:
execute code:

draw_self()


if(instance_exists(chained_to)){
if chain_is_bent
{
draw_cool_line(x,y,chain_bend_x,chain_bend_y,chain_sprite,5);
draw_cool_line(chain_bend_x,chain_bend_y,chained_to.x,chained_to.y,chain_sprite,5);
}
else
{
draw_cool_line(x,y,chained_to.x,chained_to.y,sprBullet,5);
}
}

No comments:

Post a Comment

End of Summer Sale ☀️😎

20% OFF Inside!🤯 ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏...