From e0f7033bbd46e9d7a3376e8d6080760e41f9a2ae Mon Sep 17 00:00:00 2001 From: ChinmayMittal Date: Sat, 16 Apr 2022 23:45:02 +0530 Subject: [PATCH] NPC object collision detection done --- Game.cpp | 9 +++++++++ Game.h | 1 + NPC.cpp | 61 +++++++++++++++++++++++++++++++++++++++++++++++--------- NPC.h | 2 +- 4 files changed, 63 insertions(+), 10 deletions(-) diff --git a/Game.cpp b/Game.cpp index f6bc093..2c128ed 100644 --- a/Game.cpp +++ b/Game.cpp @@ -895,4 +895,13 @@ void LGame::replaceTask(int task) it2 = std::find(currTasks.begin(), currTasks.end(), t); } while (it2 != currTasks.end()); currTasks[index] = t; +} + +int LGame :: getTileType( int x , int y ) +{ + int tileX = x / mTileWidth; + int tileY = y / mTileHeight; + // std :: cout << x << " " << tileX << "\n" ; + // std :: cout << y << " " << tileY << "\n" ; + return tiles[tileY * mTilesX + tileX].getType(); } \ No newline at end of file diff --git a/Game.h b/Game.h index 2903c23..24d4e8b 100644 --- a/Game.h +++ b/Game.h @@ -105,6 +105,7 @@ class LGame : public LScreen bool hasTask(int task); void replaceTask(std::pair task); void replaceTask(int task); + int getTileType( int x , int y ) ; }; #endif \ No newline at end of file diff --git a/NPC.cpp b/NPC.cpp index 9bf003f..f99d971 100644 --- a/NPC.cpp +++ b/NPC.cpp @@ -2,6 +2,7 @@ #include "Game.h" #include #include +#include NPC :: NPC(LTexture &myTexture, LGame &game, int NPCHeight, int NPCWidth, int right, int left, int top, int bottom) : mTexture(myTexture) , mGame(game) { @@ -12,10 +13,12 @@ NPC :: NPC(LTexture &myTexture, LGame &game, int NPCHeight, int NPCWidth, int ri mBox.y = 0 ; mBox.w = NPCHeight ; mBox.h = NPCWidth ; + this -> NPCHeight = NPCHeight ; + this -> NPCWidth = NPCWidth ; velocity = 1 ; - direction = 'U' ; + direction = 'D' ; mVelX = 0 ; - mVelY = -velocity ; + mVelY = +velocity ; this -> right = right ; this -> left = left ; this -> top = top ; @@ -109,14 +112,21 @@ int NPC::render(SDL_Renderer *renderer, SDL_Rect &camera) void NPC::move() { - + // std :: cout << mBox.x << " " << mBox.y << " "<< mVelX << " " << mVelY << "\n" ; mBox.x += mVelX ; - + bool boxFlipped = false ; if ((mBox.x < 0) || (mBox.x + NPCWidth > mGame.getLevelWidth()) ) { // move back + boxFlipped = true ; + bool positive ; + if( mBox.x < 0 ){ + positive = true ; + }else{ + positive = false ; + } mBox.x -= mVelX ; - mVelX *= -1 ; + mVelX = (positive) ? ( abs(mVelX)) : -(abs(mVelX)) ; } @@ -126,14 +136,24 @@ void NPC::move() if ((mBox.y < 0) || (mBox.y + NPCHeight > mGame.getLevelHeight()) ) { // move back - mBox.y -= mVelY ; - mVelY *= -1 ; + boxFlipped = true ; + bool positive ; + if( mBox.y < 0 ){ + positive = true ; + }else{ + positive = false ; + } + mBox.y -= mVelY ; + mVelY = (positive) ? ( abs(mVelY)) : -(abs(mVelY)) ; + } + if( mGame.getTileType( (mBox.x) + mBox.w/2 , mBox.y + ( mBox.h)/2 ) == 3 and not boxFlipped ){ + switchDirection() ; } - current ++ ; if( current == changeSpeed ) { current = 0 ; - int rem = rand()%4 ; + int rem = rand()%4 ; + mframes = 0 ; switch (rem) { case 0: @@ -155,4 +175,27 @@ void NPC::move() void NPC::cleanUp() { mTexture.free(); +} + +void NPC::switchDirection() +{ + switch ( direction ) + { + case 'D': + direction = 'U' ; + mVelY *= -1 ; + break; + case 'U': + direction = 'D' ; + mVelY *= -1 ; + break ; + case 'R': + mVelX *=-1 ; + direction = 'L' ; + break ; + case 'L' : + mVelX *= -1 ; + direction = 'R' ; + break ; + } } \ No newline at end of file diff --git a/NPC.h b/NPC.h index 4c239a9..c37563d 100644 --- a/NPC.h +++ b/NPC.h @@ -17,7 +17,7 @@ class NPC : public Renderable int render(SDL_Renderer *renderer, SDL_Rect &camera); void cleanUp(); SDL_Rect getBox() ; - + void switchDirection() ; private: LGame &mGame ; SDL_Rect mBox ;