+#include "state.h"
+
+#define GLFW_INCLUDE_NONE
+#include <GLFW/glfw3.h>
+#include <random>
+#include <memory>
+
+#include "animator.h"
+#include "eye.h"
+#include "mouth.h"
+#include "window.h"
+
+constexpr double irritationPerSecond = 1;
+constexpr double irritationThresh = 5;
+
+struct {
+ double leftEyeIrritate;
+ double rightEyeIrritate;
+
+ std::unique_ptr<animator> blinkAnimator;
+ std::random_device rd;
+} state;
+
+void initEyeIrritation() {
+ if (state.blinkAnimator)
+ state.blinkAnimator.release();
+
+ auto initIrr = state.rd() * 0.8 * irritationThresh / state.rd.max();
+
+ state.leftEyeIrritate = initIrr;
+ state.rightEyeIrritate = initIrr;
+ state.blinkAnimator.release();
+}
+
+void initState() {
+ initEyes();
+ initMouth();
+
+ initEyeIrritation();
+}
+
+void closeOnTouched() {
+ double x, y;
+ glfwGetCursorPos(window, &x, &y);
+ int close = 0;
+
+ if (x < SCREENWIDTH/2) {
+ close = 0;
+ } else {
+ close = 1;
+ }
+ eyes[close].blinkFrame = closed;
+ eyes[1-close].blinkFrame = blink_30;
+}
+
+void updateEyeState(double deltaTime) {
+ state.leftEyeIrritate += deltaTime*irritationPerSecond;
+ state.rightEyeIrritate += deltaTime*irritationPerSecond;
+
+ auto press = glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_1);
+
+ // overwrite when touched
+ if (press == GLFW_PRESS) {
+ closeOnTouched();
+ state.leftEyeIrritate = irritationThresh*0.9;
+ state.rightEyeIrritate = irritationThresh*0.9;
+ return;
+ }
+
+ if (state.blinkAnimator) {
+ state.blinkAnimator->update(deltaTime);
+ auto blinkRatio = state.blinkAnimator->getRatio();
+ auto blinkFrame = ratioToBlink(blinkRatio);
+ eyes[0].blinkFrame = blinkFrame;
+ eyes[1].blinkFrame = blinkFrame;
+
+ if (state.blinkAnimator->done()) {
+ initEyeIrritation();
+ }
+ } else if (state.leftEyeIrritate > irritationThresh || state.rightEyeIrritate > irritationThresh) {
+ state.blinkAnimator = std::make_unique<animator>(0.2);
+ } else {
+ eyes[0].blinkFrame = open;
+ eyes[1].blinkFrame = open;
+ }
+}
+
+void updateState(double deltaTime) {
+ updateEyeState(deltaTime);
+}