first commit
This commit is contained in:
		
							
								
								
									
										33
									
								
								src/game/ecs/component/visual/component_atlas.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								src/game/ecs/component/visual/component_atlas.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,33 @@
 | 
			
		||||
#include "component_atlas.h"
 | 
			
		||||
 | 
			
		||||
void 
 | 
			
		||||
component_atlas_uv_get(ComponentAtlas* self, Texture* texture, vec2 uvMin, vec2 uvMax)
 | 
			
		||||
{
 | 
			
		||||
	vec2 position;
 | 
			
		||||
 | 
			
		||||
	position[0] = (self->index % self->size[1]) * self->frameSize[0];
 | 
			
		||||
	position[1] = (s32)((f32)self->index / self->size[1]) * self->frameSize[1];
 | 
			
		||||
 | 
			
		||||
	uvMin[0] = (f32)position[0] / texture->size[0];
 | 
			
		||||
	uvMin[1] = (f32)position[1] / texture->size[1];
 | 
			
		||||
	uvMax[0] = (f32)(position[0] + self->frameSize[0]) / texture->size[0];
 | 
			
		||||
	uvMax[1] = (f32)(position[1] + self->frameSize[1]) / texture->size[1];
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
component_atlas_init(ComponentAtlas* self, ECS* ecs, const ivec2 frameSize, const ivec2 size, u32 index)
 | 
			
		||||
{
 | 
			
		||||
	glm_ivec2_copy((s32*)frameSize, self->frameSize);
 | 
			
		||||
	glm_ivec2_copy((s32*)size, self->size);
 | 
			
		||||
	self->index = index;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
component_atlas_tick(ComponentAtlas* self, ECS* ecs)
 | 
			
		||||
{
 | 
			
		||||
	ComponentTextureQuad* textureQuad;
 | 
			
		||||
 | 
			
		||||
	textureQuad = ecs_component_get(ecs, COMPONENT_TEXTURE_QUAD, self->component.id);
 | 
			
		||||
 | 
			
		||||
	component_atlas_uv_get(self, textureQuad->texture, textureQuad->uvMin, textureQuad->uvMax);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										32
									
								
								src/game/ecs/component/visual/component_atlas.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								src/game/ecs/component/visual/component_atlas.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,32 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include "component_texture_quad.h"
 | 
			
		||||
 | 
			
		||||
typedef struct ComponentAtlas
 | 
			
		||||
{
 | 
			
		||||
	Component component;
 | 
			
		||||
	ivec2 frameSize;
 | 
			
		||||
	ivec2 size;
 | 
			
		||||
	u32 index;
 | 
			
		||||
} ComponentAtlas;
 | 
			
		||||
 | 
			
		||||
void component_atlas_init(ComponentAtlas* self, ECS* ecs, const ivec2 frameSize, const ivec2 size, u32 index);
 | 
			
		||||
void component_atlas_tick(ComponentAtlas* self, ECS* ecs);
 | 
			
		||||
void component_atlas_uv_get(ComponentAtlas* self, Texture* texture, vec2 uvMin, vec2 uvMax);
 | 
			
		||||
 | 
			
		||||
static const ComponentInfo COMPONENT_ATLAS_INFO = 
 | 
			
		||||
{
 | 
			
		||||
	.system = 
 | 
			
		||||
	{
 | 
			
		||||
		.functions = 
 | 
			
		||||
		{ 
 | 
			
		||||
			NULL,
 | 
			
		||||
			NULL,
 | 
			
		||||
			(ECSFunction)component_atlas_tick,
 | 
			
		||||
			NULL,
 | 
			
		||||
			NULL,
 | 
			
		||||
		}
 | 
			
		||||
	},
 | 
			
		||||
	.type = COMPONENT_ATLAS,
 | 
			
		||||
	.size = sizeof(ComponentAtlas)
 | 
			
		||||
};
 | 
			
		||||
							
								
								
									
										66
									
								
								src/game/ecs/component/visual/component_camera_focus.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										66
									
								
								src/game/ecs/component/visual/component_camera_focus.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,66 @@
 | 
			
		||||
#include "component_camera_focus.h"
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
component_camera_focus_tick(ComponentCameraFocus* self, ECS* ecs)
 | 
			
		||||
{
 | 
			
		||||
	ComponentPhysics* physics;
 | 
			
		||||
	vec3 position;
 | 
			
		||||
	//f32 velocityPercent;
 | 
			
		||||
	//vec2 positionPercent;
 | 
			
		||||
 | 
			
		||||
	physics = ecs_component_get(ecs, COMPONENT_PHYSICS, self->entityID);
 | 
			
		||||
 | 
			
		||||
	if (!physics)
 | 
			
		||||
		return;
 | 
			
		||||
 | 
			
		||||
	glm_vec3_copy(physics->position, position);
 | 
			
		||||
	
 | 
			
		||||
	/*
 | 
			
		||||
	positionPercent[0] = (physics->position[0] + (physics->velocity[0] * COMPONENT_CAMERA_FOCUS_VELOCITY_MULTIPLIER)) / self->camera->orthographic.size[0];
 | 
			
		||||
	positionPercent[1] = (physics->position[1] + (physics->velocity[1] * COMPONENT_CAMERA_FOCUS_VELOCITY_MULTIPLIER)) / self->camera->orthographic.size[1];
 | 
			
		||||
	*/
 | 
			
		||||
 | 
			
		||||
	/*
 | 
			
		||||
	position[0] += physics->velocity[0] * COMPONENT_CAMERA_FOCUS_VELOCITY_MULTIPLIER;
 | 
			
		||||
	position[1] += physics->velocity[1] * COMPONENT_CAMERA_FOCUS_VELOCITY_MULTIPLIER;
 | 
			
		||||
	*/
 | 
			
		||||
 | 
			
		||||
	position[0] -= self->camera->orthographic.size[0] / 2;
 | 
			
		||||
	position[1] -= self->camera->orthographic.size[1] / 2;
 | 
			
		||||
 | 
			
		||||
	/*
 | 
			
		||||
	position[0] += (physics->position[0]) - ((self->camera->orthographic.size[0] / 4) * (1 - self->camera->zoom));
 | 
			
		||||
	position[1] += (physics->position[1]) - ((self->camera->orthographic.size[1] / 4) * (1 - self->camera->zoom));
 | 
			
		||||
	*/
 | 
			
		||||
 | 
			
		||||
	position[2] = 0.0f;
 | 
			
		||||
 | 
			
		||||
	//velocityPercent = 0.0f;
 | 
			
		||||
 | 
			
		||||
	/*
 | 
			
		||||
	if (fabs(physics->velocity[0]) >= fabs(physics->velocity[1]))
 | 
			
		||||
		velocityPercent = fabs(physics->velocity[0]) / physics->velocityMax[0];
 | 
			
		||||
	else if (fabs(physics->velocity[1]) > fabs(physics->velocity[0]))
 | 
			
		||||
		velocityPercent = fabs(physics->velocity[1]) / physics->velocityMax[1];
 | 
			
		||||
 | 
			
		||||
	self->camera->zoom = COMPONENT_CAMERA_FOCUS_ZOOM_MIN;
 | 
			
		||||
	self->camera->zoom += (velocityPercent * (COMPONENT_CAMERA_FOCUS_ZOOM_MAX - COMPONENT_CAMERA_FOCUS_ZOOM_MIN));
 | 
			
		||||
	*/
 | 
			
		||||
 | 
			
		||||
	//glm_vec3_print(position, stderr);
 | 
			
		||||
 | 
			
		||||
	camera_position_set(self->camera, position);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
component_camera_focus_init
 | 
			
		||||
(
 | 
			
		||||
 	ComponentCameraFocus* self,
 | 
			
		||||
	ECS* ecs,
 | 
			
		||||
	Camera* camera,
 | 
			
		||||
	u32 entityID
 | 
			
		||||
)
 | 
			
		||||
{
 | 
			
		||||
	self->camera = camera;
 | 
			
		||||
	self->entityID = entityID;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										43
									
								
								src/game/ecs/component/visual/component_camera_focus.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								src/game/ecs/component/visual/component_camera_focus.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,43 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include "../physics/component_physics.h"
 | 
			
		||||
 | 
			
		||||
static const f32 COMPONENT_CAMERA_FOCUS_VELOCITY_MULTIPLIER = 10.0f;
 | 
			
		||||
static const f32 COMPONENT_CAMERA_FOCUS_VELOCITY_ZOOM_MULTIPLIER = 0.1f;
 | 
			
		||||
static const f32 COMPONENT_CAMERA_FOCUS_ZOOM_MAX = 0.40f;
 | 
			
		||||
static const f32 COMPONENT_CAMERA_FOCUS_ZOOM_MIN = 0.25f;
 | 
			
		||||
 | 
			
		||||
typedef struct ComponentCameraFocus
 | 
			
		||||
{
 | 
			
		||||
	Component component;
 | 
			
		||||
	Camera* camera;
 | 
			
		||||
	EntityID entityID;
 | 
			
		||||
} ComponentCameraFocus;
 | 
			
		||||
 | 
			
		||||
void component_camera_focus_tick(ComponentCameraFocus* self, ECS* ecs);
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
component_camera_focus_init
 | 
			
		||||
(
 | 
			
		||||
	ComponentCameraFocus* self, 
 | 
			
		||||
	ECS* ecs,
 | 
			
		||||
	Camera* camera,
 | 
			
		||||
	EntityID entityID
 | 
			
		||||
);
 | 
			
		||||
 | 
			
		||||
static const ComponentInfo COMPONENT_CAMERA_FOCUS_INFO = 
 | 
			
		||||
{
 | 
			
		||||
	.system = 
 | 
			
		||||
	{
 | 
			
		||||
		.functions = 
 | 
			
		||||
		{ 
 | 
			
		||||
			NULL,
 | 
			
		||||
			NULL,
 | 
			
		||||
			(ECSFunction)component_camera_focus_tick,
 | 
			
		||||
			NULL,
 | 
			
		||||
			NULL
 | 
			
		||||
		}
 | 
			
		||||
	},
 | 
			
		||||
	.type = COMPONENT_CAMERA_FOCUS,
 | 
			
		||||
	.size = sizeof(ComponentCameraFocus)
 | 
			
		||||
};
 | 
			
		||||
							
								
								
									
										109
									
								
								src/game/ecs/component/visual/component_color_change.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										109
									
								
								src/game/ecs/component/visual/component_color_change.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,109 @@
 | 
			
		||||
#include "component_color_change.h"
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
component_color_change_add(ComponentColorChange* self, ECS* ecs)
 | 
			
		||||
{
 | 
			
		||||
	self->timerID = entity_timer_add(ecs, -1);
 | 
			
		||||
 | 
			
		||||
	self->component.isDisabled = true;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
component_color_change_delete(ComponentColorChange* self, ECS* ecs)
 | 
			
		||||
{
 | 
			
		||||
	ecs_entity_delete(ecs, self->timerID);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
component_color_change_tick(ComponentColorChange* self, ECS* ecs)
 | 
			
		||||
{
 | 
			
		||||
	ComponentTextureQuad* textureQuad;
 | 
			
		||||
	ComponentText* text;
 | 
			
		||||
	ComponentTimer* timer;
 | 
			
		||||
	vec4 colorStep;
 | 
			
		||||
	vec4* color;
 | 
			
		||||
	vec4* colorCover;
 | 
			
		||||
 | 
			
		||||
	if (self->isFinished)
 | 
			
		||||
		return;
 | 
			
		||||
 | 
			
		||||
	timer = ecs_component_get(ecs, COMPONENT_TIMER, self->timerID);
 | 
			
		||||
 | 
			
		||||
	ecs->isLog = false;
 | 
			
		||||
 | 
			
		||||
	textureQuad = ecs_component_get(ecs, COMPONENT_TEXTURE_QUAD, self->component.id);
 | 
			
		||||
	text = ecs_component_get(ecs, COMPONENT_TEXT, self->component.id);
 | 
			
		||||
 | 
			
		||||
	ecs->isLog = true;
 | 
			
		||||
 | 
			
		||||
	if (textureQuad)
 | 
			
		||||
	{
 | 
			
		||||
		color = &textureQuad->color;
 | 
			
		||||
		colorCover = &textureQuad->colorCover;
 | 
			
		||||
	}
 | 
			
		||||
	else if (text)
 | 
			
		||||
	{
 | 
			
		||||
		color = &text->color;
 | 
			
		||||
		colorCover = &text->colorCover;
 | 
			
		||||
	}
 | 
			
		||||
	else
 | 
			
		||||
		return;
 | 
			
		||||
 | 
			
		||||
	if (self->isColorCover)
 | 
			
		||||
		glm_vec4_scale(self->colorStep, timer->value, *colorCover); 
 | 
			
		||||
	else
 | 
			
		||||
		glm_vec4_scale(self->colorStep, timer->value, *color); 
 | 
			
		||||
 | 
			
		||||
	if (timer->isFinished)
 | 
			
		||||
	{
 | 
			
		||||
		self->isFinished = true;
 | 
			
		||||
 | 
			
		||||
		if (self->isReturn)
 | 
			
		||||
		{
 | 
			
		||||
			if (self->isColorCover)
 | 
			
		||||
				glm_vec4_copy(self->returnColor, *colorCover);
 | 
			
		||||
			else
 | 
			
		||||
				glm_vec4_copy(self->returnColor, *color);
 | 
			
		||||
		}
 | 
			
		||||
		return;
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
component_color_change_init(ComponentColorChange* self, ECS* ecs, const vec4 endColor, const vec4 beginColor, u32 length, bool isColorCover, bool isReturn)
 | 
			
		||||
{
 | 
			
		||||
	ComponentTimer* timer;
 | 
			
		||||
	ComponentTextureQuad* textureQuad;
 | 
			
		||||
	ComponentText* text;
 | 
			
		||||
	vec4 colorDifference;
 | 
			
		||||
 | 
			
		||||
	ecs->isLog = false;
 | 
			
		||||
 | 
			
		||||
	textureQuad = ecs_component_get(ecs, COMPONENT_TEXTURE_QUAD, self->component.id);
 | 
			
		||||
	text = ecs_component_get(ecs, COMPONENT_TEXT, self->component.id);
 | 
			
		||||
 | 
			
		||||
	ecs->isLog = true;
 | 
			
		||||
 | 
			
		||||
	glm_vec4_copy((f32*)beginColor, self->beginColor);
 | 
			
		||||
	glm_vec4_copy((f32*)endColor, self->endColor);
 | 
			
		||||
 | 
			
		||||
	if (textureQuad)
 | 
			
		||||
		glm_vec4_copy((f32*)textureQuad->color, self->returnColor);
 | 
			
		||||
	else if (text)
 | 
			
		||||
		glm_vec4_copy((f32*)text->color, self->returnColor);
 | 
			
		||||
 | 
			
		||||
	timer = ecs_component_get(ecs, COMPONENT_TIMER, self->timerID);
 | 
			
		||||
 | 
			
		||||
	component_timer_init(timer, ecs, length);
 | 
			
		||||
 | 
			
		||||
	timer->component.isDisabled = false;
 | 
			
		||||
 | 
			
		||||
	glm_vec4_sub(self->endColor, self->beginColor, colorDifference);
 | 
			
		||||
	glm_vec4_scale(colorDifference, (1.0f / length), self->colorStep);
 | 
			
		||||
 | 
			
		||||
	self->isColorCover = isColorCover;
 | 
			
		||||
	self->isReturn = isReturn;
 | 
			
		||||
	self->isFinished = false;
 | 
			
		||||
 | 
			
		||||
	self->component.isDisabled = false;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										51
									
								
								src/game/ecs/component/visual/component_color_change.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										51
									
								
								src/game/ecs/component/visual/component_color_change.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,51 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include "component_texture_quad.h"
 | 
			
		||||
#include "component_text.h"
 | 
			
		||||
#include "../../entity/utility/entity_timer.h"
 | 
			
		||||
 | 
			
		||||
typedef struct ComponentColorChange
 | 
			
		||||
{
 | 
			
		||||
	Component component;
 | 
			
		||||
	vec4 beginColor;
 | 
			
		||||
	vec4 endColor;
 | 
			
		||||
	vec4 returnColor;
 | 
			
		||||
	vec4 colorStep;
 | 
			
		||||
	bool isColorCover;
 | 
			
		||||
	bool isReturn;
 | 
			
		||||
	bool isFinished;
 | 
			
		||||
	EntityID timerID;
 | 
			
		||||
} ComponentColorChange;
 | 
			
		||||
 | 
			
		||||
void component_color_change_add(ComponentColorChange* self, ECS* ecs);
 | 
			
		||||
void component_color_change_delete(ComponentColorChange* self, ECS* ecs);
 | 
			
		||||
void component_color_change_tick(ComponentColorChange* self, ECS* ecs);
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
component_color_change_init
 | 
			
		||||
(
 | 
			
		||||
	ComponentColorChange* self, 
 | 
			
		||||
	ECS* ecs,
 | 
			
		||||
	const vec4 endColor,
 | 
			
		||||
	const vec4 beginColor,
 | 
			
		||||
	u32 length,
 | 
			
		||||
	bool isColorCover,
 | 
			
		||||
	bool isReturn
 | 
			
		||||
);
 | 
			
		||||
 | 
			
		||||
static const ComponentInfo COMPONENT_COLOR_CHANGE_INFO = 
 | 
			
		||||
{
 | 
			
		||||
	.system = 
 | 
			
		||||
	{
 | 
			
		||||
		.functions = 
 | 
			
		||||
		{ 
 | 
			
		||||
			(ECSFunction)component_color_change_add,
 | 
			
		||||
			(ECSFunction)component_color_change_delete,
 | 
			
		||||
			(ECSFunction)component_color_change_tick,
 | 
			
		||||
			NULL,
 | 
			
		||||
			NULL
 | 
			
		||||
		}
 | 
			
		||||
	},
 | 
			
		||||
	.type = COMPONENT_COLOR_CHANGE,
 | 
			
		||||
	.size = sizeof(ComponentColorChange)
 | 
			
		||||
};
 | 
			
		||||
							
								
								
									
										34
									
								
								src/game/ecs/component/visual/component_color_match.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								src/game/ecs/component/visual/component_color_match.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,34 @@
 | 
			
		||||
#include "component_color_match.h"
 | 
			
		||||
 | 
			
		||||
void _component_color_match_set(ComponentColorMatch* self, ECS* ecs);
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
_component_color_match_set(ComponentColorMatch* self, ECS* ecs)
 | 
			
		||||
{
 | 
			
		||||
	ComponentTextureQuad* matchTextureQuad;
 | 
			
		||||
	ComponentTextureQuad* textureQuad;
 | 
			
		||||
 | 
			
		||||
	matchTextureQuad = ecs_component_get(ecs, COMPONENT_TEXTURE_QUAD, self->matchID);
 | 
			
		||||
 | 
			
		||||
	if (matchTextureQuad == NULL)
 | 
			
		||||
		return;
 | 
			
		||||
 | 
			
		||||
	textureQuad = ecs_component_get(ecs, COMPONENT_TEXTURE_QUAD, self->component.id);
 | 
			
		||||
 | 
			
		||||
	glm_vec4_copy(matchTextureQuad->color, textureQuad->color);
 | 
			
		||||
	glm_vec4_copy(matchTextureQuad->colorCover, textureQuad->colorCover);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
component_color_match_tick(ComponentColorMatch* self, ECS* ecs)
 | 
			
		||||
{
 | 
			
		||||
	_component_color_match_set(self, ecs);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
component_color_match_init(ComponentColorMatch* self, ECS* ecs, EntityID matchID)
 | 
			
		||||
{
 | 
			
		||||
	self->matchID = matchID;
 | 
			
		||||
 | 
			
		||||
	_component_color_match_set(self, ecs);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										38
									
								
								src/game/ecs/component/visual/component_color_match.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										38
									
								
								src/game/ecs/component/visual/component_color_match.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,38 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include "component_texture_quad.h"
 | 
			
		||||
 | 
			
		||||
typedef struct ComponentColorMatch
 | 
			
		||||
{
 | 
			
		||||
	Component component;
 | 
			
		||||
	EntityID matchID;
 | 
			
		||||
} ComponentColorMatch;
 | 
			
		||||
 | 
			
		||||
void component_color_match_add(ComponentColorMatch* self, ECS* ecs);
 | 
			
		||||
void component_color_match_delete(ComponentColorMatch* self, ECS* ecs);
 | 
			
		||||
void component_color_match_tick(ComponentColorMatch* self, ECS* ecs);
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
component_color_match_init
 | 
			
		||||
(
 | 
			
		||||
	ComponentColorMatch* self, 
 | 
			
		||||
	ECS* ecs,
 | 
			
		||||
	EntityID matchID
 | 
			
		||||
);
 | 
			
		||||
 | 
			
		||||
static const ComponentInfo COMPONENT_COLOR_MATCH_INFO = 
 | 
			
		||||
{
 | 
			
		||||
	.system = 
 | 
			
		||||
	{
 | 
			
		||||
		.functions = 
 | 
			
		||||
		{ 
 | 
			
		||||
			NULL,
 | 
			
		||||
			NULL,
 | 
			
		||||
			(ECSFunction)component_color_match_tick,
 | 
			
		||||
			NULL,
 | 
			
		||||
			NULL
 | 
			
		||||
		}
 | 
			
		||||
	},
 | 
			
		||||
	.type = COMPONENT_COLOR_MATCH,
 | 
			
		||||
	.size = sizeof(ComponentColorMatch)
 | 
			
		||||
};
 | 
			
		||||
							
								
								
									
										108
									
								
								src/game/ecs/component/visual/component_dialogue.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										108
									
								
								src/game/ecs/component/visual/component_dialogue.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,108 @@
 | 
			
		||||
#include "component_dialogue.h"
 | 
			
		||||
 | 
			
		||||
static void _component_dialogue_string_set(ComponentDialogue* self, ECS* ecs);
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
_component_dialogue_string_set(ComponentDialogue* self, ECS* ecs)
 | 
			
		||||
{
 | 
			
		||||
	ComponentText* text;
 | 
			
		||||
	char string[COMPONENT_TEXT_STRING_MAX];
 | 
			
		||||
 | 
			
		||||
	memset(string, '\0', COMPONENT_TEXT_STRING_MAX);
 | 
			
		||||
	memcpy(string, self->string, self->index + 1);
 | 
			
		||||
 | 
			
		||||
	text = ecs_component_get(ecs, COMPONENT_TEXT, self->component.id);
 | 
			
		||||
 | 
			
		||||
	component_text_string_set(text, ecs, string, self->index + 1);
 | 
			
		||||
 | 
			
		||||
	sound_play(&ecs->resources->sounds[SOUND_BLIP]);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
component_dialogue_add(ComponentDialogue* self, ECS* ecs)
 | 
			
		||||
{
 | 
			
		||||
	for (s32 i = 0 ; i < COMPONENT_DIALOGUE_DEPENDENCY_COUNT; i++)
 | 
			
		||||
		ecs_component_add(ecs, COMPONENT_DIALOGUE_DEPENDENCIES[i], self->component.id);
 | 
			
		||||
 | 
			
		||||
	self->timerID = entity_timer_add(ecs, COMPONENT_DIALOGUE_SPEED_DEFAULT);
 | 
			
		||||
	self->skipTimerID = entity_timer_add(ecs, COMPONENT_DIALOGUE_SKIP_TIMER_VALUE);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
component_dialogue_delete(ComponentDialogue* self, ECS* ecs)
 | 
			
		||||
{
 | 
			
		||||
	for (s32 i = 0 ; i < COMPONENT_DIALOGUE_DEPENDENCY_COUNT; i++)
 | 
			
		||||
		ecs_component_delete(ecs, COMPONENT_DIALOGUE_DEPENDENCIES[i], self->component.id);
 | 
			
		||||
 | 
			
		||||
	ecs_entity_delete(ecs, self->timerID);
 | 
			
		||||
	ecs_entity_delete(ecs, self->skipTimerID);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
component_dialogue_tick(ComponentDialogue* self, ECS* ecs)
 | 
			
		||||
{
 | 
			
		||||
	ComponentTimer* timer;
 | 
			
		||||
 | 
			
		||||
	if (self->isFinished)
 | 
			
		||||
		return;
 | 
			
		||||
 | 
			
		||||
	timer = ecs_component_get(ecs, COMPONENT_TIMER, self->timerID);
 | 
			
		||||
 | 
			
		||||
	if (timer->isFinished)
 | 
			
		||||
	{
 | 
			
		||||
		self->index++;
 | 
			
		||||
 | 
			
		||||
		_component_dialogue_string_set(self, ecs);
 | 
			
		||||
 | 
			
		||||
		if (self->index >= self->length)
 | 
			
		||||
			self->isFinished = true;
 | 
			
		||||
		else
 | 
			
		||||
			component_timer_init(timer, ecs, self->speed);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if (control_released(ecs->control, CONTROL_ACTION_PRIMARY))
 | 
			
		||||
	{
 | 
			
		||||
		ComponentTimer* skipTimer;
 | 
			
		||||
	
 | 
			
		||||
		skipTimer = ecs_component_get(ecs, COMPONENT_TIMER, self->skipTimerID);
 | 
			
		||||
	
 | 
			
		||||
		if (!skipTimer->isFinished)
 | 
			
		||||
			return;
 | 
			
		||||
 | 
			
		||||
		self->index = self->length - 1;
 | 
			
		||||
		timer->value = 0;
 | 
			
		||||
		timer->isFinished = true;
 | 
			
		||||
 | 
			
		||||
		component_timer_init(timer, ecs, self->speed);
 | 
			
		||||
		component_timer_init(skipTimer, ecs, COMPONENT_DIALOGUE_SKIP_TIMER_VALUE);
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
component_dialogue_init
 | 
			
		||||
(
 | 
			
		||||
	ComponentDialogue* self, 
 | 
			
		||||
	ECS* ecs,
 | 
			
		||||
	const char* string,
 | 
			
		||||
	u32 speed
 | 
			
		||||
)
 | 
			
		||||
{
 | 
			
		||||
	ComponentTimer* timer;
 | 
			
		||||
	ComponentTimer* skipTimer;
 | 
			
		||||
 | 
			
		||||
	self->speed = speed;
 | 
			
		||||
 | 
			
		||||
	memcpy(self->string, string, COMPONENT_TEXT_STRING_MAX);
 | 
			
		||||
 | 
			
		||||
	timer = ecs_component_get(ecs, COMPONENT_TIMER, self->timerID);
 | 
			
		||||
	skipTimer = ecs_component_get(ecs, COMPONENT_TIMER, self->skipTimerID);
 | 
			
		||||
 | 
			
		||||
	component_timer_init(timer, ecs, self->speed);
 | 
			
		||||
	component_timer_init(skipTimer, ecs, COMPONENT_DIALOGUE_SKIP_TIMER_VALUE);
 | 
			
		||||
 | 
			
		||||
	self->length = strlen(self->string);
 | 
			
		||||
	self->index = 0;
 | 
			
		||||
	self->isFinished = false;
 | 
			
		||||
 | 
			
		||||
	_component_dialogue_string_set(self, ecs);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										57
									
								
								src/game/ecs/component/visual/component_dialogue.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										57
									
								
								src/game/ecs/component/visual/component_dialogue.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,57 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include "component_text.h"
 | 
			
		||||
 | 
			
		||||
#include "../../entity/utility/entity_timer.h"
 | 
			
		||||
 | 
			
		||||
static const u32 COMPONENT_DIALOGUE_SPEED_DEFAULT = 3;
 | 
			
		||||
static const u32 COMPONENT_DIALOGUE_SKIP_TIMER_VALUE = 1;
 | 
			
		||||
 | 
			
		||||
typedef struct ComponentDialogue
 | 
			
		||||
{
 | 
			
		||||
	Component component;
 | 
			
		||||
	char string[COMPONENT_TEXT_STRING_MAX];
 | 
			
		||||
	u32 speed;
 | 
			
		||||
	u32 index;
 | 
			
		||||
	u32 length;
 | 
			
		||||
	bool isFinished;
 | 
			
		||||
	EntityID timerID;
 | 
			
		||||
	EntityID skipTimerID;
 | 
			
		||||
} ComponentDialogue;
 | 
			
		||||
 | 
			
		||||
void component_dialogue_add(ComponentDialogue* self, ECS* ecs);
 | 
			
		||||
void component_dialogue_delete(ComponentDialogue* self, ECS* ecs);
 | 
			
		||||
void component_dialogue_tick(ComponentDialogue* self, ECS* ecs);
 | 
			
		||||
void component_dialogue_string_set(ComponentDialogue* self, ECS* ecs);
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
component_dialogue_init
 | 
			
		||||
(
 | 
			
		||||
	ComponentDialogue* self, 
 | 
			
		||||
	ECS* ecs,
 | 
			
		||||
	const char* string,
 | 
			
		||||
	u32 speed
 | 
			
		||||
);
 | 
			
		||||
 | 
			
		||||
#define COMPONENT_DIALOGUE_DEPENDENCY_COUNT 1
 | 
			
		||||
static const ComponentType COMPONENT_DIALOGUE_DEPENDENCIES[COMPONENT_DIALOGUE_DEPENDENCY_COUNT] = 
 | 
			
		||||
{
 | 
			
		||||
	COMPONENT_TEXT
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static const ComponentInfo COMPONENT_DIALOGUE_INFO = 
 | 
			
		||||
{
 | 
			
		||||
	.system = 
 | 
			
		||||
	{
 | 
			
		||||
		.functions = 
 | 
			
		||||
		{ 
 | 
			
		||||
			(ECSFunction)component_dialogue_add, 
 | 
			
		||||
			(ECSFunction)component_dialogue_delete, 
 | 
			
		||||
			(ECSFunction)component_dialogue_tick,
 | 
			
		||||
			NULL,
 | 
			
		||||
			NULL
 | 
			
		||||
		}
 | 
			
		||||
	},
 | 
			
		||||
	.type = COMPONENT_DIALOGUE,
 | 
			
		||||
	.size = sizeof(ComponentDialogue)
 | 
			
		||||
};
 | 
			
		||||
							
								
								
									
										67
									
								
								src/game/ecs/component/visual/component_drop_shadow.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										67
									
								
								src/game/ecs/component/visual/component_drop_shadow.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,67 @@
 | 
			
		||||
#include "component_drop_shadow.h"
 | 
			
		||||
 | 
			
		||||
static void _component_drop_shadow_texture_quad_set(ComponentDropShadow* self, ECS* ecs);
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
_component_drop_shadow_texture_quad_set(ComponentDropShadow* self, ECS* ecs)
 | 
			
		||||
{
 | 
			
		||||
	ComponentTextureQuad* textureQuadDropShadow;
 | 
			
		||||
	ComponentTextureQuad* textureQuad;
 | 
			
		||||
 | 
			
		||||
	textureQuad = ecs_component_get(ecs, COMPONENT_TEXTURE_QUAD, self->component.id);
 | 
			
		||||
 | 
			
		||||
	if (!self->isSpriteInit)
 | 
			
		||||
	{
 | 
			
		||||
		self->spriteID = entity_sprite_add
 | 
			
		||||
		(
 | 
			
		||||
			ecs,
 | 
			
		||||
			textureQuad->texture,
 | 
			
		||||
			textureQuad->buffer,
 | 
			
		||||
			textureQuad->origin,
 | 
			
		||||
			textureQuad->size,
 | 
			
		||||
			textureQuad->position
 | 
			
		||||
		);
 | 
			
		||||
 | 
			
		||||
		self->isSpriteInit = true;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	textureQuadDropShadow = ecs_component_get(ecs, COMPONENT_TEXTURE_QUAD, self->spriteID);
 | 
			
		||||
	
 | 
			
		||||
	memcpy(textureQuadDropShadow, textureQuad, sizeof(ComponentTextureQuad));
 | 
			
		||||
 | 
			
		||||
	textureQuadDropShadow->component.id = self->spriteID;
 | 
			
		||||
	glm_vec4_copy(self->color, textureQuadDropShadow->color);
 | 
			
		||||
 | 
			
		||||
	textureQuadDropShadow->position[0] += self->offset[0];
 | 
			
		||||
	textureQuadDropShadow->position[1] += self->offset[1];
 | 
			
		||||
	textureQuadDropShadow->position[2] += COMPONENT_DROP_SHADOW_Z_OFFSET;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
component_drop_shadow_add(ComponentDropShadow* self, ECS* ecs)
 | 
			
		||||
{
 | 
			
		||||
	self->spriteID = ecs_entity_add(ecs);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
component_drop_shadow_delete(ComponentDropShadow* self, ECS* ecs)
 | 
			
		||||
{
 | 
			
		||||
	ecs_entity_delete(ecs, self->spriteID);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
component_drop_shadow_tick(ComponentDropShadow* self, ECS* ecs)
 | 
			
		||||
{
 | 
			
		||||
	_component_drop_shadow_texture_quad_set(self, ecs);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
component_drop_shadow_init(ComponentDropShadow* self, ECS* ecs, vec4 color, vec2 offset)
 | 
			
		||||
{
 | 
			
		||||
	glm_vec4_copy(color, self->color);
 | 
			
		||||
	glm_vec2_copy(offset, self->offset);
 | 
			
		||||
	
 | 
			
		||||
	_component_drop_shadow_texture_quad_set(self, ecs);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										35
									
								
								src/game/ecs/component/visual/component_drop_shadow.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								src/game/ecs/component/visual/component_drop_shadow.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,35 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include "../../entity/visual/entity_sprite.h"
 | 
			
		||||
 | 
			
		||||
static const f32 COMPONENT_DROP_SHADOW_Z_OFFSET = 0.001f;
 | 
			
		||||
 | 
			
		||||
typedef struct ComponentDropShadow
 | 
			
		||||
{
 | 
			
		||||
	Component component;
 | 
			
		||||
	vec4 color;
 | 
			
		||||
	vec2 offset;
 | 
			
		||||
	bool isSpriteInit;
 | 
			
		||||
	EntityID spriteID;
 | 
			
		||||
} ComponentDropShadow;
 | 
			
		||||
 | 
			
		||||
void component_drop_shadow_init(ComponentDropShadow* self, ECS* ecs, vec4 color, vec2 offset);
 | 
			
		||||
void component_drop_shadow_tick(ComponentDropShadow* self, ECS* ecs);
 | 
			
		||||
void component_drop_shadow_add(ComponentDropShadow* self, ECS* ecs);
 | 
			
		||||
void component_drop_shadow_delete(ComponentDropShadow* self, ECS* ecs);
 | 
			
		||||
 | 
			
		||||
static const ComponentInfo COMPONENT_DROP_SHADOW_INFO = 
 | 
			
		||||
{
 | 
			
		||||
	.system = 
 | 
			
		||||
	{
 | 
			
		||||
		.functions = 
 | 
			
		||||
		{ 
 | 
			
		||||
			(ECSFunction)component_drop_shadow_add, 
 | 
			
		||||
			(ECSFunction)component_drop_shadow_delete, 
 | 
			
		||||
			(ECSFunction)component_drop_shadow_tick, 
 | 
			
		||||
			NULL
 | 
			
		||||
		}
 | 
			
		||||
	},
 | 
			
		||||
	.type = COMPONENT_DROP_SHADOW,
 | 
			
		||||
	.size = sizeof(ComponentDropShadow)
 | 
			
		||||
};
 | 
			
		||||
							
								
								
									
										42
									
								
								src/game/ecs/component/visual/component_pulsate.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								src/game/ecs/component/visual/component_pulsate.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,42 @@
 | 
			
		||||
#include "component_pulsate.h"
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
component_pulsate_add(ComponentPulsate* self, ECS* ecs)
 | 
			
		||||
{
 | 
			
		||||
	self->counterID = entity_counter_add(ecs);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
component_pulsate_delete(ComponentPulsate* self, ECS* ecs)
 | 
			
		||||
{
 | 
			
		||||
	ecs_entity_delete(ecs, self->counterID);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
component_pulsate_tick(ComponentPulsate* self, ECS* ecs)
 | 
			
		||||
{
 | 
			
		||||
	ComponentCounter* counter;
 | 
			
		||||
	ComponentTextureQuad* textureQuad;
 | 
			
		||||
 | 
			
		||||
	counter = ecs_component_get(ecs, COMPONENT_COUNTER, self->counterID);
 | 
			
		||||
	textureQuad = ecs_component_get(ecs, COMPONENT_TEXTURE_QUAD, self->component.id);
 | 
			
		||||
 | 
			
		||||
	textureQuad->scale[0] = 1 + ((self->maxScale[0] - self->minScale[0]) / 2.0f) * cos((f32)counter->value / self->frequency);
 | 
			
		||||
	textureQuad->scale[1] = 1 + ((self->maxScale[1] - self->minScale[1]) / 2.0f) * cos((f32)counter->value / self->frequency);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
component_pulsate_init
 | 
			
		||||
(
 | 
			
		||||
 	ComponentPulsate* self,
 | 
			
		||||
	ECS* ecs,
 | 
			
		||||
	vec2 minScale,
 | 
			
		||||
	vec2 maxScale,
 | 
			
		||||
	f32 frequency
 | 
			
		||||
)
 | 
			
		||||
{
 | 
			
		||||
	self->frequency = frequency;
 | 
			
		||||
 | 
			
		||||
	glm_vec2_copy(minScale, self->minScale);
 | 
			
		||||
	glm_vec2_copy(maxScale, self->maxScale);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										47
									
								
								src/game/ecs/component/visual/component_pulsate.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										47
									
								
								src/game/ecs/component/visual/component_pulsate.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,47 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include "../../ecs_entity.h"
 | 
			
		||||
 | 
			
		||||
#include "component_texture_quad.h"
 | 
			
		||||
 | 
			
		||||
#include "../../entity/utility/entity_counter.h"
 | 
			
		||||
 | 
			
		||||
typedef struct ComponentPulsate
 | 
			
		||||
{
 | 
			
		||||
	Component component;
 | 
			
		||||
	vec2 minScale;
 | 
			
		||||
	vec2 maxScale;
 | 
			
		||||
	f32 frequency;
 | 
			
		||||
	EntityID counterID;
 | 
			
		||||
} ComponentPulsate;
 | 
			
		||||
 | 
			
		||||
void component_pulsate_add(ComponentPulsate* self, ECS* ecs);
 | 
			
		||||
void component_pulsate_delete(ComponentPulsate* self, ECS* ecs);
 | 
			
		||||
void component_pulsate_tick(ComponentPulsate* self, ECS* ecs);
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
component_pulsate_init
 | 
			
		||||
(
 | 
			
		||||
	ComponentPulsate* self, 
 | 
			
		||||
	ECS* ecs,
 | 
			
		||||
	vec2 minScale,
 | 
			
		||||
	vec2 maxScale,
 | 
			
		||||
	f32 frequency
 | 
			
		||||
);
 | 
			
		||||
 | 
			
		||||
static const ComponentInfo COMPONENT_PULSATE_INFO = 
 | 
			
		||||
{
 | 
			
		||||
	.system = 
 | 
			
		||||
	{
 | 
			
		||||
		.functions = 
 | 
			
		||||
		{ 
 | 
			
		||||
			(ECSFunction)component_pulsate_add, 
 | 
			
		||||
			(ECSFunction)component_pulsate_delete, 
 | 
			
		||||
			(ECSFunction)component_pulsate_tick,
 | 
			
		||||
			NULL,
 | 
			
		||||
			NULL
 | 
			
		||||
		}
 | 
			
		||||
	},
 | 
			
		||||
	.type = COMPONENT_PULSATE,
 | 
			
		||||
	.size = sizeof(ComponentPulsate)
 | 
			
		||||
};
 | 
			
		||||
							
								
								
									
										17
									
								
								src/game/ecs/component/visual/component_rotate.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								src/game/ecs/component/visual/component_rotate.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,17 @@
 | 
			
		||||
#include "component_rotate.h"
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
component_rotate_tick(ComponentRotate* self, ECS* ecs)
 | 
			
		||||
{
 | 
			
		||||
	ComponentTextureQuad* textureQuad;
 | 
			
		||||
 | 
			
		||||
	textureQuad = ecs_component_get(ecs, COMPONENT_TEXTURE_QUAD, self->component.id);
 | 
			
		||||
 | 
			
		||||
	textureQuad->rotation += self->speed;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
component_rotate_init(ComponentRotate* self, ECS* ecs, f32 speed)
 | 
			
		||||
{
 | 
			
		||||
	self->speed = speed;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										38
									
								
								src/game/ecs/component/visual/component_rotate.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										38
									
								
								src/game/ecs/component/visual/component_rotate.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,38 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include "component_texture_quad.h"
 | 
			
		||||
 | 
			
		||||
typedef struct ComponentRotate
 | 
			
		||||
{
 | 
			
		||||
	Component component;
 | 
			
		||||
	f32 speed;
 | 
			
		||||
} ComponentRotate;
 | 
			
		||||
 | 
			
		||||
void component_rotate_add(ComponentRotate* self, ECS* ecs);
 | 
			
		||||
void component_rotate_delete(ComponentRotate* self, ECS* ecs);
 | 
			
		||||
void component_rotate_tick(ComponentRotate* self, ECS* ecs);
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
component_rotate_init
 | 
			
		||||
(
 | 
			
		||||
	ComponentRotate* self, 
 | 
			
		||||
	ECS* ecs,
 | 
			
		||||
	f32 speed
 | 
			
		||||
);
 | 
			
		||||
 | 
			
		||||
static const ComponentInfo COMPONENT_ROTATE_INFO = 
 | 
			
		||||
{
 | 
			
		||||
	.system = 
 | 
			
		||||
	{
 | 
			
		||||
		.functions = 
 | 
			
		||||
		{ 
 | 
			
		||||
			NULL,
 | 
			
		||||
			NULL,
 | 
			
		||||
			(ECSFunction)component_rotate_tick,
 | 
			
		||||
			NULL,
 | 
			
		||||
			NULL
 | 
			
		||||
		}
 | 
			
		||||
	},
 | 
			
		||||
	.type = COMPONENT_ROTATE,
 | 
			
		||||
	.size = sizeof(ComponentRotate)
 | 
			
		||||
};
 | 
			
		||||
							
								
								
									
										45
									
								
								src/game/ecs/component/visual/component_scale_value.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										45
									
								
								src/game/ecs/component/visual/component_scale_value.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,45 @@
 | 
			
		||||
#include "component_scale_value.h"
 | 
			
		||||
 | 
			
		||||
void _component_scale_value_set(ComponentScaleValue* self, ECS* ecs);
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
_component_scale_value_set(ComponentScaleValue* self, ECS* ecs)
 | 
			
		||||
{
 | 
			
		||||
	ComponentTextureQuad* textureQuad;
 | 
			
		||||
	void* component;
 | 
			
		||||
	f32 value;
 | 
			
		||||
	f32 max;
 | 
			
		||||
	f32 percent;
 | 
			
		||||
	vec2 sizeDifference;
 | 
			
		||||
	vec2 scaledSize;
 | 
			
		||||
 | 
			
		||||
	textureQuad = ecs_component_get(ecs, COMPONENT_TEXTURE_QUAD, self->component.id);
 | 
			
		||||
	component = ecs_component_get(ecs, self->type, self->id);
 | 
			
		||||
 | 
			
		||||
	memcpy(&value, (u8*)component + sizeof(Component), sizeof(f32));
 | 
			
		||||
	memcpy(&max, (u8*)component + sizeof(Component) + sizeof(f32), sizeof(f32));
 | 
			
		||||
 | 
			
		||||
	percent = value / max;
 | 
			
		||||
 | 
			
		||||
	glm_vec2_sub(self->maxSize, self->minSize, sizeDifference);
 | 
			
		||||
	glm_vec2_scale(sizeDifference, percent, scaledSize);
 | 
			
		||||
	glm_vec2_copy(self->minSize, textureQuad->size);
 | 
			
		||||
	glm_vec2_add(textureQuad->size, scaledSize, textureQuad->size);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
component_scale_value_init(ComponentScaleValue* self, ECS* ecs, vec2 minSize, vec2 maxSize, ComponentType type, EntityID id)
 | 
			
		||||
{
 | 
			
		||||
	self->id = id;
 | 
			
		||||
	self->type = type;
 | 
			
		||||
	glm_vec2_copy(minSize, self->minSize);
 | 
			
		||||
	glm_vec2_copy(maxSize, self->maxSize);
 | 
			
		||||
 | 
			
		||||
	_component_scale_value_set(self, ecs);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
component_scale_value_tick(ComponentScaleValue* self, ECS* ecs)
 | 
			
		||||
{
 | 
			
		||||
	_component_scale_value_set(self, ecs);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										33
									
								
								src/game/ecs/component/visual/component_scale_value.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								src/game/ecs/component/visual/component_scale_value.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,33 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include "component_texture_quad.h"
 | 
			
		||||
 | 
			
		||||
typedef struct ComponentScaleValue
 | 
			
		||||
{
 | 
			
		||||
	Component component;
 | 
			
		||||
	vec2 minSize;
 | 
			
		||||
	vec2 maxSize;
 | 
			
		||||
	ComponentType type;
 | 
			
		||||
	EntityID id;
 | 
			
		||||
} ComponentScaleValue;
 | 
			
		||||
 | 
			
		||||
void component_scale_value_init(ComponentScaleValue* self, ECS* ecs, vec2 minSize, vec2 maxSize, ComponentType type, EntityID id);
 | 
			
		||||
void component_scale_value_tick(ComponentScaleValue* self, ECS* ecs);
 | 
			
		||||
 | 
			
		||||
static const ComponentInfo COMPONENT_SCALE_VALUE_INFO = 
 | 
			
		||||
{
 | 
			
		||||
	.system = 
 | 
			
		||||
	{
 | 
			
		||||
		.functions = 
 | 
			
		||||
		{ 
 | 
			
		||||
			NULL,
 | 
			
		||||
			NULL,
 | 
			
		||||
			(ECSFunction)component_scale_value_tick,
 | 
			
		||||
			NULL,
 | 
			
		||||
			NULL
 | 
			
		||||
		}
 | 
			
		||||
	},
 | 
			
		||||
	.type = COMPONENT_SCALE_VALUE,
 | 
			
		||||
	.size = sizeof(ComponentScaleValue)
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										236
									
								
								src/game/ecs/component/visual/component_text.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										236
									
								
								src/game/ecs/component/visual/component_text.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,236 @@
 | 
			
		||||
#include "component_text.h"
 | 
			
		||||
 | 
			
		||||
void _component_text_glyph_size_get(ComponentText* self, Texture* texture, vec2 size);
 | 
			
		||||
void _component_text_glyph_position_get(ComponentText* self, Texture* texture, vec3 position);
 | 
			
		||||
void _component_text_glyph_entities_init(ComponentText* self, ECS* ecs);
 | 
			
		||||
void _component_text_glyph_entities_free(ComponentText* self, ECS* ecs);
 | 
			
		||||
void _component_text_glyphs_position_set(ComponentText* self, ECS* ecs);
 | 
			
		||||
void _component_text_glyphs_color_set(ComponentText* self, ECS* ecs);
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
_component_text_glyph_size_get(ComponentText* self, Texture* texture, vec2 size)
 | 
			
		||||
{
 | 
			
		||||
	size[0] = texture->size[0] * self->scale[0];
 | 
			
		||||
	size[1] = texture->size[1] * self->scale[1];
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
_component_text_glyph_entities_init(ComponentText* self, ECS* ecs)
 | 
			
		||||
{
 | 
			
		||||
	vec2 dropShadowOffset;
 | 
			
		||||
 | 
			
		||||
	glm_vec2_zero(dropShadowOffset);
 | 
			
		||||
 | 
			
		||||
	dropShadowOffset[1] = (s32)(self->font->size * COMPONENT_TEXT_DROP_SHADOW_FONT_SIZE_MULTIPLIER);
 | 
			
		||||
 | 
			
		||||
	for (s32 i = 0; i < (s32)self->length; i++)
 | 
			
		||||
	{
 | 
			
		||||
		EntityID glyphID;
 | 
			
		||||
		vec2 glyphSize;
 | 
			
		||||
		vec3 glyphPosition;
 | 
			
		||||
		char glyph;
 | 
			
		||||
 | 
			
		||||
		glyph = self->string[i];
 | 
			
		||||
 | 
			
		||||
		if (glyph == '\n')
 | 
			
		||||
			glyph = ' ';
 | 
			
		||||
 | 
			
		||||
		_component_text_glyph_size_get(self, &self->font->glyphTextures[(s32)glyph], glyphSize);
 | 
			
		||||
 | 
			
		||||
		glyphID = entity_glyph_add
 | 
			
		||||
		(
 | 
			
		||||
			ecs,
 | 
			
		||||
			&self->font->glyphTextures[(s32)glyph],
 | 
			
		||||
			self->buffer,
 | 
			
		||||
			glyphSize,
 | 
			
		||||
			self->position,
 | 
			
		||||
			self->color,
 | 
			
		||||
			self->dropShadowColor,
 | 
			
		||||
			dropShadowOffset
 | 
			
		||||
		);
 | 
			
		||||
 | 
			
		||||
		vector_push(&self->glyphEntities, &glyphID);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	_component_text_glyphs_position_set(self, ecs);
 | 
			
		||||
 | 
			
		||||
	for (s32 i = 0; i < (s32)self->length; i++)
 | 
			
		||||
	{
 | 
			
		||||
		EntityID* glyphEntityID;
 | 
			
		||||
		ComponentDropShadow* glyphEntityDropShadow;
 | 
			
		||||
		
 | 
			
		||||
		glyphEntityID = (EntityID*)vector_get(&self->glyphEntities, i);
 | 
			
		||||
 | 
			
		||||
		glyphEntityDropShadow = ecs_component_get(ecs, COMPONENT_DROP_SHADOW, *glyphEntityID);
 | 
			
		||||
		
 | 
			
		||||
		component_drop_shadow_tick(glyphEntityDropShadow, ecs);
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
_component_text_glyph_entities_free(ComponentText* self, ECS* ecs)
 | 
			
		||||
{
 | 
			
		||||
	for (s32 i = 0; i < (s32)self->length; i++)
 | 
			
		||||
	{
 | 
			
		||||
		EntityID* glyphEntity;
 | 
			
		||||
 | 
			
		||||
		glyphEntity = (EntityID*)vector_get(&self->glyphEntities, i);
 | 
			
		||||
		
 | 
			
		||||
		ecs_entity_delete(ecs, *glyphEntity);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	vector_clear(&self->glyphEntities);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
_component_text_glyphs_position_set(ComponentText* self, ECS* ecs)
 | 
			
		||||
{
 | 
			
		||||
	GlyphMetrics glyphMetrics;
 | 
			
		||||
	f32 currentLineWidth;
 | 
			
		||||
	f32 increment;
 | 
			
		||||
	f32 kerning;
 | 
			
		||||
	f32 lineBegin;
 | 
			
		||||
	f32 lineSkip;
 | 
			
		||||
	f32 advance;
 | 
			
		||||
	vec3 position;
 | 
			
		||||
 | 
			
		||||
	lineSkip = font_line_skip_get(self->font);
 | 
			
		||||
 | 
			
		||||
	glm_vec3_copy(self->position, position);
 | 
			
		||||
 | 
			
		||||
	currentLineWidth = 0.0f;
 | 
			
		||||
	lineBegin = position[0];
 | 
			
		||||
 | 
			
		||||
	for (s32 i = 0; i < (s32)self->length; i++)
 | 
			
		||||
	{
 | 
			
		||||
		EntityID* glyphEntityID;
 | 
			
		||||
		ComponentTextureQuad* glyphEntityTextureQuad;
 | 
			
		||||
		char glyph;
 | 
			
		||||
 | 
			
		||||
		glyph = self->string[i];
 | 
			
		||||
		glyphEntityID = (EntityID*)vector_get(&self->glyphEntities, i);
 | 
			
		||||
		glyphEntityTextureQuad = ecs_component_get(ecs, COMPONENT_TEXTURE_QUAD, *glyphEntityID);
 | 
			
		||||
 | 
			
		||||
		font_glyph_metrics_init(self->font, &glyphMetrics, glyph);
 | 
			
		||||
 | 
			
		||||
		advance = glyphMetrics.advance;
 | 
			
		||||
 | 
			
		||||
		if 
 | 
			
		||||
		(
 | 
			
		||||
			glyph == '\n' ||
 | 
			
		||||
			(glyph == ' ' && (currentLineWidth > self->wrap) && self->wrap != -1)
 | 
			
		||||
		)
 | 
			
		||||
		{
 | 
			
		||||
			position[0] = lineBegin;
 | 
			
		||||
			position[1] += lineSkip * self->scale[1];
 | 
			
		||||
 | 
			
		||||
			advance = 0.0f;
 | 
			
		||||
 | 
			
		||||
			currentLineWidth = 0.0f;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if (i < (s32)self->length)
 | 
			
		||||
			kerning = (f32)font_glyph_kerning_get(self->font, glyph, self->string[i + 1]);
 | 
			
		||||
		else
 | 
			
		||||
			kerning = 0.0f;
 | 
			
		||||
 | 
			
		||||
		glm_vec3_copy(position, glyphEntityTextureQuad->position);
 | 
			
		||||
 | 
			
		||||
		increment = advance + kerning;
 | 
			
		||||
 | 
			
		||||
		position[0] += increment;
 | 
			
		||||
		position[2] -= COMPONENT_TEXT_Z_OFFSET_INCREMENT;
 | 
			
		||||
 | 
			
		||||
		currentLineWidth += increment;
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
_component_text_glyphs_color_set(ComponentText* self, ECS* ecs)
 | 
			
		||||
{
 | 
			
		||||
	for (s32 i = 0; i < (s32)self->length; i++)
 | 
			
		||||
	{
 | 
			
		||||
		EntityID* glyphEntityID;
 | 
			
		||||
		ComponentTextureQuad* glyphEntityTextureQuad;
 | 
			
		||||
		ComponentDropShadow* glyphEntityDropShadow;
 | 
			
		||||
		ComponentTextureQuad* glyphEntityDropShadowTextureQuad;
 | 
			
		||||
 | 
			
		||||
		glyphEntityID = (EntityID*)vector_get(&self->glyphEntities, i);
 | 
			
		||||
 | 
			
		||||
		glyphEntityTextureQuad = ecs_component_get(ecs, COMPONENT_TEXTURE_QUAD, *glyphEntityID);
 | 
			
		||||
		glyphEntityDropShadow = ecs_component_get(ecs, COMPONENT_DROP_SHADOW, *glyphEntityID);
 | 
			
		||||
		glyphEntityDropShadowTextureQuad = ecs_component_get(ecs, COMPONENT_TEXTURE_QUAD, glyphEntityDropShadow->spriteID);
 | 
			
		||||
 | 
			
		||||
		glm_vec4_copy(self->color, glyphEntityTextureQuad->color);
 | 
			
		||||
		glm_vec4_copy(self->colorCover, glyphEntityTextureQuad->colorCover);
 | 
			
		||||
 | 
			
		||||
		glyphEntityDropShadowTextureQuad->color[3] = self->color[3];
 | 
			
		||||
		glm_vec4_copy(self->colorCover, glyphEntityDropShadowTextureQuad->colorCover);
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
component_text_string_set(ComponentText* self, ECS* ecs, char* string, u32 length)
 | 
			
		||||
{
 | 
			
		||||
	_component_text_glyph_entities_free(self, ecs);
 | 
			
		||||
 | 
			
		||||
	if (length > COMPONENT_TEXT_STRING_MAX)
 | 
			
		||||
		return;
 | 
			
		||||
 | 
			
		||||
	self->length = length;
 | 
			
		||||
 | 
			
		||||
	memset(self->string, '\0', sizeof(char) * COMPONENT_TEXT_STRING_MAX);
 | 
			
		||||
	memcpy(self->string, string, sizeof(char) * self->length);
 | 
			
		||||
 | 
			
		||||
	_component_text_glyph_entities_init(self, ecs);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
component_text_add(ComponentText* self, ECS* ecs)
 | 
			
		||||
{
 | 
			
		||||
	glm_vec4_copy(COMPONENT_TEXT_COLOR_DEFAULT, self->color);
 | 
			
		||||
	glm_vec4_copy(COMPONENT_TEXT_COLOR_COVER_DEFAULT, self->color);
 | 
			
		||||
	glm_vec4_copy(COMPONENT_TEXT_DROP_SHADOW_COLOR_DEFAULT, self->dropShadowColor);
 | 
			
		||||
	glm_vec2_copy(COMPONENT_TEXT_SCALE_DEFAULT, self->scale);
 | 
			
		||||
 | 
			
		||||
	self->wrap = COMPONENT_TEXT_WRAP_DEFAULT;
 | 
			
		||||
 | 
			
		||||
	vector_init(&self->glyphEntities, sizeof(EntityID));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
component_text_delete(ComponentText* self, ECS* ecs)
 | 
			
		||||
{
 | 
			
		||||
	_component_text_glyph_entities_free(self, ecs);
 | 
			
		||||
 | 
			
		||||
	vector_free(&self->glyphEntities);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
component_text_tick(ComponentText* self, ECS* ecs)
 | 
			
		||||
{
 | 
			
		||||
	_component_text_glyphs_position_set(self, ecs);
 | 
			
		||||
	_component_text_glyphs_color_set(self, ecs);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
component_text_init
 | 
			
		||||
(
 | 
			
		||||
	ComponentText* self, 
 | 
			
		||||
	ECS* ecs,
 | 
			
		||||
	Font* font,
 | 
			
		||||
	RendererBuffer buffer,
 | 
			
		||||
	char* string,
 | 
			
		||||
	vec3 position,
 | 
			
		||||
	u32 length,
 | 
			
		||||
	s32 wrap
 | 
			
		||||
)
 | 
			
		||||
{
 | 
			
		||||
	self->font = font;
 | 
			
		||||
	self->wrap = wrap;
 | 
			
		||||
	self->buffer = buffer;
 | 
			
		||||
 | 
			
		||||
	glm_vec3_copy(position, self->position);
 | 
			
		||||
 | 
			
		||||
	component_text_string_set(self, ecs, string, length);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										79
									
								
								src/game/ecs/component/visual/component_text.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										79
									
								
								src/game/ecs/component/visual/component_text.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,79 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include "../../ecs_entity.h"
 | 
			
		||||
 | 
			
		||||
#include "../../../../engine/rectangle.h"
 | 
			
		||||
#include "../../../../engine/font.h"
 | 
			
		||||
#include "../../../render/texture_quad.h"
 | 
			
		||||
#include "../../../resource/resource_shader.h"
 | 
			
		||||
 | 
			
		||||
#include "../physics/component_physics.h"
 | 
			
		||||
 | 
			
		||||
#include "../../entity/visual/entity_glyph.h"
 | 
			
		||||
 | 
			
		||||
#define COMPONENT_TEXT_LIMIT 1024
 | 
			
		||||
#define COMPONENT_TEXT_STRING_MAX 1024
 | 
			
		||||
 | 
			
		||||
#define COMPONENT_TEXT_COLOR_DEFAULT (f32*)OPAQUE
 | 
			
		||||
#define COMPONENT_TEXT_COLOR_COVER_DEFAULT (f32*)OPAQUE
 | 
			
		||||
#define COMPONENT_TEXT_DROP_SHADOW_COLOR_DEFAULT (f32*)BLACK
 | 
			
		||||
#define COMPONENT_TEXT_Z_OFFSET_INCREMENT 0.0001f
 | 
			
		||||
 | 
			
		||||
#define COMPONENT_TEXT_DROP_SHADOW_FONT_SIZE_MULTIPLIER 0.125f
 | 
			
		||||
 | 
			
		||||
static const vec2 COMPONENT_TEXT_SCALE_DEFAULT = {1.0f, 1.0f};
 | 
			
		||||
static const f32 COMPONENT_TEXT_WRAP_DEFAULT = -1;
 | 
			
		||||
static const vec3 COMPONENT_TEXT_OFFSET_DEFAULT = {0.0f, 0.0f, 0.0f};
 | 
			
		||||
 | 
			
		||||
typedef struct ComponentText
 | 
			
		||||
{
 | 
			
		||||
	Component component;
 | 
			
		||||
	Shader* shader;
 | 
			
		||||
	RendererBuffer buffer;
 | 
			
		||||
	Font* font;
 | 
			
		||||
	Vector glyphEntities; /* EntityID */
 | 
			
		||||
	char string[COMPONENT_TEXT_STRING_MAX];
 | 
			
		||||
	s32 wrap;
 | 
			
		||||
	u32 length;
 | 
			
		||||
	vec2 scale;
 | 
			
		||||
	vec3 offset;
 | 
			
		||||
	vec3 position;
 | 
			
		||||
	vec4 color;
 | 
			
		||||
	vec4 colorCover;
 | 
			
		||||
	vec4 dropShadowColor;
 | 
			
		||||
} ComponentText;
 | 
			
		||||
 | 
			
		||||
void component_text_add(ComponentText* self, ECS* ecs);
 | 
			
		||||
void component_text_delete(ComponentText* self, ECS* ecs);
 | 
			
		||||
void component_text_string_set(ComponentText* self, ECS* ecs, char* string, u32 length);
 | 
			
		||||
void component_text_tick(ComponentText* self, ECS* ecs);
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
component_text_init
 | 
			
		||||
(
 | 
			
		||||
	ComponentText* self, 
 | 
			
		||||
	ECS* ecs,
 | 
			
		||||
	Font* font,
 | 
			
		||||
	RendererBuffer buffer,
 | 
			
		||||
	char* string,
 | 
			
		||||
	vec3 position,
 | 
			
		||||
	u32 length,
 | 
			
		||||
	s32 wrap
 | 
			
		||||
);
 | 
			
		||||
 | 
			
		||||
static const ComponentInfo COMPONENT_TEXT_INFO = 
 | 
			
		||||
{
 | 
			
		||||
	.system = 
 | 
			
		||||
	{
 | 
			
		||||
		.functions = 
 | 
			
		||||
		{ 
 | 
			
		||||
			(ECSFunction)component_text_add, 
 | 
			
		||||
			(ECSFunction)component_text_delete, 
 | 
			
		||||
			(ECSFunction)component_text_tick,
 | 
			
		||||
			NULL,
 | 
			
		||||
			NULL
 | 
			
		||||
		}
 | 
			
		||||
	},
 | 
			
		||||
	.type = COMPONENT_TEXT,
 | 
			
		||||
	.size = sizeof(ComponentText)
 | 
			
		||||
};
 | 
			
		||||
							
								
								
									
										176
									
								
								src/game/ecs/component/visual/component_texture_quad.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										176
									
								
								src/game/ecs/component/visual/component_texture_quad.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,176 @@
 | 
			
		||||
#include "component_texture_quad.h"
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
component_texture_quad_size_get(ComponentTextureQuad* self, vec2 size)
 | 
			
		||||
{
 | 
			
		||||
	glm_vec2_copy(self->size, size);
 | 
			
		||||
	glm_vec2_mul(size, self->scale, size);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
component_texture_quad_position_get(ComponentTextureQuad* self, vec3 position)
 | 
			
		||||
{
 | 
			
		||||
	vec2 size;
 | 
			
		||||
 | 
			
		||||
	glm_vec3_copy(self->position, position);
 | 
			
		||||
	glm_vec3_add(position, self->offset, position);
 | 
			
		||||
 | 
			
		||||
	component_texture_quad_size_get(self, size);
 | 
			
		||||
 | 
			
		||||
	switch (self->origin)
 | 
			
		||||
	{
 | 
			
		||||
		case ORIGIN_CENTER:
 | 
			
		||||
			position[0] -= size[0] / 2;
 | 
			
		||||
			position[1] -= size[1] / 2;
 | 
			
		||||
			break;
 | 
			
		||||
		case ORIGIN_TOP_LEFT:
 | 
			
		||||
		default:
 | 
			
		||||
			break;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
component_texture_quad_origin_point_get(ComponentTextureQuad* self, vec3 originPoint)
 | 
			
		||||
{
 | 
			
		||||
	vec2 size;
 | 
			
		||||
 | 
			
		||||
	glm_vec3_zero(originPoint);
 | 
			
		||||
 | 
			
		||||
	switch (self->origin)
 | 
			
		||||
	{
 | 
			
		||||
		case ORIGIN_CENTER:
 | 
			
		||||
			component_texture_quad_size_get(self, size);
 | 
			
		||||
			originPoint[0] = size[0] / 2;
 | 
			
		||||
			originPoint[1] = size[1] / 2;
 | 
			
		||||
		case ORIGIN_TOP_LEFT:
 | 
			
		||||
		default:
 | 
			
		||||
			break;
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
component_texture_quad_model_get(ComponentTextureQuad* self, mat4 model)
 | 
			
		||||
{
 | 
			
		||||
	vec3 position;
 | 
			
		||||
	vec3 originPoint;
 | 
			
		||||
 | 
			
		||||
	component_texture_quad_position_get(self, position);
 | 
			
		||||
	component_texture_quad_origin_point_get(self, originPoint);
 | 
			
		||||
 | 
			
		||||
	glm_mat4_identity(model);
 | 
			
		||||
	glm_translate(model, position);
 | 
			
		||||
	glm_rotate_at(model, originPoint, self->rotation, (f32*)COMPONENT_TEXTURE_QUAD_ROTATION_AXIS);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
component_texture_quad_rectangle_get(ComponentTextureQuad* self, Rectangle* rectangle)
 | 
			
		||||
{
 | 
			
		||||
	vec2 size;
 | 
			
		||||
	vec2 difference;
 | 
			
		||||
 | 
			
		||||
	component_texture_quad_size_get(self, size);
 | 
			
		||||
 | 
			
		||||
	rectangle->x = (self->position[0] - (size[0] / 2)) + self->offset[0];
 | 
			
		||||
	rectangle->y = (self->position[1] - (size[1] / 2)) + self->offset[1];
 | 
			
		||||
	rectangle->w = size[0];
 | 
			
		||||
	rectangle->h = size[1];
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
s32
 | 
			
		||||
component_texture_quad_sort(ComponentTextureQuad* a, ComponentTextureQuad* b)
 | 
			
		||||
{
 | 
			
		||||
	return a->position[2] > b->position[2] ? -1 : 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void 
 | 
			
		||||
component_texture_quad_init
 | 
			
		||||
(
 | 
			
		||||
	ComponentTextureQuad* self, 
 | 
			
		||||
	ECS* ecs, 
 | 
			
		||||
	Texture* texture,
 | 
			
		||||
	Flip flip,
 | 
			
		||||
	Origin origin,
 | 
			
		||||
	RendererBuffer buffer,
 | 
			
		||||
	f32 rotation,
 | 
			
		||||
	vec2 size, 
 | 
			
		||||
	vec2 scale,
 | 
			
		||||
	vec2 uvMin, 
 | 
			
		||||
	vec2 uvMax, 
 | 
			
		||||
	vec3 offset,
 | 
			
		||||
	vec3 position, 
 | 
			
		||||
	vec4 color
 | 
			
		||||
)
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
	self->texture = texture;
 | 
			
		||||
	self->flip = flip;
 | 
			
		||||
	self->origin = origin;
 | 
			
		||||
	self->buffer = buffer;
 | 
			
		||||
	self->rotation = rotation;
 | 
			
		||||
	glm_vec2_copy(size, self->size);
 | 
			
		||||
	glm_vec2_copy(scale, self->scale);
 | 
			
		||||
	glm_vec2_copy(uvMin, self->uvMin);
 | 
			
		||||
	glm_vec2_copy(uvMax, self->uvMax);
 | 
			
		||||
	glm_vec3_copy(COMPONENT_TEXTURE_QUAD_OFFSET_DEFAULT, self->offset);
 | 
			
		||||
	glm_vec4_copy(COMPONENT_TEXTURE_QUAD_COLOR_COVER, self->colorCover);
 | 
			
		||||
	glm_vec3_copy(position, self->position);
 | 
			
		||||
	glm_vec4_copy(color, self->color);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
component_texture_quad_add(ComponentTextureQuad* self, ECS* ecs)
 | 
			
		||||
{
 | 
			
		||||
	component_texture_quad_init
 | 
			
		||||
	(
 | 
			
		||||
		self,
 | 
			
		||||
		ecs,
 | 
			
		||||
		&ecs->resources->textures[COMPONENT_TEXTURE_QUAD_TEXTURE_DEFAULT],
 | 
			
		||||
		COMPONENT_TEXTURE_QUAD_FLIP_DEFAULT,
 | 
			
		||||
		COMPONENT_TEXTURE_QUAD_ORIGIN_DEFAULT,
 | 
			
		||||
		COMPONENT_TEXTURE_QUAD_BUFFER_DEFAULT,
 | 
			
		||||
		COMPONENT_TEXTURE_QUAD_ROTATION_DEFAULT,
 | 
			
		||||
		COMPONENT_TEXTURE_QUAD_SIZE_DEFAULT,
 | 
			
		||||
		COMPONENT_TEXTURE_QUAD_SCALE_DEFAULT,
 | 
			
		||||
		COMPONENT_TEXTURE_QUAD_UV_MIN_DEFAULT,
 | 
			
		||||
		COMPONENT_TEXTURE_QUAD_UV_MAX_DEFAULT,
 | 
			
		||||
		COMPONENT_TEXTURE_QUAD_OFFSET_DEFAULT,
 | 
			
		||||
		COMPONENT_TEXTURE_QUAD_POSITION_DEFAULT,
 | 
			
		||||
		COMPONENT_TEXTURE_QUAD_COLOR_DEFAULT
 | 
			
		||||
	);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
component_texture_quad_draw(ComponentTextureQuad* self, ECS* ecs)
 | 
			
		||||
{
 | 
			
		||||
	mat4 model;
 | 
			
		||||
	mat4 view;
 | 
			
		||||
	mat4 projection;
 | 
			
		||||
	vec2 size;
 | 
			
		||||
 | 
			
		||||
	component_texture_quad_model_get(self, model);
 | 
			
		||||
	component_texture_quad_size_get(self, size);
 | 
			
		||||
 | 
			
		||||
	camera_view_get(&ecs->renderer->camera[self->buffer], view);
 | 
			
		||||
	camera_projection_get(&ecs->renderer->camera[self->buffer], projection);
 | 
			
		||||
 | 
			
		||||
	renderer_buffer_use(ecs->renderer, self->buffer);
 | 
			
		||||
 | 
			
		||||
	texture_quad_draw
 | 
			
		||||
	(
 | 
			
		||||
	 	self->texture,
 | 
			
		||||
		ecs->renderer,
 | 
			
		||||
		&ecs->resources->shaders[SHADER_TEXTURE_QUAD],
 | 
			
		||||
		self->uvMin,
 | 
			
		||||
		self->uvMax,
 | 
			
		||||
		model,
 | 
			
		||||
		view,
 | 
			
		||||
		projection,
 | 
			
		||||
		size,
 | 
			
		||||
		self->color,
 | 
			
		||||
		self->colorCover,
 | 
			
		||||
		self->flip
 | 
			
		||||
	);
 | 
			
		||||
 | 
			
		||||
	renderer_buffer_unbind();
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										85
									
								
								src/game/ecs/component/visual/component_texture_quad.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										85
									
								
								src/game/ecs/component/visual/component_texture_quad.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,85 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include "../../ecs_entity.h"
 | 
			
		||||
 | 
			
		||||
#include "../../../../engine/rectangle.h"
 | 
			
		||||
#include "../../../render/texture_quad.h"
 | 
			
		||||
#include "../../../resource/resource_shader.h"
 | 
			
		||||
 | 
			
		||||
typedef struct ComponentTextureQuad
 | 
			
		||||
{
 | 
			
		||||
	Component component;
 | 
			
		||||
	Flip flip;
 | 
			
		||||
	Origin origin;
 | 
			
		||||
	RendererBuffer buffer;
 | 
			
		||||
	Texture* texture;
 | 
			
		||||
	f32 rotation;
 | 
			
		||||
	vec2 scale;
 | 
			
		||||
	vec2 size;
 | 
			
		||||
	vec2 uvMax;
 | 
			
		||||
	vec2 uvMin;
 | 
			
		||||
	vec3 offset;
 | 
			
		||||
	vec3 position;
 | 
			
		||||
	vec4 color;
 | 
			
		||||
	vec4 colorCover;
 | 
			
		||||
} ComponentTextureQuad;
 | 
			
		||||
 | 
			
		||||
void component_texture_quad_init
 | 
			
		||||
(
 | 
			
		||||
	ComponentTextureQuad* self, 
 | 
			
		||||
	ECS* ecs, 
 | 
			
		||||
	Texture* texture,
 | 
			
		||||
	Flip flip,
 | 
			
		||||
	Origin origin,
 | 
			
		||||
	RendererBuffer buffer,
 | 
			
		||||
	f32 rotation,
 | 
			
		||||
	vec2 size, 
 | 
			
		||||
	vec2 scale,
 | 
			
		||||
	vec2 uvMin, 
 | 
			
		||||
	vec2 uvMax, 
 | 
			
		||||
	vec3 position,
 | 
			
		||||
	vec3 offset,
 | 
			
		||||
	vec4 color
 | 
			
		||||
);
 | 
			
		||||
 | 
			
		||||
void component_texture_quad_size_get(ComponentTextureQuad* self, vec2 size);
 | 
			
		||||
void component_texture_quad_position_get(ComponentTextureQuad* self, vec3 position);
 | 
			
		||||
void component_texture_quad_origin_point_get(ComponentTextureQuad* self, vec3 originPoint);
 | 
			
		||||
void component_texture_quad_model_get(ComponentTextureQuad* self, mat4 model);
 | 
			
		||||
void component_texture_quad_rectangle_get(ComponentTextureQuad* self, Rectangle* rectangle);
 | 
			
		||||
s32 component_texture_quad_sort(ComponentTextureQuad* a, ComponentTextureQuad* b);
 | 
			
		||||
void component_texture_quad_draw(ComponentTextureQuad* self, ECS* ecs);
 | 
			
		||||
 | 
			
		||||
static const ComponentInfo COMPONENT_TEXTURE_QUAD_INFO = 
 | 
			
		||||
{
 | 
			
		||||
	.system = 
 | 
			
		||||
	{
 | 
			
		||||
		.functions = 
 | 
			
		||||
		{ 
 | 
			
		||||
			NULL,
 | 
			
		||||
			NULL, 
 | 
			
		||||
			NULL,
 | 
			
		||||
			NULL,
 | 
			
		||||
			(ECSFunction)component_texture_quad_draw
 | 
			
		||||
		}
 | 
			
		||||
	},
 | 
			
		||||
	.type = COMPONENT_TEXTURE_QUAD,
 | 
			
		||||
	.size = sizeof(ComponentTextureQuad)
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#define COMPONENT_TEXTURE_QUAD_UV_MIN_DEFAULT TEXTURE_QUAD_UV_MIN_DEFAULT
 | 
			
		||||
#define COMPONENT_TEXTURE_QUAD_UV_MAX_DEFAULT TEXTURE_QUAD_UV_MAX_DEFAULT
 | 
			
		||||
#define COMPONENT_TEXTURE_QUAD_COLOR_DEFAULT TEXTURE_QUAD_COLOR_DEFAULT
 | 
			
		||||
#define COMPONENT_TEXTURE_QUAD_FLIP_DEFAULT TEXTURE_QUAD_FLIP_DEFAULT
 | 
			
		||||
#define COMPONENT_TEXTURE_QUAD_ORIGIN_DEFAULT TEXTURE_QUAD_ORIGIN_DEFAULT
 | 
			
		||||
 | 
			
		||||
static const RendererBuffer COMPONENT_TEXTURE_QUAD_BUFFER_DEFAULT = RENDERER_BUFFER_WORLD;
 | 
			
		||||
static const ShaderType COMPONENT_TEXTURE_QUAD_SHADER_DEFAULT = SHADER_TEXTURE_QUAD;
 | 
			
		||||
static const TextureType COMPONENT_TEXTURE_QUAD_TEXTURE_DEFAULT = TEXTURE_DEFAULT;
 | 
			
		||||
static const f32 COMPONENT_TEXTURE_QUAD_ROTATION_DEFAULT = 0.0f;
 | 
			
		||||
static const vec2 COMPONENT_TEXTURE_QUAD_SCALE_DEFAULT = {1.0f, 1.0f};
 | 
			
		||||
static const vec2 COMPONENT_TEXTURE_QUAD_SIZE_DEFAULT = {32.0f, 32.0f};
 | 
			
		||||
static const vec3 COMPONENT_TEXTURE_QUAD_POSITION_DEFAULT = {0.0f, 0.0f, 0.0f};
 | 
			
		||||
static const vec3 COMPONENT_TEXTURE_QUAD_OFFSET_DEFAULT = {0.0f, 0.0f, 0.0f};
 | 
			
		||||
static const vec3 COMPONENT_TEXTURE_QUAD_ROTATION_AXIS = {0.0f, 0.0f, 1.0f};
 | 
			
		||||
static const vec4 COMPONENT_TEXTURE_QUAD_COLOR_COVER = {0.0f, 0.0f, 0.0f, 1.0f};
 | 
			
		||||
							
								
								
									
										128
									
								
								src/game/ecs/component/visual/component_value_text.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										128
									
								
								src/game/ecs/component/visual/component_value_text.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,128 @@
 | 
			
		||||
#include "component_value_text.h"
 | 
			
		||||
 | 
			
		||||
s32 _component_value_text_value_get(ComponentValueText* self, ECS* ecs);
 | 
			
		||||
s32 _component_value_text_max_get(ComponentValueText* self, ECS* ecs);
 | 
			
		||||
void _component_value_text_set(ComponentValueText* self, ECS* ecs);
 | 
			
		||||
 | 
			
		||||
s32
 | 
			
		||||
_component_value_text_value_get(ComponentValueText* self, ECS* ecs)
 | 
			
		||||
{
 | 
			
		||||
	u8* component;
 | 
			
		||||
	s32 value;
 | 
			
		||||
 | 
			
		||||
	component = (u8*)ecs_component_get(ecs, self->type, self->id);
 | 
			
		||||
 | 
			
		||||
	// Get first value of component, after the component struct. If it segfaults, not my problem.
 | 
			
		||||
	memcpy(&value, component + sizeof(Component), sizeof(u32));
 | 
			
		||||
 | 
			
		||||
	return value;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
s32
 | 
			
		||||
_component_value_text_max_get(ComponentValueText* self, ECS* ecs)
 | 
			
		||||
{
 | 
			
		||||
	u8* component;
 | 
			
		||||
	s32 max;
 | 
			
		||||
 | 
			
		||||
	component = (u8*)ecs_component_get(ecs, self->type, self->id);
 | 
			
		||||
 | 
			
		||||
	// Get second value of component, after the component struct and the value. If it segfaults, not my problem.
 | 
			
		||||
	memcpy(&max, component + sizeof(Component) + sizeof(u32), sizeof(u32));
 | 
			
		||||
 | 
			
		||||
	return max;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
_component_value_text_set(ComponentValueText* self, ECS* ecs)
 | 
			
		||||
{
 | 
			
		||||
	ComponentText* text;
 | 
			
		||||
	u32 minutes;
 | 
			
		||||
	u32 seconds;
 | 
			
		||||
	u32 milliseconds;
 | 
			
		||||
	u32 max;
 | 
			
		||||
 | 
			
		||||
	char string[COMPONENT_VALUE_TEXT_STRING_MAX];
 | 
			
		||||
 | 
			
		||||
	text = ecs_component_get(ecs, COMPONENT_TEXT, self->component.id);
 | 
			
		||||
 | 
			
		||||
	memset(string, '\0', COMPONENT_VALUE_TEXT_STRING_MAX);
 | 
			
		||||
 | 
			
		||||
	switch (self->textType)
 | 
			
		||||
	{
 | 
			
		||||
		case COMPONENT_VALUE_TEXT_TYPE_TIMER:
 | 
			
		||||
			time_formatted_get(self->value, &minutes, &seconds, &milliseconds);
 | 
			
		||||
			snprintf(string, COMPONENT_VALUE_TEXT_STRING_MAX, self->format, minutes, seconds, milliseconds);
 | 
			
		||||
			break;
 | 
			
		||||
		case COMPONENT_VALUE_TEXT_TYPE_INTEGER_MAX:
 | 
			
		||||
			max = _component_value_text_max_get(self, ecs);
 | 
			
		||||
			snprintf(string, COMPONENT_VALUE_TEXT_STRING_MAX, self->format, self->value, max);
 | 
			
		||||
			break;
 | 
			
		||||
		case COMPONENT_VALUE_TEXT_TYPE_INTEGER:
 | 
			
		||||
		default:
 | 
			
		||||
			snprintf(string, COMPONENT_VALUE_TEXT_STRING_MAX, self->format, self->value);
 | 
			
		||||
			break;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	component_text_string_set(text, ecs, string, strlen(string));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
component_value_text_add(ComponentValueText* self, ECS* ecs)
 | 
			
		||||
{
 | 
			
		||||
	ecs_components_add
 | 
			
		||||
	(
 | 
			
		||||
		ecs, 
 | 
			
		||||
		COMPONENT_VALUE_TEXT_DEPENDENCIES, 
 | 
			
		||||
		COMPONENT_VALUE_TEXT_DEPENDENCY_COUNT, 
 | 
			
		||||
		self->component.id
 | 
			
		||||
	);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
component_value_text_tick(ComponentValueText* self, ECS* ecs)
 | 
			
		||||
{
 | 
			
		||||
	self->value = _component_value_text_value_get(self, ecs);
 | 
			
		||||
 | 
			
		||||
	if (self->value != self->previousValue)
 | 
			
		||||
	{
 | 
			
		||||
		_component_value_text_set(self, ecs);
 | 
			
		||||
		self->previousValue = self->value;
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
component_value_text_init
 | 
			
		||||
(
 | 
			
		||||
	ComponentValueText* self, 
 | 
			
		||||
	ECS* ecs,
 | 
			
		||||
	Font* font,
 | 
			
		||||
	RendererBuffer buffer,
 | 
			
		||||
	const vec3 position,
 | 
			
		||||
	ComponentType type,
 | 
			
		||||
	ComponentValueTextType textType,
 | 
			
		||||
	const char* format,
 | 
			
		||||
	EntityID id
 | 
			
		||||
)
 | 
			
		||||
{
 | 
			
		||||
	component_text_init
 | 
			
		||||
	(
 | 
			
		||||
		ecs_component_get(ecs, COMPONENT_TEXT, self->component.id), 
 | 
			
		||||
		ecs, 
 | 
			
		||||
		font,
 | 
			
		||||
		buffer,
 | 
			
		||||
		"1",
 | 
			
		||||
		position,
 | 
			
		||||
		0,
 | 
			
		||||
		-1
 | 
			
		||||
	);
 | 
			
		||||
 | 
			
		||||
	self->format = format;
 | 
			
		||||
	self->type = type;
 | 
			
		||||
	self->textType = textType;
 | 
			
		||||
	self->id = id;
 | 
			
		||||
	self->value = _component_value_text_value_get(self, ecs);
 | 
			
		||||
	self->previousValue = self->value;
 | 
			
		||||
	
 | 
			
		||||
	_component_value_text_set(self, ecs);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										64
									
								
								src/game/ecs/component/visual/component_value_text.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										64
									
								
								src/game/ecs/component/visual/component_value_text.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,64 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include "component_text.h"
 | 
			
		||||
#include "../../../../engine/time.h"
 | 
			
		||||
 | 
			
		||||
#define COMPONENT_VALUE_TEXT_STRING_MAX 64
 | 
			
		||||
 | 
			
		||||
typedef enum ComponentValueTextType
 | 
			
		||||
{
 | 
			
		||||
	COMPONENT_VALUE_TEXT_TYPE_INTEGER,
 | 
			
		||||
	COMPONENT_VALUE_TEXT_TYPE_INTEGER_MAX,
 | 
			
		||||
	COMPONENT_VALUE_TEXT_TYPE_TIMER,
 | 
			
		||||
} ComponentValueTextType;
 | 
			
		||||
 | 
			
		||||
typedef struct ComponentValueText
 | 
			
		||||
{
 | 
			
		||||
	Component component;
 | 
			
		||||
	ComponentType type;
 | 
			
		||||
	ComponentValueTextType textType;
 | 
			
		||||
	EntityID id;
 | 
			
		||||
	const char* format;
 | 
			
		||||
	s32 previousValue;
 | 
			
		||||
	s32 value;
 | 
			
		||||
} ComponentValueText;
 | 
			
		||||
 | 
			
		||||
void component_value_text_add(ComponentValueText* self, ECS* ecs);
 | 
			
		||||
void component_value_text_tick(ComponentValueText* self, ECS* ecs);
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
component_value_text_init
 | 
			
		||||
(
 | 
			
		||||
	ComponentValueText* self, 
 | 
			
		||||
	ECS* ecs,
 | 
			
		||||
	Font* font,
 | 
			
		||||
	RendererBuffer buffer,
 | 
			
		||||
	const vec3 position,
 | 
			
		||||
	ComponentType type,
 | 
			
		||||
	ComponentValueTextType textType,
 | 
			
		||||
	const char* format,
 | 
			
		||||
	EntityID id
 | 
			
		||||
);
 | 
			
		||||
 | 
			
		||||
#define COMPONENT_VALUE_TEXT_DEPENDENCY_COUNT 1
 | 
			
		||||
static const ComponentType COMPONENT_VALUE_TEXT_DEPENDENCIES[COMPONENT_VALUE_TEXT_DEPENDENCY_COUNT] = 
 | 
			
		||||
{
 | 
			
		||||
	COMPONENT_TEXT
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static const ComponentInfo COMPONENT_VALUE_TEXT_INFO = 
 | 
			
		||||
{
 | 
			
		||||
	.system = 
 | 
			
		||||
	{
 | 
			
		||||
		.functions = 
 | 
			
		||||
		{ 
 | 
			
		||||
			(ECSFunction)component_value_text_add, 
 | 
			
		||||
			NULL,
 | 
			
		||||
			(ECSFunction)component_value_text_tick,
 | 
			
		||||
			NULL,
 | 
			
		||||
			NULL
 | 
			
		||||
		}
 | 
			
		||||
	},
 | 
			
		||||
	.type = COMPONENT_VALUE_TEXT,
 | 
			
		||||
	.size = sizeof(ComponentText)
 | 
			
		||||
};
 | 
			
		||||
		Reference in New Issue
	
	Block a user