#include <stddef.h>
#include "world.h"

// the beeg one. hooooooooooooooooh boy

const float scale = 43.7445319335f;
const float g = 9.81f * scale;

const float PMV = 10000.0*scale; //Player Max Velocity
const int PLAYER_SPEED_FACTOR =2;
const float JUMP_FORCE = 300.0f*scale;

const float AIR_DRAG = 0.999f;

const float GROUND_FRICTION = 1200.0f;
const float MAX_PLAYER_SPEED = 300.0f;

const float BOUNCE = 0.45f;

const float ground_y = 550.0f;

Entity entities[MAX_ENTITIES];

Entity *player = NULL;

bool isSimulating = true;

float simTime = 0.0f;

void ApplyForce(Entity *e, Vector2 force)
{
    if (!e || !e->active)
        return;

    e->force.x += force.x;
    e->force.y += force.y;
}

bool IsGrounded(Entity *e)
{
    if (!e)
        return false;

    return (
        e->position.y +
        e->size * 0.5f >=
        ground_y - 2.0f
    );
}

Entity *SpawnEntity(
    float x,
    float y,
    float size,
    float mass,
    Color color)
{
    for (int i = 0; i < MAX_ENTITIES; i++)
    {
        if (!entities[i].active)
        {
            Entity *e = &entities[i];

            e->active = true;

            e->isPlayer = false;
            e->affectedByGravity = true;

            e->mass = mass;
            e->size = size;

            e->position = (Vector2){x, y};
            e->velocity = (Vector2){0, 0};
            e->acceleration = (Vector2){0, 0};
            e->force = (Vector2){0, 0};

            e->color = color;

            return e;
        }
    }

    return NULL;
}
// say bye bye to EVERYTHINGGGG AHAHAHAHAHAAH
// im a little mad with power
// this is literally just a helper function to clear the entities and player
void ClearWorld(void)
{
    for (int i = 0; i < MAX_ENTITIES; i++)
    {
        entities[i].active = false;
    }

    player = NULL;
}

// used in main.c to start the actual engine behaviors. spawns the player, and a few other boxes for funsies.
void InitWorld(void)
{
    ClearWorld();

    player = SpawnEntity(
        120,
        120,
        40,
        1.0f,
        BLUE);

    if (player)
    {
        player->isPlayer = true;
    }

    for (int i = 0; i < 8; i++)
    {
        SpawnEntity(
            300 + i * 50,
            50,
            25,
            1.0f,
            RED);
    }

    simTime = 0.0f;
}

// helper function: count number of entities. Something somewhere uses this. I think it's probably for entity counting. Don't quote me on that.
int CountEntities(void)
{
    int count = 0;

    for (int i = 0; i < MAX_ENTITIES; i++)
    {
        if (entities[i].active)
            count++;
    }

    return count;
}
