diff --git a/C++ProjectTemplate b/C++ProjectTemplate index ff203e7..3e86830 100755 Binary files a/C++ProjectTemplate and b/C++ProjectTemplate differ diff --git a/main.cpp b/main.cpp index 7dcf444..ac67b47 100644 --- a/main.cpp +++ b/main.cpp @@ -27,6 +27,7 @@ CurrentCutscene->AdvanceAction(); \ #define 액션 (CutsceneAction*)new +std::vector OBJECTS; using namespace olc; @@ -54,13 +55,16 @@ class Animation{ } }; +vd2d cameraPos = {0,0}; +PixelGameEngine*GAME; + class Object{ private: vd2d scale={1,1}; + vd2d pos; public: int id; Animation*spr; - vd2d pos; int frameIndex=0; int frameCount=0; int animationSpd=12; //How many frames to wait between each frame. Setting to 0 pauses the animation. @@ -73,6 +77,11 @@ class Object{ int objArrElement; //Which element in the object array this object is located in. For sorting purposes. bool temp=false; //If set to true, it's marked for deletion after cutscene handling. //animationSpd is how long to wait before switching frames. + bool Collision(vd2d pos) { + GAME->SetDrawTarget(layer::COLLISION); + Pixel collisionData = GAME->GetDrawTarget()->GetPixel((int)pos.x-cameraPos.x,(int)pos.y-cameraPos.y); + return collisionData!=MAGENTA; + } Object(int id,std::string name,vd2d pos,Animation*spr,vd2d scale={1,1},Pixel color=WHITE,int animationSpd=1,bool temp=false) { this->spr=spr; this->pos=pos; @@ -90,6 +99,107 @@ class Object{ vd2d GetScale() { return scale; } + vd2d GetPos() { + return pos; + } + void Move(vd2d move) { + if (move.y==0) { + pos+=move; + return; + } else { + if (move.y<0) { + if (objArrElement>0&&OBJECTS[objArrElement-1]->pos.y+OBJECTS[objArrElement-1]->originPoint.y>pos.y+originPoint.y+move.y) { + OBJECTS[objArrElement]=OBJECTS[objArrElement-1]; + OBJECTS[objArrElement-1]=this; + OBJECTS[objArrElement]->objArrElement=objArrElement; + objArrElement--; + } + } else { + if (objArrElementpos.y+OBJECTS[objArrElement+1]->originPoint.yobjArrElement=objArrElement; + objArrElement++; + } + } + pos+=move; + } + } + void SetPos(vd2d pos) { + Move(pos-this->pos); + } + void SmoothMove(vd2d move) { + const int wiggleRoom=5; + vd2d originPos = {pos.x+originPoint.x,pos.y-1+originPoint.y}; + if (!Collision(originPos+move)) { + Move(move); + return; + } else + if (move.x>0) { + for (int i=0;i0) { + for (int i=0;icomposition; int baseDmg; //The base damage of the attack. int randomDmg; //Additional random roll damage to add onto the base damage. bool pctDamage; //Uses % damage for the base damage instead of flat damage. std::vector> properties; //The int is used to determine the chance of something occurring. - Move(std::string name,int baseDmg,int randomDmg,int composition[4]={},bool pctDamage=false,std::vector> properties={}) - :name(name),randomDmg(randomDmg),baseDmg(baseDmg),pctDamage(pctDamage),properties(properties){ - for (int i=0;i<4;i++) { - this->composition[i]=composition[i]; - } - } + Move(std::string name,int baseDmg,int randomDmg,std::arraycomposition,bool pctDamage=false,std::vector> properties={}) + :name(name),randomDmg(randomDmg),baseDmg(baseDmg),composition(composition),pctDamage(pctDamage),properties(properties){} }; } @@ -212,7 +318,6 @@ public: int EDITING_LAYER=layer::DYNAMIC; int SELECTED_OBJ_ID = PLAYER; int OBJ_DISPLAY_OFFSET = 0; - vd2d cameraPos = {0,0}; bool GAME_FLAGS[128]={}; Object* PLAYER_OBJ; bool messageBoxVisible=false; @@ -242,10 +347,10 @@ public: std::map SPRITES; std::map ANIMATIONS; std::map OBJ_INFO; - std::vector OBJECTS; bool OnUserCreate() override { + GAME=this; for (int i=1;i<6;i++) { CreateLayer(); EnableLayer(i,true); @@ -386,7 +491,7 @@ goes on a very long time, I hope you can understand this is only for testing pur case ActionType::CREATE_OBJECTS:{ for (auto&obj:((CreateObjects*)CurrentCutscene->GetAction())->GetObjects()) { obj->temp=true; - AddObjectToWorld(CurrentCutscene->AddCutsceneObject(new Object(obj->id,obj->name,obj->pos,obj->spr,obj->GetScale(),obj->color,obj->animationSpd,true))); + AddObjectToWorld(CurrentCutscene->AddCutsceneObject(new Object(obj->id,obj->name,obj->GetPos(),obj->spr,obj->GetScale(),obj->color,obj->animationSpd,true))); } CurrentCutscene->AdvanceAction(); }break; @@ -498,16 +603,16 @@ goes on a very long time, I hope you can understand this is only for testing pur } if (PlayerCanMove()) { if (GetKey(I).bHeld) { - SmoothMove(PLAYER_OBJ,{0,-1}); + PLAYER_OBJ->SmoothMove({0,-1}); } if (GetKey(K).bHeld) { - SmoothMove(PLAYER_OBJ,{0,1}); + PLAYER_OBJ->SmoothMove({0,1}); } if (GetKey(J).bHeld) { - SmoothMove(PLAYER_OBJ,{-1,0}); + PLAYER_OBJ->SmoothMove({-1,0}); } if (GetKey(L).bHeld) { - SmoothMove(PLAYER_OBJ,{1,0}); + PLAYER_OBJ->SmoothMove({1,0}); } } int selectedTileX=(GetMouseX()+cameraPos.x)/32; @@ -693,7 +798,7 @@ goes on a very long time, I hope you can understand this is only for testing pur } else if (EDITING_LAYER==layer::OBJECT&&GetMouse(1).bReleased) { for (int i=0;ipos==HIGHLIGHTED_TILE*32) { + if (OBJECTS[i]->GetPos()==HIGHLIGHTED_TILE*32) { delete OBJECTS[i]; OBJECTS.erase(OBJECTS.begin()+i--); } @@ -723,10 +828,10 @@ goes on a very long time, I hope you can understand this is only for testing pur for (int y=-1;ydrawn&&obj->pos.y+obj->originPoint.y>(y+yTileOffset)*32&&obj->pos.y+obj->originPoint.y<=(y+yTileOffset+1)*32) { + if (!obj->drawn&&obj->GetPos().y+obj->originPoint.y>(y+yTileOffset)*32&&obj->GetPos().y+obj->originPoint.y<=(y+yTileOffset+1)*32) { obj->drawn=true; SetDrawTarget(layer::DYNAMIC); - DrawPartialDecal(obj->pos-cameraPos,obj->spr->spr,{(obj->frameIndex%obj->spr->frames)*obj->spr->width,0},{obj->spr->width,obj->spr->spr->sprite->height},obj->GetScale(),obj->color); + DrawPartialDecal(obj->GetPos()-cameraPos,obj->spr->spr,{(obj->frameIndex%obj->spr->frames)*obj->spr->width,0},{obj->spr->width,obj->spr->spr->sprite->height},obj->GetScale(),obj->color); } } for (int x=-1;xpos.x)+";"+std::to_string(OBJECTS[i]->pos.y)+";"+std::to_string(OBJECTS[i]->id); + const std::string obj="OBJECT"+std::to_string(OBJECTS[i]->GetPos().x)+";"+std::to_string(OBJECTS[i]->GetPos().y)+";"+std::to_string(OBJECTS[i]->id); for (int i=0;iGetPixel((int)pos.x-cameraPos.x,(int)pos.y-cameraPos.y); - return collisionData!=MAGENTA; - } - - void Move(Object*obj,vd2d move) { - if (move.y==0) { - obj->pos+=move; - return; - } else { - if (move.y<0) { - if (obj->objArrElement>0&&OBJECTS[obj->objArrElement-1]->pos.y+OBJECTS[obj->objArrElement-1]->originPoint.y>obj->pos.y+obj->originPoint.y+move.y) { - OBJECTS[obj->objArrElement]=OBJECTS[obj->objArrElement-1]; - OBJECTS[obj->objArrElement-1]=obj; - OBJECTS[obj->objArrElement]->objArrElement=obj->objArrElement; - obj->objArrElement--; - } - } else { - if (obj->objArrElementobjArrElement+1]->pos.y+OBJECTS[obj->objArrElement+1]->originPoint.ypos.y+obj->originPoint.y+move.y) { - OBJECTS[obj->objArrElement]=OBJECTS[obj->objArrElement+1]; - OBJECTS[obj->objArrElement+1]=obj; - OBJECTS[obj->objArrElement]->objArrElement=obj->objArrElement; - obj->objArrElement++; - } - } - obj->pos+=move; - } - } - - void SmoothMove(Object*obj,vd2d move) { - const int wiggleRoom=5; - vd2d originPos = {obj->pos.x+obj->originPoint.x,obj->pos.y-1+obj->originPoint.y}; - if (!Collision(originPos+move)) { - Move(PLAYER_OBJ,move); - return; - } else - if (move.x>0) { - for (int i=0;i0) { - for (int i=0;i::const_iterator it = OBJECTS.begin(); if (obj->id==PLAYER&&!obj->temp) { @@ -1333,7 +1335,7 @@ goes on a very long time, I hope you can understand this is only for testing pur } bool inserted=false; for (int i=0;ipos.y+OBJECTS[i]->originPoint.y>obj->pos.y+obj->originPoint.y) { + if (!inserted&&OBJECTS[i]->GetPos().y+OBJECTS[i]->originPoint.y>obj->GetPos().y+obj->originPoint.y) { OBJECTS.insert(it,obj); obj->objArrElement=i; inserted=true; @@ -1353,7 +1355,7 @@ goes on a very long time, I hope you can understand this is only for testing pur //printf("OBJECTS (%d):\n",OBJECTS.size()); for (int i=0;iobjArrElement) { - printf("%d :: Object %s - %d (%lf,%lf)\n",i,OBJECTS[i]->name.c_str(),OBJECTS[i]->objArrElement,OBJECTS[i]->pos.x,OBJECTS[i]->pos.y); + printf("%d :: Object %s - %d (%lf,%lf)\n",i,OBJECTS[i]->name.c_str(),OBJECTS[i]->objArrElement,OBJECTS[i]->GetPos().x,OBJECTS[i]->GetPos().y); } assert(i==OBJECTS[i]->objArrElement); } @@ -1480,16 +1482,16 @@ goes on a very long time, I hope you can understand this is only for testing pur bool MoveObjectTowardsPoint(Object*obj,vd2d targetPos,PriorityDirection dir,double moveSpd,bool secondRun=false) { bool reachedPosition=true; if (dir==HORZ_FIRST||dir==BOTH) { - if (obj->pos.x!=targetPos.x) { - if (obj->pos.xpos.x+=moveSpd; - if (obj->pos.x>targetPos.x) { - obj->pos.x=targetPos.x; + if (obj->GetPos().x!=targetPos.x) { + if (obj->GetPos().xMove({moveSpd,0}); + if (obj->GetPos().x>targetPos.x) { + obj->SetPos({targetPos.x,obj->GetPos().y}); } } else { - obj->pos.x-=moveSpd; - if (obj->pos.xpos.x=targetPos.x; + obj->Move({-moveSpd,0}); + if (obj->GetPos().xSetPos({targetPos.x,obj->GetPos().y}); } } reachedPosition=false; @@ -1499,16 +1501,16 @@ goes on a very long time, I hope you can understand this is only for testing pur } } if (dir==VERT_FIRST||dir==BOTH) { - if (obj->pos.y!=targetPos.y) { - if (obj->pos.ypos.y+=moveSpd; - if (obj->pos.y>targetPos.y) { - obj->pos.y=targetPos.y; + if (obj->GetPos().y!=targetPos.y) { + if (obj->GetPos().yMove({0,moveSpd}); + if (obj->GetPos().y>targetPos.y) { + obj->SetPos({obj->GetPos().x,targetPos.y}); } } else { - obj->pos.y-=moveSpd; - if (obj->pos.ypos.y=targetPos.y; + obj->Move({0,-moveSpd}); + if (obj->GetPos().ySetPos({obj->GetPos().x,targetPos.y}); } } reachedPosition=false; @@ -1536,7 +1538,12 @@ goes on a very long time, I hope you can understand this is only for testing pur std::vector{new Entity( new Object( NPC1_4,"Test Obj",{pos.x+20,pos.y+48},ANIMATIONS["player.png"] - ),70,70,14,std::array{0,0,0,0},0,std::vector{} + ),70,70,14,std::array{0,0,0,0},0,std::vector{ + new Battle::Move("Test Move 1",30,5,std::array{0,0,0,0}), + new Battle::Move("Test Move 2",30,5,std::array{0,0,0,0}), + new Battle::Move("Test Move 3",30,5,std::array{0,0,0,0}), + new Battle::Move("Test Move 4",30,5,std::array{0,0,0,0}), + } )} ,chance) );