首页 > Sktech > 习作AD_带场景地图的喷气飞船_扩展v1.02
2017
09-18

习作AD_带场景地图的喷气飞船_扩展v1.02

习作AD_带场景地图的喷气飞船_扩展v1.02 - 第1张  | Processing编程艺术


加入了浮靶的敌对要素和飞船的RPG要素(血条, 能量条, 等级).

浮靶现在会尽量避免靠近地图边缘, 在感知到飞船后会积极靠近飞船并攻击, 在感知到飞船攻击后会回避.(浮靶靠近动作参考 nature of code 第六章 agents 的 seek 范例重写. )

飞船在接触到浮靶后会受到轻伤害, 在被击中后会受到伤害. 飞船在HP为零后死亡, 得分和等级清零.

飞船的喷气和攻击动作现在需要能量, 但能量不足时仍能够喷气. 能量按一定速率自动回复.

飞船击坠浮靶后能获得经验值(不可见), 积累后可升级. 升级只影响血量上限, 能量上限, 和速度上限.


加入了新的操作:

[M]:暂停.

[N]:隐藏调试信息.

[Spacebar]:死亡后开始新游戏.


加入了新武器:

[J]:单射::CD短, 范围小, 威力大, 残弹制, 射尽后三秒装弹. 红色. (… …ビームライフル?)

[I]:霰射::CD长, 范围广, 威力中, 能耗制. 黄色. (… …拡散メガ粒子砲?)

[L]:连射::无CD, 范围中, 威力小, 能耗制. 蓝色. (… …ビームマシンガン?)

[U]:照射::须蓄力, 光束攻击, 连续伤害, 能耗制. 黄色. (… …ハイメガランチャー?)


– 问题:子弹类结构设计的不好, 个体差别只有初始位置和初始速度, 在前一个子弹没有飞出地图前应用样式的话会导致前一个子弹样式变更.

– 问题:飞船触碰到地图边缘后会导致照射武器射角改变.


/* *** *** *** ***
 *
 * ex_Asteroids_Agent_Mod
 *
 * *** *** *** ***
 *///- --- --- ---

//--
static final int CST_MAX_ENEMY=64;
static final int CST_DEFAULT_COLOR=0x77;//[0x77]dark..[0xEE]bright..
//--
int pbLeRoller=0;
int pbCoolDownInterval=0;
PVector pbTheOrigin;
//--
boolean pbPause=true;
boolean pbHideInfo=false;
//--
EcMap pbTheMap= new EcMap(CST_DEFAULT_COLOR);
Spaceship ship = new Spaceship(pbTheMap);
EcCordinator pbTheCord= new EcCordinator(pbTheMap,ship,3);
//--


void setup() {
  size(320, 240);frameRate(32);
  textSize(11);textAlign(LEFT,TOP);
  pbTheOrigin=new PVector(width/2,height/2);
}//+++


void draw() {background(0);pbLeRoller++;pbLeRoller&=0x1F;
  //--
  int lpMillisA=millis();
  //--
  pbTheMap.ccUpdate();
  if(pbPause){
    //--
    pbCoolDownInterval--;if(pbCoolDownInterval<0){pbCoolDownInterval=0;}
    //--
    fsCheckCamera();
    pbTheCord.ccUpdate();
    ship.ccRefresh();
    //--
    if(pbLeRoller==7){pbTheCord.ccNewDummy(CST_MAX_ENEMY);}
    //--
    fsKeyOperate();
  }else{
    fill(0xEE);
    textAlign(CENTER,CENTER);text("P A U S E",width/2,height/2);
    textAlign(LEFT,TOP);
  }
  drStatus();
  fill(0xFF-CST_DEFAULT_COLOR,0xFF,0xFF-CST_DEFAULT_COLOR);
  drBrief(!pbHideInfo);drInfo(!pbHideInfo);
  int lpMillisB=millis();
  if(!pbHideInfo){fnTextInt("ms/f:",lpMillisB-lpMillisA,27);}
  textAlign(RIGHT,TOP);text("Score:"+nf(pbTheCord.cmScore,6),width-5,2);textAlign(LEFT,TOP);
  //--
}//+++


