- Organització dels templates en categories
- Al afegir un actor com a template, "neteja" el nom de numerets - El numeros del final de nom ara tenen el format "-XX"
This commit is contained in:
127
source/actor.cpp
127
source/actor.cpp
@@ -88,6 +88,7 @@ namespace actor
|
||||
act->anim_frame=0;
|
||||
act->react_mask = act->react_push = 0;
|
||||
act->flags = FLAG_NONE;
|
||||
act->template_category = 0;
|
||||
return act;
|
||||
}
|
||||
|
||||
@@ -204,6 +205,16 @@ namespace actor
|
||||
|
||||
char tmp[255];
|
||||
|
||||
void cleanName(actor_t *act)
|
||||
{
|
||||
int size = strlen(act->name);
|
||||
if (act->name[size-3] == '-' && act->name[size-1] >= 48 && act->name[size-1] <= 57 && act->name[size-2] >= 48 && act->name[size-2] <= 57 )
|
||||
{
|
||||
act->name[size-3] = 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Li donem al actor un nom únic
|
||||
void setUniqueName(actor_t *act)
|
||||
{
|
||||
@@ -217,11 +228,12 @@ namespace actor
|
||||
// ... 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
|
||||
// Si el nom actual no acaba en guió + 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 )
|
||||
if (act->name[size-3] != '-' || act->name[size-1] < 48 || act->name[size-1] > 57 || act->name[size-2] < 48 || act->name[size-2] > 57 )
|
||||
{
|
||||
strcat(act->name, "01");
|
||||
strcat(other->name, "-00");
|
||||
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;
|
||||
@@ -332,23 +344,25 @@ namespace actor
|
||||
return alpha;
|
||||
}
|
||||
|
||||
void saveToFile(FILE *f, actor_t *act)
|
||||
void saveToFile(FILE *f, actor_t *act, bool tab)
|
||||
{
|
||||
fprintf(f, "\nactor{\n");
|
||||
fprintf(f, " name: %s\n", act->name);
|
||||
fprintf(f, " bmp: %s\n", act->bmp);
|
||||
fprintf(f, " bmp-rect: %i %i %i %i\n", act->bmp_rect.x, act->bmp_rect.y, act->bmp_rect.w, act->bmp_rect.h);
|
||||
fprintf(f, " bmp-offset: %i %i\n", act->bmp_offset.x, act->bmp_offset.y);
|
||||
fprintf(f, " pos: %i %i %i\n", act->pos.x, act->pos.y, act->pos.z);
|
||||
fprintf(f, " size: %i %i %i\n", act->size.x, act->size.y, act->size.z);
|
||||
if (act->orient!=0) fprintf(f, " orient: %s\n", numToOrient(act->orient));
|
||||
if (act->anim_cycle!=0) fprintf(f, " anim-cycle: %s\n", act->anim_cycle==0 ? "WALK" : act->anim_cycle==1 ? "SEQ" : "MIN");
|
||||
if (act->anim_wait!=0) fprintf(f, " anim-wait: %i\n", act->anim_wait);
|
||||
if (act->flags!=0) fprintf(f, " flags: %s\n", numToFlags(act->flags));
|
||||
if (act->react_mask!=0) fprintf(f, " react-mask: %s\n", numToOrient(act->react_mask));
|
||||
if (act->react_push!=0) fprintf(f, " react-push: %s\n", numToOrient(act->react_push));
|
||||
if (act->movement!=0) fprintf(f, " movement: %s\n", numToMov(act->movement));
|
||||
fprintf(f, "}\n");
|
||||
char ws[5] = "";
|
||||
if (tab) strcpy(ws, " ");
|
||||
fprintf(f, "\n%sactor{\n", ws);
|
||||
fprintf(f, " %sname: %s\n", ws, act->name);
|
||||
fprintf(f, " %sbmp: %s\n", ws, act->bmp);
|
||||
fprintf(f, " %sbmp-rect: %i %i %i %i\n", ws, act->bmp_rect.x, act->bmp_rect.y, act->bmp_rect.w, act->bmp_rect.h);
|
||||
fprintf(f, " %sbmp-offset: %i %i\n", ws, act->bmp_offset.x, act->bmp_offset.y);
|
||||
fprintf(f, " %spos: %i %i %i\n", ws, act->pos.x, act->pos.y, act->pos.z);
|
||||
fprintf(f, " %ssize: %i %i %i\n", ws, act->size.x, act->size.y, act->size.z);
|
||||
if (act->orient!=0) fprintf(f, " %sorient: %s\n", ws, numToOrient(act->orient));
|
||||
if (act->anim_cycle!=0) fprintf(f, " %sanim-cycle: %s\n", ws, act->anim_cycle==0 ? "WALK" : act->anim_cycle==1 ? "SEQ" : "MIN");
|
||||
if (act->anim_wait!=0) fprintf(f, " %sanim-wait: %i\n", ws, act->anim_wait);
|
||||
if (act->flags!=0) fprintf(f, " %sflags: %s\n", ws, numToFlags(act->flags));
|
||||
if (act->react_mask!=0) fprintf(f, " %sreact-mask: %s\n", ws, numToOrient(act->react_mask));
|
||||
if (act->react_push!=0) fprintf(f, " %sreact-push: %s\n", ws, numToOrient(act->react_push));
|
||||
if (act->movement!=0) fprintf(f, " %smovement: %s\n", ws, numToMov(act->movement));
|
||||
fprintf(f, "%s}\n", ws);
|
||||
}
|
||||
|
||||
const bool check_2d_collision(actor_t *obj1, actor_t *obj2)
|
||||
@@ -1306,14 +1320,18 @@ namespace actor
|
||||
namespace templates
|
||||
{
|
||||
std::vector<actor_t> templates;
|
||||
std::vector<std::string> categories;
|
||||
|
||||
void load()
|
||||
{
|
||||
//newCategory("default");
|
||||
categories.clear();
|
||||
templates.clear();
|
||||
char filename[] = "templates.txt";
|
||||
int filesize=0;
|
||||
char *buffer = file::getFileBuffer(filename, filesize, true);
|
||||
char *original_buffer = buffer;
|
||||
int current_category = 0;
|
||||
|
||||
if (buffer)
|
||||
{
|
||||
@@ -1321,12 +1339,25 @@ namespace actor
|
||||
{
|
||||
const char* key = file::readString(&buffer);
|
||||
|
||||
if (util::strcomp(key, "actor{"))
|
||||
if (util::strcomp(key, "category{"))
|
||||
{
|
||||
const char* key = file::readString(&buffer);
|
||||
if (util::strcomp(key, "name:")) {
|
||||
const char *val = file::readString(&buffer);
|
||||
current_category = newCategory(val);
|
||||
}
|
||||
}
|
||||
else if (util::strcomp(key, "actor{"))
|
||||
{
|
||||
actor_t *t = createFromFile(&buffer);
|
||||
t->template_category = current_category;
|
||||
templates.push_back(*t);
|
||||
free(t);
|
||||
}
|
||||
else if (util::strcomp(key, "}")) {
|
||||
current_category = 0;
|
||||
}
|
||||
|
||||
}
|
||||
free(original_buffer);
|
||||
}
|
||||
@@ -1336,18 +1367,56 @@ namespace actor
|
||||
{
|
||||
FILE *f = fopen("data/templates.txt", "w");
|
||||
|
||||
for (int i=0; i<templates.size(); ++i)
|
||||
for (auto cat_name : categories)
|
||||
{
|
||||
|
||||
fprintf(f, "\ncategory{\n");
|
||||
fprintf(f, " name: %s\n", cat_name.c_str());
|
||||
|
||||
auto actors = getByCategory(cat_name.c_str());
|
||||
for (auto t : actors )
|
||||
{
|
||||
saveToFile(f, &t, true);
|
||||
}
|
||||
|
||||
fprintf(f, "\n}\n");
|
||||
}
|
||||
|
||||
/*for (int i=0; i<templates.size(); ++i)
|
||||
{
|
||||
actor_t t = templates[i];
|
||||
saveToFile(f, &t);
|
||||
}
|
||||
}*/
|
||||
|
||||
fclose(f);
|
||||
}
|
||||
|
||||
const int size()
|
||||
/*const int size()
|
||||
{
|
||||
return templates.size();
|
||||
}*/
|
||||
|
||||
std::vector<actor::actor_t> getByCategory(const char* category)
|
||||
{
|
||||
std::string catname = category;
|
||||
std::vector<actor_t> actors;
|
||||
for (auto act : templates)
|
||||
{
|
||||
if (act.template_category>=categories.size()) act.template_category = 0;
|
||||
if (categories[act.template_category]==catname) actors.push_back(act);
|
||||
}
|
||||
return actors;
|
||||
}
|
||||
|
||||
std::vector<std::string> getCategories()
|
||||
{
|
||||
return categories;
|
||||
}
|
||||
|
||||
const int newCategory(const char *name)
|
||||
{
|
||||
categories.push_back(name);
|
||||
return categories.size()-1;
|
||||
}
|
||||
|
||||
actor_t *get(const int index)
|
||||
@@ -1381,16 +1450,22 @@ namespace actor
|
||||
|
||||
void add(actor_t *act)
|
||||
{
|
||||
// Fem una copia del actor
|
||||
actor_t new_template;
|
||||
copy(&new_template, act);
|
||||
// Li fiquem la categoria per defecte
|
||||
new_template.template_category = 0;
|
||||
// Netejem el nom de numerets
|
||||
cleanName(&new_template);
|
||||
|
||||
// Si ja hi ha una plantilla amb eixe nom...
|
||||
if (actor::templates::getByName(act->name))
|
||||
{
|
||||
// ... la actualitzem amb les dades del actor seleccionat
|
||||
actor_t *existing_template = actor::templates::getByName(act->name);
|
||||
copy(existing_template, act);
|
||||
copy(existing_template, &new_template);
|
||||
} 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();
|
||||
|
||||
Reference in New Issue
Block a user