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 | |
parent | b92adb44b618db0272819cb77e5727441c566838 (diff) |
Mouse control working
Diffstat (limited to 'src')
-rw-r--r-- | src/PID.c | 51 | ||||
-rw-r--r-- | src/PID.h | 41 | ||||
-rw-r--r-- | src/entities/antifaShip.c | 68 | ||||
-rw-r--r-- | src/entities/antifaShip.h | 9 | ||||
-rw-r--r-- | src/entities/soldato.c | 4 | ||||
-rw-r--r-- | src/entities/soldato.h | 4 | ||||
-rw-r--r-- | src/entity.h | 4 | ||||
-rw-r--r-- | src/game.c | 5 | ||||
-rw-r--r-- | src/game.h | 2 | ||||
-rw-r--r-- | src/settings.c | 9 | ||||
-rw-r--r-- | src/settings.h | 20 | ||||
-rw-r--r-- | src/util.c | 20 | ||||
-rw-r--r-- | src/util.h | 3 | ||||
-rw-r--r-- | src/world.c | 4 |
14 files changed, 229 insertions, 15 deletions
diff --git a/src/PID.c b/src/PID.c new file mode 100644 index 0000000..edecd5e --- /dev/null +++ b/src/PID.c @@ -0,0 +1,51 @@ +#include "PID.h" + +PID createPID(PIDConfig config) { + PID pid = (PID){ + .p = 0.0, + .i = 0.0, + .d = 0.0, + .kP = config.kP, + .kI = config.kI, + .kD = config.kD, + .doClamp = config.doClamp, + .min = config.min, + .max = config.max, + .error = 0.0, + .pastError = 0.0, + .output = 0.0 + }; + + return pid; +} + +float runPID(float setpoint, float processValue, PID * pid) { + // Get error. + pid->error = setpoint - processValue; + + // Set p, i and d. + pid->p = pid->error * pid->kP; + pid->i += pid->error * pid->kI; + pid->d = (pid->error - pid->pastError) * pid->kD; + + // Update error. + pid->pastError = pid->error; + + // Set output. + pid->output = pid->p + pid->i + pid->d; + + // Clamp. + if (pid->doClamp) + pid->output = Clamp(pid->output, pid->min, pid->max); + + return pid->output; +} + +void resetPID(PID * pid) { + pid->p = 0.0; + pid->i = 0.0; + pid->d = 0.0; + pid->error = 0.0; + pid->pastError = 0.0; + pid->output = 0.0; +} diff --git a/src/PID.h b/src/PID.h new file mode 100644 index 0000000..649a113 --- /dev/null +++ b/src/PID.h @@ -0,0 +1,41 @@ +#include "gameCommon.h" + +#ifndef PID_H +#define PID_H + +// Hehehehe! PID CONTROLLER INDEED! + +typedef struct PIDConfig { + float kP; + float kI; + float kD; + + bool doClamp; + float min; + float max; +} PIDConfig; + +typedef struct PID { + float p; + float i; + float d; + + float kP; + float kI; + float kD; + + bool doClamp; + float min; + float max; + + float error; + float pastError; + + float output; +} PID; + +PID createPID(PIDConfig config); +float runPID(float setpoint, float processValue, PID * pid); +void resetPID(PID * pid); + +#endif 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 diff --git a/src/entity.h b/src/entity.h index 128365f..98db0f5 100644 --- a/src/entity.h +++ b/src/entity.h @@ -22,8 +22,8 @@ typedef int16_t EntityId; // Id in world. typedef uint32_t EntityFingerprint; // Callbacks. -typedef void (*EntityUpdateCb)(Game * game, Entity * entity, EntityId id); -typedef void (*EntityDrawCb)(Game * game, Entity * entity, EntityId id); +typedef void (*EntityUpdateCb)(Game * game, Entity * entity); +typedef void (*EntityDrawCb)(Game * game, Entity * entity); // Acceleration indeed hehehe. typedef struct EntityAcceleration { @@ -4,6 +4,9 @@ void initGame(Game * game) { // Window. InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Killa Facsista"); + // Settings. + initSettings(&game->settings); + // Assets. LoadAssets(&game->assets); @@ -17,7 +20,7 @@ void initGame(Game * game) { initPlayerCamera(&game->playerCamera); SetTargetFPS(60); - //DisableCursor(); + DisableCursor(); // World. initWorld(&game->world); @@ -5,6 +5,7 @@ #include "entity.h" #include "assets.h" #include "world.h" +#include "settings.h" #ifndef GAME_H #define GAME_H @@ -21,6 +22,7 @@ typedef struct Game { Camera3D playerCamera; Assets assets; World world; + Settings settings; } Game; void initGame(Game * game); diff --git a/src/settings.c b/src/settings.c new file mode 100644 index 0000000..d436d4a --- /dev/null +++ b/src/settings.c @@ -0,0 +1,9 @@ +#include "settings.h" + +void initSettings(Settings * settings) { + *settings = (Settings){ + .mouseSensitivity = 50.0, + .scrollBarSpeed = 10.0, + .controlMode = KEYBOARD_AND_MOUSE_CONTROL + }; +} diff --git a/src/settings.h b/src/settings.h new file mode 100644 index 0000000..a880a2c --- /dev/null +++ b/src/settings.h @@ -0,0 +1,20 @@ +#include "gameCommon.h" + +#ifndef SETTINGS_H +#define SETTINGS_H + +// What is the player using for control. +typedef enum ControlMode { + JOYSTICK_CONTROL, + KEYBOARD_AND_MOUSE_CONTROL +} ControlMode; + +typedef struct Settings { + float mouseSensitivity; + float scrollBarSpeed; + ControlMode controlMode; +} Settings; + +void initSettings(Settings * settings); + +#endif @@ -3,3 +3,23 @@ AxisAngle AxisAngleIdentity() { return (AxisAngle){Vector3Zero(), 0.0}; } + +float signum(float n) { + if (n > 0.0) + return 1.0; + else if (n < 0.0) + return -1.0; + else + return 0.0; +} + +float closestAngle(float a1, float a2) { + float a = fmodf(a1, PI); + float b = fmodf(a2, PI); + float dir = b - a; + + if (fabsf(dir) > (PI/2)) + dir = -(signum(dir) * PI) + dir; + + return dir; +} @@ -17,4 +17,7 @@ typedef struct AxisAngle { AxisAngle AxisAngleIdentity(); +float signum(float n); +float closestAngle(float a1, float a2); + #endif diff --git a/src/world.c b/src/world.c index 5fd654b..0ec3b32 100644 --- a/src/world.c +++ b/src/world.c @@ -234,7 +234,7 @@ void updateWorld(World * world, Game * game) { // Call update callback. if (entity->updateCb != NULL) - entity->updateCb(game, entity, i); + entity->updateCb(game, entity); } } @@ -247,6 +247,6 @@ void drawWorld(World * world, Game * game) { // Call draw callback. if (entity->drawCb != NULL) - entity->drawCb(game, entity, i); + entity->drawCb(game, entity); } } |