#include "antifaShip.h" #include "game.h" #include "settings.h" // TODO: Get rid of some magic numbers. void initAntifaShip(Entity * entity, Game * game) { entity->model = &game->assets.models[ANTIFA_SHIP_ASSET]; // Acceleration stuff. entity->useAcceleration = true; entity->acceleration = (EntityAcceleration){ .speedUp = 30.0, .speedDown = 15, .rotation = (Vector3){2.0, 2.0, 2.0} }; // Set Data pointer. entity->data = KF_MALLOC(sizeof(AntifaShip)); if (entity->data == NULL) { ALLOCATION_ERROR; return; } AntifaShip * data = (AntifaShip*)entity->data; data->lastMouse = Vector2Zero(); data->forwardSpeed = 0.0; } void closeAntifaShip(Entity * entity) { if (entity->data != NULL) KF_FREE(entity->data); } void controlAntifaShipJoystick(Game * game, Entity * entity) { Settings settings = game->settings; int gamePadNum = settings.gamePadNum; // Get joystick values. float pitchStick = GetGamepadAxisMovement(gamePadNum, settings.pitchStick); float yawStick = GetGamepadAxisMovement(gamePadNum, settings.yawStick); float rollStick = GetGamepadAxisMovement(gamePadNum, settings.rollStick); float speedStick = GetGamepadAxisMovement(gamePadNum, settings.speedStick); Vector3 stick = (Vector3){ pitchStick, -yawStick, rollStick }; stick = Vector3Scale(stick, settings.joystickSensitivity); float speed = fabs(speedStick * ANTIFA_SHIP_MAX_SPEED); entityJoystickControl(entity, stick, speed); } void controlAntifaShipKeyboardAndMouse(Game * game, Entity * entity) { AntifaShip * data = (AntifaShip*)entity->data; // Get mouse values. Vector2 mouse = GetMousePosition(); float speed = GetMouseWheelMove(); data->forwardSpeed += (speed * game->settings.scrollBarSpeed); if (data->forwardSpeed < 0.0) data->forwardSpeed = 0.0; else if (data->forwardSpeed > ANTIFA_SHIP_MAX_SPEED) data->forwardSpeed = ANTIFA_SHIP_MAX_SPEED; Vector2 v = Vector2Subtract(mouse, data->lastMouse); data->lastMouse = mouse; // Using mouse as a joystick. Vector3 mouseStick = (Vector3){ v.y * game->settings.mouseSensitivity, -v.x * game->settings.mouseSensitivity, 0.0, }; // Swap axis for more movement with mouse. if (IsMouseButtonDown(MOUSE_BUTTON_MIDDLE)) { mouseStick.z = -mouseStick.y; mouseStick.y = 0.0; } entityJoystickControl(entity, mouseStick, data->forwardSpeed); } void updateAntifaShip(Game * game, Entity * entity) { switch (game->settings.controlMode) { case JOYSTICK_CONTROL: controlAntifaShipJoystick(game, entity); break; case KEYBOARD_AND_MOUSE_CONTROL: controlAntifaShipKeyboardAndMouse(game, entity); break; default: break; } } void drawAntifaShip(Game * game, Entity * entity) { entityDraw(entity); }