diff options
author | nathansmithsmith <thenathansmithsmith@gmail.com> | 2023-07-09 00:53:10 -0600 |
---|---|---|
committer | nathansmithsmith <thenathansmithsmith@gmail.com> | 2023-07-09 00:53:10 -0600 |
commit | 3f0be672f9c5a07a98be0dc703b95f1bbe73f33e (patch) | |
tree | aeb7776878e3460f7ba1f33cf2c0d195eb45f2c5 /src/entities/antifaShip.c | |
parent | b92adb44b618db0272819cb77e5727441c566838 (diff) |
Mouse control working
Diffstat (limited to 'src/entities/antifaShip.c')
-rw-r--r-- | src/entities/antifaShip.c | 68 |
1 files changed, 64 insertions, 4 deletions
diff --git a/src/entities/antifaShip.c b/src/entities/antifaShip.c index f43a7df..7dfbe26 100644 --- a/src/entities/antifaShip.c +++ b/src/entities/antifaShip.c @@ -1,21 +1,38 @@ #include "antifaShip.h" #include "game.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){0.7, 0.7, 0.7} + .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 updateAntifaShip(Game * game, Entity * entity, EntityId id) { +void controlAntifaShipJoystick(Game * game, Entity * entity) { Vector3 stick = (Vector3){ GetGamepadAxisMovement(0, 1), -GetGamepadAxisMovement(0, 0), @@ -23,10 +40,53 @@ void updateAntifaShip(Game * game, Entity * entity, EntityId id) { }; stick = Vector3Scale(stick, 0.5); - entityJoystickControl(entity, stick, fabs(GetGamepadAxisMovement(0, 3) * 300.0)); } -void drawAntifaShip(Game * game, Entity * entity, EntityId id) { +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; + + Vector2 v = Vector2Subtract(mouse, data->lastMouse); + data->lastMouse = mouse; + + // Using mouse as a joystick. + Vector3 mouseStick = (Vector3){ + (v.y / GetScreenHeight()) * game->settings.mouseSensitivity, + (-v.x / GetScreenWidth()) * 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); } |