157 lines
3.6 KiB
C++
157 lines
3.6 KiB
C++
#include <array>
|
|
#include <cmath>
|
|
|
|
#include "App.hpp"
|
|
#include "Math.hpp"
|
|
|
|
#include <raylib.h>
|
|
#include <raymath.h>
|
|
|
|
#define TOP 1 - pos
|
|
#define BOTTOM pos
|
|
|
|
void App::init(int screenWidth, int screenHeight)
|
|
{
|
|
this->screenWidth = screenWidth;
|
|
this->screenHeight = screenHeight;
|
|
this->canvas.init(screenWidth);
|
|
|
|
for (size_t i = 0; i < canvasTexure.size(); i++)
|
|
{
|
|
canvasTexure[i] = LoadRenderTexture(screenWidth, screenWidth);
|
|
}
|
|
|
|
manager.init();
|
|
upTex(Liked::tbd);
|
|
while (!canvas.tick(canvasTexure[TOP]))
|
|
{
|
|
// wait to finish drawing
|
|
}
|
|
pos = 1 - pos;
|
|
upTex(Liked::tbd);
|
|
while (!canvas.tick(canvasTexure[TOP]))
|
|
{
|
|
// wait to finish drawing
|
|
}
|
|
pos = 1 - pos;
|
|
|
|
float posY = (screenHeight - screenWidth) / 2.0f;
|
|
|
|
destB = {0, posY, (float)screenWidth, (float)screenWidth};
|
|
destA = destB;
|
|
|
|
float recPosX = screenWidth * 0.2f;
|
|
disLikeBox = {0, posY, (float)recPosX, (float)screenWidth};
|
|
likeBox = {screenWidth - recPosX, posY, (float)recPosX, (float)screenWidth};
|
|
}
|
|
|
|
void App::upTex(Liked liked)
|
|
{
|
|
if (liked != Liked::tbd)
|
|
{
|
|
unit[TOP].liked = liked;
|
|
manager.like(unit[TOP]);
|
|
}
|
|
unit[TOP] = manager.next();
|
|
if (unit[TOP].dna != nullptr)
|
|
{
|
|
canvas.newGen(canvasTexure[TOP], unit[TOP].dna);
|
|
return;
|
|
}
|
|
|
|
BeginTextureMode(canvasTexure[TOP]);
|
|
ClearBackground(BLACK);
|
|
DrawText("NEXT GEN", 10, 10, 20, WHITE);
|
|
EndTextureMode();
|
|
}
|
|
|
|
void App::update()
|
|
{
|
|
bool isDone = canvas.tick(canvasTexure[BOTTOM]);
|
|
Vector2 mousePosition = GetMousePosition();
|
|
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
|
|
{
|
|
mouseStart = mousePosition;
|
|
validHit = CheckCollisionPointRec(mouseStart, destA);
|
|
len = Vector2Distance(mouseStart, {destB.x, destB.y});
|
|
ofset = std::atan2(destB.x - mouseStart.x, destB.y - mouseStart.y);
|
|
}
|
|
|
|
if (IsMouseButtonDown(MOUSE_BUTTON_LEFT) && validHit)
|
|
{
|
|
float dist = mousePosition.x - mouseStart.x;
|
|
float l = dist / screenWidth;
|
|
rotation = Lerp(45.0f, -45.0f, (l + 1) / 2);
|
|
|
|
float angle = ((rotation)*PI) / 180.0f;
|
|
angle += ofset;
|
|
Vector2 newCenter = {.x = len * std::sin(angle), .y = len * std::cos(angle)};
|
|
|
|
destA.x = newCenter.x + mousePosition.x;
|
|
destA.y = newCenter.y + mousePosition.y;
|
|
|
|
if (CheckCollisionPointRec(mousePosition, disLikeBox))
|
|
{
|
|
topLiked = Liked::no;
|
|
}
|
|
else if (CheckCollisionPointRec(mousePosition, likeBox))
|
|
{
|
|
topLiked = Liked::yes;
|
|
}
|
|
else
|
|
{
|
|
topLiked = Liked::tbd;
|
|
}
|
|
}
|
|
|
|
if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT) && validHit)
|
|
{
|
|
if (isDone)
|
|
{
|
|
if (topLiked != Liked::tbd)
|
|
{
|
|
upTex(topLiked);
|
|
pos = 1 - pos; // switch bottom and top
|
|
}
|
|
}
|
|
rotation = 0.0f;
|
|
destA = destB;
|
|
}
|
|
}
|
|
|
|
void App::draw()
|
|
{
|
|
ClearBackground(BLUE);
|
|
|
|
Rectangle source = {0, 0, (float)screenWidth, (float)-screenWidth};
|
|
Vector2 origin = {0.0f, 0.0f};
|
|
|
|
DrawTexturePro(canvasTexure[BOTTOM].texture, source, destB, origin, 0.0f, WHITE);
|
|
|
|
DrawTexturePro(canvasTexure[TOP].texture, source, destA, origin, 360 - rotation, WHITE);
|
|
|
|
const char *text = TextFormat("GEN %d: %d / %d", manager.generation, unit[TOP].index + 1, NUM_PER_GEN);
|
|
DrawText(text, destB.x + 10, destB.y - 30, 20, BLACK);
|
|
|
|
switch (topLiked)
|
|
{
|
|
case Liked::yes:
|
|
DrawText("LIKED", destB.x + 10, destB.y - 50, 20, BLACK);
|
|
break;
|
|
case Liked::no:
|
|
DrawText("DISLIKE", destB.x + 10, destB.y - 50, 20, BLACK);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
void App::deinit()
|
|
{
|
|
for (size_t i = 0; i < canvasTexure.size(); i++)
|
|
{
|
|
UnloadRenderTexture(canvasTexure[i]);
|
|
}
|
|
canvas.deinit();
|
|
manager.deinit();
|
|
} |