Skip to content

Commit

Permalink
NPC collision detection and hostel spawning added
Browse files Browse the repository at this point in the history
  • Loading branch information
ChinmayMittal committed Apr 17, 2022
1 parent 296530e commit 473fe07
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 8 deletions.
15 changes: 13 additions & 2 deletions Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,18 @@ void LGame::update()
SDL_Rect NPCbox = NPCs[i].getBox();
int NPCtileX = (NPCbox.x + NPCbox.w / 2) / mTileWidth;
int NPCtileY = (NPCbox.y + NPCbox.h / 2) / mTileHeight;
std::cout << NPCtileX << " " << NPCtileY << "\n";
if (((tileX >= NPCtileX - 1) && (tileX <= NPCtileX + 1)) && ((tileY >= NPCtileY - 1) && (tileY <= NPCtileY + 1)))
// std::cout << NPCtileX << " " << NPCtileY << "\n";
if (((tileX >= NPCtileX - 1) && (tileX <= NPCtileX + 1)) && ((tileY >= NPCtileY - 1) && (tileY <= NPCtileY + 1)) && !NPCs[i].isBusy() && !players[0].isBusy())
{
std ::cout << "collision with " << NPCs[i].getName() << "\n";
NPCs[i].setCoolDown( 7000) ;
players[0].changeStateParameters(
{
0 ,
0,
-1
}
) ;
}
}
entities[tileType].collided(players[0], displayText);
Expand All @@ -98,6 +106,9 @@ void LGame::update()
players[0].setCamera(camera);

players[0].update();
for( int i = 0 ; i < NPCs.size() ; i ++ ){
NPCs[i].update() ;
}
SDL_Rect playerBoxNew = players[0].getBox();
int newTileX = (playerBoxNew.x + playerBoxNew.w / 2) / mTileWidth;
int newTileY = (playerBoxNew.y + playerBoxNew.h / 2) / mTileHeight;
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Entity.o: Player.h

Animation.o: Renderable.h MyTexture.h

NPC.o: Renderable.h Game.h MyTexture.h
NPC.o: Renderable.h Game.h MyTexture.h Timer.h

