#pragma once
#include "global.h"
#include "MyStructDef.h"
#include "GamePid.h"
#include "EntityDll.h"
#include "PathfindingE.h"
//实体资源类OcStore版
struct GroupByDraw
{
	MyRect src, dst;
	DWORD color;
};

class EntityRes
{
public:
	//GameFunc
	static std::unordered_map<MapLayer*, EntityLayer>* GetLoadedEntity(EntityRes* entityRes);
	//EntityRes
	EntityRes();
	virtual ~EntityRes();
	EntityRes(const EntityRes&) = delete;//禁止对象实例复制
	EntityRes& operator=(const EntityRes&) = delete;//禁止对象实例复制
	void Init(MyDX11Renderer* pRenderer, MyView* pView, int* pMapW, int* pMapH);
	void Reset();//重置
	void RefreshImage();
	void StepEntityLayer(MyMapLayer* pLayer);//执行一个实体层
	void StepEntityLayerPe(MyMapLayer* pLayer);//只执行该实体层中的效果实体
	void DrawEntityLayer(MyMapLayer* pLayer, bool bApplyMyFar, bool inTopDraw = false);//绘制一个实体层
	void DeltaTimeEntityLayer(MyMapLayer* pLayer, float deltaTime);//执行一个实体层
	void DrawAddEntity(std::shared_ptr<Entity>& we, bool inTopDraw = false);
	void DrawShaderEntity(std::shared_ptr<Entity>& we, bool inTopDraw = false);//绘制着色器实体
	void DrawEntity();
	void DrawEntityText(std::weak_ptr<Entity> e, DWORD color, bool inTopDraw = false);//绘制实体文本
	void DrawEntityCbRectLayer(MyMapLayer* pLayer);//绘制实体碰撞框
	void EntityInitCheck(std::shared_ptr<Entity>& we);
	void EntitySharedPtrCheck(std::shared_ptr<Entity>& we);
	void StepList(std::list<std::shared_ptr<Entity>>* entityList);
	void GetLoadedEntityDllInfo(std::unordered_map<std::wstring, std::vector<std::wstring>>* infoList);
	void AmStep(EntityLayer* entityLayer);//弹药抛射物的碰撞
	bool AmTarget(std::shared_ptr<Entity>& am, std::shared_ptr<Entity>& target);
	static void CollisionEntity(std::weak_ptr<Entity> e, std::vector<LONG>* pxmArr, std::vector<int>* pxaArr, std::map<LONG, LONG>* updatedPxmArr, int pxmArr_w, int pxaArr_w);
	static void CollisionEntityBig(std::weak_ptr<Entity> e, std::vector<LONG>* pxmArr, std::vector<int>* pxaArr, std::map<LONG, LONG>* updatedPxmArr, int pxmArr_w, int pxaArr_w);
	static void CollisionEntitySmall(std::weak_ptr<Entity> e, std::vector<LONG>* pxmArr, std::vector<int>* pxaArr, std::map<LONG, LONG>* updatedPxmArr, int pxmArr_w, int pxaArr_w);
	void CollisionMovingPlatform(std::weak_ptr<Entity> e);
	static void SetEntity(std::shared_ptr<Entity> parent, std::shared_ptr<Entity> newE);
	void SetCollisionEntityLayer(MyMapLayer* pLayer, PxaData* pxaData);
	CString entityLanguage;
	void ReloadLanguage();//重载语言部分
	CString GetErrorMsg();
	ID3D11VertexShader* GetVertexShader(const wchar_t* vsName);//取顶点着色器
	ID3D11PixelShader* GetPixelShader(const wchar_t* psName);//取像素着色器
	static void GetDisplayInfo(EntityRes* pEntityRes, float* outWindow_w, float* outWindow_h, float* outScale, float* outOffX, float* outOffY);
	static void GetMousePos(EntityRes* pEntityRes, float* pX, float* pY);
	static bool InScreen(EntityRes* pEntityRes, float x, float y);//该坐标是否在屏幕显示范围内
	static void GridTouch(EntityRes* entityRes, std::weak_ptr<Entity> src, EntityLayer* targetLayer);//取与这个实体框接触的所有实体
	void ClearLightList();//清除上一次的发光实体记录
	void DrawLightEntity(MyMapLayer* pLayer, bool playing);//绘制发光实体
	void MarkLightEntity(MyMapLayer* pLayer);//标记当前层所有的灯光实体
    static PathCell* GetPath(std::weak_ptr<Entity> e, float dstX, float dstY, float srcX, float srcY);
    static bool LineTest(std::weak_ptr<Entity> e, float srcX, float srcY, float dstX, float dstY);
	void UpdatePathFinder();
	static void GetMapSize(EntityRes* pEntityRes, int* outMapW, int* outMapH);
	void GridRemoveEntity(std::shared_ptr<Entity>& e);
	void GridSetEntity(std::shared_ptr<Entity>& e);
private:
	MyDX11Renderer* pRenderer = nullptr;
	MyView* pView = nullptr;
    int* pMapW = nullptr;
    int* pMapH = nullptr;
	GamePid gamePid;
	std::unordered_map<MyMapLayer*, EntityLayer> loadedEntity;
	std::unordered_map<std::wstring, MyTextureAndSRV> loadedPidTexture;
	TTF_Font* myfont14 = nullptr;
	std::unordered_map<wchar_t, MyFontData> fontMap_myfont14;
	std::unordered_map<std::wstring, std::vector<GroupByDraw>> groupByDraw;
	std::vector<MyDX11PenVertex> lineList;
	std::vector<MyDX11PenVertex> fillList;
	MyTextureAndSRV* GetPidTexture(CString pidName);
	EntityLayer* GetEntityLayer(MyMapLayer* pLayer);
	std::weak_ptr<Entity> LoadEntity(const EntityLoad* el, MyMapLayer* pLayer, EntityLayer* entityLayer);
	void LoadEntityDll();
	static std::shared_ptr<Entity> GameCreateEntity(const wchar_t* name);
	void EntityAniStep(std::shared_ptr<Entity>& we);
	void ClearTexture(std::unordered_map<std::wstring, MyTextureAndSRV>* textureRes);
	CString err;
	void DrawAddEntityCbRect(std::weak_ptr<Entity> e, DWORD c);
	//着色器
	std::unordered_map<std::wstring, ID3D11VertexShader*> loadedVertexShader;//载入的顶点着色器
	std::unordered_map<std::wstring, ID3D11PixelShader*> loadedPixelShader;//载入的像素着色器
	//实体预渲染
	ExTextureBuffer preTexture;
	ID3D11Texture2D* GetPreTexture(UINT w, UINT h);
	//光线实体
	std::vector<std::weak_ptr<Entity>> lightList;//光线实体
	//碰撞优化
	void GridItemTarget(std::shared_ptr<Entity>& we);
	bool GridAmTarget(std::shared_ptr<Entity>& we);
    //寻路系统
    std::unordered_map<EntityLayer*, PathfindingE> pfSys;
	//记录解码
	static void DecodeRecord(const wchar_t* recordStr, const void** outInputArr, size_t* outDataSize);
};
