dos-vga-arena-shooter-game/movement.c

110 lines
3.5 KiB
C

#include <math.h>
#include "const.h"
#include "movement.h"
#include "system/mouse_io.h"
#include "system/pc_stuff.h"
void captureAndLimitMousePosition(
struct RabbitPosition *pos,
struct MouseStatus *mouseStatus
) {
pos->oldMouseDotPosition[0] = pos->mouseDotPosition[0];
pos->oldMouseDotPosition[1] = pos->mouseDotPosition[1];
pos->mouseDotPosition[0] = mouseStatus->xPosition;
pos->mouseDotPosition[1] = mouseStatus->yPosition;
if (pos->mouseDotPosition[0] < MOUSE_LIMIT_LEFT) pos->mouseDotPosition[0] = MOUSE_LIMIT_LEFT;
if (pos->mouseDotPosition[0] >= MOUSE_LIMIT_RIGHT) pos->mouseDotPosition[0] = MOUSE_LIMIT_RIGHT;
if (pos->mouseDotPosition[1] < MOUSE_LIMIT_TOP) pos->mouseDotPosition[1] = MOUSE_LIMIT_TOP;
if (pos->mouseDotPosition[1] >= MOUSE_LIMIT_BOTTOM) pos->mouseDotPosition[1] = MOUSE_LIMIT_BOTTOM;
}
void calculateTargetAngle(
struct RabbitPosition *pos,
struct MouseStatus *mouseStatus
) {
float distanceX, distanceY;
float angle;
// Position the cursor
distanceX = pos->mouseDotPosition[0] - pos->rabbitPosition[0];
distanceY = pos->mouseDotPosition[1] - pos->rabbitPosition[1];
angle = atan2(distanceY, distanceX) * RAD2DEG + 90;
if (angle < 0) angle += 360;
distanceX = sin(angle * DEG2RAD) * MOUSE_DISTANCE;
distanceY = -cos(angle * DEG2RAD) * MOUSE_DISTANCE;
pos->oldMousePosition[0] = pos->mousePosition[0];
pos->oldMousePosition[1] = pos->mousePosition[1];
pos->mousePosition[0] = pos->rabbitPosition[0] + distanceX;
pos->mousePosition[1] = pos->rabbitPosition[1] + distanceY;
// get the shot angle
distanceX = pos->mouseDotPosition[0] - pos->rabbitPosition[0];
distanceY = pos->mouseDotPosition[1] - pos->rabbitPosition[1];
angle = atan2(distanceY, distanceX) * RAD2DEG + 90;
if (angle < 0) angle += 360;
pos->mouseAngle = angle;
}
void handleRabbitMovement(
struct RabbitPosition *pos,
struct KeyboardKeydownState *keyboardKeydownState
) {
int i;
if (keyboardKeydownState->KEY_W) {
pos->rabbitVelocity[1] -= RABBIT_MOTION_ACCELERATION;
if (pos->rabbitVelocity[1] < -RABBIT_MOTION_MAX_SPEED) {
pos->rabbitVelocity[1] = -RABBIT_MOTION_MAX_SPEED;
}
}
if (keyboardKeydownState->KEY_S) {
pos->rabbitVelocity[1] += RABBIT_MOTION_ACCELERATION;
if (pos->rabbitVelocity[1] > RABBIT_MOTION_MAX_SPEED) {
pos->rabbitVelocity[1] = RABBIT_MOTION_MAX_SPEED;
}
}
if (keyboardKeydownState->KEY_A) {
pos->rabbitVelocity[0] -= RABBIT_MOTION_ACCELERATION;
if (pos->rabbitVelocity[0] < -RABBIT_MOTION_MAX_SPEED) {
pos->rabbitVelocity[0] = -RABBIT_MOTION_MAX_SPEED;
}
}
if (keyboardKeydownState->KEY_D) {
pos->rabbitVelocity[0] += RABBIT_MOTION_ACCELERATION;
if (pos->rabbitVelocity[0] > RABBIT_MOTION_MAX_SPEED) {
pos->rabbitVelocity[0] = RABBIT_MOTION_MAX_SPEED;
}
}
for (i = 0; i < 2; ++i) {
pos->oldRabbitPosition[i] = pos->rabbitPosition[i];
pos->rabbitPosition[i] += (pos->rabbitVelocity[i] / RABBIT_MOTION_VELOCITY_DECAY);
if (pos->rabbitPosition[i] < pos->rabbitLimits[0][i]) {
pos->rabbitPosition[i] = pos->rabbitLimits[0][i];
}
if (pos->rabbitPosition[i] >= pos->rabbitLimits[1][i]) {
pos->rabbitPosition[i] = pos->rabbitLimits[1][i];
}
if (pos->rabbitVelocity[i] < 0) {
pos->rabbitVelocity[i] += RABBIT_MOTION_DRAG;
if (pos->rabbitVelocity[i] > 0) pos->rabbitVelocity[i] = 0;
}
if (pos->rabbitVelocity[i] > 0) {
pos->rabbitVelocity[i] -= RABBIT_MOTION_DRAG;
if (pos->rabbitVelocity[i] < 0) pos->rabbitVelocity[i] = 0;
}
}
}