void keyPressed(){switch(key){
  case 'n':pbHideInfo=!pbHideInfo;break;
  case 'm':pbPause=!pbPause;break;
  case ' ':if(ship.ccReborn()){pbTheCord.cmScore=0;}break;
  case 'q':fsPover();break;
  default:break;
}}//+++


void keyReleased(){
  if(key=='j' || key=='i' || key=='l'){
    pbCoolDownInterval=16;
  }
}//+++


void fsPover(){
  //--
  exit();
  //--
}//+++


void fsCheckCamera(){
  PVector lpDist=PVector.sub(ship.cmAbsolutePos,pbTheOrigin);
  if(lpDist.mag()>32){
    lpDist.normalize();
    lpDist.mult(ship.velocity.mag());
    pbTheMap.cmPos.sub(lpDist);
  }
}//+++

void fnTextFloat(String pxTitle, float pxVal, int pxLine){boolean lpRight=pxLine>(height/16);
  text(pxTitle+nfc(pxVal,2),lpRight?width/2+16:16,16*(lpRight?pxLine-height/16:pxLine));
}//+++

void fnTextInt(String pxTitle, int pxVal, int pxLine){boolean lpRight=pxLine>(height/16);
  text(pxTitle+nf(pxVal,4),lpRight?width/2+16:16,16*(lpRight?pxLine-height/16:pxLine));
}//+++

void fnTextString(String pxTitle, int pxLine){boolean lpRight=pxLine>(height/16);
  text(pxTitle,lpRight?width/2+16:16,16*(lpRight?pxLine-height/16:pxLine));
}//+++

//** *** *** ***
//-- Operate
//** *** *** ***

void fsKeyOperate(){
    if (keyPressed) {switch(key){
    case 'a':ship.turn(-0.15);ship.cmTurnPlus   = true;break;
    case 'd':ship.turn( 0.15);ship.cmTurnMinus  = true;break;
    case 'w':ship.thrust( 1);ship.thrusting     = true;break;
    case 's':ship.thrust(-1);ship.backThrusting = true;break;
    //--
    case 'u':fsMegaBeamLauncher();break;
    case 'l':fsBeamMachineGun();break;
    case 'j':case 'e':fsBeamRifle();break;
    case 'i':fsSprayParticleCannon();break;
    case 'k':case 'f':ship.cmBarrier = (ship.cmEN>5);break;
    //--
    default:break;
  }}
}//+++


void fsMegaBeamLauncher(){if(pbCoolDownInterval!=0){return;}
  int lpCost=10;
  ship.cmCharge+=6;
  if(ship.cmEN<lpCost){pbCoolDownInterval=16;return;}
  if(ship.cmCharge>=32){pbTheCord.cmBeam.ccShoot();ship.ccShiftEN(-lpCost);}
}//+++


void fsBeamRifle(){if(pbCoolDownInterval!=0){return;}pbCoolDownInterval=16;
  if(ship.cmCartridge==0){return;}
  pbTheCord.cmBulletList.ccShoot(ship.location,ship.heading,'m');ship.ccShiftCartridge(-1);
}//+++


void fsSprayParticleCannon(){if(pbCoolDownInterval!=0){return;}pbCoolDownInterval=32;
  int lpCost=60;
  if(ship.cmEN<lpCost){return;}
  pbTheCord.cmBulletList.ccShoot(ship.location,ship.heading,'k');ship.ccShiftEN(-lpCost);
}//+++


void fsBeamMachineGun(){if(pbCoolDownInterval!=0){return;}
  int lpCost=5;
  if(ship.cmEN<lpCost*4){return;}
  pbTheCord.cmBulletList.ccShoot(ship.location,ship.heading,'x');ship.ccShiftEN(-lpCost);
}//+++


