Door and map changing
This commit is contained in:
@ -8,7 +8,92 @@
|
||||
#include "assetmap.h"
|
||||
#include "asset/asset.h"
|
||||
#include "assert/assert.h"
|
||||
#include "asset/assetjson.h"
|
||||
|
||||
void assetMapLoadEntity(
|
||||
assetjson_t *jEnt,
|
||||
map_t *map
|
||||
) {
|
||||
int32_t x = (int32_t)assetJsonGetObjectValue(jEnt, "x")->number;
|
||||
int32_t y = (int32_t)assetJsonGetObjectValue(jEnt, "y")->number;
|
||||
entitytype_t type = (entitytype_t)assetJsonGetObjectValue(
|
||||
jEnt, "type"
|
||||
)->number;
|
||||
|
||||
entity_t *ent = mapEntityAdd(map);
|
||||
entityInit(ent, type, map);
|
||||
entityPositionSet(ent, x, y);
|
||||
|
||||
assetjson_t *val;
|
||||
switch(type) {
|
||||
case ENTITY_TYPE_NPC:
|
||||
val = assetJsonGetObjectValue(jEnt, "name");
|
||||
if(val != NULL) {
|
||||
assertTrue(
|
||||
val->type == ASSET_JSON_DATA_TYPE_STRING,
|
||||
"assetMapLoad: NPC name is not a string!"
|
||||
);
|
||||
npcNameSet(&ent->npc, val->string);
|
||||
}
|
||||
|
||||
val = assetJsonGetObjectValue(jEnt, "text");
|
||||
if(val != NULL) {
|
||||
assertTrue(
|
||||
val->type == ASSET_JSON_DATA_TYPE_STRING,
|
||||
"assetMapLoad: NPC text is not a string!"
|
||||
);
|
||||
npcTextSet(&ent->npc, val->string);
|
||||
}
|
||||
break;
|
||||
|
||||
case ENTITY_TYPE_SIGN:
|
||||
val = assetJsonGetObjectValue(jEnt, "text");
|
||||
if(val != NULL) {
|
||||
assertTrue(
|
||||
val->type == ASSET_JSON_DATA_TYPE_STRING,
|
||||
"assetMapLoad: Sign text is not a string!"
|
||||
);
|
||||
signTextSet(&ent->sign, val->string);
|
||||
}
|
||||
break;
|
||||
|
||||
case ENTITY_TYPE_DOOR:
|
||||
val = assetJsonGetObjectValue(jEnt, "door");
|
||||
if(val != NULL) {
|
||||
assetjson_t *door = assetJsonGetObjectValue(val, "x");
|
||||
x = (int32_t)door->number;
|
||||
assertTrue(
|
||||
door->type == ASSET_JSON_DATA_TYPE_NUMBER,
|
||||
"assetMapLoad: Door x is not a number!"
|
||||
);
|
||||
|
||||
door = assetJsonGetObjectValue(val, "y");
|
||||
y = (int32_t)door->number;
|
||||
assertTrue(
|
||||
door->type == ASSET_JSON_DATA_TYPE_NUMBER,
|
||||
"assetMapLoad: Door y is not a number!"
|
||||
);
|
||||
entitydirection_t direction = (entitydirection_t)(
|
||||
assetJsonGetObjectValue(val, "direction")->number
|
||||
);
|
||||
door = assetJsonGetObjectValue(val, "map");
|
||||
maplist_t list;
|
||||
if(door == NULL) {
|
||||
list = map->list;
|
||||
} else if(door->type == ASSET_JSON_DATA_TYPE_STRING) {
|
||||
list = mapListGet(door->string);
|
||||
} else if(door->type == ASSET_JSON_DATA_TYPE_NUMBER) {
|
||||
list = (maplist_t)door->number;
|
||||
} else {
|
||||
assertUnreachable("assetMapLoad: Door map not string or number!");
|
||||
}
|
||||
doorDestinationSet(&ent->door, x, y, direction, list);
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void assetMapLoad(
|
||||
const char_t *path,
|
||||
@ -47,7 +132,7 @@ void assetMapLoad(
|
||||
int32_t layerCount = layers->array.length;
|
||||
assertTrue(layerCount == MAP_LAYERS_MAX, "assetMapLoad: No layers found!");
|
||||
|
||||
mapInit(map, width, height, layerCount);
|
||||
mapInit(map, mapListGet(path), width, height, layerCount);
|
||||
|
||||
// Load tile data.
|
||||
for(int32_t i = 0; i < layerCount; i++) {
|
||||
@ -86,78 +171,7 @@ void assetMapLoad(
|
||||
jEnt->type == ASSET_JSON_DATA_TYPE_OBJECT,
|
||||
"assetMapLoad: Entity is not an object!"
|
||||
);
|
||||
|
||||
int32_t x = (int32_t)assetJsonGetObjectValue(jEnt, "x")->number;
|
||||
int32_t y = (int32_t)assetJsonGetObjectValue(jEnt, "y")->number;
|
||||
entitytype_t type = (entitytype_t)assetJsonGetObjectValue(
|
||||
jEnt, "type"
|
||||
)->number;
|
||||
|
||||
entity_t *ent = mapEntityAdd(map);
|
||||
entityInit(ent, type, map);
|
||||
entityPositionSet(ent, x, y);
|
||||
|
||||
assetjson_t *val;
|
||||
switch(type) {
|
||||
case ENTITY_TYPE_NPC:
|
||||
val = assetJsonGetObjectValue(jEnt, "name");
|
||||
if(val != NULL) {
|
||||
assertTrue(
|
||||
val->type == ASSET_JSON_DATA_TYPE_STRING,
|
||||
"assetMapLoad: NPC name is not a string!"
|
||||
);
|
||||
npcNameSet(&ent->npc, val->string);
|
||||
}
|
||||
|
||||
val = assetJsonGetObjectValue(jEnt, "text");
|
||||
if(val != NULL) {
|
||||
assertTrue(
|
||||
val->type == ASSET_JSON_DATA_TYPE_STRING,
|
||||
"assetMapLoad: NPC text is not a string!"
|
||||
);
|
||||
npcTextSet(&ent->npc, val->string);
|
||||
}
|
||||
break;
|
||||
|
||||
case ENTITY_TYPE_SIGN:
|
||||
val = assetJsonGetObjectValue(jEnt, "text");
|
||||
if(val != NULL) {
|
||||
assertTrue(
|
||||
val->type == ASSET_JSON_DATA_TYPE_STRING,
|
||||
"assetMapLoad: Sign text is not a string!"
|
||||
);
|
||||
signTextSet(&ent->sign, val->string);
|
||||
}
|
||||
break;
|
||||
|
||||
case ENTITY_TYPE_DOOR:
|
||||
val = assetJsonGetObjectValue(jEnt, "door");
|
||||
if(val != NULL) {
|
||||
assetjson_t *door = assetJsonGetObjectValue(val, "x");
|
||||
x = (int32_t)door->number;
|
||||
assertTrue(
|
||||
door->type == ASSET_JSON_DATA_TYPE_NUMBER,
|
||||
"assetMapLoad: Door x is not a number!"
|
||||
);
|
||||
|
||||
door = assetJsonGetObjectValue(val, "y");
|
||||
y = (int32_t)door->number;
|
||||
assertTrue(
|
||||
door->type == ASSET_JSON_DATA_TYPE_NUMBER,
|
||||
"assetMapLoad: Door y is not a number!"
|
||||
);
|
||||
entitydirection_t direction = (entitydirection_t)assetJsonGetObjectValue(
|
||||
val, "direction"
|
||||
)->number;
|
||||
doorDestinationSet(&ent->door, x, y, direction);
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
// TODO: Parse any extra data.
|
||||
assetMapLoadEntity(jEnt, map);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -7,7 +7,25 @@
|
||||
|
||||
#pragma once
|
||||
#include "rpg/world/map.h"
|
||||
#include "asset/assetjson.h"
|
||||
|
||||
/**
|
||||
* Loads an entity from the specified JSON object.
|
||||
*
|
||||
* @param jEnt JSON object to load the entity from.
|
||||
* @param map Map to load the entity into.
|
||||
*/
|
||||
void assetMapLoadEntity(
|
||||
assetjson_t *jEnt,
|
||||
map_t *map
|
||||
);
|
||||
|
||||
/**
|
||||
* Loads a map from the specified path.
|
||||
*
|
||||
* @param path Path to the map file.
|
||||
* @param map Map to load the data into.
|
||||
*/
|
||||
void assetMapLoad(
|
||||
const char_t *path,
|
||||
map_t *map
|
||||
|
Reference in New Issue
Block a user