Devan as your follower

Version:

1.0

Added on:

28 Mar 2014 22:31

Tags:

Description:
/*Devan as your follower*/
/*Author: szmol96*/

/** System data **/
bool keyJHeld;
bool keyDHeld;
/** Settings **/
const int shootDelay = 16; //The delay between Devan's shoots if you hold the fire key. (Decrease to shoot faster.)
const int runSpeed = 4; //How fast Devan runs. (WARNING: At high speeds he may get stuck in walls.)
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.)
const float gravity = 0.5; //How fast Devan gains vertical speed.
const float maxFallSpeed = 6; //The maximum speed Devan can fall with. (WARNING: At high speeds he may fall through the ground.)
const int followDist = 100;
/*************************************************************************************************/
void followerNpc(jjOBJ@ npc) {
  float dx = p.xPos - npc.xPos;
  float dy = p.yPos - npc.yPos;
  float distX = sqrt(dx*dx); //The horizontal distance between you and Devan.
  float distY = sqrt(dy*dy); //The vertical distance between you and Devan.
  int gunSpotY; //The height he shoots from.
  int shootTimer; //Counts his shoot delay.
  int shootAnimTimer; //This lets his shoot animation play.
  bool onGround; //Does not let him jump in the air.
  bool pickedUp;
  
  jjPLAYER@ p = jjLocalPlayers[0];
  
  npc.yPos = npc.yPos + npc.ySpeed;
  
  if (jjMaskedHLine(npc.xPos - 17, 34, npc.yPos - 11)) npc.ySpeed = 1;
  
  if (!jjMaskedHLine(npc.xPos - 17, 34, npc.yPos + 37)) {
    npc.ySpeed = npc.ySpeed + gravity;  //Fall if nothing is below.
    onGround = false;
  } else {
    npc.ySpeed = 0;
    onGround = true;
  }
  if (npc.ySpeed > maxFallSpeed) npc.ySpeed = maxFallSpeed; //He can't accelerate to infinity.
  
  if (npc.state == STATE::START) {
    npc.state = STATE::IDLE;
    npc.direction = 1;
    shootAnimTimer = 0;
    pickedUp = false;
  }
  if (distX > followDist && npc.state != STATE::DUCK) {
    if (!jjMaskedVLine(npc.xPos + npc.direction * 21, npc.yPos - 10, 34)) {  //check if nothing is in front
      npc.state = STATE::WALK;
      npc.xPos = npc.xPos + npc.direction * runSpeed;
        npc.determineCurAnim(ANIM::DEVILDEVAN, 18);
        npc.frameID = (++npc.counter >> 2) % 13;
    if (jjMaskedVLine(npc.xPos + npc.direction * 16, npc.yPos + 25, 11)) npc.yPos = npc.yPos - 7; //walk up on slopes
    } else if (jjMaskedVLine(npc.xPos + npc.direction * 21, npc.yPos - 10, 34) && onGround) {
      npc.ySpeed = jumpHeight;
    }
    npc.direction = (npc.xPos > p.xPos) ? -1 : 1;
  } else {
    npc.state = STATE::IDLE;
    npc.xSpeed = 0;
    if (shootAnimTimer == 0) {
      npc.determineCurAnim(ANIM::DEVILDEVAN, 21);
      npc.frameID = (++npc.counter >> 3) % 4;
    } else {
      shootAnimTimer--;
      npc.determineCurAnim(ANIM::DEVILDEVAN, 14);
      npc.frameID = (++npc.counter >> 1) % 10;
    }
  } 
  jjOBJ@ bullet;
  if(p.keyFire == true) {
    npc.state = STATE::ATTACK;
    if (shootTimer == 0) {
      @bullet = jjObjects[jjAddObject(OBJECT::BLASTERBULLET, npc.xPos, gunSpotY, npc.objectID, CREATOR::OBJECT)];
      bullet.xSpeed = npc.direction * 10;
      bullet.determineCurAnim(ANIM::DEVILDEVAN, 17);
      shootAnimTimer = 15;
      jjSample(npc.xPos, npc.yPos, SOUND::DEVILDEVAN_PHASER2, 63, 0);
      shootTimer = shootDelay;
    }
    shootTimer--;
  } else {
    shootTimer = 0;
  }
  if (p.keyDown == true) {
    npc.state = STATE::DUCK;
    npc.determineCurAnim(ANIM::DEVILDEVAN, 13);
    npc.frameID = 3;
    gunSpotY = npc.yPos + 20;
  } else {
    gunSpotY = npc.yPos + 5;
  }
    
  if (pickedUp == true) {
    npc.xPos = p.xPos - p.direction * 10;
    npc.yPos = p.yPos - 30;
    npc.direction = -1 * p.direction;
    npc.ySpeed = 0;
    if (p.xSpeed > 5) p.xSpeed = 5;
    if (p.xSpeed < -5) p.xSpeed = -5;
    p.jumpStrength = -8;
  } else {
    p.jumpStrength = -10;
  }
  
  if (distX < 15 && distY < 31 && p.keyDown == true && !keyDHeld) {
    if (pickedUp == false) {
      pickedUp = true;
    } else {
      pickedUp = false;
    }
  }
  
//  if (p.keyJump == true && !keyJHeld && onGround) {
//    npc.ySpeed = jumpHeight;
//  }

  npc.determineCurFrame();
  npc.draw();
  
  keyJHeld = p.keyJump;
  keyDHeld = p.keyDown;
}  

void onLevelLoad() {
  jjObjectPresets[OBJECT::TURTLESHELL].behavior = followerNpc;
  jjObjectPresets[OBJECT::TURTLESHELL].bulletHandling = HANDLING::IGNOREBULLET; //Your bullets just ignore him and go through him.
  jjObjectPresets[OBJECT::TURTLESHELL].playerHandling = HANDLING::PARTICLE; //Don't interact with the player.
  jjAlert("Press down to pick Devan up.");
}