void drStatus(){
  fill(0x55);rect(50,4,120,4);
  fill(0xEE,0x77,0x77);rect(50,5,map(ship.cmHP,0,ship.cmMaxHP,1,120),2);
  fill(0x55);rect(50,8,120,4);
  fill(0x77,0x77,0xEE);rect(50,9,map(ship.cmEN,0,ship.cmMaxEN,1,120),2);
  fill(CST_DEFAULT_COLOR<0xAA?0xFF:0x0);
  text("LV:"+nf(ship.cmLV,2),8,1);
  text("BR:"+nf(ship.cmCartridge,2),175,1);
  if(ship.cmReloadTime!=0){text("Reloading...",175,17);}
  if(ship.cmHP<5){
    textAlign(CENTER,CENTER);text("YOU ARE DEAD \n Press SpaceBar to Start a New Game",width/2,height/2);
    textAlign(LEFT,TOP);
  }
  //--
}//+++


void drBrief(boolean pxAct){if(!pxAct){return;}
  if(pbLeRoller<20){
  fnTextString("[W][S][A][D]to move ship...",2);
  fnTextString("[Q] to exit [M]to pause [N]to hide Info...",3);
  fnTextString("[J]Beam Rifle [K] D-Field...",4);
  fnTextString("[U]Hi-Mega Launcher [I]SP-Cannon [L] Beam-MG...",5);}
}//+++


void drInfo(boolean pxAct){if(!pxAct){return;}
  fnTextFloat("fps:",frameRate,9);
  fnTextFloat("cameraX:",pbTheMap.cmPos.x,11);
  fnTextFloat("cameraY:",pbTheMap.cmPos.y,12);
  fnTextFloat("shipX:",ship.location.x,13);
  fnTextFloat("shipY:",ship.location.y,14);
  fnTextInt("CD:",pbCoolDownInterval,25);
  fnTextFloat("speed:",ship.velocity.mag(),26);
  fnTextInt("bullets:",pbTheCord.cmBulletList.cmPos.size(),28);
  fnTextInt("dummies:",pbTheCord.cmDummyList.size(),29);
}//+++

//** *** *** ***
//-- Class
//** *** *** ***

class EcMap{
  //--
  PVector cmPos;
  PVector cmDia;
  int cmColorSet;
  //--
  EcMap(int cmColor){
    cmPos=new PVector(32,32);
    cmDia=new PVector(1400,1400);
    cmColorSet=cmColor;
  }
  //--
  void ccUpdate(){
    fill(cmColorSet,cmColorSet);rect(cmPos.x,cmPos.y,cmDia.x,cmDia.y);
    float lpDiv=cmDia.x/8;
    fill(0xFF-cmColorSet);
    for(int i=0;i<64;i++){
      text(nf(i,3),i%8*lpDiv+cmPos.x,i/8*lpDiv+cmPos.y);
    }
  }
  //--
}//+++


class EcCordinator{
  //--
  EcMap cmMap;
  Spaceship cmProtagon;
  //--
  ArrayList<EcDummy> cmDummyList;
  EcBulletList cmBulletList;
  EcBulletList cmHarmfulList;
  EcMega cmBeam;
  //--
  int cmScore;
  //--
  EcCordinator(EcMap pxMap,Spaceship pxShip,int pxInitDummyAmount){
    //--
    cmMap=pxMap;
    cmProtagon=pxShip;
    //--
    cmDummyList=new ArrayList<EcDummy>();
    cmBulletList= new EcBulletList(cmMap);
    cmHarmfulList= new EcBulletList(cmMap);
    cmBeam= new EcMega(cmMap,cmProtagon);
    for(int i=0;i<pxInitDummyAmount;i++){cmDummyList.add(new EcDummy(cmMap));}
    cmScore=0;
  }
  //--
  void ccNewDummy(int pxMax){if(cmDummyList.size()>(pxMax-1)){return;}
    cmDummyList.add(new EcDummy(cmMap));
  }
  void ccUpdate(){
    cmBulletList.ccUpdate();
    cmHarmfulList.ccUpdate();
    cmBeam.ccUpdate();
    cmProtagon.ccCheckShots(cmHarmfulList);
    for (int i = cmDummyList.size()-1; i >= 0; i--) {
      EcDummy lpDummy=cmDummyList.get(i);
      lpDummy.ccUpdate();
      if(lpDummy.ccCheckShots(cmBulletList,cmBeam,cmProtagon)){
        PVector lpGather=PVector.sub(cmProtagon.location,lpDummy.cmRelativePos);
        cmHarmfulList.ccShoot(lpDummy.cmRelativePos,lpGather.heading()+PI/2+random(-0.5,0.5),'n');
      }
      if(lpDummy.ccIsDestroyed()){
        cmDummyList.remove(i);cmScore+=cmProtagon.cmBarrier?2:10;
        cmProtagon.ccGainingEXP(5);
      }
      //--
    }
    //--
  }
  //--
}//+++


