cuantas cosas!!!!
-¿Estas usando un motor independiente?
R: si, estoy utilizando un TCanvas y los sprites en un bucle que recorre la rejilla tiles y dibuja el mapa principal y luego otra funcion situa los items (cofres, comida etc)
-¿Que lenguaje de programacion estas usando?
C++ builder 10.1 Seattle, por puro vicio que le tengo a este entorno
-¿Cuantas personas estais con las manos en la masa?
R: estabamos yo y el tio que hizo el muñecajo del jugador que aun sigo utilizando a falta de otro mejor....el pavo desaparecio...no cogia el telefono ni respondia al wassap ni al skype....en fin....
-¿Para que tipo de plataforma vais encaminados? (lo digo por los botoncitos para moverse que parecen ir dirigidos a una interfaz movil-touch, en vez de usar las flechas o el recurrente WSAD) pues para PC inicialmente aunque podria intentar compilarlo para Android y tablets mas adelante
-¿El programador paga el café?
R: Si y las putas tambien, cuando recaudemos los 20.000 del crodfunding
-¿De donde eres o sois?
R: Galicia
como te veo interesado, te pongo un trozo de codigo para que veas como es por dentro
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
appPath = ExtractFilePath(Application->ExeName);
// Hace mas fino el centro de la barra de volumen
SetWindowLong(trkbVolumen->Handle, GWL_STYLE,
GetWindowLong(trkbVolumen->Handle, GWL_STYLE) & ~TBS_ENABLESELRANGE);
if (BASS_GetVersion() != MAKELONG(2,0))
throw Exception("No se pudo cargar la versión 2 de BASS");
if (!BASS_Init(1, 44100, 0, Handle, 0))
throw Exception("Error inicializando audio");
BASS_SetConfig(BASS_CONFIG_NET_BUFFER, 12000);
Form1->DoubleBuffered = True;
stream=BASS_StreamCreateFile(FALSE,".\\sounds\\mistery_music1.mp3",0,0,0);
sample1 = BASS_StreamCreateFile(FALSE,".\\sounds\\door_open_close.mp3",0,0,0);
sample_treasure_chest = BASS_StreamCreateFile(FALSE,".\\sounds\\treasure_chest.mp3",0,0,0);
sample_spiderbite = BASS_StreamCreateFile(FALSE,".\\sounds\\spiderbite.mp3",0,0,0);
sample_grump = BASS_StreamCreateFile(FALSE,".\\sounds\\grump.mp3",0,0,0);
reproduciendo = BASS_StreamPlay(stream, false, BASS_SAMPLE_LOOP);
BASS_SetVolume(100);
}
//---------------------------------------------------------------------------
///////////////////////////////////////////////////////////////////////////////
//Global Variables for Actor/Enemies Colour and backup of background tile
///////////////////////////////////////////////////////////////////////////////
AnsiString backup_fondo;
TColor ACTOR_Color;
int nSlot = -1;
///////////////////////////////////////////////////////////////////////////////
// Random map creation classes
///////////////////////////////////////////////////////////////////////////////
class Dungeon
{
int xmax;
int ymax;
int xsize;
int ysize;
int objects;
int chanceRoom;
int chanceCorridor;
int* dungeon_map;
long oldseed;
enum
{
tileUnused = 0,
tileDirtWall,
tileDirtFloor,
tileStoneWall,
tileCorridor,
tileDoor,
tileUpStairs,
tileDownStairs,
tileChest
};
std::string msgXSize;
std::string msgYSize;
std::string msgMaxObjects;
std::string msgNumObjects;
std::string msgHelp;
std::string msgDetailedHelp;
void setCell(int x, int y, int celltype)
{
dungeon_map[x + xsize * y] = celltype;
}
int getCell(int x, int y)
{
return dungeon_map[x + xsize * y];
}
int getRand(int min, int max)
{
time_t seed;
seed = time(NULL) + oldseed;
oldseed = seed;
srand(seed);
int n = max - min + 1;
int i = rand() % n;
if(i < 0)
i = -i;
return min + i;
}
bool makeCorridor(int x, int y, int lenght, int direction)
{
int len = getRand(2, lenght);
int floor = tileCorridor;
int dir = 0;
if(direction > 0 && direction < 4) dir = direction;
int xtemp = 0;
int ytemp = 0;
switch(dir)
{
case 0:
{
if(x < 0 || x > xsize) return false;
else xtemp = x;
for(ytemp = y; ytemp > (y-len); ytemp--)
{
if(ytemp < 0 || ytemp > ysize) return false;
if(getCell(xtemp, ytemp) != tileUnused) return false;
}
for(ytemp = y; ytemp > (y - len); ytemp--)
{
setCell(xtemp, ytemp, floor);
}
break;
}
case 1:
{
if(y < 0 || y > ysize) return false;
else ytemp = y;
for(xtemp = x; xtemp < (x + len); xtemp++)
{
if(xtemp < 0 || xtemp > xsize) return false;
if(getCell(xtemp, ytemp) != tileUnused) return false;
}
for(xtemp = x; xtemp < (x + len); xtemp++)
{
setCell(xtemp, ytemp, floor);
}
break;
}
case 2:
{
if(x < 0 || x > xsize) return false;
else xtemp = x;
for(ytemp = y; ytemp < (y + len); ytemp++)
{
if(ytemp < 0 || ytemp > ysize) return false;
if(getCell(xtemp, ytemp) != tileUnused) return false;
}
for (ytemp = y; ytemp < (y+len); ytemp++){
setCell(xtemp, ytemp, floor);
}
break;
}
case 3:
{
if (ytemp < 0 || ytemp > ysize) return false;
else ytemp = y;
for (xtemp = x; xtemp > (x-len); xtemp--){
if (xtemp < 0 || xtemp > xsize) return false;
if (getCell(xtemp, ytemp) != tileUnused) return false;
}
for (xtemp = x; xtemp > (x-len); xtemp--){
setCell(xtemp, ytemp, floor);
}
break;
}
}
//woot, we're still here! let's tell the other guys we're done!!
return true;
}
bool makeRoom(int x, int y, int xlength, int ylength, int direction){
//define the dimensions of the room, it should be at least 4x4 tiles (2x2 for walking on, the rest is walls)
int xlen = getRand(4, xlength);
int ylen = getRand(4, ylength);
//the tile type it's going to be filled with
int floor = tileDirtFloor; //jordgolv..
int wall = tileDirtWall; //jordv????gg
//choose the way it's pointing at
int dir = 0;
if (direction > 0 && direction < 4) dir = direction;
switch(dir){
case 0:
//north
//Check if there's enough space left for it
for (int ytemp = y; ytemp > (y-ylen); ytemp--){
if (ytemp < 0 || ytemp > ysize) return false;
for (int xtemp = (x-xlen/2); xtemp < (x+(xlen+1)/2); xtemp++){
if (xtemp < 0 || xtemp > xsize) return false;
if (getCell(xtemp, ytemp) != tileUnused) return false; //no space left...
}
}
//we're still here, build
for (int ytemp = y; ytemp > (y-ylen); ytemp--){
for (int xtemp = (x-xlen/2); xtemp < (x+(xlen+1)/2); xtemp++){
//start with the walls
if (xtemp == (x-xlen/2)) setCell(xtemp, ytemp, wall);
else if (xtemp == (x+(xlen-1)/2)) setCell(xtemp, ytemp, wall);
else if (ytemp == y) setCell(xtemp, ytemp, wall);
else if (ytemp == (y-ylen+1)) setCell(xtemp, ytemp, wall);
//and then fill with the floor
else setCell(xtemp, ytemp, floor);
}
}
break;
case 1:
//east
for (int ytemp = (y-ylen/2); ytemp < (y+(ylen+1)/2); ytemp++){
if (ytemp < 0 || ytemp > ysize) return false;
for (int xtemp = x; xtemp < (x+xlen); xtemp++){
if (xtemp < 0 || xtemp > xsize) return false;
if (getCell(xtemp, ytemp) != tileUnused) return false;
}
}
for (int ytemp = (y-ylen/2); ytemp < (y+(ylen+1)/2); ytemp++){
for (int xtemp = x; xtemp < (x+xlen); xtemp++){
if (xtemp == x) setCell(xtemp, ytemp, wall);
else if (xtemp == (x+xlen-1)) setCell(xtemp, ytemp, wall);
else if (ytemp == (y-ylen/2)) setCell(xtemp, ytemp, wall);
else if (ytemp == (y+(ylen-1)/2)) setCell(xtemp, ytemp, wall);
else setCell(xtemp, ytemp, floor);
}
}
break;
case 2:
//south
for (int ytemp = y; ytemp < (y+ylen); ytemp++){
if (ytemp < 0 || ytemp > ysize) return false;
for (int xtemp = (x-xlen/2); xtemp < (x+(xlen+1)/2); xtemp++){
if (xtemp < 0 || xtemp > xsize) return false;
if (getCell(xtemp, ytemp) != tileUnused) return false;
}
}
for (int ytemp = y; ytemp < (y+ylen); ytemp++){
for (int xtemp = (x-xlen/2); xtemp < (x+(xlen+1)/2); xtemp++){
if (xtemp == (x-xlen/2)) setCell(xtemp, ytemp, wall);
else if (xtemp == (x+(xlen-1)/2)) setCell(xtemp, ytemp, wall);
else if (ytemp == y) setCell(xtemp, ytemp, wall);
else if (ytemp == (y+ylen-1)) setCell(xtemp, ytemp, wall);
else setCell(xtemp, ytemp, floor);
}
}
break;
case 3:
//west
for (int ytemp = (y-ylen/2); ytemp < (y+(ylen+1)/2); ytemp++){
if (ytemp < 0 || ytemp > ysize) return false;
for (int xtemp = x; xtemp > (x-xlen); xtemp--){
if (xtemp < 0 || xtemp > xsize) return false;
if (getCell(xtemp, ytemp) != tileUnused) return false;
}
}
for (int ytemp = (y-ylen/2); ytemp < (y+(ylen+1)/2); ytemp++){
for (int xtemp = x; xtemp > (x-xlen); xtemp--){
if (xtemp == x) setCell(xtemp, ytemp, wall);
else if (xtemp == (x-xlen+1)) setCell(xtemp, ytemp, wall);
else if (ytemp == (y-ylen/2)) setCell(xtemp, ytemp, wall);
else if (ytemp == (y+(ylen-1)/2)) setCell(xtemp, ytemp, wall);
else setCell(xtemp, ytemp, floor);
}
}
break;
}
//yay, all done
return true;
}
void showDungeon(){
for (int y = 0; y < ysize; y++){
for (int x = 0; x < xsize; x++){
//System.out.print(getCell(x, y));
switch(getCell(x, y)){
case tileUnused:
nMapArray[y][x] = 10;
break;
case tileDirtWall:
nMapArray[y][x] = 1;
break;
case tileDirtFloor:
nMapArray[y][x] = 4;
break;
case tileStoneWall:
nMapArray[y][x] = 7;
break;
case tileCorridor:
nMapArray[y][x] = 4;
break;
case tileDoor:
nMapArray[y][x] = 6;
break;
case tileUpStairs:
nMapArray[y][x] = 0;
break;
case tileDownStairs:
nMapArray[y][x] = 8;
break;
case tileChest:
nMapArray[y][x] = 5;
break;
};
}
//if (xsize <= xmax) printf("\n");
}
}
bool createDungeon(int inx, int iny, int inobj){
if (inobj < 1) objects = 10;
else objects = inobj;
//justera kartans storlek, om den ????r st????rre eller mindre ????n "gr????nserna"
//adjust the size of the map, if it's smaller or bigger than the limits
if (inx < 3) xsize = 3;
else if (inx > xmax) xsize = xmax;
else xsize = inx;
if (iny < 3) ysize = 3;
else if (iny > ymax) ysize = ymax;
else ysize = iny;
//printf("%s %d\n", msgXSize.c_str(), xsize);
//printf("%s %d\n", msgYSize.c_str(), + ysize);
//printf("%s %d\n", msgMaxObjects.c_str(), objects);
num_Rooms = objects;
//redefine the map var, so it's adjusted to our new map size
dungeon_map = new int[xsize * ysize];
//start with making the "standard stuff" on the map
for (int y = 0; y < ysize; y++){
for (int x = 0; x < xsize; x++){
//ie, making the borders of unwalkable walls
if (y == 0) setCell(x, y, tileStoneWall);
else if (y == ysize-1) setCell(x, y, tileStoneWall);
else if (x == 0) setCell(x, y, tileStoneWall);
else if (x == xsize-1) setCell(x, y, tileStoneWall);
//and fill the rest with dirt
else setCell(x, y, tileUnused);
}
}
/*******************************************************************************
And now the code of the random-map-generation-algorithm begins!
*******************************************************************************/
//start with making a room in the middle, which we can start building upon
makeRoom(xsize/2, ysize/2, 8, 6, getRand(0,3)); //getrand saken f????r att slumpa fram riktning p?? rummet
//keep count of the number of "objects" we've made
int currentFeatures = 1; //+1 for the first room we just made
//then we sart the main loop
for (int countingTries = 0; countingTries < 1000; countingTries++){
//check if we've reached our quota
if (currentFeatures == objects){
break;
}
//start with a random wall
int newx = 0;
int xmod = 0;
int newy = 0;
int ymod = 0;
int validTile = -1;
//1000 chances to find a suitable object (room or corridor)..
//(yea, i know it's kinda ugly with a for-loop... -_-')
for (int testing = 0; testing < 1000; testing++){
newx = getRand(1, xsize-1);
newy = getRand(1, ysize-1);
validTile = -1;
//System.out.println("tempx: " + newx + "\ttempy: " + newy);
if (getCell(newx, newy) == tileDirtWall || getCell(newx, newy) == tileCorridor){
//check if we can reach the place
if (getCell(newx, newy+1) == tileDirtFloor || getCell(newx, newy+1) == tileCorridor){
validTile = 0; //
xmod = 0;
ymod = -1;
}
else if (getCell(newx-1, newy) == tileDirtFloor || getCell(newx-1, newy) == tileCorridor){
validTile = 1; //
xmod = +1;
ymod = 0;
}
else if (getCell(newx, newy-1) == tileDirtFloor || getCell(newx, newy-1) == tileCorridor){
validTile = 2; //
xmod = 0;
ymod = +1;
}
else if (getCell(newx+1, newy) == tileDirtFloor || getCell(newx+1, newy) == tileCorridor){
validTile = 3; //
xmod = -1;
ymod = 0;
}
//check that we haven't got another door nearby, so we won't get alot of openings besides
//each other
if (validTile > -1){
if (getCell(newx, newy+1) == tileDoor) //north
validTile = -1;
else if (getCell(newx-1, newy) == tileDoor)//east
validTile = -1;
else if (getCell(newx, newy-1) == tileDoor)//south
validTile = -1;
else if (getCell(newx+1, newy) == tileDoor)//west
validTile = -1;
}
//if we can, jump out of the loop and continue with the rest
if (validTile > -1) break;
}
}
if (validTile > -1){
//choose what to build now at our newly found place, and at what direction
int feature = getRand(0, 100);
if (feature <= chanceRoom){ //a new room
if (makeRoom((newx+xmod), (newy+ymod), 8, 6, validTile)){
currentFeatures++; //add to our quota
//then we mark the wall opening with a door
setCell(newx, newy, tileDoor);
//clean up infront of the door so we can reach it
setCell((newx+xmod), (newy+ymod), tileDirtFloor);
}
}
else if (feature >= chanceRoom){ //new corridor
if (makeCorridor((newx+xmod), (newy+ymod), 6, validTile)){
//same thing here, add to the quota and a door
currentFeatures++;
setCell(newx, newy, tileDoor);
}
}
}
}
/*******************************************************************************
All done with the building, let's finish this one off
*******************************************************************************/
//sprinkle out the bonusstuff (stairs, chests etc.) over the map
int newx = 0;
int newy = 0;
int ways = 0; //from how many directions we can reach the random spot from
int state = 0; //the state the loop is in, start with the stairs
while (state != 10){
for (int testing = 0; testing < 1000; testing++){
newx = getRand(1, xsize-1);
newy = getRand(1, ysize-2); //cheap bugfix, pulls down newy to 0<y<24, from 0<y<25
//System.out.println("x: " + newx + "\ty: " + newy);
ways = 4; //the lower the better
//check if we can reach the spot
if (getCell(newx, newy+1) == tileDirtFloor || getCell(newx, newy+1) == tileCorridor){
//north
if (getCell(newx, newy+1) != tileDoor)
ways--;
}
if (getCell(newx-1, newy) == tileDirtFloor || getCell(newx-1, newy) == tileCorridor){
//east
if (getCell(newx-1, newy) != tileDoor)
ways--;
}
if (getCell(newx, newy-1) == tileDirtFloor || getCell(newx, newy-1) == tileCorridor){
//south
if (getCell(newx, newy-1) != tileDoor)
ways--;
}
if (getCell(newx+1, newy) == tileDirtFloor || getCell(newx+1, newy) == tileCorridor){
//west
if (getCell(newx+1, newy) != tileDoor)
ways--;
}
if (state == 0){
if (ways == 0){
//we're in state 0, let's place a "upstairs" thing
setCell(newx, newy, tileUpStairs);
state = 1;
break;
}
}
else if (state == 1){
if (ways == 0){
//state 1, place a "downstairs"
setCell(newx, newy, tileDownStairs);
state = 10;
break;
}
}
}
}
//all done with the map generation, tell the user about it and finish
//printf("%s %d\n",msgNumObjects.c_str(), currentFeatures);
return true;
}
void cmain()
{
int x = 65;
int y = 50;
int dungeon_objects = 200;
dungeon_map = new int[x * y];
if(createDungeon(x, y, dungeon_objects))
showDungeon();
std::cin.get();
}
public:
Dungeon()
{
xmax = 65;
ymax = 50;
xsize = 0;
ysize = 0;
objects = 0;
chanceRoom = 200;
chanceCorridor = 50;
msgXSize = "X size of dungeon: \t";
msgYSize = "Y size of dungeon: \t";
msgMaxObjects = "max # of objects: \t";
msgNumObjects = "# of objects made: \t";
msgHelp = "";
msgDetailedHelp = "";
cmain();
}
};
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//////////////////////////////////////////////////////////////////////////////////////////
//
// 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;
};
//---------------------------------------------------------------------------
// Lista de actores
Actor *p_cActorList [MAX_ACTORS];
bool AddActorToList( Actor *p_cNewActor );
bool RemoveActorFromList( Actor *p_cActor );
// 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->Canvas->Brush->Color = ACTOR_Color;
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() % 5) - 1;
int iDeltaY = (rand() % 5) - 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();
}
}
}
//---------------------------------------------------------------------------
// 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;
if(Form1->StringGrid1->Cells[x][y] == 'P')
return false;
if(Form1->StringGrid1->Cells[x][y] == '@')
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::FormShow(TObject *Sender)
{
//sImage_Pantalla->Picture->LoadFromFile("logo_splash_screen_Devil_Inside_Studios.jpg");
//sImage_Pantalla->Refresh();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::StopStream(void)
{
if (stream)
BASS_StreamFree(stream);
stream = 0;
reproduciendo = false;
BASS_Free();
if (!BASS_Init(1, 44100, 0, Handle, 0))
throw Exception("Error inicializando audio");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
StopStream();
BASS_Free();
}
//---------------------------------------------------------------------------
void dibuja_fondo(void)
{
unsigned char num, code;
char string[8];
int iteraciones_bucle = 0;
int XCount = 20;
int YCount = 20;
char buffer = 'y';
int num_tile = 0;
AnsiString numero;
int paso = 0;
int BASE[3220];
long double altura = 0;
int CX = 0;
int CY = 0;
FILE *stream;
char ch;
AnsiString linea;
int cont = 0;
if ((stream = fopen("castillito.csv", "r"))
== NULL)
{
ShowMessage("Cannot open map file.");
return;
}
fseek(stream, SEEK_SET, 0);
Form1->sMemo1->Lines->Clear();
strcpy(string, "+");
for(int cc=0; cc<3220; cc++)
{
BASE[cc] = 0;
}
while(buffer != EOF)
{
buffer = fgetc(stream);
//ShowMessage(buffer);
//ShowMessage(static_cast<int>(buffer));
if(static_cast<int>(buffer) >= 48 && static_cast<int>(buffer) <=57)
{
switch(static_cast<int>(buffer))
{
case 48: code = 0x30; // ASCII Code for 9 in Hex
num = 0x30| 0; strcat(string, "0"); break;
case 49: code = 0x31; // ASCII Code for 9 in Hex
num = 0x31| 1; strcat(string, "1"); break;
case 50: code = 0x32; // ASCII Code for 9 in Hex
num = 0x32| 2; strcat(string, "2"); break;
case 51: code = 0x33; // ASCII Code for 9 in Hex
num = 0x33| 3; strcat(string, "3"); break;
case 52: code = 0x34; // ASCII Code for 9 in Hex
num = 0x34| 4; strcat(string, "4"); break;
case 53: code = 0x35; // ASCII Code for 9 in Hex
num = 0x35| 5; strcat(string, "5"); break;
case 54: code = 0x36; // ASCII Code for 9 in Hex
num = 0x36| 6; strcat(string, "6"); break;
case 55: code = 0x37; // ASCII Code for 9 in Hex
num = 0x37| 7; strcat(string, "7"); break;
case 56: code = 0x38; // ASCII Code for 9 in Hex
num = 0x38| 8; strcat(string, "8"); break;
case 57: code = 0x39; // ASCII Code for 9 in Hex
num = 0x39| 9; strcat(string, "9"); break;
default: break;
}
//ShowMessage("Sumando Numeros");
//ShowMessage(string);
}
if(buffer == '-') {
buffer = fgetc(stream);
if(buffer == '1')
{
num_tile = -1;
BASE[paso] = num_tile;
Form1->sMemo1->Lines->Add(BASE[paso]);
Form1->sMemo1->Refresh();
paso++;
num_tile = -2;
}
}
if(buffer == ',')
{
//ShowMessage("COMMA!");
if(num_tile != -1)
{
if(atoi(string) > 0)
{
BASE[paso] = atoi(string);
Form1->sMemo1->Lines->Add(BASE[paso]);
Form1->sMemo1->Refresh();
paso++;
numero = "";
memset(string,'\0',strlen(string));
strcpy(string, "+");
//ShowMessage("CADENA BORRADA");
}
}
}
//ShowMessage(num_tile);
iteraciones_bucle++;
}
BASE[paso++] = -2;
paso = 0;
num_tile = 0;
int lineY = 0;
int lineX = 0;
ShowMessage(iteraciones_bucle);
//Dibujamos el mapa de pantalla cuadricula
Form1->g = 0;
Form1->g = Form2->sImage3->Picture->Graphic;
//Form1->Image_fondo_mapa->Canvas->Brush->Color = clNavy;
//Form1->Image_fondo_mapa->Canvas->Rectangle(0, 0, 664, 441);
Form1->Image_fondo_mapa->Canvas->Draw(0,0, Form1->g);
for(int cont1=0; cont1<3200; cont1++)
{
num_tile = BASE[cont1];
if(lineX >39) { lineY++; lineX = 0;}
altura = floor(static_cast<float>(num_tile) / 30);
CY = altura * 32;
CX = (num_tile - (altura * 30))* 32;
Form1->Image_fondo_mapa->Canvas->CopyRect(
//first rect is destination
TRect(lineX * 32, lineY* 32, lineX * 32 + 32, lineY * 32 + 32)
//second is canvas of source
,Form2->sImage1->Picture->Bitmap->Canvas
//third is rect of source that you want to copy
,TRect(CX, CY, CX + 32, CY + 32)
);
num_tile = 0;
paso++;
lineX++;
}
Form1->Image_fondo_mapa->Canvas->StretchDraw(TRect(128,128, 128 + 64, 128 + 64), Form2->sImage2->Picture->Bitmap);
ShowMessage("Finalizado...");
fclose(stream);
Form1->Image_Pantalla->Canvas->CopyRect(TRect(0,0,Form1->Image_fondo_mapa->Width,Form1->Image_fondo_mapa->Height)
,Form1->Image_fondo_mapa->Picture->Bitmap->Canvas
,TRect(0,0,Form1->Image_fondo_mapa->Width, Form1->Image_fondo_mapa->Height));
Form1->Image_Pantalla->Repaint();
}
void MAZMORRA_A_CC(void)
{
int cc = 0;
int cont1 = 0;
int cont2 = 0;
for(cc=0; cc<3136; cc++)
{
if(Form1->StringGrid1->Cells[cont1][cont2] == 'P')
{
BASE[cc] = 1;
}
else if(Form1->StringGrid1->Cells[cont1][cont2] == 'X')
{
BASE[cc] = 5;
}
else if(Form1->StringGrid1->Cells[cont1][cont2] == '<')
{
BASE[cc] = 6;
}
else if(Form1->StringGrid1->Cells[cont1][cont2] == '>')
{
BASE[cc] = 7;
}
else if(Form1->StringGrid1->Cells[cont1][cont2] == '+')
{
BASE[cc] = 8;
if(first_door == false) { first_door = true;
nPlayerX = cont1; nPlayerY = cont2;}
}
else if(Form1->StringGrid1->Cells[cont1][cont2] == '#')
{
BASE[cc] = 9;
}
else if(Form1->StringGrid1->Cells[cont1][cont2] == '.')
{
BASE[cc] = 0;
}
else if(Form1->StringGrid1->Cells[cont1][cont2] == '@')
{
BASE[cc] = 2;
}
else if(Form1->StringGrid1->Cells[cont1][cont2] == 's')
{
BASE[cc] = -1;
}
else if(Form1->StringGrid1->Cells[cont1][cont2] == 'h')
{
BASE[cc] = -2;
}
else if(Form1->StringGrid1->Cells[cont1][cont2] == 'c')
{
BASE[cc] = -3;
}
else BASE[cc] = 0;
cont1++;
if(cont1 >64) { cont1 = 0; cont2++; }
if(cont2 >49) break;
}
//ShowMessage(cc);
}
void MAZMORRA_CREA_ITEMS(void)
{
int cc = 0;
int cont1 = 0;
int cont2 = 0;
int casillax = 0;
int casillay = 0;
int item_selected = 0;
int count_objetos = 0;
randomize();
for(int cc=0; cc<200; cc++)
{
casillax = rand() % 64;
casillay = rand() % 49;
if(Form1->StringGrid1->Cells[casillax][casillay] == '.')
{
item_selected = rand() % 3;
switch(item_selected)
{
case 0:
count_objetos++;
Form1->StringGrid1->Cells[casillax][casillay] = 's';
break;
case 1:
count_objetos++;
Form1->StringGrid1->Cells[casillax][casillay] = 'h';
break;
case 2:
count_objetos++;
Form1->StringGrid1->Cells[casillax][casillay] = 'c';
break;
}
}
else {
casillax = rand() % 64;
casillay = rand() % 49;
if(Form1->StringGrid1->Cells[casillax][casillay] == '.')
{
item_selected = rand() % 3;
switch(item_selected)
{
case 0:
count_objetos++;
Form1->StringGrid1->Cells[casillax][casillay] = 's';
break;
case 1:
count_objetos++;
Form1->StringGrid1->Cells[casillax][casillay] = 'h';
break;
case 2:
count_objetos++;
Form1->StringGrid1->Cells[casillax][casillay] = 'c';
break;
}
}
}
}
//ShowMessage(count_objetos);
}
void dibuja_Mazmorra_en_pantalla(void)
{
int cont1 = 0;
int cont2 = 0;
long double altura = 0;
int CX = 0;
int CY = 0;
int paso = 0;
int num_tile = 0;
int lineY = 0;
int lineX = 0;
int loseta = 0;
for(int cont1=0; cont1<3136; cont1++)
{
num_tile = BASE[cont1];
switch(num_tile)
{
case -1:
CX = 0;
CY = 0;
break;
case -2:
CX = 0;
CY = 64;
break;
case -3:
CX = 192;
CY = 256;
break;
case 0:
case ' ':
case '.':
CX = 64;
CY = 0;
break;
case 9: CX = 320; CY = 64;
break;
case 1: CX = 320; CY = 0;
break;
case 5: CX = 256; CY = 0;
break;
case 8: CX = 256; CY = 64;
break;
case 6: CX = 256; CY = 128;
break;
case 7: CX = 256; CY = 192;
break;
case 2: CX = 256; CY = 256;
}
if(lineX >64) { lineY++; lineX = 0;}
if(CX == 256 && CY == 0)
{
}
else
{
Form1->Image_fondo_mapa->Canvas->CopyRect(
//first rect is destination
TRect(lineX * 64, lineY* 64, lineX * 64 + 64, lineY * 64 + 64)
//second is canvas of source
,Form2->sImage1->Picture->Bitmap->Canvas
//third is rect of source that you want to copy
,TRect(CX, CY, CX + 64, CY + 64)
);
}
num_tile = 0;
paso++;
lineX++;
}
Form1->Image_fondo_mapa->Canvas->StretchDraw(TRect(PLAYER1X * 64, PLAYER1Y * 64, PLAYER1X * 64 + 64, PLAYER1Y * 64 + 64), Form2->sImage2->Picture->Bitmap);
Form1->Image_Pantalla->Canvas->CopyRect(TRect(0,0,Form1->Image_fondo_mapa->Width,Form1->Image_fondo_mapa->Height)
,Form1->Image_fondo_mapa->Picture->Bitmap->Canvas
,TRect(0,0,Form1->Image_fondo_mapa->Width, Form1->Image_fondo_mapa->Height));
Form1->Image_Pantalla->Repaint();
//Form1->Image_fondo_mapa->Canvas->StretchDraw(TRect(PLAYER1X * 64,PLAYER1Y * 64, PLAYER1X * 64 + 64, PLAYER1Y * 64 + 64), Form2->sImage2->Picture->Bitmap);
//ShowMessage("Finalizado...");
}
void CENTRA_MAPA(void)
{
Form1->sScrollBox1->HorzScrollBar->Position = nPlayerX * 64 - (4 * 64);
Form1->sScrollBox1->VertScrollBar->Position = nPlayerY * 64 - (4 * 64);
}
void __fastcall TForm1::sButton1Click(TObject *Sender)
{
Panel1->Visible = false;
Dungeon d; //CREAMOS EL DUNGEON ALEATORIO
DrawMap();
MAZMORRA_A_CC();
dibuja_Mazmorra_en_pantalla();
CENTRA_MAPA();
Create_NPCs();
NPC_Draw();
MAZMORRA_CREA_ITEMS();
MAZMORRA_A_CC();
dibuja_Mazmorra_en_pantalla();
//Timer1->Enabled = true;
Form1->StringGrid1->Refresh();
Bucle_Juego();
}
//---------------------------------------------------------------------------
void Bucle_Juego(void)
{
int i =0;
for(i=0; i<20000; i++)
{
//draw_fondo_y_sprites();
Form1->Image_Pantalla->Canvas->CopyRect(TRect(0,0,Form1->Image_fondo_mapa->Width,Form1->Image_fondo_mapa->Height)
,Form1->Image_fondo_mapa->Picture->Bitmap->Canvas
,TRect(0,0,Form1->Image_fondo_mapa->Width, Form1->Image_fondo_mapa->Height));
Application->ProcessMessages();
Form1->sLabel1->Caption = i;
if(SALIR_FLAG == true)
{
i=20001;
Form1->Close();
}
else if (i == 19999) { i = 0; }
Turno_JUGADORES();
}
}
void Turno_JUGADORES(void)
{
switch(NUM_TURNO)
{
case 1:
MOVE_PLAYER1(); break;
case 2:
MOVE_PLAYER2(); break;
case 3:
MOVE_PLAYER3(); break;
case 4:
MOVE_PLAYER3(); break;
case 5:
MOVE_IA(); break;
}
}
void MOVE_PLAYER1(void)
{
}
void MOVE_PLAYER2(void)
{
}
void MOVE_PLAYER3(void)
{
}
void MOVE_PLAYER4(void)
{
}
void MOVE_IA(void)
{
}
void __fastcall TForm1::sButton2Click(TObject *Sender)
{
SALIR_FLAG = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::trkbVolumenChange(TObject *Sender)
{
volumen = 100 - trkbVolumen->Position;
BASS_ChannelSetAttributes(stream, 0, volumen, -101);
}
//---------------------------------------------------------------------------
// DrawTile Function /////////////////////////////////////////////////////////////////////
//
// Draws a map tile for the map coordinates specified.
//
void DrawTile( int x, int y )
{
// Is there an item present at this location?
TColor nColor;
char nCharacter;
if( nItemArray[y][x] != ITEM_NONE )
{
int nItemType = nItemArray[y][x];
nColor = sItemIndex[nItemType].nColorCode;
nCharacter = sItemIndex[nItemType].nCharacter;
}
else
{
int nTileType = nMapArray[y][x];
nColor = sTileIndex[nTileType].nColorCode;
nCharacter = sTileIndex[nTileType].nCharacter;
}
switch(Char(Form1->StringGrid1->Cells[y][x].c_str()))
{
case 's':
nCharacter = 's';
break;
case 'h':
nCharacter = 'h';
break;
case 'c':
nCharacter = 'c';
break;
}
// Draw the tile to the screen
Form1->StringGrid1->Canvas->Brush->Color = nColor;
Form1->StringGrid1->Canvas->Font->Color = clBlack;
Form1->StringGrid1->Cells[x][y] = nCharacter;
}
// DrawMAP Function //////////////////////////////////////////////////////////////
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
Form1->StringGrid1->Canvas->Font->Color = clBlack;
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();
}
void __fastcall TForm1::sButton3Click(TObject *Sender)
{
if(MUESTRA_MAPA == true)
{
Form1->StringGrid1->Visible = false;
Form1->StringGrid1->Repaint();
MUESTRA_MAPA = false;
}
else {
Form1->StringGrid1->Visible = true;
Form1->StringGrid1->Repaint();
MUESTRA_MAPA = true;
}
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void __fastcall TForm1::sButton4Click(TObject *Sender)
{
TICKS_MOV_PLAYER = (rand() % 8) + 1;
apoints1->Caption = TICKS_MOV_PLAYER;
NPC_Draw();
MAZMORRA_CREA_ITEMS();
MAZMORRA_A_CC();
dibuja_Mazmorra_en_pantalla();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::sButton7Click(TObject *Sender)
{
Movement_Player(VK_UP);
}
//---------------------------------------------------------------------------
void Movement_Player(int Key)
{
switch(Key)
{
case VK_F10: KeysDown[VK_F10] = true; break;
case VK_UP: KeysDown[VK_UP] = true; break;
case VK_LEFT: KeysDown[VK_LEFT] = true; break;
case VK_RIGHT: KeysDown[VK_RIGHT] = true; break;
case VK_DOWN: KeysDown[VK_DOWN] = true; break;
case VK_ESCAPE: KeysDown[VK_ESCAPE] = true; break;
}
if(KeysDown[VK_F10])
{
char numero[4];
AnsiString mensaje_final = "Numero de Habitaciones Totales en Mapa = ";
AnsiString ROOMS = itoa(num_Rooms, numero, 10);
mensaje_final = mensaje_final + ROOMS;
ShowMessage(mensaje_final);
}
int nDeltaX;
int nDeltaY;
if(TICKS_MOV_PLAYER == 0) PERMITIR_MOVER = false;
else
{
PERMITIR_MOVER = true;
}
if(PERMITIR_MOVER == true)
{
// Process the input
// Move up
if(KeysDown[VK_UP])
{
nDeltaX = 0;
nDeltaY = -1;
}
// Move left
if(KeysDown[VK_LEFT])
{
nDeltaX = -1;
nDeltaY = 0;
}
// Move right
if(KeysDown[VK_RIGHT])
{
nDeltaX = 1;
nDeltaY = 0;
}
// Move down
if(KeysDown[VK_DOWN])
{
nDeltaX = 0;
nDeltaY = 1;
}
// Escape key
if(KeysDown[VK_ESCAPE])
{
// Quit the program
ShowMessage("ESC Key Pressed!!!");
}
}
// Check and see if we're allowed to move in the direction specified
if( IsPassable(nPlayerX + nDeltaX, nPlayerY + nDeltaY) )
{
// If allowed, move in the direction specified
Form1->StringGrid1->Cells[nPlayerX][nPlayerY] = backup_fondo; //Put the background character
backup_fondo = Form1->StringGrid1->Cells[nPlayerX+nDeltaX][nPlayerY+nDeltaY]; // Save new position background
//Stablish new coordenates for the Player and draw the player
nPlayerX += nDeltaX;
nPlayerY += nDeltaY;
PLAYER1X = nPlayerX;
PLAYER1Y = nPlayerY;
if(Form1->StringGrid1->Cells[nPlayerX][nPlayerY] == '+')
{
BASS_StreamPlay(sample1, false, NULL);
}
if(Form1->StringGrid1->Cells[nPlayerX][nPlayerY] == 'c')
{
Form1->StringGrid1->Cells[nPlayerX][nPlayerY] = '.';
TotalCoins += rand() % 300;
Form1->Label_Coins->Caption = TotalCoins;
backup_fondo = Form1->StringGrid1->Cells[nPlayerX][nPlayerY]; // Save new position background
BASS_StreamPlay(sample_treasure_chest, false, NULL);
MAZMORRA_A_CC();
dibuja_Mazmorra_en_pantalla();
}
if(Form1->StringGrid1->Cells[nPlayerX][nPlayerY] == 's')
{
Form1->StringGrid1->Cells[nPlayerX][nPlayerY] = '.';
Form1->sGauge1->Progress -= 5;
backup_fondo = Form1->StringGrid1->Cells[nPlayerX][nPlayerY]; // Save new position background
BASS_StreamPlay(sample_spiderbite, false, NULL);
MAZMORRA_A_CC();
dibuja_Mazmorra_en_pantalla();
}
if(Form1->StringGrid1->Cells[nPlayerX][nPlayerY] == 'h')
{
Form1->StringGrid1->Cells[nPlayerX][nPlayerY] = '.';
Form1->sGauge1->Progress += 10;
backup_fondo = Form1->StringGrid1->Cells[nPlayerX][nPlayerY]; // Save new position background
BASS_StreamPlay(sample_grump, false, NULL);
MAZMORRA_A_CC();
dibuja_Mazmorra_en_pantalla();
}
Form1->StringGrid1->Font->Color = clBlue;
Form1->StringGrid1->Cells[nPlayerX][nPlayerY] = "P";
TICKS_MOV_PLAYER--;
Form1->apoints1->Caption = TICKS_MOV_PLAYER;
CENTRA_MAPA();
Key=NULL; fflush(stdin);
nDeltaX = 0; nDeltaY = 0;
dibuja_Mazmorra_en_pantalla();
for(int i = 0; i<255; i++)
{
KeysDown[i] = false;
}
}
else {
Key=NULL; fflush(stdin);
nDeltaX = 0; nDeltaY = 0;
dibuja_Mazmorra_en_pantalla();
for(int i = 0; i<255; i++)
{
KeysDown[i] = false;
}
}
}
void __fastcall TForm1::sButton8Click(TObject *Sender)
{
Movement_Player(VK_DOWN);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::sButton9Click(TObject *Sender)
{
Movement_Player(VK_LEFT);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::sButton10Click(TObject *Sender)
{
Movement_Player(VK_RIGHT);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
contador_anim_paredes++;
dibuja_Mazmorra_en_pantalla();
if (contador_anim_paredes == 32) {
contador_anim_paredes = 0;
}
}
//---------------------------------------------------------------------------