Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// @description _proj_doreflect(enemy, proj)
- /// @param enemy
- /// @param proj
- function _proj_doreflect(enemy, proj) {
- var player = get_player_object();
- if (!instance_exists(player))
- exit;
- //Get projectile direction
- var speed_x = abs(proj.x - proj.xprevious) != 0 ? proj.x - proj.xprevious : proj.xspeed;
- var speed_y = abs(proj.y - proj.yprevious) != 0 ? proj.y - proj.yprevious : proj.yspeed;
- var sign_x = sign(speed_x);
- var sign_y = sign(speed_y);
- var angle_init = point_direction(xprevious, yprevious, x, y) % 90;
- var angle_final = angle_init - (angle_init == 0 ? 45 : 90);
- //Check if we're colliding horizontally or vertically
- var horz = rectangle_in_rectangle(proj.bbox_left, proj.bbox_top, proj.bbox_right, proj.bbox_bottom,
- global.sectionLeft, enemy.bbox_top, global.sectionRight, enemy.bbox_bottom);
- var vert = rectangle_in_rectangle(proj.bbox_left, proj.bbox_top, proj.bbox_right, proj.bbox_bottom,
- enemy.bbox_left, global.sectionTop, enemy.bbox_right, global.sectionBottom);
- //Check what side we're colliding from
- var left = horz && !collision_line(proj.bbox_left, proj.bbox_top, proj.bbox_left, proj.bbox_bottom, enemy, false, false);
- var right = horz && !collision_line(proj.bbox_right, proj.bbox_top, proj.bbox_right, proj.bbox_bottom, enemy, false, false);
- var top = vert && !collision_line(proj.bbox_left, proj.bbox_top, proj.bbox_right, proj.bbox_top, enemy, false, false);
- var bottom = vert && !collision_line(proj.bbox_left, proj.bbox_bottom, proj.bbox_right, proj.bbox_bottom, enemy, false, false);
- //Check if we're hitting the corner of the object
- var corner = !horz && !vert;
- //Check if both axes are at speed == 0
- var zero = false;
- //Correct for hitting opposite corners
- if (speed_x != 0 && speed_y != 0) {
- if (speed_x > 0) {
- if (left && (top || bottom)) {
- top = false;
- bottom = false;
- } else if (right && (top || bottom)) {
- right = false;
- }
- } else {
- if (right && (top || bottom)) {
- top = false;
- bottom = false;
- } else if (left && (top || bottom)) {
- left = false;
- }
- }
- } else {
- zero = true;
- }
- //Calculate new trajectory
- if ((left || right || top || bottom || corner) && !zero) {
- //If we're not fully inside the enemy, calculate based on speed and what side we hit
- if (speed_x != 0 && (left || right || corner)) {
- sign_x = -sign(speed_x);
- if (sign_y == 0) {sign_y = -player.image_yscale};
- }
- if (speed_y != 0 && (top || bottom || corner)) {
- sign_y = -sign(speed_y);
- if (sign_x == 0) {sign_x = -proj.shootDir};
- }
- } else {
- //If we're fully inside the enemy, GO THE OTHER WAY
- //Also failsafe for when speed == 0 on both axes
- sign_x = (speed_x != 0) ? -sign(speed_x) : -proj.shootDir;
- sign_y = (speed_y != 0) ? -sign(speed_y) : -player.image_yscale;
- }
- //Set new projectile speed
- //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.
- proj.xspeed = ceil(proj.reflectSpeed * dcos(angle_final) * sign_x);
- proj.yspeed = ceil(proj.reflectSpeed * -dsin(angle_final) * sign_y);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement