From 3f0be672f9c5a07a98be0dc703b95f1bbe73f33e Mon Sep 17 00:00:00 2001 From: nathansmithsmith Date: Sun, 9 Jul 2023 00:53:10 -0600 Subject: Mouse control working --- src/entities/antifaShip.c | 68 ++++++++++++++++++++++++++++++++++++++++++++--- src/entities/antifaShip.h | 9 +++++-- src/entities/soldato.c | 4 +-- src/entities/soldato.h | 4 +-- 4 files changed, 75 insertions(+), 10 deletions(-) (limited to 'src/entities') 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); } diff --git a/src/entities/antifaShip.h b/src/entities/antifaShip.h index a1b97d2..61d843c 100644 --- a/src/entities/antifaShip.h +++ b/src/entities/antifaShip.h @@ -4,9 +4,14 @@ #ifndef ANTIFA_SHIP_H #define ANTIFA_SHIP_H +typedef struct AntifaShip { + Vector2 lastMouse; + float forwardSpeed; +} AntifaShip; + void initAntifaShip(Entity * entity, Game * game); void closeAntifaShip(Entity * entity); -void updateAntifaShip(Game * game, Entity * entity, EntityId id); -void drawAntifaShip(Game * game, Entity * entity, EntityId id); +void updateAntifaShip(Game * game, Entity * entity); +void drawAntifaShip(Game * game, Entity * entity); #endif diff --git a/src/entities/soldato.c b/src/entities/soldato.c index c1903d4..4abe5af 100644 --- a/src/entities/soldato.c +++ b/src/entities/soldato.c @@ -9,10 +9,10 @@ void initSoldato(Entity * entity, Game * game) { void closeSoldato(Entity * entity) { } -void updateSoldato(Game * game, Entity * entity, EntityId id) { +void updateSoldato(Game * game, Entity * entity) { entityUpdateRotation(entity); } -void drawSoldato(Game * game, Entity * entity, EntityId id) { +void drawSoldato(Game * game, Entity * entity) { entityDraw(entity); } diff --git a/src/entities/soldato.h b/src/entities/soldato.h index 4069b26..6992e00 100644 --- a/src/entities/soldato.h +++ b/src/entities/soldato.h @@ -6,7 +6,7 @@ void initSoldato(Entity * entity, Game * game); void closeSoldato(Entity * entity); -void updateSoldato(Game * game, Entity * entity, EntityId id); -void drawSoldato(Game * game, Entity * entity, EntityId id); +void updateSoldato(Game * game, Entity * entity); +void drawSoldato(Game * game, Entity * entity); #endif -- cgit v1.2.3