class EcDummy{
  //--
  EcMap cmMap;
  //--
  PVector cmRelativePos,cmSpeed,cmAccel,cmWonder;
  float cmNoiseX,cmNoiseY;
  float cmNAdjX, cmNAdjY;
  float cmMaxSpeed;
  //--
  int cmExplodeTimer;
  int cmShowsHpGaugeTimer;
  //--
  float cmHP;
  float cmScale;
  //--
  EcDummy(EcMap pxMap){
    cmMap=pxMap;
    cmRelativePos=new PVector(random(cmMap.cmDia.x),random(cmMap.cmDia.x));
    cmSpeed=new PVector(0,0);
    cmAccel=new PVector(0,0);
    cmWonder=new PVector(0,0);
    cmNoiseX=random(5,31); cmNAdjX=0.01;
    cmNoiseY=random(32,60); cmNAdjY=0.01;
    cmMaxSpeed=4;
    cmExplodeTimer=0;
    cmShowsHpGaugeTimer=0;
    cmHP=192;
    cmScale=2;
  }
  //--
  void ccUpdate(){
    float lpNoiseX=map(noise(cmNoiseX),0,1,-cmMaxSpeed,cmMaxSpeed);
    float lpNoiseY=map(noise(cmNoiseY),0,1,-cmMaxSpeed,cmMaxSpeed);
    cmWonder.x=lpNoiseX;
    cmWonder.y=lpNoiseY;
    cmNoiseX+=cmNAdjX;if(cmNoiseX<1 || cmNoiseX>64){cmNAdjX*=-1;}
    cmNoiseY+=cmNAdjY;if(cmNoiseY<1 || cmNoiseY>64){cmNAdjY*=-1;}
    //--
    cmSpeed.add(cmAccel);
    cmSpeed.limit(cmMaxSpeed);
    cmRelativePos.add(cmSpeed);
    cmRelativePos.add(cmWonder);
    //--
    if(ccIsHangingOnEdge(10)){if(random(1)<0.1){
      PVector lpCenter=cmMap.cmDia.get();
      lpCenter.mult(0.5);
      PVector lpTowardsCenter=PVector.sub(lpCenter,cmRelativePos);
      lpTowardsCenter.normalize();lpTowardsCenter.mult(16);
      cmAccel.add(lpTowardsCenter);
    }}
    //--
    cmRelativePos.x=constrain(cmRelativePos.x,0,cmMap.cmDia.x);
    cmRelativePos.y=constrain(cmRelativePos.y,0,cmMap.cmDia.y);
    float lpAbsoX=cmMap.cmPos.x+cmRelativePos.x;
    float lpAbsoY=cmMap.cmPos.y+cmRelativePos.y;
    //--
    cmExplodeTimer--;if(cmExplodeTimer<0){cmExplodeTimer=0;}
    cmShowsHpGaugeTimer--;if(cmShowsHpGaugeTimer<0){cmShowsHpGaugeTimer=0;}
    cmScale+=0.5;if(cmScale>10){cmScale=10;}
    //--
    pushMatrix();translate(lpAbsoX,lpAbsoY);rotate(cmWonder.heading());rectMode(CENTER);
    stroke(0);fill(0xEE);rect(0,0,cmScale,cmScale*2);
    noStroke();fill(0x33);rect(0,-cmScale/2,cmScale/2,cmScale/2);
    rectMode(CORNER);
    if(cmExplodeTimer!=0){fill(0xEE,0x77,0x77,0xAA);ellipse(random(8),random(8),16,16);}
    popMatrix();
    //--
    if(cmShowsHpGaugeTimer!=0){
      fill(0x33);rect(lpAbsoX-10,lpAbsoY-10,20,4);
      fill(0x33,0xEE,0x33);rect(lpAbsoX-10,lpAbsoY-10,map(cmHP,0,200,1,20),4);
    }
    //--
  }
  //--
  void ccSeek(PVector pxTarget){
    PVector lpDesired=PVector.sub(pxTarget,cmRelativePos);
    lpDesired.setMag(cmMaxSpeed);
    PVector lpSteer = PVector.sub(lpDesired,cmSpeed);
    lpSteer.setMag(1);
    cmAccel.add(lpSteer);
  }
  //--
  boolean ccCheckShots(EcBulletList pxList,EcMega pxBeam,Spaceship pxShip){
    boolean lpFightBack=false;
    ArrayList<PVector> lpList=pxList.ccGetList();
    PVector lpProtagonPos=pxShip.location.get();
    for (int i = lpList.size()-1; i >= 0; i--) {
      PVector lpBullet = lpList.get(i);
      float lpDist = PVector.dist(cmRelativePos,lpBullet);
      if(lpDist<20){ccDamaged(pxList.cmDamage);}
      if(lpDist<60){if(random(1)<0.5){ccAvoid(20);}}
    }
    float lpProtagonDist = PVector.dist(cmRelativePos,lpProtagonPos);
    if(lpProtagonDist<80 && pxShip.cmBarrier){ccDamaged(4);}
    if(lpProtagonDist<16 && !pxShip.cmBarrier){pxShip.ccShiftHP(-2.0);}
    
    if(lpProtagonDist>200 && lpProtagonDist<220  ){
      ccSeek(pxShip.location);
      lpFightBack=(random(1)<0.5);
    }else{/*still not sure what to do*/;}
    //--
    if(pxBeam.ccTellDist(cmRelativePos)<pxBeam.cmRadius){ccDamaged(10);}
    //--
    return lpFightBack;
  }
  //--
  boolean ccIsDestroyed(){return cmHP<5;}
  //--
  boolean ccIsHangingOnEdge(float pxMargin){
    return (cmRelativePos.x<pxMargin)||(cmRelativePos.x>cmMap.cmDia.x-pxMargin)||(cmRelativePos.y<pxMargin)||(cmRelativePos.y>cmMap.cmDia.y-pxMargin);
  }
  //--
  void ccAvoid(float pxForce){
    PVector lpAvoid=PVector.random2D();lpAvoid.mult(pxForce);
    cmAccel.add(lpAvoid);
  } 
  void ccDamaged(float pxDamage){
    cmExplodeTimer=7;cmShowsHpGaugeTimer=32;
    cmHP-=pxDamage;
    ccAvoid(40);
  }
  //--
}//+++



