77 lines
1.6 KiB
C
77 lines
1.6 KiB
C
/*
|
|
* DESCRIPTION:
|
|
* Rotation component.
|
|
*/
|
|
|
|
#include "c_rotation.h"
|
|
|
|
static void update_f(struct CRotation* self);
|
|
|
|
/* Adds a rotation component. */
|
|
static void
|
|
add(struct CRotation* self)
|
|
{
|
|
self->angle = C_ROTATION_ANGLE_DEFAULT;
|
|
self->velocity = C_ROTATION_VELOCITY_DEFAULT;
|
|
}
|
|
|
|
/* Draws a rotation component. */
|
|
static void
|
|
update_f(struct CRotation* self)
|
|
{
|
|
if (self->isDisabled)
|
|
return;
|
|
|
|
if
|
|
(
|
|
keyboard_held(&game.keyboard, KEYBOARD_LEFT) ||
|
|
keyboard_held(&game.keyboard, KEYBOARD_A) ||
|
|
mouse_held(&game.mouse, MOUSE_LEFT)
|
|
)
|
|
self->velocity += self->speed;
|
|
|
|
if
|
|
(
|
|
keyboard_held(&game.keyboard, KEYBOARD_RIGHT) ||
|
|
keyboard_held(&game.keyboard, KEYBOARD_D) ||
|
|
mouse_held(&game.mouse, MOUSE_RIGHT)
|
|
)
|
|
self->velocity -= self->speed;
|
|
|
|
self->velocity *= self->friction;
|
|
|
|
self->velocity = self->velocity > self->velocityMax ? self->velocityMax : self->velocity;
|
|
self->velocity = self->velocity < -self->velocityMax ? -self->velocityMax : self->velocity;
|
|
|
|
self->angle += self->velocity;
|
|
|
|
self->angle = self->angle > RADIANS_MAX ? RADIANS_MIN : self->angle;
|
|
self->angle = self->angle < RADIANS_MIN ? RADIANS_MAX : self->angle;
|
|
}
|
|
|
|
/* Sets rotation component values. */
|
|
void
|
|
c_rotation_set(struct CRotation* self, f32 speed, f32 velocityMax, f32 friction)
|
|
{
|
|
self->speed = speed;
|
|
self->velocityMax = velocityMax;
|
|
self->friction = friction;
|
|
}
|
|
|
|
/* Registers rotation component. */
|
|
void
|
|
c_rotation_register(void)
|
|
{
|
|
ecs_register
|
|
(
|
|
ECS_C_ROTATION,
|
|
sizeof(struct CRotation),
|
|
C_ROTATION_LIMIT,
|
|
(ECSFunction)add,
|
|
NULL,
|
|
(ECSFunction)update_f,
|
|
NULL,
|
|
NULL
|
|
);
|
|
}
|