Adding hitbox component and test entity.
This commit is contained in:
parent
d4da072f48
commit
357839cc71
|
|
@ -2,6 +2,7 @@
|
|||
SOURCES := \
|
||||
game.c \
|
||||
components/transform_component.c \
|
||||
components/hitbox_component.c \
|
||||
components/sprite_component.c \
|
||||
components/animation_system.c \
|
||||
components/player_system.c
|
||||
|
|
|
|||
|
|
@ -4,8 +4,10 @@
|
|||
|
||||
#if DEBUG >= 2
|
||||
COMPONENT(TRANSFORM_COMPONENT, transform_component, DRAW)
|
||||
COMPONENT(HITBOX_COMPONENT, hitbox_component, DRAW)
|
||||
#else
|
||||
COMPONENT(TRANSFORM_COMPONENT, transform_component, NO_DRAW)
|
||||
COMPONENT(HITBOX_COMPONENT, hitbox_component, NO_DRAW)
|
||||
#endif // DEBUG >= 2
|
||||
|
||||
COMPONENT(SPRITE_COMPONENT, sprite_component, DRAW)
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
#define COMPONENTS_H
|
||||
|
||||
#include "transform_component.h"
|
||||
#include "hitbox_component.h"
|
||||
#include "sprite_component.h"
|
||||
#include "animation_system.h"
|
||||
#include "player_system.h"
|
||||
|
|
|
|||
|
|
@ -0,0 +1,25 @@
|
|||
#ifndef HITBOX_COMPONENT_H
|
||||
#define HITBOX_COMPONENT_H
|
||||
|
||||
#include "ecs.h"
|
||||
#include "transform_component.h"
|
||||
#include "vector2d.h"
|
||||
|
||||
typedef struct hitbox_component_data_t {
|
||||
transform_component_data_t *transform_component_data;
|
||||
fvector2d_t position;
|
||||
fvector2d_t scale;
|
||||
frect_t bounds;
|
||||
} hitbox_component_data_t;
|
||||
|
||||
int hitbox_component_init(component_t *component);
|
||||
|
||||
int hitbox_component_update(component_t *component);
|
||||
|
||||
#if DEBUG >= 2
|
||||
int hitbox_component_draw(component_t *component);
|
||||
#endif
|
||||
|
||||
int hitbox_component_destroy(component_t *component);
|
||||
|
||||
#endif // HITBOX_COMPONENT_H
|
||||
|
|
@ -0,0 +1,62 @@
|
|||
#include "hitbox_component.h"
|
||||
|
||||
#include "transform_component.h"
|
||||
#include "memory_alloc.h"
|
||||
#include "game.h"
|
||||
|
||||
// Args : frect_t bounds, double speed
|
||||
int hitbox_component_init(component_t *component)
|
||||
{
|
||||
hitbox_component_data_t *component_data = component->component_data;
|
||||
|
||||
component_data->transform_component_data = entity_get_component(component->entity, TRANSFORM_COMPONENT)->component_data;
|
||||
if(!component_data->transform_component_data) return EXIT_FAILURE;
|
||||
|
||||
component_data->position = (fvector2d_t) {
|
||||
.x = 0.0f,
|
||||
.y = 0.0f
|
||||
};
|
||||
|
||||
component_data->scale = (fvector2d_t) {
|
||||
.x = 1.0f,
|
||||
.y = 1.0f
|
||||
};
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
int hitbox_component_update(component_t *component)
|
||||
{
|
||||
hitbox_component_data_t *component_data = component->component_data;
|
||||
transform_component_data_t *transform_component_data = component_data->transform_component_data;
|
||||
|
||||
component_data->bounds.x = transform_component_data->bounds.x + (transform_component_data->bounds.w - component_data->bounds.w) * component_data->position.x;
|
||||
component_data->bounds.y = transform_component_data->bounds.y + (transform_component_data->bounds.h - component_data->bounds.h) * component_data->position.y;
|
||||
component_data->bounds.w = transform_component_data->bounds.w * component_data->scale.x;
|
||||
component_data->bounds.h = transform_component_data->bounds.h * component_data->scale.y;
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
#if DEBUG >= 2
|
||||
#include "display.h"
|
||||
#include "errors.h"
|
||||
|
||||
int hitbox_component_draw(component_t *component)
|
||||
{
|
||||
hitbox_component_data_t *component_data = component->component_data;
|
||||
|
||||
if(display_draw_frect(&component_data->bounds, C_RED, 128)) return EXIT_FAILURE;
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
#endif // DEBUG >= 2
|
||||
|
||||
inline int hitbox_component_destroy(component_t *component)
|
||||
{
|
||||
hitbox_component_data_t *component_data = component->component_data;
|
||||
|
||||
free(component_data);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
31
src/game.c
31
src/game.c
|
|
@ -22,6 +22,7 @@ int game_init(void)
|
|||
|
||||
entity_manager_init(&game.entity_manager);
|
||||
|
||||
/* ======== Player ======== */
|
||||
entity_t *player = entity_manager_new_entity(&game.entity_manager, 0);
|
||||
if(!player) return EXIT_FAILURE;
|
||||
|
||||
|
|
@ -35,10 +36,40 @@ int game_init(void)
|
|||
.h = 32.0f
|
||||
};
|
||||
|
||||
component = entity_new_component(player, HITBOX_COMPONENT);
|
||||
if(!component) return EXIT_FAILURE;
|
||||
|
||||
((hitbox_component_data_t *)component->component_data)->position = (fvector2d_t) {
|
||||
.x = 0.5f,
|
||||
.y = 1.0f
|
||||
};
|
||||
|
||||
((hitbox_component_data_t *)component->component_data)->scale = (fvector2d_t) {
|
||||
.x = 0.5f,
|
||||
.y = 0.5f
|
||||
};
|
||||
|
||||
if(!entity_new_component(player, SPRITE_COMPONENT)) return EXIT_FAILURE;
|
||||
if(!entity_new_component(player, ANIMATION_SYSTEM)) return EXIT_FAILURE;
|
||||
if(!entity_new_component(player, PLAYER_SYSTEM)) return EXIT_FAILURE;
|
||||
|
||||
/* ======== Test entity ======== */
|
||||
entity_t *entity = entity_manager_new_entity(&game.entity_manager, 1);
|
||||
if(!entity) return EXIT_FAILURE;
|
||||
|
||||
component = entity_new_component(entity, TRANSFORM_COMPONENT);
|
||||
if(!component) return EXIT_FAILURE;
|
||||
|
||||
((transform_component_data_t *)component->component_data)->bounds = (frect_t) {
|
||||
.x = 120.0f,
|
||||
.y = 30.0f,
|
||||
.w = 32.0f,
|
||||
.h = 32.0f
|
||||
};
|
||||
|
||||
if(!entity_new_component(entity, HITBOX_COMPONENT)) return EXIT_FAILURE;
|
||||
if(!entity_new_component(entity, SPRITE_COMPONENT)) return EXIT_FAILURE;
|
||||
|
||||
game.is_running = true;
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
|
|
|
|||
Loading…
Reference in New Issue