class EcBulletList{
  //--
  EcMap cmMap;
  ArrayList<PVector> cmPos;
  ArrayList<PVector> cmSpeed;
  //--
  int cmCoolDownTimer;
  float cmDamage;
  //--
  int cmColor;
  int cmLength;
  //--
  EcBulletList(EcMap pxMap){
    cmPos = new ArrayList();
    cmSpeed = new ArrayList();
    cmMap=pxMap;
    cmCoolDownTimer=0;
    cmDamage=10;
    cmColor=color(0xFF,0x77,0x77);
    cmLength=20;
  }
  //--
  void ccUpdate(){
    ccUpdateBullets();
    cmCoolDownTimer--;if(cmCoolDownTimer<0){cmCoolDownTimer=0;} 
  }
  void ccUpdateBullets(){for(int i=cmPos.size()-1;i>=0;i--){
    PVector lpPos=cmPos.get(i);
    PVector lpSpeed=cmSpeed.get(i);
    float lpHeading=lpSpeed.heading();
    float lpAbsX=cmMap.cmPos.x+lpPos.x;
    float lpAbsY=cmMap.cmPos.y+lpPos.y;
    lpPos.add(lpSpeed);
    //--
    pushMatrix();translate(lpAbsX,lpAbsY);rotate(lpHeading);
    fill(cmColor);rect(0,-1,cmLength,2);
    popMatrix();
    //--
    //--
    if(lpPos.x<0 || lpPos.x>cmMap.cmDia.x ||
       lpPos.y<0 || lpPos.y>cmMap.cmDia.y
    ){cmPos.remove(i);cmSpeed.remove(i);}
    //--
  }}
  //--
  ArrayList ccGetList(){return cmPos;}
  void ccShoot(PVector pxPos, float pxHeading, char pxMode){
    //--
    float lpRandom=0.1;
    //--
    if(pxMode=='x')
      {if(cmCoolDownTimer!=0){return;}cmLength=10;cmColor=color(0x77,0x77,0xFF);lpRandom=0.3;cmDamage=10;}   
    //--
    if(pxMode=='m')
      {if(cmCoolDownTimer!=0){return;}else{cmCoolDownTimer=16;}cmLength=24;cmColor=color(0xFF,0x77,0x77);lpRandom=0.03;cmDamage=150;}
    //--
    if(pxMode=='n')
      {if(cmCoolDownTimer!=0){return;}else{cmCoolDownTimer=16;}cmLength=30;cmColor=color(0x77,0xFF,0xFF);lpRandom=0.03;cmDamage=4;}
    //--
    if(pxMode=='k')
      {if(cmCoolDownTimer!=0){return;}else{cmCoolDownTimer=32;}cmLength=66;cmColor=color(0xEE,0xEE,0x77);lpRandom=0.4;cmDamage=20;}
    //--
    int lpCounts=(pxMode=='k')?32:1;
    for(int i=0;i<lpCounts;i++){
    cmPos.add(pxPos.get());
    float lpHeading = pxHeading -PI/2 +random(-1*lpRandom,lpRandom);
    PVector lpSpeed = new PVector(cos(lpHeading),sin(lpHeading));
    //--
    lpSpeed.mult(32);
    cmSpeed.add(lpSpeed.get());
    }
  }
  //--
}//+++

