File WfPlayerStart.c

File List > entities > WfPlayerStart.c

Go to the documentation of this file

#include "WfPlayerStart.h"
#include "Entities.h"
#include "BinarySerializer.h"
#include "Game2DLayer.h"
#include "ObjectPool.h"
#include "WfPlayer.h"
#include "GameFramework.h"
#include "WfWorld.h"
#include "string.h"
#include "Log.h"
#include "Network.h"

struct WfPlayerStartData
{
    char from[64];
    char thisLocation[64];
};

static HEntity2D hCurrentLocalPlayer = NULL_HANDLE;

static OBJECT_POOL(struct WfPlayerStartData) gPlayerStartDataPool;

HEntity2D WfGetCurrentLocalPlayer()
{
    return hCurrentLocalPlayer;
}

void WfInitPlayerStart()
{
    gPlayerStartDataPool = NEW_OBJECT_POOL(struct WfPlayerStartData, 32);
}

void WfPlayerStartEntityOnInit(struct Entity2D* pEnt, struct GameFrameworkLayer* pLayer, DrawContext* pDrawCtx, InputContext* pInputCtx)
{
    struct GameLayer2DData* pLayerData = pLayer->userData;
    Entity2DOnInit(pEnt, pLayer, pDrawCtx, pInputCtx);
    Log_Info("init playerstart entity");

    if(strcmp(WfWorld_GetCurrentLocationName(), gPlayerStartDataPool[pEnt->user.hData].from) == 0)
    {
        Log_Info("Spawning local player");
        struct Entity2D ent;
        ent.user.hData = NULL_HANDLE;
        ent.nextSibling = NULL_HANDLE;
        ent.previousSibling = NULL_HANDLE;
        WfMakeIntoPlayerEntity(&ent, pLayer, pEnt->transform.position);
        hCurrentLocalPlayer = Et2D_AddEntity(&pLayerData->entities, &ent);
        Log_Info("Local player entity ID %i, net id %i", hCurrentLocalPlayer, Et2D_GetEntity(&pLayerData->entities, hCurrentLocalPlayer)->networkID);
        WfWorld_SetCurrentLocationName(gPlayerStartDataPool[pEnt->user.hData].thisLocation);
    }
}

void WfPlayerStartEntityOnDestroy(struct Entity2D* pEnt, struct GameFrameworkLayer* pData)
{
    FreeObjectPoolIndex(gPlayerStartDataPool, pEnt->user.hData);
    Entity2DOnDestroy(pEnt, pData);
}

void WfDeSerializePlayerStartEntityV1(struct BinarySerializer* bs, struct Entity2D* pOutEnt, struct GameLayer2DData* pData)
{
    gPlayerStartDataPool = GetObjectPoolIndex(gPlayerStartDataPool, &pOutEnt->user.hData);
    BS_DeSerializeStringInto(gPlayerStartDataPool[pOutEnt->user.hData].from, bs);

    BS_DeSerializeStringInto(gPlayerStartDataPool[pOutEnt->user.hData].thisLocation, bs);
    HGeneric hPlayer = pOutEnt->user.hData;
    pOutEnt->init = &WfPlayerStartEntityOnInit;
    pOutEnt->bKeepInDynamicList = false;
    pOutEnt->bKeepInQuadtree = false;
    pOutEnt->bSerializeToDisk = true;
    pOutEnt->bSerializeToNetwork = true;
}

void WfDeSerializePlayerStartEntity(struct BinarySerializer* bs, struct Entity2D* pOutEnt, struct GameLayer2DData* pData)
{
    u32 version;
    BS_DeSerializeU32(&version, bs);
    switch (version)
    {
    case 1:
        /* code */
        WfDeSerializePlayerStartEntityV1(bs, pOutEnt, pData);
        break;   
    default:
        break;
    }
}

void WfSerializePlayerStartEntity(struct BinarySerializer* bs, struct Entity2D* pInEnt, struct GameLayer2DData* pData)
{
    BS_SerializeU32(1, bs); // version
    BS_SerializeString(gPlayerStartDataPool[pInEnt->user.hData].from, bs);
    BS_SerializeString(gPlayerStartDataPool[pInEnt->user.hData].thisLocation, bs);
}