Jump to content
  • Advertisement
Sign in to follow this  
Anubiss

Creating A New Object Ingame

This topic is 2517 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey friends.
I hope you can help me with my lil game what i´m writing in C with the PaLib library for my nintendo ds. I have a ship what´s flying through the orbit. when the ship is landing in the space-station i want to have the option "buy new station". But how do i code the creation of a new station. i just need the idea of how doing that.
Will be happy about any help.


Here´s the code till now:


// Includes
#include <PA9.h>
#include "gfx/all_gfx.c"
#include "gfx/all_gfx.h"
int main(int argc, char ** argv)
{
PA_Init();
PA_InitVBL();
PA_EasyBgLoad(0, 1, aa);
PA_InitText(1,0);
PA_LoadSpritePal(0, 0, (void*)vaisseau_Pal);
PA_LoadSpritePal(0, 1, (void*)station01_Pal);
PA_CreateSprite(0, 0,(void*)vaisseau_Sprite, OBJ_SIZE_32X32,1, 0, 128-16, 96-16); // Das SChiff auf Mitte des unteren Screens setzen
PA_SetSpriteRotEnable(0,0,0);
u16 angle = 0;
int x = (128) << 8;
int y = (96) << 8;
int sx;
int sy;
typedef struct{
int x;
int xmax;
int schalter;
}pflanze;
pflanze buche;
buche.xmax=1000;
buche.schalter=0;
int scrollx = 0;
int scrolly = 0;

//Raumstation:
/////////////////////////////////////////////////
typedef struct{
int x;
int y;
int flag;
int entfx;
int entfy;
}station;
station rs;
//station fremd[10];//0-9
rs.x=200;
rs.y=96;
rs.flag=0;
rs.entfx;
rs.entfy;
void station01test(void){
rs.entfx=(sx>>8)-(rs.x);//Entfernung zwischen Station und Raumschiff messen
if (rs.entfx<0){rs.entfx=-rs.entfx;}//Betrag, falls Entfernung negativ!
rs.entfy=(sy>>8)-(rs.y);
if (rs.entfy<0){rs.entfy=-rs.entfy;}


if (rs.flag==0){
PA_CreateSprite(0, 2,(void*)station01_Sprite, OBJ_SIZE_64X64,1, 1, rs.x-16, rs.y-16);
rs.flag=1;
}

if ((rs.entfx>100)&&rs.flag==1){
PA_DeleteSprite (0, 2);
rs.flag=0;
}

if ((rs.entfy>100)&&rs.flag==1){
PA_DeleteSprite (0, 2);
rs.flag=0;
}

if ((rs.entfx<30)&&(rs.entfy<30)){
PA_OutputText(1,0,2,"'START' zum Landen-");
}
if ((rs.entfx>30)||(rs.entfy>30)){
PA_OutputText(1,0,2," ");
}

}
//Einfacher test:
/////////////////////////////////////////////////
void wachse(void){
if (buche.schalter==1)
if(buche.x<buche.xmax)
buche.x++;
if(buche.x==buche.xmax)
buche.x=buche.xmax;

}

//STATUSBILDSCHIRM
//////////////////////////////////////////////////////////////////
void statusscreen(void){
//Ausgabe von Informationen auf oberen Screen:
////////////////////////////////////////////////////////////////////////////////
PA_OutputText(1,0,12,"Map-x: %02d", scrollx>>8);
PA_OutputText(1,0,13,"Map-y: %02d", scrolly>>8);
PA_OutputText(1,0,14,"-------------------");
PA_OutputText(1,0,15,"Schiff-x: %02d", sx>>8);
PA_OutputText(1,0,16,"Schiff-y: %02d", sy>>8);
PA_OutputText(1,0,17,"-------------------");
PA_OutputText(1,0,18,"x von Station entfernt: %02d", rs.entfx);
PA_OutputText(1,0,19,"y von Station entfernt: %02d", rs.entfy);
PA_OutputText(1,0,20,"Winkel des Schiffes: %02d", angle);
}

//MAIN:
////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
while (1)
{
//Test-Buche: zählt von 0 bis 1000, wenn "A" gedrückt wird
if (Pad.Newpress.A) buche.schalter=1;
wachse();
PA_OutputText(1,0,0,"%02d", buche.x);


//Winkelberechnung-Flugschiff:
///////////////////////////////////////////////////////////////////////////////
angle = PA_GetAngle(x>>8, y>>8, Stylus.X, Stylus.Y); //
PA_SetRotsetNoZoom(0, 0, angle); // Änderung des Schiffswinkels //
//Flugschiff-Koordinaten AUSSERHALB DER ENGINE:
sx=(scrollx+x);
sy=(scrolly+y);

//Hintergrund-Scrolling:
//////////////////////////////////////////////////////////////////////////////
if(Pad.Held.B){ //
scrollx += PA_Cos(angle); //
scrolly += PA_Sin(angle);
};
//Map-Grenzen: //
if((scrollx>>8)>(2560-255)){
scrollx=590080;
};
if((scrollx>>8)<0){
scrollx=0;
};
if((-scrolly>>8)>(1600-191)){
scrolly=-360704;
};
if((-scrolly>>8)<0){
scrolly=0;
};
//Set-Map:
PA_LargeScrollXY(0, 1, scrollx>>8, -scrolly>>8);

statusscreen();

PA_SetSpriteXY(0, 0, (x>>8)-16, (y>>8)-16);
PA_SetSpriteXY(0, 2, ((rs.x-16)+(-scrollx>>8))-16, ((rs.y-16)+(scrolly>>8))-16);
station01test();
PA_WaitForVBL();
}

return 0;

}