Server: server/server.cpp MessageStructs.cpp
$(CXX) server/server.cpp MessageStructs.cpp $(COMPILER_FLAGS) -o server/server.out
Expand Down
34 changes: 33 additions & 1 deletion NPC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ NPC :: NPC(LTexture &myTexture, std::string nameOfNPC , LGame &game, int NPCHeig
mframes = 0 ;
this -> numOfAnimationImages = myTexture.getWidth() / NPCWidth ;
NPCImages.resize( numOfAnimationImages*4);
coolDownTime = 0 ;
coolDownTimer = LTimer() ;
for (int col = 0; col < numOfAnimationImages; col++)
{
// right image in column col
Expand Down Expand Up @@ -72,6 +74,7 @@ SDL_Rect NPC::getBox()

int NPC::render(SDL_Renderer *renderer, SDL_Rect &camera)
{

if (mVelX < 0)
{
direction = 'L';
Expand Down Expand Up @@ -116,13 +119,15 @@ int NPC::render(SDL_Renderer *renderer, SDL_Rect &camera)

mTexture.render(renderer, mBox.x - camera.x, mBox.y - camera.y, &NPCImages[dimension + offset]);

mframes = (mframes + 1) % (numOfAnimationImages * animationSpeed);
if(!isBusy()) mframes = (mframes + 1) % (numOfAnimationImages * animationSpeed);
return 0;
}

void NPC::move()
{
// std :: cout << mBox.x << " " << mBox.y << " "<< mVelX << " " << mVelY << "\n" ;
if( isBusy()) return ;
// std::cout << coolDownTimer.isStarted() << "\n" ;
mBox.x += mVelX ;
bool boxFlipped = false ;
if ((mBox.x < 0) || (mBox.x + NPCWidth > mGame.getLevelWidth()) )
Expand Down Expand Up @@ -213,4 +218,31 @@ void NPC::switchDirection()
std::string NPC::getName()
{
return name ;
}

bool NPC::isBusy()
{
if( coolDownTimer.isStarted() and coolDownTimer.getTicks() <= coolDownTime){
return true ;
}else{
return false ;
}
}

void NPC::setCoolDown( int t)
{
coolDownTime = t ;
coolDownTimer.start() ;
mframes = 0 ;
direction = 'D' ;
mVelX = 0 ;
mVelY = velocity ;
}

void NPC::update()
{
if( coolDownTimer.isStarted() and coolDownTimer.getTicks() > coolDownTime){
coolDownTime = 0 ;
coolDownTimer.stop() ;
}
}
6 changes: 6 additions & 0 deletions NPC.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include<string>
#include"Renderable.h"
#include"MyTexture.h"
#include "Timer.h"

class LGame;

Expand All @@ -20,6 +21,9 @@ class NPC : public Renderable
SDL_Rect getBox() ;
void switchDirection() ;
std::string getName() ;
bool isBusy() ;
void update() ;
void setCoolDown( int t ) ;
private:
LGame &mGame ;
std::string name ;
Expand All @@ -36,6 +40,8 @@ class NPC : public Renderable
int mVelX , mVelY ;
int changeSpeed ;
int current ;
LTimer coolDownTimer ;
int coolDownTime ;

} ;

Expand Down
29 changes: 25 additions & 4 deletions Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include <stdlib.h>
#include "constants.h"
#include <algorithm>
#include <map>
#include<utility>

std ::vector<std ::string> hostelNames{"nilgiri", "kara", "aravali", "jwala", "kumaon", "vindy", "satpura", "udai_girnar", "himadri", "kailash"};

Expand All @@ -21,14 +23,26 @@ Player ::Player(LTexture &myTexture, LGame &game, int playerHeight, int playerWi
// Initialize the collision box
mCollisionMusic = Mix_LoadWAV("resources/collision.wav");
mMovementMusic = Mix_LoadWAV("resources/collision.wav");
mBox.x = 32 * 7;
mBox.y = 0;
mBox.w = playerWidth;
mBox.h = playerHeight;
taskAnimation = NULL;
srand(time(0));
std::map< std::string , std::pair< int , int > > hostels ;
hostels["nilgiri"] = { 15*32 , 7*32 } ;
hostels["aravali"] = { 47*32 , 7*32} ;
hostels["kara"] = {31*32,7*32 } ;
hostels["jwala"] ={64*32 , 7*32} ;
hostels["kumaon"] = { 63*32 , 18*32} ;
hostels["vindy"] = { 63*32 , 29*32} ;
hostels["satpura"] = { 63*32, 40*32} ;
hostels["shivalik"] = { 45*32 , 51*32} ;
hostels["zanskar"] = { 27*32 , 51*32} ;
hostels["himadri"] = {16*32 , 104*32} ;
hostels["kailash"] = {16*32 , 104*32} ;
hostels["udai_girnar"] = { 63*32 , 51*32} ;
hostelName = hostelNames[rand() % hostelNames.size()];
hostelName = "nilgiri"; // for testing
// hostelName = "nilgiri"; // for testing
mBox.x = hostels[hostelName].first ;
mBox.y = hostels[hostelName].second ;
currentTaskTime = 0;
currentTaskTimer = LTimer();
updateState = {0.0, 0, 0, 0};
Expand Down Expand Up @@ -491,6 +505,13 @@ bool Player::isBusy()
}
}

void Player::changeStateParameters( playerStateUpdate s)
{
setHealth(getHealth() + s.health);
setPoints(getPoints() + s.points);
setMoney(getMoney() + s.money);
}

void Player::updateStateParameters(playerStateUpdate s)
{
setHealth(getHealth() + s.health);
Expand Down
1 change: 1 addition & 0 deletions Player.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class Player : public Renderable
bool isBusy();
void updateStateParameters(playerStateUpdate s);
void setUpdateStateParameters(playerStateUpdate s);
void changeStateParameters( playerStateUpdate s) ;
bool hadLunch();
bool hadBreakFast();
bool hadDinner();
Expand Down

0 comments on commit 473fe07

Please sign in to comment.