Snaked |
01-11-2016 11:09:54 |
estructuras para el posicionamiento de enemigos en el mapa
hola Ecfisa y compañia
veréis, tengo esta estructura que define una serie de enemigos en base a un mapa de celdas en un StringList
la cuestion es que tenia pensado utilizarla para situar los enemigos en un mapa X,Y de celdas
estoy probando cosas....y queria saber si se os ocurre algun tipo de mejora
Código PHP:
#define MAX_ACTORS 20 TColor ACTOR_Color;
// Map dimensions #define MAP_WIDTH 48 #define MAP_HEIGHT 30
////////////////////////////////////////////////////////////////////////////////////////// // // Actor Class Declaration // //////////////////////////////////////////////////////////////////////////////////////////
class Actor {
public:
// //////////////////////////////////////////////////////////////////////////////////////// // // Constructors and Destructors // // ////////////////////////////////////////////////////////////////////////////////////////
// Default constructor Actor( void );
// //////////////////////////////////////////////////////////////////////////////////////// // // Public Methods // // ////////////////////////////////////////////////////////////////////////////////////////
// Changes how the actor appears in the game world void SetAppearance( char nDisplayChar, TColor nDisplayColor );
// Changes the position of the actor void SetPos( int x, int y );
// Draws the actor to the screen void Draw( void );
// Periodic update routine for the actor void Update( void );
protected:
// //////////////////////////////////////////////////////////////////////////////////////// // // Protected Properties // // ////////////////////////////////////////////////////////////////////////////////////////
// Horizontal coordinate of the actor, relative to the level's origin int nPosX;
// Vertical coordinate of the actor, relative to the level's origin. int nPosY;
// ASCII character code used to draw the actor to the screen char nDisplayChar;
// Color code for this actor TColor nColorCode;
};
bool AddActorToList( Actor *p_cNewActor ); bool RemoveActorFromList( Actor *p_cActor ); void Create_NPCs(void); void NPC_Draw(void); //---------------------------------------------------------------------------
/ Constructor /////////////////////////////////////////////////////////////////////////////////// // // This routine initializes all properties for the Actor class. // Actor::Actor( void ) { // Initialize properties this->nDisplayChar = '@'; this->nColorCode = clRed; this->nPosX = 0; this->nPosY = 0; }
// SetAppearance Method ////////////////////////////////////////////////////////////////////////// // // This method changes the appearance of an actor. // void Actor::SetAppearance( char nDisplayChar, TColor nDisplayColor ) { this->nDisplayChar = nDisplayChar; this->nColorCode = nDisplayColor; }
// SetPos Method ///////////////////////////////////////////////////////////////////////////////// // // This method changes the location of an actor. // void Actor::SetPos( int x, int y ) { // Don't change anything if the new coordinates are invalid if( (x < 0) || (x >= MAP_WIDTH) || (y< 0) || (y >= MAP_HEIGHT ) ) return;
// Move the actor to the coordinates specified this->nPosX = x; this->nPosY = y; }
// Draw Method /////////////////////////////////////////////////////////////////////////////////// // // This method draws an actor to the screen. // void Actor::Draw( void ) { // Skip drawing if the actor's coordinates aren't on the map if( (this->nPosX < 0) || (this->nPosX >= MAP_WIDTH) || (this->nPosY < 0) || (this->nPosY >= MAP_HEIGHT ) || Form1->StringGrid1->Cells[nPosX][nPosY]== '#' ) return;
// Draw the actor as it wants to be drawn ACTOR_Color = nColorCode; Form1->StringGrid1->Cells[this->nPosX][this->nPosY] = (char)this->nDisplayChar; }
// Update Method ///////////////////////////////////////////////////////////////////////////////// // // This method is the periodic update routine for this actor, and is invoked once per game turn. // void Actor::Update( void ) { // Generate a new set of deltas for this actor int iDeltaX = (rand() % 3) - 1; int iDeltaY = (rand() % 3) - 1;
// See if this new position is allowed if( IsPassable(this->nPosX+iDeltaX, this->nPosY+iDeltaY) ) { this->nPosX += iDeltaX; this->nPosY += iDeltaY; }
}
///////////////////////////////////////////////////////////////////////////// // Functions for game /////////////////////////////////////////////////////////////////////////////
void NPC_Draw(void) { DrawMap(); //We Draw the Map First int i; // Update and draw each NPC for( i = 0; i < MAX_ACTORS; i++ ) { if( p_cActorList[i] != NULL ) { p_cActorList[i]->Update(); p_cActorList[i]->Draw(); } } }
void DrawMap(void) { //First of all, we erase the Grid for empty start for(int y=0; y<MAP_HEIGHT; y++) { for(int x=0; x<MAP_WIDTH;x++) { // Draw the tile Form1->StringGrid1->Cells[x][y] = ""; } // end of for loop } // end of for loop
for(int y=0; y<MAP_HEIGHT; y++) { for(int x=0; x<MAP_WIDTH;x++) { // Draw the tile DrawTile(x,y);
//This lines is for debugging at Runtime...it showns the loop rides //Form1->Edit2->Text = x; Form1->Edit2->Refresh(); //Form1->Edit1->Text = y; Form1->Edit1->Refresh();
} // end of for loop } // end of for loop
Form1->StringGrid1->Cells[nPlayerX][nPlayerY] = "P"; Form1->StringGrid1->Refresh(); }
// IsPassable Function /////////////////////////////////////////////////////////////////// // // This function analyzes the coordinates of the map array specified and returns // true if the coordinate is passable (able for the player to occupy), false if not. //////////////////////////////////////////////////////////////////////////////////////////
bool IsPassable( int x, int y ) { // Before we do anything, make darn sure that the coordinates are valid if( x < 0 || x >= MAP_WIDTH || y < 0 || y >= MAP_HEIGHT ) return false; // Store the value of the tile specified int nTileValue = nMapArray[y][x]; // Return true if it's passable return sTileIndex[nTileValue].bPassable; }
///////////////////////////////////////////////////////////////////////////////
// AddActorToList Function ///////////////////////////////////////////////////////////////////// //
bool AddActorToList( Actor *p_cNewActor ) { int i; // Run through the list looking for an empty slot for( i = 0; i < MAX_ACTORS; i++ ) { // Is this empty? if( p_cActorList[i] == NULL ) { // If so, use it! p_cActorList[i] = p_cNewActor; // Finished! Report success return true; } } // Couldn't find a free slot. Report failure. return false; }
bool RemoveActorFromList( Actor *p_cActor ) { int i; // Run through the list, looking for the specified actor instance. for( i = 0; i < MAX_ACTORS; i++ ) { // Is this the actor? if( p_cActorList[i] == p_cActor ) { // If so, deallocate it! delete p_cActor; // Clear the slot, allowing it to be used again. p_cActorList[i] = NULL; // Finished! Report success return true; } } // Couldn't find the actor in the list. Report failure. return false; }
void Create_NPCs(void) { int i; // Create a bunch of actors for( i = 0; i < MAX_ACTORS; i++ ) { int x, y; Actor *p_cNewActor = new Actor(); do { x = rand() % MAP_WIDTH; y = rand() % MAP_HEIGHT; } while( !IsPassable(x,y) );
p_cNewActor->SetAppearance( '@', clRed ); p_cNewActor->SetPos( x, y ); AddActorToList( p_cNewActor ); } }
void __fastcall TForm1::Timer1Timer(TObject *Sender) {
// Each 5 seconds, we DRAW the NPC (Actors/Enemies) NPC_Draw(); }
|