Debugged entity system

This commit is contained in:
Eric-Paul Ickhorn 2023-10-14 16:38:38 +02:00
parent 5008ae2dbd
commit 9ed734c860
7 changed files with 206 additions and 33 deletions

View File

@ -36,7 +36,7 @@ void tc_register_entity_type(tc_entity_type_s type)
++tc_game_state_g.entity_registry.num_types;
}
tc_entity_type_s * tc_get_entity_type_from_name(char *internal_name)
tc_entity_type_s * tc_get_entity_type_with_name(char *internal_name)
{
uint32_t num_types = tc_game_state_g.entity_registry.num_types;
tc_entity_type_s *types = tc_game_state_g.entity_registry.types;
@ -52,7 +52,7 @@ tc_entity_type_s * tc_get_entity_type_from_name(char *internal_name)
tc_entity_s * tc_create_entity(char *type)
{
tc_entity_type_s *type_struct = tc_get_entity_type_from_name(type);
tc_entity_type_s *type_struct = tc_get_entity_type_with_name(type);
if(type_struct == NULL) return NULL;
return type_struct->functions.fn_create();
@ -75,6 +75,117 @@ void tc_delete_entity(tc_entity_s *entity)
tc_entity_attribute_s * tc_get_entity_attribute_with_name(tc_entity_s *entity, char *name)
{
uint32_t attribute_index = 0;
while(attribute_index < entity->attributes_capacity)
{
tc_entity_attribute_s attribute = entity->attributes[attribute_index];
if(attribute.name == NULL)
{
++attribute_index;
continue;
}
if(!strcmp(attribute.name, name)) return &entity->attributes[attribute_index];
++attribute_index;
}
return NULL;
}
tc_entity_attribute_s * tc_allocate_entity_attribute(tc_entity_s *entity)
{
if(entity->num_attributes >= entity->attributes_capacity)
{
entity->attributes_capacity *= 2;
entity->attributes = realloc(entity->attributes, sizeof(tc_entity_s) * entity->attributes_capacity);
}
++entity->num_attributes;
return &entity->attributes[entity->num_attributes-1];
}
tc_entity_attribute_s * tc_get_or_allocate_entity_attribute_with_name(tc_entity_s *entity, char *name)
{
tc_entity_attribute_s *attribute;
if((attribute = tc_get_entity_attribute_with_name(entity, name)) == NULL)
{
attribute = tc_allocate_entity_attribute(entity);
attribute->name = name;
}
return attribute;
}
void tc_set_pointer_for_entity(tc_entity_s *entity, char *attr_name, void *pointer)
{
tc_get_or_allocate_entity_attribute_with_name(entity, attr_name)->value.pointer = pointer;
}
void tc_set_string_for_entity(tc_entity_s *entity, char *attr_name, char *string)
{
tc_get_or_allocate_entity_attribute_with_name(entity, attr_name)->value.string = string;
}
void tc_set_integer_for_entity(tc_entity_s *entity, char *attr_name, int64_t integer)
{
tc_get_or_allocate_entity_attribute_with_name(entity, attr_name)->value.integer = integer;
}
void tc_set_float_for_entity(tc_entity_s *entity, char *attr_name, double floating)
{
tc_get_or_allocate_entity_attribute_with_name(entity, attr_name)->value.floating = floating;
}
void * tc_get_pointer_from_entity(tc_entity_s *entity, char *attr_name)
{
return tc_get_or_allocate_entity_attribute_with_name(entity, attr_name)->value.pointer;
}
char * tc_get_string_from_entity(tc_entity_s *entity, char *attr_name)
{
return tc_get_or_allocate_entity_attribute_with_name(entity, attr_name)->value.string;
}
int64_t tc_get_integer_from_entity(tc_entity_s *entity, char *attr_name)
{
return tc_get_or_allocate_entity_attribute_with_name(entity, attr_name)->value.integer;
}
double tc_get_float_from_entity(tc_entity_s *entity, char *attr_name)
{
return tc_get_or_allocate_entity_attribute_with_name(entity, attr_name)->value.floating;
}
void tc_forget_entity_attribute(tc_entity_s *entity, char *attr_name)
{
uint32_t attribute_index = 0;
while(attribute_index < entity->attributes_capacity)
{
tc_entity_attribute_s *attribute = &entity->attributes[attribute_index];
if(attribute->name == NULL)
{
++attribute_index;
continue;
}
if(!strcmp(attribute->name, attr_name))
{
entity->attributes[attribute_index] = entity->attributes[entity->num_attributes];
--entity->num_attributes;
break;
}
++attribute_index;
}
return;
}
void tc_send_pointer_to_entity(tc_entity_s *entity, char *event_name, void *pointer)
{
tc_entity_event_s event;

View File

@ -54,6 +54,7 @@ struct tc_entity_attribute
void *pointer;
char *string;
int64_t integer;
double floating;
} value;
};
@ -92,6 +93,16 @@ void tc_spawn_entity (tc_entity_s *en
void tc_teleport_entity (tc_entity_s *entity, tc_location_s location);
void tc_delete_entity (tc_entity_s *entity);
void tc_set_pointer_for_entity (tc_entity_s *entity, char *attr_name, void *pointer);
void tc_set_string_for_entity (tc_entity_s *entity, char *attr_name, char *string);
void tc_set_integer_for_entity (tc_entity_s *entity, char *attr_name, int64_t integer);
void tc_set_float_for_entity (tc_entity_s *entity, char *attr_name, double floating);
void * tc_get_pointer_from_entity (tc_entity_s *entity, char *attr_name);
char * tc_get_string_from_entity (tc_entity_s *entity, char *attr_name);
int64_t tc_get_integer_from_entity (tc_entity_s *entity, char *attr_name);
double tc_get_float_from_entity (tc_entity_s *entity, char *attr_name);
void tc_send_pointer_to_entity (tc_entity_s *entity, char *event_name, void *pointer);
void tc_send_string_to_entity (tc_entity_s *entity, char *event_name, char *string);
void tc_send_integer_to_entity (tc_entity_s *entity, char *event_name, int64_t integer);

View File

@ -56,5 +56,5 @@ void tc_register_player_entity()
tc_register_entity_type(type);
tc_player_entity_type_g = tc_get_entity_type_from_name("player");
tc_player_entity_type_g = tc_get_entity_type_with_name("player");
}

View File

@ -12,7 +12,7 @@ tc_entity_s * tc_allocate_entity()
{
if(registry.entities[entity_index] == NULL)
{
registry.entities[entity_index] = malloc(sizeof(tc_entity_s));
registry.entities[entity_index] = calloc(sizeof(tc_entity_s), 1);
++registry.num_entities;
return registry.entities[entity_index];
}

View File

@ -20,9 +20,11 @@ void tc_init_renderer(techneck_s *techneck)
SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE
);
techneck->renderer.active_camera.position.x = 0.0f;
techneck->renderer.active_camera.position.y = 0.0f;
techneck->renderer.active_camera.position.z = 0.0f;
techneck->renderer.active_camera = calloc(sizeof(tc_camera_s), 1);
techneck->renderer.active_camera->position.x = 0.0f;
techneck->renderer.active_camera->position.y = 0.0f;
techneck->renderer.active_camera->position.z = 0.0f;
techneck->renderer.gl_context = SDL_GL_CreateContext(techneck->renderer.window);
gladLoadGLLoader(&SDL_GL_GetProcAddress);
@ -61,14 +63,16 @@ void tc_load_textures()
void tc_init()
{
tc_init_renderer(&tc_game_state_g);
tc_game_state_g.asset_storage = tc_init_asset_storage("assets/");
tc_game_state_g.asset_storage = tc_init_asset_storage("assets/");
tc_load_textures();
tc_game_state_g.block_registry = tc_init_blocks();
tc_game_state_g.block_registry = tc_init_blocks();
tc_create_blocks();
tc_game_state_g.entity_registry = tc_init_entity_registry();
tc_game_state_g.entity_registry = tc_init_entity_registry();
tc_register_entities();
tc_game_state_g.player = tc_create_entity("player");
tc_game_state_g.viewer = tc_game_state_g.player;
tc_init_worlds();
tc_game_state_g.main_world = tc_new_world(&tc_default_terrain_generator_g);
tc_game_state_g.main_world = tc_new_world(&tc_default_terrain_generator_g);
puts("Finished initializing!");
}

View File

@ -64,8 +64,41 @@ float triangle_vertices[108] =
0.5f, -0.5f, -0.5f
};
tc_vec3_s tc_camera_position = { 0.0f, 0.0f, 0.0f };
// tc_vec3_s tc_camera_rotation = { 0.0f, 0.0f, 0.0f };
void tc_move_viewer_to(float x, float y, float z)
{
if(tc_game_state_g.viewer == NULL) return;
tc_set_float_for_entity(tc_game_state_g.viewer, "pos_x", x);
tc_set_float_for_entity(tc_game_state_g.viewer, "pos_y", y);
tc_set_float_for_entity(tc_game_state_g.viewer, "pos_z", z);
}
void tc_rotate_viewer_to(float x, float y, float z)
{
if(tc_game_state_g.viewer == NULL) return;
tc_set_float_for_entity(tc_game_state_g.viewer, "rot_x", x);
tc_set_float_for_entity(tc_game_state_g.viewer, "rot_y", y);
tc_set_float_for_entity(tc_game_state_g.viewer, "rot_z", z);
}
void tc_get_viewer_position(float *x, float *y, float *z)
{
if(tc_game_state_g.viewer == NULL) return;
if(x != NULL) (*x) = tc_get_float_from_entity(tc_game_state_g.viewer, "pos_x");
if(y != NULL) (*y) = tc_get_float_from_entity(tc_game_state_g.viewer, "pos_y");
if(z != NULL) (*z) = tc_get_float_from_entity(tc_game_state_g.viewer, "pos_z");
}
void tc_get_viewer_rotation(float *x, float *y, float *z)
{
if(tc_game_state_g.viewer == NULL) return;
if(x != NULL) (*x) = tc_get_float_from_entity(tc_game_state_g.viewer, "rot_x");
if(y != NULL) (*y) = tc_get_float_from_entity(tc_game_state_g.viewer, "rot_y");
if(z != NULL) (*z) = tc_get_float_from_entity(tc_game_state_g.viewer, "rot_z");
}
void render_block(tc_block_s block)
{
@ -115,14 +148,18 @@ void render()
glViewport(0, 0, fb_width, fb_height);
mat4x4_perspective(projection_matrix, 120, ((float) fb_width) / ((float) fb_height), 0.0001f, 1000.0f);
tc_vec3_s camera_rotation = tc_game_state_g.renderer.active_camera.rotation;
tc_vec3_s camera_rotation;
tc_get_viewer_rotation(&camera_rotation.x, &camera_rotation.y, &camera_rotation.z);
tc_vec3_s camera_position;
tc_get_viewer_position(&camera_position.x, &camera_position.y, &camera_position.z);
mat4x4_identity(view_matrix);
mat4x4_rotate_X(view_matrix, view_matrix, camera_rotation.x);
mat4x4_rotate_Y(view_matrix, view_matrix, camera_rotation.y);
mat4x4_rotate_Z(view_matrix, view_matrix, camera_rotation.z);
mat4x4_translate_in_place(view_matrix, -tc_camera_position.x, -tc_camera_position.y, tc_camera_position.z);
mat4x4_translate_in_place(view_matrix, -camera_position.x, -camera_position.y, camera_position.z);
glUseProgram(tc_game_state_g.renderer.draw_shader.program_id);
@ -149,7 +186,9 @@ bool rotate_right = false;
bool update()
{
tc_vec3_s camera_rotation;
tc_get_viewer_rotation(&camera_rotation.x, &camera_rotation.y, &camera_rotation.z);
SDL_Event event;
while(SDL_PollEvent(&event))
{
@ -258,27 +297,30 @@ bool update()
case SDL_MOUSEMOTION:
{
if(!(event.motion.state & SDL_BUTTON_LMASK)) break;
tc_game_state_g.renderer.active_camera.rotation.y += event.motion.xrel / 50.0f;
tc_game_state_g.renderer.active_camera.rotation.x += event.motion.yrel / 50.0f;
camera_rotation.y += event.motion.xrel / 50.0f;
camera_rotation.x += event.motion.yrel / 50.0f;
if(tc_game_state_g.renderer.active_camera.rotation.x > (3.1415f/2.0f))
tc_game_state_g.renderer.active_camera.rotation.x = (3.1415f/2.0f);
if(camera_rotation.x > (3.1415f/2.0f))
camera_rotation.x = (3.1415f/2.0f);
if(tc_game_state_g.renderer.active_camera.rotation.x < -(3.1415f/2.0f))
tc_game_state_g.renderer.active_camera.rotation.x = -(3.1415f/2.0f);
if(camera_rotation.x < -(3.1415f/2.0f))
camera_rotation.x = -(3.1415f/2.0f);
} break;
}
}
tc_vec3_s camera_position;
tc_get_viewer_position(&camera_position.x, &camera_position.y, &camera_position.z);
if(shift_pressed)
{
tc_camera_position.y -= 0.25;
camera_position.y -= 0.25;
}
if(go_up)
{
tc_camera_position.y += 0.25;
camera_position.y += 0.25;
}
@ -313,20 +355,23 @@ bool update()
mat4x4_translate(position_matrix, difference_in_perspective.x, 0.0f, difference_in_perspective.z);
mat4x4_rotate_X(position_matrix, position_matrix, tc_game_state_g.renderer.active_camera.rotation.x);
mat4x4_rotate_Y(position_matrix, position_matrix, tc_game_state_g.renderer.active_camera.rotation.y);
mat4x4_rotate_Z(position_matrix, position_matrix, tc_game_state_g.renderer.active_camera.rotation.z);
mat4x4_rotate_X(position_matrix, position_matrix, camera_rotation.x);
mat4x4_rotate_Y(position_matrix, position_matrix, camera_rotation.y);
mat4x4_rotate_Z(position_matrix, position_matrix, camera_rotation.z);
vec4 viewed_difference = { difference_in_perspective.x, 0.0f, difference_in_perspective.z };
vec4 raw_difference = { 0.0f, 0.0f, 0.0f };
mat4x4_mul_vec4(raw_difference, position_matrix, viewed_difference);
tc_camera_position.x += raw_difference[0];
tc_camera_position.z += raw_difference[2];
camera_position.x += raw_difference[0];
camera_position.z += raw_difference[2];
tc_game_state_g.main_world->spawn_loader->center.x = ((int32_t) tc_camera_position.x) / 32 - UPDATE_DISTANCE;
tc_game_state_g.main_world->spawn_loader->center.y = ((int32_t) tc_camera_position.y) / 32 - UPDATE_DISTANCE;
tc_game_state_g.main_world->spawn_loader->center.z = ((int32_t) tc_camera_position.z) / 32 - UPDATE_DISTANCE;
tc_move_viewer_to(camera_position.x, camera_position.y, camera_position.z);
tc_rotate_viewer_to(camera_rotation.x, camera_rotation.y, camera_rotation.z);
tc_game_state_g.main_world->spawn_loader->center.x = ((int32_t) camera_position.x) / 32 - UPDATE_DISTANCE;
tc_game_state_g.main_world->spawn_loader->center.y = ((int32_t) camera_position.y) / 32 - UPDATE_DISTANCE;
tc_game_state_g.main_world->spawn_loader->center.z = ((int32_t) camera_position.z) / 32 - UPDATE_DISTANCE;
tc_update_world(tc_game_state_g.main_world);

View File

@ -23,7 +23,7 @@ typedef struct tc_camera
typedef struct tc_renderer
{
tc_camera_s active_camera;
tc_camera_s *active_camera;
tc_shader_program_s draw_shader;
SDL_Window *window;
@ -38,6 +38,8 @@ typedef struct
tc_block_registry_s block_registry;
tc_entity_registry_s entity_registry;
tc_world_s *main_world;
tc_entity_s *viewer;
tc_entity_s *player;
tc_image_s *block_texture_atlas;