Advertisement
Threxx

fuck projectiles

Dec 4th, 2022 (edited)
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /// @description _proj_doreflect(enemy, proj)
  2. /// @param enemy
  3. /// @param  proj
  4. function _proj_doreflect(enemy, proj) {
  5.     var player = get_player_object();
  6.     if (!instance_exists(player))
  7.         exit;
  8.    
  9.     //Get projectile direction
  10.     var speed_x = abs(proj.x - proj.xprevious) != 0 ? proj.x - proj.xprevious : proj.xspeed;
  11.     var speed_y = abs(proj.y - proj.yprevious) != 0 ? proj.y - proj.yprevious : proj.yspeed;
  12.     var sign_x = sign(speed_x);
  13.     var sign_y = sign(speed_y);
  14.     var angle_init = point_direction(xprevious, yprevious, x, y) % 90;
  15.     var angle_final = angle_init - (angle_init == 0 ? 45 : 90);
  16.    
  17.     //Check if we're colliding horizontally or vertically
  18.     var horz = rectangle_in_rectangle(proj.bbox_left, proj.bbox_top, proj.bbox_right, proj.bbox_bottom,
  19.                                       global.sectionLeft, enemy.bbox_top, global.sectionRight, enemy.bbox_bottom);
  20.     var vert = rectangle_in_rectangle(proj.bbox_left, proj.bbox_top, proj.bbox_right, proj.bbox_bottom,
  21.                                       enemy.bbox_left, global.sectionTop, enemy.bbox_right, global.sectionBottom);
  22.                                      
  23.     //Check what side we're colliding from
  24.     var left = horz && !collision_line(proj.bbox_left, proj.bbox_top, proj.bbox_left, proj.bbox_bottom, enemy, false, false);
  25.     var right = horz && !collision_line(proj.bbox_right, proj.bbox_top, proj.bbox_right, proj.bbox_bottom, enemy, false, false);
  26.     var top = vert && !collision_line(proj.bbox_left, proj.bbox_top, proj.bbox_right, proj.bbox_top, enemy, false, false);
  27.     var bottom = vert && !collision_line(proj.bbox_left, proj.bbox_bottom, proj.bbox_right, proj.bbox_bottom, enemy, false, false);
  28.    
  29.     //Check if we're hitting the corner of the object
  30.     var corner = !horz && !vert;
  31.    
  32.     //Check if both axes are at speed == 0
  33.     var zero = false;
  34.    
  35.     //Correct for hitting opposite corners
  36.     if (speed_x != 0 && speed_y != 0) {
  37.         if (speed_x > 0) {
  38.             if (left && (top || bottom)) {
  39.                 top = false;
  40.                 bottom = false;
  41.             } else if (right && (top || bottom)) {
  42.                 right = false;
  43.             }
  44.         } else {
  45.             if (right && (top || bottom)) {
  46.                 top = false;
  47.                 bottom = false;
  48.             } else if (left && (top || bottom)) {
  49.                 left = false;
  50.             }
  51.         }
  52.     } else {
  53.         zero = true;   
  54.     }
  55.    
  56.     //Calculate new trajectory
  57.     if ((left || right || top || bottom || corner) && !zero) {
  58.         //If we're not fully inside the enemy, calculate based on speed and what side we hit
  59.         if (speed_x != 0 && (left || right || corner)) {
  60.             sign_x = -sign(speed_x);
  61.             if (sign_y == 0) {sign_y = -player.image_yscale};          
  62.         }
  63.         if (speed_y != 0 && (top || bottom || corner)) {
  64.             sign_y = -sign(speed_y);
  65.             if (sign_x == 0) {sign_x = -proj.shootDir};
  66.         }
  67.     } else {
  68.         //If we're fully inside the enemy, GO THE OTHER WAY
  69.         //Also failsafe for when speed == 0 on both axes
  70.         sign_x = (speed_x != 0) ? -sign(speed_x) : -proj.shootDir;
  71.         sign_y = (speed_y != 0) ? -sign(speed_y) : -player.image_yscale;
  72.     }
  73.    
  74.     //Set new projectile speed
  75.     //NOTE: I ceil this since I account for getting an integer value I want for the reflected speed in the SD weapon code, but you don't have to ceil this if you don't want to.
  76.     proj.xspeed = ceil(proj.reflectSpeed * dcos(angle_final) * sign_x);
  77.     proj.yspeed = ceil(proj.reflectSpeed * -dsin(angle_final) * sign_y);
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement