00001 /************************************************************************* 00002 fgitem.cc 00003 Project Piedmont game 00004 ------------------------- 00005 00006 Date Created: 11/08/00 Last Modified: 05/08/08 00007 Programmer: David Foster 00008 Copyright: (C) 2000 David Foster (see file COPYING) 00009 E-mail: dfoster@computer.org 00010 00011 Description: 00012 00013 This program is free software; you can redistribute it and/or modify 00014 it under the terms of the GNU General Public License as published by 00015 the Free Software Foundation; either version 2 of the License, or 00016 (at your option) any later version. 00017 00018 This program is distributed in the hope that it will be useful, 00019 but WITHOUT ANY WARRANTY; without even the implied warranty of 00020 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00021 GNU General Public License for more details. 00022 00023 You should have received a copy of the GNU General Public License 00024 along with this program; if not, write to the Free Software 00025 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00026 00027 ************************************************************************/ 00028 00029 #include "fgitem.h" 00030 00031 FGItem::FGItem(){ 00032 num_frames = 1; 00033 x = -1; 00034 y = -1; 00035 next_level = 0; 00036 direction = 0; 00037 need_key = 0; 00038 point_change = 0; 00039 score_change = 0; 00040 monster_index = 0; 00041 hitpoints = 0; 00042 damage = 0; 00043 stationary = 1; 00044 rate_div = 0; 00045 rate_count = 0; 00046 animate = 0; 00047 walkable = 0; 00048 } 00049 00050 FGItem::~FGItem(){ 00051 } 00052 00053 void FGItem::set_nextlevel(int first_prop){ 00054 next_level = first_prop; 00055 } 00056 00057 void FGItem::set_needkey(int first_prop){ 00058 need_key = first_prop; 00059 set_rate_div(15); 00060 } 00061 00062 void FGItem::set_pointchange(int first_prop){ 00063 point_change = first_prop; 00064 } 00065 00066 void FGItem::set_scorechange(int first_prop){ 00067 score_change = first_prop; 00068 } 00069 00070 void FGItem::set_damage(int first_prop){ 00071 damage = first_prop; 00072 } 00073 00074 void FGItem::set_teleporterinfo(int first_prop, int second_prop){ 00075 x = first_prop; 00076 y = second_prop; 00077 set_rate_div(5); 00078 animate = 1; 00079 direction = 1; // count up 00080 } 00081 00082 void FGItem::set_generatorinfo(int first_prop, int second_prop){ 00083 monster_index = first_prop; 00084 hitpoints = second_prop; 00085 } 00086 00087 void FGItem::set_numframes(int first_prop){ 00088 num_frames = first_prop; 00089 } 00090 00091 void FGItem::set_rate_div(int rate){ 00092 rate_div = rate; 00093 } 00094 00095 int FGItem::activate(){ 00096 int i = get_index(); 00097 if(i >= KEY_BEGIN && i <= KEY_END){ 00098 pinfo->keys++; 00099 return(1); 00100 }else if(i >= TELEPORTER_BEGIN && i <= TELEPORTER_END){ 00101 if(current_frame == (num_frames - 1)) direction = -1; 00102 if(current_frame == 0) direction = 1; 00103 if (rate_count == rate_div){ 00104 current_frame += direction; 00105 rate_count = 0; 00106 }else{ 00107 rate_count++; 00108 } 00109 // pinfo->paul_ax = x; 00110 // pinfo->paul_ay = y; 00111 // pinfo->map_ax = pinfo->paul_ax + (pinfo->tilesize/2) - (pinfo->win_width/2); 00112 // pinfo->map_ay = pinfo->paul_ay + (pinfo->tilesize/2) - (pinfo->win_height/2); 00113 return(0); 00114 }else if(i >= EXIT_BEGIN && i <= EXIT_END){ 00115 return(0); 00116 }else if(i >= SCROLL_BEGIN && i <= SCROLL_END){ 00117 return(0); 00118 }else if(i >= DOOR_BEGIN && i <= DOOR_END){ 00119 if (current_frame != num_frames - 1){ 00120 if (animate){ 00121 if (rate_count == rate_div){ 00122 current_frame++; 00123 rate_count = 0; 00124 }else{ 00125 rate_count++; 00126 } 00127 }else{ 00128 if (need_key & pinfo->keys > 0){ 00129 animate = 1; 00130 rate_count++; 00131 pinfo->keys--; 00132 }else if( !need_key){ 00133 animate = 1; 00134 rate_count++; 00135 }else{ 00136 } 00137 } 00138 }else{ 00139 walkable = 1; 00140 animate = 0; 00141 } 00142 return(0); 00143 }else if(i >= POWERUP_BEGIN && i <= POWERUP_END){ 00144 pinfo->hitpoints += point_change; 00145 return(1); 00146 }else if(i >= GOLD_BEGIN && i <= GOLD_END){ 00147 pinfo->score += score_change; 00148 return(1); 00149 }else if(i >= GENERATOR_BEGIN && i <= GENERATOR_END){ 00150 return(0); 00151 }else if(i >= TRAPS_BEGIN && i <= TRAPS_END){ 00152 pinfo->hitpoints -= damage; 00153 return(1); 00154 } 00155 return(0); 00156 } 00157 00158 void FGItem::update(){ 00159 if(FGITEM_DEBUG)printf("update fgitem (%d %d)\n", box.x, box.y); 00160 00161 // Because box is a tile location and tile_start|stop are tile locations, we can 00162 // do a simple compare to figure out if any part of this FGE is onscreen 00163 if((box.x >= pinfo->tile_start_x) && (box.x <= pinfo->tile_stop_x) && 00164 (box.y >= pinfo->tile_start_y) && (box.y <= pinfo->tile_stop_y)){ 00165 onscreen = 1; 00166 // This is not a particularly intuitive calculation 00167 // box - tile_start gives us the relative location of the tile (in t units) in the window 00168 // Multiply by tilesize to get the pixel location 00169 // Add tile_aN_in_win to get the tile offset if the window isn't aligned on tile borders 00170 current_box.x = pinfo->tile_ax_in_win + (box.x - pinfo->tile_start_x)*pinfo->tilesize; 00171 current_box.y = pinfo->tile_ay_in_win + (box.y - pinfo->tile_start_y)*pinfo->tilesize; 00172 00173 }else{ 00174 onscreen = 0; 00175 } 00176 00177 } 00178