#include "antifaShip.h" #include "game.h" #include "settings.h" #include "bullets.h" #include "assets.h" void initAntifaShip(Entity * entity, Game * game) { entity->model = &game->assets.models[ANTIFA_SHIP_ASSET]; entity->collisionModel = entityCreateCollisionModel(*entity->model); entity->transformedCollisionModel = entityCreateCollisionModel(*entity->model); setEntityRadius(entity); // Acceleration stuff. entity->useAcceleration = true; entity->acceleration = (EntityAcceleration){ .speedUp = 30.0, .speedDown = 15, .rotation = (Vector3){2000000.0, 2000000.0, 2000000.0} }; // Set Data pointer. entity->data = KF_MALLOC(sizeof(AntifaShip)); if (entity->data == NULL) { ALLOCATION_ERROR; return; } AntifaShip * data = (AntifaShip*)entity->data; data->lastMouse = GetMousePosition(); data->forwardSpeed = 0.0; data->shouldInitMousePosition = true; } void closeAntifaShip(Entity * entity) { if (entity->data != NULL) KF_FREE(entity->data); entityFreeCollisionModel(entity->collisionModel); entityFreeCollisionModel(entity->transformedCollisionModel); } 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); // Shoot button. if (IsGamepadButtonPressed(gamePadNum, 8)) { Bullet bullet = createBulletFromEntity(*entity, 1.0); BulletHitInfo info = shootBullet(&game->world, bullet); if (info.hit) { Entity * hitEntity = getEntityFromWorld(game->world, info.hitId); printVector3(hitEntity->position); } else { puts("no stink"); } } 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; // Resets mouse on init. if (data->shouldInitMousePosition) { data->shouldInitMousePosition = false; SetMousePosition(0, 0); } // Get mouse values. Vector2 mouse = GetMousePosition(); float speed = GetMouseWheelMove(); data->forwardSpeed += (speed * game->settings.scrollBarSpeed); if (data->forwardSpeed < 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, -v.x, 0.0}; mouseStick = Vector3Scale(mouseStick, game->settings.mouseSensitivity); // 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) { entityUpdateLastValues(entity); switch (game->settings.controlMode) { case JOYSTICK_CONTROL: controlAntifaShipJoystick(game, entity); break; case KEYBOARD_AND_MOUSE_CONTROL: controlAntifaShipKeyboardAndMouse(game, entity); break; default: break; } //printf("%f\n", entity->health); entityCheckTransformedCollisionModel(entity); } void drawAntifaShip(Game * game, Entity * entity) { entityDraw(entity); }