Club Delphi  
    Paypal   FTP   CCD     Buscar   Trucos   Trabajo   Foros

Retroceder   Foros Club Delphi > Otros entornos y lenguajes > C++ Builder
Registrarse FAQ Miembros Calendario Guía de estilo Buscar Temas de Hoy Marcar Foros Como Leídos

Respuesta
 
Herramientas Buscar en Tema Desplegado
  #1  
Antiguo 01-11-2016
Snaked Snaked is offline
Baneado
NULL
 
Registrado: sep 2016
Posts: 102
Poder: 0
Snaked Va por buen camino
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();
} 
Responder Con Cita
  #2  
Antiguo 01-11-2016
Avatar de ecfisa
ecfisa ecfisa is offline
Moderador
 
Registrado: dic 2005
Ubicación: Tres Arroyos, Argentina
Posts: 10.508
Poder: 38
ecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to behold
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 getPos( void );
    
void setPos( const TPoint& pt );
    
bool IsPassable( const TPoint& pt );
  public:
    
Actor( void );
    
void SetAppearance( const char &nDisplayChar, TColor nDisplayColor );
    
void Draw( TStringGrid* grd );
    
void Update( void );
    const 
int mapWidth( void )  { return mapWidth_;  }
    const 
int mapHeight( void ) { return mapHeight_; }
    const 
int maxActors( void ) { return maxActors_; }
    
__property TPoint Position = { read = getPos, write = setPos };
};

/* ------------------------- PUBLIC ------------------------- */
Actor::Actor( void )
{
  
mapWidth_     = 48;
  
mapHeight_    = 30;
  
maxActors_    = 20;
  
pt_           = Point( 0, 0);
  
nDisplayChar_ = '@';
  
nColorCode_   = clRed;
}

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

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

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

bool Actor::IsPassable( const TPoint& pt )
{
  if( 
pt.x < 0 || pt.x >= mapWidth() || pt.y < 0 || pt.y >= mapHeight() )
    return 
false;

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

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

  if( 
IsPassable( Point(pt_.x + iDeltaX, pt_.y + iDeltaY) ) ) {
    
pt_.x += iDeltaX;
    
pt_.y += iDeltaY;
  }
}

/* ------------------------- PRIVATE ------------------------- */
TPoint Actor::getPos( void )
{
  return 
pt_;
}

void Actor::setPos( const TPoint& pt  )
{
  if( ( 
pt.x < 0 ) || ( pt.x >= mapWidth() ) ||
      ( 
pt.y < 0 ) || ( pt.y >= 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 = Point( 3, 2 );
  
a.Draw( StringGrid1 );
  ...
} 
Saludos
__________________
Daniel Didriksen

Guía de estilo - Uso de las etiquetas - La otra guía de estilo ....

Última edición por ecfisa fecha: 01-11-2016 a las 19:28:29. Razón: identación
Responder Con Cita
  #3  
Antiguo 01-11-2016
Snaked Snaked is offline
Baneado
NULL
 
Registrado: sep 2016
Posts: 102
Poder: 0
Snaked Va por buen camino
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
Responder Con Cita
Respuesta


Herramientas Buscar en Tema
Buscar en Tema:

Búsqueda Avanzada
Desplegado

Normas de Publicación
no Puedes crear nuevos temas
no Puedes responder a temas
no Puedes adjuntar archivos
no Puedes editar tus mensajes

El código vB está habilitado
Las caritas están habilitado
Código [IMG] está habilitado
Código HTML está deshabilitado
Saltar a Foro

Temas Similares
Tema Autor Foro Respuestas Último mensaje
¿componente de mapa de mesas para restaurante? JXJ Varios 9 15-11-2013 01:29:28
posicionamiento web en php elcigarra PHP 13 26-01-2012 16:24:46
Gif para posicionamiento css nightynvid HTML, Javascript y otros 3 20-04-2007 18:42:52
El Mapa de los países enemigos de Internet Sasuke_Cub Noticias 6 19-11-2005 00:56:28
Estructuras?¿ CHiCoLiTa PHP 7 03-01-2005 10:09:53


La franja horaria es GMT +2. Ahora son las 22:21:19.


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