class EcMega{
  //--
  EcMap cmMap;
  Spaceship cmShip;
  PVector cmStart;
  PVector cmEnd;
  float cmRadius;
  //--
  EcMega(EcMap pxMap, Spaceship pxShip){
    cmStart=new PVector(99,99);
    cmEnd=new PVector(2800,2800);
    cmRadius=0;
    cmMap=pxMap;
    cmShip=pxShip;
  }
  //--
  void ccUpdate(){
    //--
    cmRadius-=2;if(cmRadius<0){cmRadius=0;}
    cmShip.cmBeam=(cmRadius!=0);
    //--
    if(cmRadius==0){return;}else{
      strokeWeight(cmRadius*2);
      stroke(0xEE,0xDD,0x77,192);
      line(cmMap.cmPos.x+cmStart.x,cmMap.cmPos.y+cmStart.y,cmMap.cmPos.x+cmEnd.x,cmMap.cmPos.y+cmEnd.y);
      strokeWeight(1);noStroke();
    }
    //--
  }
  //--
  float ccTellDist(PVector pxTarget){
    PVector lpSitA=PVector.sub(pxTarget,cmStart);
    PVector lpSitB=PVector.sub(pxTarget,cmEnd);
    float lpAngle=PVector.angleBetween(lpSitA,lpSitB);
    if(lpAngle<(PI/2)){return 99;}
    PVector lpNorm=getNormalPoint(pxTarget,cmStart,cmEnd);
    return PVector.dist(lpNorm,pxTarget);
  }
  //--
  PVector getNormalPoint(PVector p, PVector a, PVector b) {
    PVector ap = PVector.sub(p, a);
    PVector ab = PVector.sub(b, a);
    ab.normalize();
    ab.mult(ap.dot(ab));
    PVector normalPoint = PVector.add(a, ab);
    return normalPoint;
  }
  //--
  void ccShoot(){
    cmRadius=45;cmStart=cmShip.location;
    cmEnd=new PVector(1,0);
    cmEnd.rotate(cmShip.heading-PI/2);
    cmEnd.normalize();
    cmEnd.mult(2800);
  }
  //--
}//+++

