131 lines
2.5 KiB
C
131 lines
2.5 KiB
C
#include "component_control_move.h"
|
|
|
|
/* DEPENDENCIES: Physics */
|
|
|
|
/* Ticks control_move component. */
|
|
void
|
|
component_control_move_init(ComponentControlMove* self, f32 speed)
|
|
{
|
|
self->speed = speed;
|
|
}
|
|
|
|
/* Ticks control_move component. */
|
|
void
|
|
component_control_move_tick(ComponentControlMove* self, ECS* ecs)
|
|
{
|
|
if
|
|
(
|
|
input_held(&ecs->game->input, INPUT_LEFT) ||
|
|
input_held(&ecs->game->input, INPUT_RIGHT) ||
|
|
input_held(&ecs->game->input, INPUT_UP) ||
|
|
input_held(&ecs->game->input, INPUT_DOWN)
|
|
)
|
|
{
|
|
ComponentPhysics* physics;
|
|
f32 angle;
|
|
vec2 velocity;
|
|
|
|
angle = 0.0f;
|
|
|
|
physics = ecs_component_get(ecs, ECS_COMPONENT_PHYSICS, self->id);
|
|
|
|
glm_vec2_zero(velocity);
|
|
|
|
self->previousDirection = self->direction;
|
|
self->direction = DIRECTION_NONE;
|
|
|
|
velocity[0] = 1.0f;
|
|
velocity[1] = 0.0f;
|
|
|
|
glm_vec2_norm(velocity);
|
|
|
|
glm_vec2_scale(velocity, self->speed, velocity);
|
|
|
|
|
|
if
|
|
(
|
|
input_held(&ecs->game->input, INPUT_LEFT) &&
|
|
input_held(&ecs->game->input, INPUT_UP)
|
|
)
|
|
{
|
|
angle = PI + PI_FOURTH;
|
|
self->direction = DIRECTION_NORTH_WEST;
|
|
}
|
|
else if
|
|
(
|
|
input_held(&ecs->game->input, INPUT_RIGHT) &&
|
|
input_held(&ecs->game->input, INPUT_UP)
|
|
)
|
|
{
|
|
angle = TAU - PI_FOURTH;
|
|
self->direction = DIRECTION_NORTH_EAST;
|
|
}
|
|
else if
|
|
(
|
|
input_held(&ecs->game->input, INPUT_DOWN) &&
|
|
input_held(&ecs->game->input, INPUT_LEFT)
|
|
)
|
|
{
|
|
angle = PI - PI_FOURTH;
|
|
self->direction = DIRECTION_SOUTH_WEST;
|
|
}
|
|
else if
|
|
(
|
|
input_held(&ecs->game->input, INPUT_DOWN) &&
|
|
input_held(&ecs->game->input, INPUT_RIGHT)
|
|
)
|
|
{
|
|
angle = PI_FOURTH;
|
|
self->direction = DIRECTION_SOUTH_EAST;
|
|
}
|
|
else if
|
|
(
|
|
input_held(&ecs->game->input, INPUT_LEFT) &&
|
|
input_held(&ecs->game->input, INPUT_RIGHT)
|
|
)
|
|
{
|
|
if
|
|
(
|
|
self->direction =
|
|
self->previousDirection == INPUT_LEFT
|
|
)
|
|
{
|
|
self->direction = DIRECTION_
|
|
}
|
|
}
|
|
else if (input_held(&ecs->game->input, INPUT_LEFT))
|
|
{
|
|
angle = PI;
|
|
self->direction = DIRECTION_WEST;
|
|
}
|
|
else if (input_held(&ecs->game->input, INPUT_RIGHT))
|
|
{
|
|
angle = 0.0f;
|
|
self->direction = DIRECTION_EAST;
|
|
}
|
|
else if (input_held(&ecs->game->input, INPUT_UP))
|
|
{
|
|
angle = PI + PI_HALF;
|
|
self->direction = DIRECTION_NORTH;
|
|
}
|
|
else if (input_held(&ecs->game->input, INPUT_DOWN))
|
|
{
|
|
angle = PI - PI_HALF;
|
|
self->direction = DIRECTION_SOUTH;
|
|
}
|
|
|
|
if (self->direction == DIRECITON_NORTH)
|
|
{
|
|
|
|
|
|
}
|
|
else if (self->direction == DIRECTION
|
|
|
|
glm_vec2_rotate(velocity, angle, velocity);
|
|
|
|
physics->velocity[0] += velocity[0];
|
|
physics->velocity[1] += velocity[1];
|
|
}
|
|
|
|
}
|