108 lines
3.1 KiB
C
108 lines
3.1 KiB
C
#include "component_animation_target.h"
|
|
|
|
void _component_animation_target_set(ComponentAnimationTarget* self, ECS* ecs, AnimationTargetType type);
|
|
void _component_animation_target_determine(ComponentAnimationTarget* self, ECS* ecs);
|
|
|
|
void
|
|
_component_animation_target_set(ComponentAnimationTarget* self, ECS* ecs, AnimationTargetType type)
|
|
{
|
|
ComponentAnimation* animation;
|
|
|
|
if (type == self->type)
|
|
return;
|
|
|
|
self->type = type;
|
|
|
|
animation = ecs_component_get(ecs, COMPONENT_ANIMATION, self->component.id);
|
|
|
|
component_animation_set(animation, ecs, &ANIMATION_TARGET_STRUCTS[type]);
|
|
}
|
|
|
|
void
|
|
_component_animation_target_determine(ComponentAnimationTarget* self, ECS* ecs)
|
|
{
|
|
ComponentStage* stage;
|
|
AnimationTargetType type;
|
|
|
|
stage = ecs_component_get(ecs, COMPONENT_STAGE, self->component.id);
|
|
|
|
type = (AnimationTargetType)(self->action + ((ANIMATION_TARGET_ACTION_COUNT) * stage->value));
|
|
|
|
_component_animation_target_set(self, ecs, type);
|
|
}
|
|
|
|
void
|
|
component_animation_target_init(ComponentAnimationTarget* self, ECS* ecs, AnimationTargetType type, EntityID playerID)
|
|
{
|
|
self->type = -1;
|
|
self->action = ANIMATION_TARGET_ACTION_IDLE;
|
|
self->playerID = playerID;
|
|
|
|
_component_animation_target_set(self, ecs, type);
|
|
}
|
|
|
|
void
|
|
component_animation_target_tick(ComponentAnimationTarget* self, ECS* ecs)
|
|
{
|
|
ComponentFood* playerFood;
|
|
ComponentTakeFood* takeFood;
|
|
ComponentAnimation* animation;
|
|
|
|
animation = ecs_component_get(ecs, COMPONENT_ANIMATION, self->component.id);
|
|
|
|
if
|
|
(
|
|
(self->action == ANIMATION_TARGET_ACTION_GULP || self->action == ANIMATION_TARGET_ACTION_BURP) &&
|
|
!animation->isFinished
|
|
)
|
|
return;
|
|
|
|
if (self->action == ANIMATION_TARGET_ACTION_GULP && animation->isFinished)
|
|
{
|
|
self->action = ANIMATION_TARGET_ACTION_BURP;
|
|
|
|
sound_play(&ecs->resources->sounds[SOUND_BURP + RANDOM_S32(0, SOUND_BURP_COUNT - 1)]);
|
|
|
|
_component_animation_target_determine(self, ecs);
|
|
|
|
return;
|
|
}
|
|
|
|
playerFood = ecs_component_get(ecs, COMPONENT_FOOD, self->playerID);
|
|
takeFood = ecs_component_get(ecs, COMPONENT_TAKE_FOOD, self->component.id);
|
|
|
|
if (self->action == ANIMATION_TARGET_ACTION_BURP && animation->isFinished)
|
|
{
|
|
sound_play(&ecs->resources->sounds[SOUND_GURGLE + RANDOM_S32(0, SOUND_GURGLE_COUNT - 1)]);
|
|
takeFood->component.isDisabled = false;
|
|
}
|
|
|
|
self->action = ANIMATION_TARGET_ACTION_IDLE;
|
|
|
|
if (takeFood->isTakeFood)
|
|
{
|
|
self->action = ANIMATION_TARGET_ACTION_GULP;
|
|
|
|
takeFood->isTakeFood = false;
|
|
takeFood->component.isDisabled = true;
|
|
|
|
sound_play(&ecs->resources->sounds[SOUND_GULP + RANDOM_S32(0, SOUND_GULP_COUNT - 1)]);
|
|
}
|
|
else if (playerFood->value > 0)
|
|
{
|
|
ComponentPhysics* playerPhysics;
|
|
ComponentPhysics* physics;
|
|
f32 distance;
|
|
|
|
playerPhysics = ecs_component_get(ecs, COMPONENT_PHYSICS, self->playerID);
|
|
physics = ecs_component_get(ecs, COMPONENT_PHYSICS, self->component.id);
|
|
|
|
distance = DISTANCE_2D(playerPhysics->position[0], physics->position[0], playerPhysics->position[1], physics->position[1]);
|
|
|
|
if (distance <= ANIMATION_TARGET_POINT_RADIUS && !self->isPointDisabled)
|
|
self->action = ANIMATION_TARGET_ACTION_POINT;
|
|
}
|
|
|
|
_component_animation_target_determine(self, ecs);
|
|
}
|