class Spaceship {
  //--
  EcMap cmMap;
  //--
  PVector cmAbsolutePos;
  //--
  PVector location,velocity,acceleration;
  float damping,topspeed,heading,r;
  boolean thrusting,backThrusting;
  //--
  boolean cmTurnMinus,cmTurnPlus;
  //--
  boolean cmBarrier,cmAnch,cmBeam;
  int cmCharge;
  //--
  float cmHP,cmMaxHP;
  float cmEN,cmMaxEN;
  int cmCartridge,cmReloadTime;
  int cmEXP,cmLV,cmPreLV;
  int cmExplodeTimer;
  int cmShowLevelUpTimer;
  //--
  Spaceship(EcMap pxMap) {
    //--
    cmMap=pxMap;
    cmAbsolutePos = new PVector();
    location = new PVector(99,99);velocity = new PVector();acceleration = new PVector();
    damping=0.995;topspeed=8;heading=2;r=8;
    thrusting=false;backThrusting=false;
    cmTurnMinus=false;cmTurnPlus=false;
    cmBarrier=false;cmAnch=false;cmBeam=false;cmCharge=0;
    cmHP=200;cmEN=200;cmCartridge=16;cmEXP=0;
    cmMaxHP=200;cmMaxEN=200;cmReloadTime=0;cmLV=1;
    //--
    cmExplodeTimer=0;
    cmPreLV=1;
    //--
  } 
  //--
  void ccRefresh(){if(cmHP<5){velocity.mult(0.01);return;}update();display();}
  void update() { 
    damping=cmBarrier?0.9:0.995;if(cmAnch){damping=0.8;}
    velocity.add(acceleration);velocity.mult(damping);velocity.limit(topspeed);
    location.add(velocity);
    location.x=constrain(location.x,0,cmMap.cmDia.x);
    location.y=constrain(location.y,0,cmMap.cmDia.y);
    acceleration.mult(0);
    //--
    cmExplodeTimer--;if(cmExplodeTimer<0){cmExplodeTimer=0;}
    cmReloadTime--;if(cmReloadTime<0){cmReloadTime=0;}
    cmShowLevelUpTimer--;if(cmShowLevelUpTimer<0){cmShowLevelUpTimer=0;}
    if(cmReloadTime==1){cmCartridge=16;}
    //--
    cmCharge-=5;cmCharge=constrain(cmCharge,0,32);
    cmAnch=cmBeam || (cmCharge>0);
    ccShiftEN(cmMaxEN/400);
    if(cmBarrier){ccShiftEN(-1*cmMaxEN/300);}
    //--
  }
  //--
  void ccCheckShots(EcBulletList pxList){
    ArrayList<PVector> lpList=pxList.ccGetList();
    for (int i = lpList.size()-1; i >= 0; i--) {
      PVector lpBullet = lpList.get(i);
      float lpDist = PVector.dist(location,lpBullet);
      if(lpDist<20){ccShiftHP(-1*pxList.cmDamage);}
    }
  }
  //--
  void ccShiftHP(float pxOffset){
    cmHP+=pxOffset;cmHP=constrain(cmHP,0,cmMaxHP);
    cmExplodeTimer=7;
  }
  //--
  void ccShiftEN(float pxOffset){cmEN+=pxOffset;cmEN=constrain(cmEN,0,cmMaxEN);}
  void ccShiftCartridge(int pxOffset){
    cmCartridge+=pxOffset;cmCartridge=constrain(cmCartridge,0,16);
    if(cmCartridge==0){cmReloadTime=128;}
  }
  //--
  void applyForce(PVector force) {PVector f = force.get();acceleration.add(f);}
  void turn(float a) {heading += cmAnch?0:a;thrust(a*0.3);}
  //--
  void thrust(float pxDirect) {
    float angle = heading - pxDirect*PI/2;
    PVector force = new PVector(cos(angle),sin(angle));
    force.mult(cmAnch?0:0.3);
    applyForce(force);
    ccShiftEN(-1*cmMaxEN/300);
  }
  //--
  void display() { 
    //--
    cmAbsolutePos=PVector.add(location,cmMap.cmPos);
    //--
    pushMatrix();translate(cmAbsolutePos.x,cmAbsolutePos.y);rotate(heading);rectMode(CENTER);noStroke();
    //--
    if(cmBarrier&&(frameCount%3==1)){fill(0xEE,0xEE);ellipse(0,0,128,128);};
    fill(thrusting?color(0x77,0x77,0xFF):color(0x33,0x11));rect(-r/2,r,r/2,2*r);rect(r/2,r,r/2,2*r);
    fill(backThrusting?color(0x77,0x77,0xFF):color(0x33,0x11));rect(-r*3/4,-r/2,r/4,r);rect(r*3/4,-r/2,r/4,r);
    fill(cmTurnMinus?color(0x77,0x77,0xFF):color(0x33,0x11));rect(-r,-r,10,r/4);rect( r,0,10,r/4);
    fill(cmTurnPlus?color(0x77,0x77,0xFF):color(0x33,0x11));rect( r,-r,10,4/4);rect(-r,0,10,r/4);
    //--
    fill(175);stroke(0);beginShape();
    vertex(-r,r);vertex(0,-2*r);vertex(r,r);
    endShape(CLOSE);
    rectMode(CORNER);noStroke();
    if(cmExplodeTimer!=0){fill(0xFF,0xAA);ellipse(random(8),random(8),16,16);}
    popMatrix();
    if(cmShowLevelUpTimer!=0){fill(0xFF);text("LevelUP!!",cmAbsolutePos.x,cmAbsolutePos.y-8+2*cmShowLevelUpTimer);}
    //--
    if(cmCharge>0){
      fill(0xAA);rect(cmAbsolutePos.x-7,cmAbsolutePos.y+3,20,4);
      fill(0xFF);rect(cmAbsolutePos.x-8,cmAbsolutePos.y+4,map(cmCharge,1,32,1,20),2);
    }
    //--
    thrusting=false;backThrusting=false;
    cmTurnMinus=false;cmTurnPlus=false;
    cmBarrier=false;
  }
  //--
  boolean ccReborn(){if(cmHP>=5){return false;}
    cmHP=cmMaxHP;
    cmEN=cmMaxEN;
    cmReloadTime=2;
    cmEXP=0;
    cmLV=1;
    location.add(new PVector(random(100),random(100)));
    velocity=new PVector(8,8);
    return true;
  }
  //--
  void ccGainingEXP(int pxEXP){
    int lpLV=0;
    cmEXP+=5;
    for(int i=27;i>0;i--){if(cmEXP>int(pow(2,i+1))){
      lpLV=i;break;
    }}
    //--
    cmPreLV=cmLV;cmLV=lpLV;
    if(cmLV>cmPreLV){ccLevelUp();}
    //--
  }
  //--
  void ccLevelUp(){
    cmShowLevelUpTimer=32;
    cmMaxHP=200+cmLV* 2;cmHP=cmMaxHP;
    cmMaxEN=200+cmLV*25;cmEN=cmMaxEN;
    topspeed=8+cmLV*2;
  }
  //--
}//+++

 



最后编辑:
作者:constrain
nullpointerexception

习作AD_带场景地图的喷气飞船_扩展v1.02》有 4 条评论

  1. 卡萨布兰卡 说:

卡萨布兰卡的回复 取消回复

你的email不会被公开。