- [NEW] actor::duplicate()

- [NEW] actor::setUniqueName()
- [CHG] Ara si actor::templates::add() detecta que ja hi ha una plantilla amb eixe nom, el que fa es actualitzar-la
This commit is contained in:
2024-06-14 12:20:46 +02:00
parent 196801000e
commit e101d276e8
2 changed files with 63 additions and 11 deletions

View File

@@ -75,14 +75,20 @@ namespace actor
return act; return act;
} }
actor_t *duplicate(actor_t *act)
{
actor_t *new_act = createEmptyActor();
actor::templates::copy(new_act, act);
new_act->surface = draw::loadSurface(new_act->bmp);
new_act->prev = new_act->next = new_act->above = new_act->below = nullptr;
return new_act;
}
actor_t *createFromTemplate(const char *name) actor_t *createFromTemplate(const char *name)
{ {
actor_t *templ = actor::templates::getByName(name); actor_t *templ = actor::templates::getByName(name);
if (!templ) return nullptr; if (!templ) return nullptr;
return duplicate(templ);
actor_t *act = createEmptyActor();
actor::templates::copy(act, templ);
return act;
} }
actor_t *createFromFile(char **buffer) actor_t *createFromFile(char **buffer)
@@ -163,6 +169,39 @@ namespace actor
char tmp[255]; char tmp[255];
// Li donem al actor un nom únic
void setUniqueName(actor_t *act)
{
// Recorrem tots els actors
actor_t *other = first;
while (other != nullptr)
{
// Si no som eixe actor...
if (other != act)
{
// ... però tenim el mateix nom...
if (strcmp(act->name, other->name)==0)
{
// Si el nom actual no acaba en dos digits, li afegim "01" al final
int size = strlen(act->name);
if (act->name[size-1] < 48 || act->name[size-1] > 57 || act->name[size-2] < 48 || act->name[size-2] > 57 )
{
strcat(act->name, "01");
} else {
// Si ja acaba en dos digits, agafem el numero, li sumem 1, i li'l tornem a ficar
int num = (act->name[size-1]-48) + (act->name[size-2]-48)*10;
num++;
act->name[size-1] = (num % 10)+48;
act->name[size-2] = int(num / 10)+48;
}
// I tornem a començar des del principi amb el nou nom
other = first;
}
}
other = other->next;
}
}
const char *numToOrient(uint8_t value) const char *numToOrient(uint8_t value)
{ {
tmp[0]=0; tmp[0]=0;
@@ -1030,12 +1069,21 @@ namespace actor
dest->movement = source->movement; dest->movement = source->movement;
} }
void add(actor_t *actor) void add(actor_t *act)
{ {
actor_t new_template; // Si ja hi ha una plantilla amb eixe nom...
copy(&new_template, actor); if (actor::templates::getByName(act->name))
templates.push_back(new_template); {
// ... la actualitzem amb les dades del actor seleccionat
actor_t *existing_template = actor::templates::getByName(act->name);
copy(existing_template, act);
} else {
// ... i sinó, afegim el actor seleccionat a la llista de plantilles
actor_t new_template;
copy(&new_template, act);
templates.push_back(new_template);
}
save();
} }
} }
} }

View File

@@ -90,10 +90,14 @@ namespace actor
// Torna un nou actor // Torna un nou actor
actor_t *create(std::string name, vec3_t p, vec3_t s, std::string bmp, SDL_Rect r, SDL_Point o); actor_t *create(std::string name, vec3_t p, vec3_t s, std::string bmp, SDL_Rect r, SDL_Point o);
actor_t *duplicate(actor_t *act);
actor_t *createFromTemplate(const char *name); actor_t *createFromTemplate(const char *name);
actor_t *createFromFile(char **buffer); actor_t *createFromFile(char **buffer);
void setUniqueName(actor_t *act);
void saveToFile(FILE *f, actor_t *act); void saveToFile(FILE *f, actor_t *act);
void setDirty(actor_t *act, const bool force=false); void setDirty(actor_t *act, const bool force=false);
@@ -129,6 +133,6 @@ namespace actor
actor_t *get(const int index); actor_t *get(const int index);
actor_t *getByName(const char *name); actor_t *getByName(const char *name);
void copy(actor_t *dest, actor_t *source); void copy(actor_t *dest, actor_t *source);
void add(actor_t *actor); void add(actor_t *act);
} }
} }