Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/ChinmayMittal/Maze-Game int…
Browse files Browse the repository at this point in the history
…o main
  • Loading branch information
TanishTuteja committed Apr 16, 2022
2 parents 0971358 + 5bb6940 commit 7590203
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 10 deletions.
10 changes: 10 additions & 0 deletions Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1021,4 +1021,14 @@ void LGame::replaceTask(int task)
} while (it2 != currTasks.end());
currTasks[index] = t;
taskTexts[index]->setText(t.second);
}

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();

}
1 change: 1 addition & 0 deletions Game.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ class LGame : public LScreen
bool hasTask(int task);
void replaceTask(std::pair<int, std::string> task);
void replaceTask(int task);
int getTileType( int x , int y ) ;
};

#endif
61 changes: 52 additions & 9 deletions NPC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "Game.h"
#include <stdlib.h>
#include<time.h>
#include<iostream>

NPC :: NPC(LTexture &myTexture, LGame &game, int NPCHeight, int NPCWidth, int right, int left, int top, int bottom) : mTexture(myTexture) , mGame(game)
{
Expand All @@ -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 ;
Expand Down Expand Up @@ -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)) ;
}


Expand All @@ -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:
Expand All @@ -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 ;
}
}
2 changes: 1 addition & 1 deletion NPC.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 ;
Expand Down

0 comments on commit 7590203

Please sign in to comment.