home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR3 / FGSCROLL.ZIP / SPRITES.H < prev    next >
C/C++ Source or Header  |  1993-10-29  |  8KB  |  292 lines

  1. const unsigned char AHERO = 200;
  2.  
  3. const int SMALLESTTILE = 1; // don't break down the grid yet
  4.  
  5. //* used by sprite objects
  6.  
  7. void appendsprite(int newx, int newy, int SpriteType);
  8.  
  9.  
  10.  
  11.  
  12. class Icon_Obj {
  13. public:
  14.     unsigned char numframes;          // total number of frames in sprite animation
  15.     unsigned char currentframe;       // pointer to current frame (frame count)
  16.     char hitpoints;                   // number of points it takes to die
  17.     Boolean destroyed;                // set to true of sprite was killed
  18.     unsigned char pointvalue;         // number of points you get when the sprite is destoryed
  19.     unsigned char Tile2Draw;          // unique sprite value for indexing
  20.     int X;                            // cordinates of sprite
  21.     int Y;
  22.     // working temp variables
  23.     unsigned char indexoffset,spritewidth,spriteheigth;
  24.     int tempx,tempy;
  25.  
  26.      Icon_Obj(int startx, int starty);
  27.      virtual void Draw();
  28.      virtual void Move();
  29.      virtual void MoveTo(int newx, int newy);
  30.      virtual void FillMap();
  31.      virtual Boolean Collision();
  32.      virtual void DestroySprite();
  33.      virtual Boolean IsDestroyed();
  34.      virtual void DrawWreckage();
  35.      virtual Boolean badicon(unsigned char avalue);
  36.                  // returns true if the values in the
  37.                  // global map will cause this sprite
  38.                  // to blow up
  39.      virtual void calcoffsets();
  40. };
  41.  
  42.  Icon_Obj::Icon_Obj(int startx, int starty){
  43.     numframes     = 0;
  44.     currentframe  = 0;
  45.     hitpoints     = 0;
  46.     destroyed     = false;
  47.     pointvalue    = 0;
  48.     Tile2Draw     = 0;
  49.     X             = startx;
  50.     Y             = starty;
  51.  };
  52.  
  53.  void Icon_Obj::calcoffsets(){
  54.  
  55.   indexoffset = Tile2Draw + currentframe;
  56.   tempx = TILES[indexoffset].x; // first tile value, plus frame advance value
  57.   tempy = TILES[indexoffset].y;
  58.   spritewidth = TILES[indexoffset].spritewidth /SMALLESTTILE;
  59.   spriteheigth = TILES[indexoffset].spriteheigth /SMALLESTTILE;
  60.    if(spritewidth == 0) spritewidth++; if(spriteheigth == 0) spriteheigth++; // keep an index of at least 1
  61.   currentframe++;
  62.   if(currentframe > numframes) currentframe = 0;
  63.  // fills the global play map with the sprites value
  64.  
  65.    };
  66.   void Icon_Obj::FillMap(){
  67.   // fills the global play map with the sprites value
  68.  
  69.  
  70.    int ty = Y/SMALLESTTILE;
  71.    int tx = X/SMALLESTTILE;
  72.  
  73.    unsigned char temph,tempw;
  74.    for(temph=0;temph<spriteheigth;temph++){
  75.     for(tempw=0;tempw<spritewidth;tempw++){
  76.      spritemap[temph+ty][tempw+tx] = Tile2Draw;
  77.        }
  78.      }
  79.  
  80.  };
  81.  
  82.  
  83.  // will the values in the table cause this icon to blow up??
  84.  Boolean Icon_Obj::badicon(unsigned char avalue){
  85.       if(avalue < 3) return true;
  86.       return false;
  87.  };
  88.  
  89.  Boolean Icon_Obj::Collision(){
  90.  
  91.    unsigned char temph,tempw,avalue;
  92.    Boolean retval = false;
  93.    int ty = Y/SMALLESTTILE;
  94.    int tx = X/SMALLESTTILE;
  95.    for(temph=0;temph<spriteheigth;temph++){
  96.     for(tempw=0;tempw<spritewidth;tempw++){
  97.     avalue = spritemap[temph+ty][tempw+tx];
  98.     if(badicon(avalue)) retval = true; // if something there besides the tile then explode
  99.        }
  100.      }
  101.   return retval;
  102.  };
  103.  
  104.  
  105.  void Icon_Obj::Draw(){
  106.  // trasfer icon from tile page to backdrop
  107.  int sw,sh;
  108.  sw = TILES[indexoffset].spritewidth;
  109.  sh = TILES[indexoffset].spriteheigth;
  110.  fg_tcxfer(tempx,tempx+sw,tempy,tempy+sh,X,Y+21,TILEPAGE,WORKPAGE);
  111.  
  112. };
  113.  
  114.  void Icon_Obj::DrawWreckage(){
  115.  // trasfer icon from tile page to backdrop
  116.  fg_tcxfer(170,170+8,0,0+8,X,Y,TILEPAGE,WORKPAGE);
  117. };
  118.  
  119. void Icon_Obj::Move(){
  120.   calcoffsets();
  121.   FillMap();
  122. //  currentframe++;       // cycle to the next sprite frame
  123. //  if(currentframe > numframes) currentframe = 0;
  124. };
  125.  
  126. void Icon_Obj::DestroySprite(){
  127.   destroyed = true;
  128.  };
  129.  
  130. Boolean Icon_Obj::IsDestroyed(){
  131.  
  132.  if(Collision()){
  133.     DestroySprite();
  134.     FillMap();
  135.     DrawWreckage();
  136.   }
  137.  
  138.   return destroyed;
  139. }
  140.  
  141. void Icon_Obj::MoveTo(int newx, int newy){
  142.  X=newx; Y=newy;
  143. };
  144.  
  145.  
  146. // fills in the map array with the tile number
  147. // used for collision detection
  148. // derive a class off of icon_obj
  149. class AHero : public Icon_Obj {
  150. public:
  151.  AHero(int startx, int starty);
  152.  virtual Boolean badicon(unsigned char avalue);
  153.  virtual void DestroySprite();
  154.  virtual void Move();
  155. };
  156.  
  157. void AHero::Move(){
  158.   currentframe = HerosDirection;
  159.   calcoffsets();
  160.   FillMap();
  161. };
  162. AHero::AHero(int startx, int starty) : Icon_Obj(startx,starty)
  163. {
  164.     numframes     = 0;
  165.     currentframe  = 0;
  166.     hitpoints     = 0;
  167.     destroyed     = false;
  168.     pointvalue    = 0;
  169.     Tile2Draw     = AHERO;         // TILES index value of this sprite
  170.     X             = startx;
  171.     Y             = starty;
  172. };
  173.  void AHero::DestroySprite(){
  174.  if(!GODMODEACTIVE){ // don't die if in god mode
  175.   destroyed = true;
  176.   DEAD = true;   // set global crash variable
  177.  }
  178.  };
  179.  
  180.  Boolean AHero::badicon(unsigned char avalue){
  181.       if(avalue > 2) return true;
  182.       return false;
  183.  };
  184.  
  185.  
  186.  
  187. //----- use this down here because Icon_Obj appends sprites to the
  188. //    list, but the atomstruct  references an Icon_Obj object
  189. // keep sprites in a list, cycle through only drawing visible sprites
  190. // once the level is complete, then delete all sprites from the list.
  191. //const unsigned char MAXSPRITES = 35;
  192.  
  193. void updateallsprites();
  194. void moveallsprites();
  195. void showallsprites();
  196. void freelist();
  197. Icon_Obj *geticon(int newx, int newy, unsigned char SpriteType);
  198.  
  199.  
  200.  
  201. struct atomstruct{
  202.    int avalue;
  203.    Icon_Obj *asprite;
  204.     struct atomstruct *prev;
  205.     struct atomstruct *next;
  206.    };
  207.  
  208. struct atomstruct *alist;  // the spritelist
  209. struct atomstruct *ptrlast;
  210.  
  211.  
  212. void appendsprite(int newx, int newy, int SpriteType){
  213. struct atomstruct *NEWATOM;
  214.  //  NEWATOM = (atomstruct *) malloc(sizeof(atomstruct));
  215.    NEWATOM = new atomstruct;
  216.    NEWATOM->asprite = geticon(newx,newy,SpriteType);
  217.    NEWATOM->avalue=SpriteType;
  218.    NEWATOM->next = NULL;
  219.    NEWATOM->prev = ptrlast;
  220.    ptrlast->next = NEWATOM;
  221.    ptrlast = NEWATOM;
  222. }
  223.  
  224. // destroys the whole list
  225. void freelist(){
  226. struct atomstruct *TEMP;
  227.    TEMP = ptrlast;
  228.   do{
  229.      delete TEMP->next->asprite;
  230.      delete TEMP->next;
  231.      TEMP=TEMP->prev;
  232.  
  233.    }while(TEMP != NULL);
  234.    alist->next = NULL;
  235.    ptrlast=alist;
  236. }
  237.  
  238.  
  239. // calls the Draw() member function for each object in the list
  240. void showallsprites(){
  241. struct atomstruct *TEMP;
  242.    TEMP=alist;
  243.  do{
  244.    TEMP->asprite->Draw();
  245.    TEMP=TEMP->next;
  246.    }while(TEMP != NULL);
  247. }
  248.  
  249. void moveallsprites(){
  250. struct atomstruct *TEMP;
  251.    TEMP=alist;
  252.  do{
  253.    TEMP->asprite->Move();
  254.    TEMP=TEMP->next;
  255.    }while(TEMP != NULL);
  256. }
  257.  
  258.  
  259. // calls the Move() member function for each object in the list
  260. void updateallsprites(){
  261. struct atomstruct *TEMP,*NOW,*NEXT,*PREV,*LAST;
  262.    NOW=alist;
  263. //   if(NOW->asprite->Move() == 99) {
  264.      // ship was blown up
  265. //    }
  266. //    else{
  267.       if(NOW->asprite->IsDestroyed());  // don't do anything just check if the ship is hit since the list
  268.                     // below starts with the second sprite in the list
  269.       while(NOW->next != NULL){
  270.  
  271.      if(NOW->next->asprite->IsDestroyed() == true){
  272.      NEXT = NOW->next;
  273.      NOW->next = NOW->next->next;
  274.      delete NEXT->asprite;
  275.      delete NEXT;
  276.      }
  277.      else
  278.      NOW = NOW->next;
  279.        }
  280.       ptrlast = NOW;
  281. //     } end else
  282. }
  283.  
  284. Icon_Obj *geticon(int newx, int newy, unsigned char SpriteType){
  285.  
  286. switch(SpriteType)
  287.    {
  288.     case AHERO : return new AHero(newx,newy);
  289.    }
  290. //   return new Icon_Obj(newx,newy,SpriteType);
  291.  return NULL;
  292. }