diff --git a/C++ProjectTemplate b/C++ProjectTemplate index 7b9af50..a9b096f 100755 Binary files a/C++ProjectTemplate and b/C++ProjectTemplate differ diff --git a/SeasonI.h b/SeasonI.h index 2e2654e..84200ac 100644 --- a/SeasonI.h +++ b/SeasonI.h @@ -140,7 +140,7 @@ class SeasonI:public PixelGameEngine{ float ReadFloatFromStream(std::ifstream&f); double ReadDoubleFromStream(std::ifstream&f); std::string ReadStringFromStream(std::ifstream&f); - void CenterCameraOnPlayer(); + void CenterCameraOnPlayer(); 됐어 int GetPointQuadrantRelativeToLine(vi2d x1y1,vi2d x2y2,vi2d point); TILE GetSafeTileData(std::vector>&data); char GetTileDegreeSafely(std::vector>&data); @@ -149,7 +149,9 @@ class SeasonI:public PixelGameEngine{ void ReleaseTestKey(Key k); 됐어 void PressTestKeyAndRun(Key k); 됐어 void ResetTestKeys(); 됐어 - void SetupGameDrawing(); + void SetupGameDrawing(); 됐어 + void UpdateCollisionGrid(); + void UpdateCamera(vd2d newpos); }; extern SeasonI*GAME; #endif \ No newline at end of file diff --git a/main.cpp b/main.cpp index 23e761d..d27991c 100644 --- a/main.cpp +++ b/main.cpp @@ -40,6 +40,7 @@ std::vector OBJECTS; const vd2d NO_NEIGHBOR = {-999,-999}; SeasonI*GAME; +//THIS VARIABLE SHOULD NOT BE UPDATED DIRECTLY! Use UpdateCamera(pos) to update the camera position! vd2d cameraPos={0,0}; std::vector PARTY_INVENTORY; @@ -1389,7 +1390,7 @@ void SeasonI::updateGame(){ if (movementComponents.mag()>0) { movementComponents=movementComponents.norm(); PARTY_MEMBER_OBJ[0]->frameIndex+=frameCount%PARTY_MEMBER_OBJ[0]->animationSpd==0; - if (PARTY_MEMBER_OBJ[0]->SmoothMove(movementComponents)) {` + if (PARTY_MEMBER_OBJ[0]->SmoothMove(movementComponents)) { UpdatePlayerTrail(movementComponents,facingDir); for (int i=0;itriggers.size();i++) { if (CURRENT_MAP->triggers[i]->IsInside(PARTY_MEMBER_OBJ[0]->GetPosWithOrigin())) { @@ -4245,24 +4246,28 @@ void SeasonI::AdvanceMessageBox() { } void SeasonI::cameraUpdate() { + vd2d adjustCamera=cameraPos; switch (GAME_STATE) { case GameState::EDITOR: case GameState::MAP_POSITION_SELECT:{ //CAMERA MOVEMENTS MUST BE LAST!!! if (UpHeld()) { - cameraPos.y-=CAMERA_MOVESPD; + adjustCamera.y-=CAMERA_MOVESPD; } if (RightHeld()) { - cameraPos.x+=CAMERA_MOVESPD; + adjustCamera.x+=CAMERA_MOVESPD; } if (LeftHeld()) { - cameraPos.x-=CAMERA_MOVESPD; + adjustCamera.x-=CAMERA_MOVESPD; } if (DownHeld()) { - cameraPos.y+=CAMERA_MOVESPD; + adjustCamera.y+=CAMERA_MOVESPD; } }break; } + if (adjustCamera!=cameraPos) { + UpdateCamera(adjustCamera); + } } void SeasonI::StartEffect(Effect*eff) { @@ -4608,9 +4613,26 @@ void SeasonI::DrawRollingCounter(const olc::vi2d &pos,int val,int*rollcounter,in DrawPartialDecal({(float)(pos.x+41),(float)(pos.y-1)},{7,9},SPRITES["rollingcounter.png"],{0,(float)(rollcounter[0]*13+rolloffset[0]+13)},{7,9},Pixel(255,255,255,boxAlpha)); } +void SeasonI::UpdateCollisionGrid() { + for (int x=-1;x=0&&x+xTileOffset=0&&y+yTileOffsettileX!=15||MAP5[y+yTileOffset][x+xTileOffset]->tileY!=15) { + SetDrawTarget(layer::COLLISION); + DrawPartialSprite({(int)(x*32-fmod(cameraPos.x,32)),(int)(y*32-fmod(cameraPos.y,32))},CURRENT_MAP->tileset->sprite,{(int)(MAP5[y+yTileOffset][x+xTileOffset]->tileX*32),(int)(MAP5[y+yTileOffset][x+xTileOffset]->tileY*32)},{32,32}); + tiles|=1<<5; + } + } + } + } +} + void SeasonI::DrawGameWorld() { for (int y=-1;ydrawn&&(!obj->dead||EDITING_LAYER==layer::ENCOUNTER)&& (GetTileDegreeSafely(MAP2)==0&& @@ -5002,18 +5024,21 @@ void SeasonI::DrawArrow() { bool SeasonI::MoveCameraTowardsPoint(vd2d targetPos,PriorityDirection dir,double spd,bool secondRun) { bool reachedPosition=true; + vd2d newCameraPos=cameraPos; if (dir==HORZ_FIRST||dir==BOTH) { - if (cameraPos.x!=targetPos.x) { - if (cameraPos.xtargetPos.x) { - cameraPos.x=targetPos.x; + if (newCameraPos.x!=targetPos.x) { + if (newCameraPos.xtargetPos.x) { + newCameraPos.x=targetPos.x; } + UpdateCamera(newCameraPos); } else { - cameraPos.x-=spd; - if (cameraPos.xtargetPos.y) { - cameraPos.y=targetPos.y; + if (newCameraPos.y!=targetPos.y) { + if (newCameraPos.ytargetPos.y) { + newCameraPos.y=targetPos.y; } + UpdateCamera(newCameraPos); } else { - cameraPos.y-=spd; - if (cameraPos.yGetPos()+PARTY_MEMBER_OBJ[0]->originPoint/2-cameraOffset; + UpdateCamera(PARTY_MEMBER_OBJ[0]->GetPos()+PARTY_MEMBER_OBJ[0]->originPoint/2-cameraOffset); } int SeasonI::GetPointQuadrantRelativeToLine(vi2d x1y1,vi2d x2y2,vi2d point){ diff --git a/object.h b/object.h index 8a964f7..5d533d5 100644 --- a/object.h +++ b/object.h @@ -61,6 +61,7 @@ class Object{ bool Collision(vd2d pos) { GAME->SetDrawTarget(layer::COLLISION); Pixel collisionData = GAME->GetDrawTarget()->GetPixel((int)pos.x-cameraPos.x,(int)pos.y-cameraPos.y); + Pixel collisionData2 = GAME->GetDrawTarget()->GetPixel((int)pos.x-cameraPos.x-1,(int)pos.y-cameraPos.y); if (collisionData!=MAGENTA) { return true; } else { diff --git a/test/test.cpp b/test/test.cpp index 8e06876..41ddbf3 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -646,21 +646,41 @@ bool SeasonI::OnUserCreate(){ PressTestKeyAndRun(DOWN); Test("Player has moved down-left", PARTY_MEMBER_OBJ[0]->GetPos().xGetPos().y>prevPlayerPos.y); + ReleaseTestKey(LEFT); + prevPlayerPos = PARTY_MEMBER_OBJ[0]->GetPos(); + OnUserUpdate(1/60.f); + Test("Player has moved down", + PARTY_MEMBER_OBJ[0]->GetPos().x==prevPlayerPos.x&&PARTY_MEMBER_OBJ[0]->GetPos().y>prevPlayerPos.y); + ReleaseTestKey(DOWN); + prevPlayerPos = PARTY_MEMBER_OBJ[0]->GetPos(); + PressTestKeyAndRun(UP); + Test("Player has moved up", + PARTY_MEMBER_OBJ[0]->GetPos().x==prevPlayerPos.x&&PARTY_MEMBER_OBJ[0]->GetPos().ySetPos({29-PARTY_MEMBER_OBJ[0]->originPoint.x,16-PARTY_MEMBER_OBJ[0]->originPoint.y}); - Test("Player is now centered at 29,16", - PARTY_MEMBER_OBJ[0]->GetPosWithOrigin().x==29&&PARTY_MEMBER_OBJ[0]->GetPosWithOrigin().y==16); + PARTY_MEMBER_OBJ[0]->SetPos({32-PARTY_MEMBER_OBJ[0]->originPoint.x,16-PARTY_MEMBER_OBJ[0]->originPoint.y}); + Test("Player is now centered at 32,16", + PARTY_MEMBER_OBJ[0]->GetPosWithOrigin().x==32&&PARTY_MEMBER_OBJ[0]->GetPosWithOrigin().y==16); CenterCameraOnPlayer(); - PressTestKeyAndRun(LEFT); - std::cout<GetPosWithOrigin()<GetPosWithOrigin()<GetPosWithOrigin()<GetPosWithOrigin()<GetPosWithOrigin().x==29&&PARTY_MEMBER_OBJ[0]->GetPosWithOrigin().y==16); + for (int i=0;i<10;i++) { + PressTestKeyAndRun(LEFT); + ReleaseTestKey(LEFT); + } + Test("Player does not move due to tile collision", + PARTY_MEMBER_OBJ[0]->GetPosWithOrigin().x==32&&PARTY_MEMBER_OBJ[0]->GetPosWithOrigin().y==16); + SetupGameDrawing(); + SetDrawTarget(layer::COLLISION); + bool isAllMagenta=true; + for (int x=0;xGetData()[y*WIDTH+x]!=MAGENTA) { + isAllMagenta=false; + goto endOfScreenCheck; + } + } + } + endOfScreenCheck: + Test("Entire screen should be wiped to magenta before drawing anything", + isAllMagenta); return true; }