Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   C++ Builder (https://www.clubdelphi.com/foros/forumdisplay.php?f=13)
-   -   estructuras para el posicionamiento de enemigos en el mapa (https://www.clubdelphi.com/foros/showthread.php?t=91053)

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
        
Actorvoid );

        
// ////////////////////////////////////////////////////////////////////////////////////////
        //
        //    Public Methods
        //
        // ////////////////////////////////////////////////////////////////////////////////////////

        // Changes how the actor appears in the game world
        
void    SetAppearancechar nDisplayCharTColor nDisplayColor );

        
// Changes the position of the actor
        
void    SetPosint xint y );

        
// Draws the actor to the screen
        
void    Drawvoid );

        
// Periodic update routine for the actor
        
void    Updatevoid );


    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 AddActorToListActor *p_cNewActor );
bool RemoveActorFromListActor *p_cActor );
void Create_NPCs(void);
void NPC_Draw(void);
//---------------------------------------------------------------------------

Constructor ///////////////////////////////////////////////////////////////////////////////////
//
//    This routine initializes all properties for the Actor class.
//
Actor::Actorvoid )
{
    
// 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::SetAppearancechar nDisplayCharTColor nDisplayColor )
{
    
this->nDisplayChar nDisplayChar;
    
this->nColorCode nDisplayColor;
}


// SetPos Method /////////////////////////////////////////////////////////////////////////////////
//
//    This method changes the location of an actor.
//
void Actor::SetPosint xint y )
{
    
// Don't change anything if the new coordinates are invalid
    
if( (0) || (>= MAP_WIDTH) ||
        (
y0) || (>= 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::Drawvoid )
{
    
// 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::Updatevoid )
{
    
// 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+iDeltaXthis->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( 0MAX_ACTORSi++ )
 {
   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=0y<MAP_HEIGHTy++)
  {
    for(
int x=0x<MAP_WIDTH;x++)
     {
       
// Draw the tile
       
Form1->StringGrid1->Cells[x][y] = "";
      } 
// end of for loop
  
// end of for loop

  
for(int y=0y<MAP_HEIGHTy++)
  {
    for(
int x=0x<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 IsPassableint xint y )
{
// Before we do anything, make darn sure that the coordinates are valid
if( || >= MAP_WIDTH || || >= 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 AddActorToListActor *p_cNewActor )
{
  
int i;
// Run through the list looking for an empty slot
for( 0MAX_ACTORSi++ )
{
// 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 RemoveActorFromListActor *p_cActor )
{
  
int i;
// Run through the list, looking for the specified actor instance.
for( 0MAX_ACTORSi++ )
{
// 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( 0MAX_ACTORSi++ )
    {
      
int xy;
      
Actor *p_cNewActor = new Actor();
      do {
          
rand() % MAP_WIDTH;
          
rand() % MAP_HEIGHT;
          } while( !
IsPassable(x,y) );

      
p_cNewActor->SetAppearance'@'clRed );
      
p_cNewActor->SetPosx);
      
AddActorToListp_cNewActor );
     }
}


void __fastcall TForm1::Timer1Timer(TObject *Sender)
{

  
// Each 5 seconds, we DRAW the NPC (Actors/Enemies)
  
NPC_Draw();



ecfisa 01-11-2016 18:30:00

Hola Snake.

No es sencillo hacer mejoras para una clase de la que no se conoce a fondo su funcionalidad, pero revisa estos pequeños cambios:
Código PHP:

#include <grids.hpp>

TColor ACTOR_Color;

class 
Actor {
  private:
    
int mapWidth_;
    
int mapHeight_;
    
int maxActors_;
    
TPoint pt_;
    
char   nDisplayChar_;
    
TColor nColorCode_;
    
TPoint getPosvoid );
    
void setPos( const TPointpt );
    
bool IsPassable( const TPointpt );
  public:
    
Actorvoid );
    
void SetAppearance( const char &nDisplayCharTColor nDisplayColor );
    
void DrawTStringGridgrd );
    
void Updatevoid );
    const 
int mapWidthvoid )  { return mapWidth_;  }
    const 
int mapHeightvoid ) { return mapHeight_; }
    const 
int maxActorsvoid ) { return maxActors_; }
    
__property TPoint Position = { read getPoswrite setPos };
};

/* ------------------------- PUBLIC ------------------------- */
Actor::Actorvoid )
{
  
mapWidth_     48;
  
mapHeight_    30;
  
maxActors_    20;
  
pt_           Point00);
  
nDisplayChar_ '@';
  
nColorCode_   clRed;
}

void Actor::SetAppearance( const char &nDisplayCharTColor nDisplayColor )
{
  
nDisplayChar_ nDisplayChar;
  
nColorCode_   nDisplayColor;
}

void Actor::DrawTStringGridgrd )
{
  if( 
pt_.0  ||  pt_.>= mapWidth()  ||
      
pt_.0  ||  pt_.>= mapHeight() || grd->Cells[pt_.x][pt_.y] == '#' )
    return;

  
ACTOR_Color nColorCode_;
  
grd->Cells[pt_.x][pt_.y] = nDisplayChar_;
}

bool Actor::IsPassable( const TPointpt )
{
  if( 
pt.|| pt.>= mapWidth() || pt.|| pt.>= mapHeight() )
    return 
false;

  return 
true;
  
//  nMapArray ????
  // int nTileValue = nMapArray[y][x];
  // return sTileIndex[nTileValue].bPassable;
}

void Actor::Updatevoid )
{
  
int iDeltaX = (rand() % 3) - 1;
  
int iDeltaY = (rand() % 3) - 1;

  if( 
IsPassablePoint(pt_.iDeltaXpt_.iDeltaY) ) ) {
    
pt_.+= iDeltaX;
    
pt_.+= iDeltaY;
  }
}

/* ------------------------- PRIVATE ------------------------- */
TPoint Actor::getPosvoid )
{
  return 
pt_;
}

void Actor::setPos( const TPointpt  )
{
  if( ( 
pt.) || ( pt.>= mapWidth() ) ||
      ( 
pt.) || ( pt.>= mapHeight() ) )
    return;
  
pt_ pt;
}

... 

Puse las partes del código afectadas por las modificaciones en la definición de la clase.

Un ejemplo de llamada podría ser:
Código PHP:

...
{
  
Actor a;

  
a.Position Point3);
  
a.DrawStringGrid1 );
  ...


Saludos :)

Snaked 01-11-2016 21:04:45

pues muchas gracias por compactarlo con un poco mas de diseño en la estructura

estare ocupado esta semana trabajando en las "rutas" comerciales y los enemigos estos de la clase Actor


La franja horaria es GMT +2. Ahora son las 07:28:30.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi