Devan as your follower

Version:

1.0

Added on:

28 Mar 2014 23:31

Tags:

Description:
  1. /*Devan as your follower*/
  2. /*Author: szmol96*/
  3.  
  4. /** System data **/
  5. bool keyJHeld;
  6. bool keyDHeld;
  7. /** Settings **/
  8. const int shootDelay = 16; //The delay between Devan's shoots if you hold the fire key. (Decrease to shoot faster.)
  9. const int runSpeed = 4; //How fast Devan runs. (WARNING: At high speeds he may get stuck in walls.)
  10. const int jumpHeight = -12; //How high Devan jumps. It must be a negative number or he will jump downwards. (WARNING: Too high jumps may result him getting stuck in tiles above him.)
  11. const float gravity = 0.5; //How fast Devan gains vertical speed.
  12. const float maxFallSpeed = 6; //The maximum speed Devan can fall with. (WARNING: At high speeds he may fall through the ground.)
  13. const int followDist = 100;
  14. /*************************************************************************************************/
  15. void followerNpc(jjOBJ@ npc) {
  16.   float dx = p.xPos - npc.xPos;
  17.   float dy = p.yPos - npc.yPos;
  18.   float distX = sqrt(dx*dx); //The horizontal distance between you and Devan.
  19.   float distY = sqrt(dy*dy); //The vertical distance between you and Devan.
  20.   int gunSpotY; //The height he shoots from.
  21.   int shootTimer; //Counts his shoot delay.
  22.   int shootAnimTimer; //This lets his shoot animation play.
  23.   bool onGround; //Does not let him jump in the air.
  24.   bool pickedUp;
  25.  
  26.   jjPLAYER@ p = jjLocalPlayers[0];
  27.  
  28.   npc.yPos = npc.yPos + npc.ySpeed;
  29.  
  30.   if (jjMaskedHLine(npc.xPos - 17, 34, npc.yPos - 11)) npc.ySpeed = 1;
  31.  
  32.   if (!jjMaskedHLine(npc.xPos - 17, 34, npc.yPos + 37)) {
  33.     npc.ySpeed = npc.ySpeed + gravity;  //Fall if nothing is below.
  34.     onGround = false;
  35.   } else {
  36.     npc.ySpeed = 0;
  37.     onGround = true;
  38.   }
  39.   if (npc.ySpeed > maxFallSpeed) npc.ySpeed = maxFallSpeed; //He can't accelerate to infinity.
  40.  
  41.   if (npc.state == STATE::START) {
  42.     npc.state = STATE::IDLE;
  43.     npc.direction = 1;
  44.     shootAnimTimer = 0;
  45.     pickedUp = false;
  46.   }
  47.   if (distX > followDist && npc.state != STATE::DUCK) {
  48.     if (!jjMaskedVLine(npc.xPos + npc.direction * 21, npc.yPos - 10, 34)) {  //check if nothing is in front
  49.       npc.state = STATE::WALK;
  50.       npc.xPos = npc.xPos + npc.direction * runSpeed;
  51.         npc.determineCurAnim(ANIM::DEVILDEVAN, 18);
  52.         npc.frameID = (++npc.counter >> 2) % 13;
  53.     if (jjMaskedVLine(npc.xPos + npc.direction * 16, npc.yPos + 25, 11)) npc.yPos = npc.yPos - 7; //walk up on slopes
  54.     } else if (jjMaskedVLine(npc.xPos + npc.direction * 21, npc.yPos - 10, 34) && onGround) {
  55.       npc.ySpeed = jumpHeight;
  56.     }
  57.     npc.direction = (npc.xPos > p.xPos) ? -1 : 1;
  58.   } else {
  59.     npc.state = STATE::IDLE;
  60.     npc.xSpeed = 0;
  61.     if (shootAnimTimer == 0) {
  62.       npc.determineCurAnim(ANIM::DEVILDEVAN, 21);
  63.       npc.frameID = (++npc.counter >> 3) % 4;
  64.     } else {
  65.       shootAnimTimer--;
  66.       npc.determineCurAnim(ANIM::DEVILDEVAN, 14);
  67.       npc.frameID = (++npc.counter >> 1) % 10;
  68.     }
  69.   }
  70.   jjOBJ@ bullet;
  71.   if(p.keyFire == true) {
  72.     npc.state = STATE::ATTACK;
  73.     if (shootTimer == 0) {
  74.       @bullet = jjObjects[jjAddObject(OBJECT::BLASTERBULLET, npc.xPos, gunSpotY, npc.objectID, CREATOR::OBJECT)];
  75.       bullet.xSpeed = npc.direction * 10;
  76.       bullet.determineCurAnim(ANIM::DEVILDEVAN, 17);
  77.       shootAnimTimer = 15;
  78.       jjSample(npc.xPos, npc.yPos, SOUND::DEVILDEVAN_PHASER2, 63, 0);
  79.       shootTimer = shootDelay;
  80.     }
  81.     shootTimer--;
  82.   } else {
  83.     shootTimer = 0;
  84.   }
  85.   if (p.keyDown == true) {
  86.     npc.state = STATE::DUCK;
  87.     npc.determineCurAnim(ANIM::DEVILDEVAN, 13);
  88.     npc.frameID = 3;
  89.     gunSpotY = npc.yPos + 20;
  90.   } else {
  91.     gunSpotY = npc.yPos + 5;
  92.   }
  93.    
  94.   if (pickedUp == true) {
  95.     npc.xPos = p.xPos - p.direction * 10;
  96.     npc.yPos = p.yPos - 30;
  97.     npc.direction = -1 * p.direction;
  98.     npc.ySpeed = 0;
  99.     if (p.xSpeed > 5) p.xSpeed = 5;
  100.     if (p.xSpeed < -5) p.xSpeed = -5;
  101.     p.jumpStrength = -8;
  102.   } else {
  103.     p.jumpStrength = -10;
  104.   }
  105.  
  106.   if (distX < 15 && distY < 31 && p.keyDown == true && !keyDHeld) {
  107.     if (pickedUp == false) {
  108.       pickedUp = true;
  109.     } else {
  110.       pickedUp = false;
  111.     }
  112.   }
  113.  
  114. //  if (p.keyJump == true && !keyJHeld && onGround) {
  115. //    npc.ySpeed = jumpHeight;
  116. //  }
  117.  
  118.   npc.determineCurFrame();
  119.   npc.draw();
  120.  
  121.   keyJHeld = p.keyJump;
  122.   keyDHeld = p.keyDown;
  123. }  
  124.  
  125. void onLevelLoad() {
  126.   jjObjectPresets[OBJECT::TURTLESHELL].behavior = followerNpc;
  127.   jjObjectPresets[OBJECT::TURTLESHELL].bulletHandling = HANDLING::IGNOREBULLET; //Your bullets just ignore him and go through him.
  128.   jjObjectPresets[OBJECT::TURTLESHELL].playerHandling = HANDLING::PARTICLE; //Don't interact with the player.
  129.   jjAlert("Press down to pick Devan up.");
  130. }