// map.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include "map.h"
#include "engine.h"
static ThingTypeID static_box_thing_id = -1;
void update_static_box(ThingID id);
void render_static_box(ThingID id);
Model* cube;
DEFINE_FIELDS(static_box_fields,
{ "pos", FIELD_VEC3 },
{ "half_extents", FIELD_VEC3 },
{ "shape", FIELD_INT },
{ "color", FIELD_VEC3 }
);
ThingTypeDef static_box_thing = {
"static_box",
static_box_fields,
static_box_fields_count,
update_static_box,
render_static_box,
default_physics
};
static float parse_float(const char* token) {
float value = strtof(token, NULL);
return roundf(value * 10000.0f) / 10000.0f;
}
void map_load(const char* filename) {
FILE* file = fopen(filename, "r");
if (!file) {
printf("[Map] Failed to open: %s\n", filename);
return;
}
char line[256];
int line_num = 0;
if (static_box_thing_id == -1)
static_box_thing_id = thing_register_type(&static_box_thing);
cube = model_load("assets/maps/cube.obj");
while (fgets(line, sizeof(line), file)) {
line_num++;
if (line[0] == '#' || line[0] == '\n') continue;
char* tokens[16];
int count = 0;
char* tok = strtok(line, " \t\n");
while (tok && count < 16) {
tokens[count++] = tok;
tok = strtok(NULL, " \t\n");
}
if (count == 0) continue;
if (strcmp(tokens[0], "box") == 0) {
if (count < 10) {
printf("[Map] Invalid box format at line %d\n", line_num);
continue;
}
vec3 pos = {
parse_float(tokens[1]),
parse_float(tokens[2]),
parse_float(tokens[3])
};
vec3 size = {
parse_float(tokens[4]),
parse_float(tokens[5]),
parse_float(tokens[6])
};
vec3 color = {
parse_float(tokens[7]),
parse_float(tokens[8]),
parse_float(tokens[9])
};
printf("[Map] Box: pos.y=%.4f size.y=%.4f\n", pos[1], size[1]);
ThingID id = thing_spawn(static_box_thing_id);
if (id != -1) {
thing_set_vec3(id, "pos", pos);
thing_set_vec3(id, "half_extents", size);
thing_set_vec3(id, "color", color);
thing_set_int(id, "shape", COLLISION_BOX);
}
}
else if (strcmp(tokens[0], "light") == 0) {
if (count < 8) {
printf("[Map] Invalid light format at line %d\n", line_num);
continue;
}
vec3 pos = {
parse_float(tokens[1]),
parse_float(tokens[2]),
parse_float(tokens[3])
};
vec3 color = {
parse_float(tokens[4]),
parse_float(tokens[5]),
parse_float(tokens[6])
};
float intensity = parse_float(tokens[7]);
light_create(pos, color, intensity);
}
else {
printf("[Map] Unknown type '%s' at line %d\n", tokens[0], line_num);
}
}
fclose(file);
}
void update_static_box(ThingID id) {}
void render_static_box(ThingID id) {
vec3 pos, half_extents, color, rot = { 0, 0, 0 };
thing_get_vec3(id, "pos", pos);
thing_get_vec3(id, "half_extents", half_extents);
thing_get_vec3(id, "color", color);
vec3 scale = { half_extents[0] * 2.0f, half_extents[1] * 2.0f, half_extents[2] * 2.0f };
renderer_draw_model(cube, NULL, color, pos, rot, scale);
//printf("ID %d: pos=(%.2f,%.2f,%.2f) half_extents=(%.2f,%.2f,%.2f) render scale=(%.2f,%.2f,%.2f)\n",
// id, pos[0], pos[1], pos[2], half_extents[0], half_extents[1], half_extents[2],
// scale[0], scale[1], scale[2]);
}