Share this post


Link to post
Share on other sites
Advertisement
-edit- Yeah, like Boogyman said this is for C#. Sorry i skipped over your code and thought you where asking help for C#.

You could just code the station like you always do. Just make a class for it and give it attributes. It should probably have a owner attribute and a location attribute too.

When a player buys it you instantiate the class and maybe put it in a list.


class Station
{

public int health { get; private set; }
public int ownerID { get; private set; }
public Vector2 location { get; private set; }


public Station(int stationType, int ownerID, Vector2 location)
{

if (stationType == 1)
{
this.health = 500;
}
else if (stationType == 2)
{
this.health = 750;
}

this.ownerID = ownerID;

this.location = location;
}

}


When your player buys a station do something like this.

Station newstation = new Station(stationType, playerID, location);
//offcourse these variables need to get filled.

Share this post


Link to post
Share on other sites
I believe the language in question is C, not C#, alas, the new keyword is non existent, nor are constructors.

To the OP, I take it you have not yet covered dynamic memory. If you have not, you can always create an array of stations (if you haven't learned about arrays, I guess you can create a "StationManager" struct and put however many stations you want) and each station could contain a variable rather it's activated or not (this will be very painful to code. It will be much easier if you use arrays). This little hack should allow you to do what you want, but you should probably limit your use of it to this program only biggrin.png Besides, this setup is limited to a static number of stations.

Of course, the best solution is to use malloc() and free() to create them during execution. I don't know why I didn't mention that.

Share this post


Link to post
Share on other sites
Make a struct to represent the stations data, use the malloc function to get a pointer to a new instance. You should call the free() function with this pointer when your'e done with it.

Share this post


Link to post
Share on other sites
this malloc function sounds very good, but it seems like i need to understand a bit more about this and also pointers.
So i made now a struct for the stations i want to buy:


typedef struct{
int id;
int x;
int y;
int flag;
int entfx;
int entfy;
}ownstation;
ownstation neu[10];


Is it correct to make an array now if i for example don´t want to have more than 10 stations?

then i have found following for this malloc:


char * String;
String = (char *) malloc(1000);


so if the code would be:

if (klicking on "buy"){
do what?
}

EDIT:
ownstation* neu(neu[10](int id[10]));

something like this maybe? (i know that it is not correct, but maybe i am on the right way)

Share this post


Link to post
Share on other sites
If you don't want more than 10 stations, that code is what you want. If you want to use malloc to dynamically allocate an array of n ownstations then you want
ownstation* stations;
stations = (ownstation*) malloc(n*sizeof(ownstation))

before you use the array and free(stations) to release the memory once you are finished with it. To resize the array you want the following code:
ownstation* temp;
temp = (ownstation*) malloc(newlen*sizeof(ownstation));
memcpy(temp, stations, oldlen*sizeof(ownstation));
free(stations);
stations=temp

which will extend the length of the stations array from oldlen to newlen. If you want to make the array pointed to by stations to be shorter, simply replace oldlen with newlen.

Share this post


Link to post
Share on other sites
allright. the 1st part i checked.

now i do following:


void menu(void){
if (Pad.Newpress.X) n++;
}


and


if ((rs.entfx<30)&&(rs.entfy<30)){
PA_OutputText(1,0,2,"'X' zum kaufen-");
menu();
}


So i add "1" to n, when the X-Button is pressed.
And how are the stations looking like? I mean, if n=2, do i have stations[0] and stations[1] then?

Share this post


Link to post
Share on other sites
Alright. It´s working so far. I can add a new station. But after making one station the game freezes.


void ladestationen(void){
while (istationen<n){
stations[istationen].x=210;
stations[istationen].y=100;
if (stations[istationen].flag!=1){
PA_CreateSprite(0, istationen,(void*)station01_Sprite, OBJ_SIZE_64X64,1, 1, stations[istationen].x-16, stations[istationen].y-16);
stations[istationen].flag=1;
}
PA_SetSpriteXY(0, 3, ((stations[istationen].x-16)+(-scrollx>>8))-16, ((stations[istationen].y-16)+(scrolly>>8))-16);
}
}


I almost think, that this is correct.
But following I wrote out of a function and out of the main-function:

ownstation* stations;
stations = (ownstation*) malloc(n*sizeof(ownstation));


And how does it work with resizing?
I don´t really understand where exactly this has to be written.

EDIT:
That´s wrong, aye?


void ladestationen(void){
stations = (ownstation*) malloc(n*sizeof(ownstation));
while (istationen<n){
stations[istationen].x=210;
stations[istationen].y=100;
if (stations[istationen].flag!=1){
PA_CreateSprite(0, istationen,(void*)station01_Sprite, OBJ_SIZE_64X64,1, 1, stations[istationen].x-16, stations[istationen].y-16);
stations[istationen].flag=1;
}
PA_SetSpriteXY(0, 3, ((stations[istationen].x-16)+(-scrollx>>8))-16, ((stations[istationen].y-16)+(scrolly>>8))-16);
}

temp = (ownstation*) malloc(newlen*sizeof(ownstation));
memcpy(temp, stations, oldlen*sizeof(ownstation));
free(stations);
stations=temp;

}

Share this post


Link to post
Share on other sites
The line
stations = (ownstation*) malloc(n*sizeof(ownstation));
allocates enough space for n ownstation structs. You replace n with however many ownstations you want in your array. With resizing, I just remembered that you can use the realloc function so you can just say stations = (ownstation*) realloc(stations,newlen); where newlen is the expression representing the length you wish to resize the array to.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!