Advertisement
ReaperZX7

Danmakufu ph3, Final code form, Wavering Homing Player Shot

Jan 27th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.09 KB | None | 0 0
  1. //remember to set these global variables for easy adjustment
  2. let SHOT_HOMING_ACCEL = /*how quickly the bullets can steer*/
  3. let SHOT_HOMING_LIMIT = /*the top limit on how fast the bullets can turn*/
  4. let SHOT_MAX_HOMING_DIST = /*the shot will home in on enemies within this distance*/
  5.  
  6. //you also need this function
  7. function angleTo(obj,dest) {
  8.     let dir = atan2(ObjMove_GetY(dest)-ObjMove_GetY(obj),ObjMove_GetX(dest)-ObjMove_GetX(obj));
  9.     return dir;
  10. }
  11.  
  12. //whenever you need to fire a homing shot just use this section
  13. let bullet = CreatePlayerShotA1(input desired values);
  14. shotCommands(bullet);
  15. //----------------------------
  16.  
  17. //insert this somewhere else, probably near the bottom
  18. task shotCommands(obj) {
  19.     //A:
  20.     let angularVelocity = 0;
  21.     //B:
  22.     while (!Obj_IsDeleted(obj)) {
  23.         //1:
  24.         let enemyMatrix = GetIntersectionRegistedEnemyID;
  25.         //2:
  26.         let homingBool = false;
  27.         //3:
  28.         if (length(enemyMatrix) > 0) {
  29.             //3.1:
  30.             let targetDist = SHOT_MAX_HOMING_DIST;
  31.             //3.2:
  32.             let targetID = 0;
  33.            
  34.             //3.3:
  35.             ascent (i in 0..length(enemyMatrix)) {
  36.                 //3.3.1:
  37.                 if (GetObjectDistance(obj,enemy[i]) < targetDist) {
  38.                     //3.3.1.1:
  39.                     targetDist = GetObjectDistance(obj,enemy[i]);
  40.                     //3.3.1.2:
  41.                     targetID = enemyMatrix[i];
  42.                     //3.3.1.3:
  43.                     homingBool = true;
  44.                 }
  45.             }
  46.             //3.4:
  47.             if (homingBool == true) {
  48.                 //3.4.1:
  49.                 let shotAngle = ObjMove_GetAngle(obj);
  50.                     //3.4.1.1:
  51.                     while (shotAngle >= 360) {
  52.                         shotAngle -= 360;
  53.                     }
  54.                     while (shotAngle < 0) {
  55.                         shotAngle += 360;
  56.                     }
  57.                 //3.4.2:
  58.                 let targetAngle = angleTo(obj,target);
  59.                     //3.4.2.1:
  60.                     while (targetAngle >= 360) {
  61.                         targetAngle -= 360;
  62.                     }
  63.                     while (targetAngle < 0) {
  64.                         targetAngle += 360;
  65.                     }
  66.                
  67.                 //3.4.3:
  68.                 let targetAngleN = targetAngle-180;
  69.                     //3.4.3.1:
  70.                     while (targetAngleN >= 360) {
  71.                         targetAngleN -= 360;
  72.                     }
  73.                     while (targetAngleN < 0) {
  74.                         targetAngleN += 360;
  75.                     }
  76.                
  77.                 //3.4.4:
  78.                 if (targetAngle < 180) {
  79.                     //3.4.4.1:
  80.                     if (shotAngle > targetAngleN || shotAngle < targetAngle) {
  81.                         //3.4.4.1.1:
  82.                         if (angularVelocity < SHOT_HOMING_LIMIT) {
  83.                             //3.4.4.1.1.1:
  84.                             angularVelocity += SHOT_HOMING_ACCEL;  
  85.                         }
  86.                     }
  87.                     //3.4.4.2:
  88.                     if (shotAngle < targetAngleN && shotAngle > targetAngle) {
  89.                         //3.4.4.2.1:
  90.                         if (angularVelocity > -SHOT_HOMING_LIMIT) {
  91.                             //3.4.4.2.1.1:
  92.                             angularVelocity -= SHOT_HOMING_ACCEL;
  93.                         }
  94.                     }
  95.                 }
  96.                 //3.4.5:
  97.                 if (targetAngle > 180) {
  98.                     //3.4.5.1:
  99.                     if (shotAngle > targetAngleN && shotAngle < targetAngle) {
  100.                         //3.4.5.1.1:
  101.                         if (angularVelocity < SHOT_HOMING_LIMIT) {
  102.                             //3.4.5.1.1.1:
  103.                             angularVelocity += SHOT_HOMING_ACCEL;  
  104.                         }
  105.                     }
  106.                     //3.4.5.2:
  107.                     if (shotAngle < targetAngleN || shotAngle > targetAngle) {
  108.                         //3.4.5.2.1:
  109.                         if (angularVelocity > -SHOT_HOMING_LIMIT) {
  110.                             //3.4.5.2.1.1:
  111.                             angularVelocity -= SHOT_HOMING_ACCEL;
  112.                         }
  113.                     }
  114.                 }
  115.                 //3.4.6:
  116.                 ObjMove_SetAngle(obj,shotAngle+angularVelocity);
  117.             }
  118.         }
  119.         //4:
  120.         yield;
  121.     }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement