Skip to content

Commit

Permalink
fixed pacman
Browse files Browse the repository at this point in the history
  • Loading branch information
borbrudar committed Sep 21, 2021
1 parent 916137b commit e94f40f
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 49 deletions.
14 changes: 7 additions & 7 deletions src/include/Ghost.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ class Ghost {
void setup(int type, Vector2f pos, Vector2f size, Vector2f start, std::vector<std::vector<int>> field);
void draw(RenderWindow& window);
//blinky and clyde
void update(Vector2f player, bool fright = 0);
void update(Vector2f player, bool fright, float delta);
//pinky
void update(Vector2f player, float rot, bool fright = 0);
void update(Vector2f player, float rot, bool fright, float delta);
//inky
void update(Vector2f player, float rot, Vector2f blinky, bool fright = 0);
void update(Vector2f player, float rot, Vector2f blinky, bool fright, float delta);
//call when collision and other stuff
void die();
void die(float delta);

void reset();

Expand All @@ -27,15 +27,15 @@ class Ghost {
private:
void findPath(Vector2i target, Vector2i curPos);
void random(Vector2i curPos);
bool move(bool fright = 0);
bool move(float delta, bool fright = 0);


std::vector<int> beggining;
int st = 1;
std::vector<std::vector<int>> field;
int type = 0, instruction = -1;
float speed = 1.3f;
bool beg = 1;
float speed = 120.f;
bool beg = 1;

float distTraveled = 0.f;
Vector2i prevPos;
Expand Down
2 changes: 1 addition & 1 deletion src/include/Pac_Man.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Pac_Man : public State {
//pacman
Animation pacman;
bool up = 0, down = 0, left = 0, right = 0;
float speed = 1.5f, pscale = 0.2f;
float speed = 150.f, pscale = 0.2f;
Vector2f pSize{ 80,80 }, prevPos;
Vector2i tSize;

Expand Down
60 changes: 30 additions & 30 deletions src/src/Ghost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ void Ghost::draw(RenderWindow& window)
}

//blinky and clyde
void Ghost::update(Vector2f player, bool fright)
void Ghost::update(Vector2f player, bool fright, float delta)
{
//normalize coords to integers
Vector2i tar = Vector2i((std::round((player.x - start.x) / tSize.x)), (std::round((player.y - start.y) / tSize.y)));
Vector2i pos = Vector2i(std::round((this->pos.x - start.x) / tSize.x), std::round((this->pos.y - start.y)/ tSize.y));

if (alive == 0) die();
if (alive == 0) die(delta);
else if (fright) {
if (move(fright)) random(pos);
if (move(delta,fright)) random(pos);
}
else {
if (beg && type == 0) {
Expand All @@ -47,22 +47,22 @@ void Ghost::update(Vector2f player, bool fright)
else if (beg && type == 3) {
if (st != -1) instruction = beggining[st]; else {
beg = 0;}
if (move()) st--;
if (move(delta)) st--;
}

//normal movement
if(!beg) {
if(!beg) {
st = 1;
//blinky
if (type == 0) {
//if move done find next target
if (move()) { findPath(tar, pos); }
if (move(delta)) { findPath(tar, pos); }
}//idiot clyde
else if (type == 3) {
float dist = std::sqrt(std::pow(pos.x - tar.x, 2) + std::pow(pos.y - tar.y, 2));

if (dist > 4) {
if (move(fright)) findPath(tar, pos);
if (move(delta,fright)) findPath(tar, pos);
}
else {
//else run to the nearest corner
Expand All @@ -81,7 +81,7 @@ void Ghost::update(Vector2f player, bool fright)
}
}

if (move(fright)) findPath(tar, pos);
if (move(delta,fright)) findPath(tar, pos);
}
}
}
Expand All @@ -90,21 +90,21 @@ void Ghost::update(Vector2f player, bool fright)
}

//pinky
void Ghost::update(Vector2f player, float rot, bool fright)
void Ghost::update(Vector2f player, float rot, bool fright, float delta)
{
//normalize coords to integers
Vector2i tar;
Vector2i pos = Vector2i(std::round((this->pos.x - start.x) / tSize.x), std::round((this->pos.y - start.y) / tSize.y));

if (alive == 0) die();
if (alive == 0) die(delta);
else if (fright) {
if(move(fright)) random(pos);
if(move(delta,fright)) random(pos);
}
else if(beg) {
if (st != -1) instruction = beggining[st]; else {
beg = 0;
}
if (move()) st--;
if (move(delta)) st--;
}
else {
st = 1;
Expand All @@ -127,27 +127,27 @@ void Ghost::update(Vector2f player, float rot, bool fright)
findPath(tar, pos); beg = 0;
}
//if move done find next target
if (move(fright)) { findPath(tar, pos); }
if (move(delta,fright)) { findPath(tar, pos); }
}
}

//inky
void Ghost::update(Vector2f player, float rot, Vector2f blinky, bool fright)
void Ghost::update(Vector2f player, float rot, Vector2f blinky, bool fright, float delta)
{
//normalize coords to integers
Vector2i tar, origin;
Vector2i pos = Vector2i(std::round((this->pos.x - start.x) / tSize.x), std::round((this->pos.y - start.y) / tSize.y));
Vector2i bli = Vector2i(std::round((blinky.x - start.x) / tSize.x), std::round((blinky.y - start.y) / tSize.y));

if (alive == 0) die();
if (alive == 0) die(delta);
else if (fright) {
if (move(fright)) random(pos);
if (move(delta, fright)) random(pos);
}
else if (beg) {
if (st != -1) instruction = beggining[st]; else {
beg = 0;
}
if (move()) st--;
if (move(delta)) st--;
}
else {
st = 2;
Expand Down Expand Up @@ -176,7 +176,7 @@ void Ghost::update(Vector2f player, float rot, Vector2f blinky, bool fright)
findPath(tar, pos); beg = 0;
}
//if move done find next target
if (move(fright)) findPath(tar, pos);
if (move(delta,fright)) findPath(tar, pos);
}
}

Expand Down Expand Up @@ -243,46 +243,46 @@ void Ghost::random(Vector2i curPos)
else if (type == 3) findPath(c[3], curPos);
}

bool Ghost::move(bool fright)
bool Ghost::move(float delta, bool fright)
{
if (distTraveled > tSize.x) {
if(instruction == 0) pos.x -= speed;
if (instruction == 1) pos.x += speed;
if (instruction == 2) pos.y -= speed;
if (instruction == 3) pos.y += speed;
if (instruction == 0) pos.x -= speed * delta;
if (instruction == 1) pos.x += speed * delta;
if (instruction == 2) pos.y -= speed * delta;
if (instruction == 3) pos.y += speed * delta;
distTraveled = 0.f;
return 1;
}
distTraveled += speed;
distTraveled += speed * delta;

switch (instruction) {
case 0:
//right
if (fright) animation.setup("res/pacman/gh.png", size, Vector2f(0, size.y * 4));
else if (!alive) animation.setup("res/pacman/gh.png", dSize, Vector2f(dSize.x * 6, size.y * 5));
else animation.setup("res/pacman/gh.png", size, Vector2f(size.x * 6, type * size.y));
pos.x += speed;
pos.x += speed * delta;
break;
case 1:
//left
if (fright) animation.setup("res/pacman/gh.png", size, Vector2f(0, size.y * 4));
else if (!alive) animation.setup("res/pacman/gh.png", dSize, Vector2f(dSize.x * 4, size.y * 5));
else animation.setup("res/pacman/gh.png", size, Vector2f(size.x * 4, type * size.y));
pos.x -= speed;
pos.x -= speed * delta;
break;
case 2:
//down
if (fright) animation.setup("res/pacman/gh.png", size, Vector2f(0, size.y * 4));
else if (!alive) animation.setup("res/pacman/gh.png", dSize, Vector2f(dSize.x * 2, size.y * 5));
else animation.setup("res/pacman/gh.png", size, Vector2f(size.x * 2, type * size.y));
pos.y += speed;
pos.y += speed * delta;
break;
case 3:
//up
if (fright) animation.setup("res/pacman/gh.png", size, Vector2f(0, size.y * 4));
else if (!alive) animation.setup("res/pacman/gh.png", dSize, Vector2f(0, size.y * 5));
else animation.setup("res/pacman/gh.png", size, Vector2f(0, type * size.y));
pos.y -= speed;
pos.y -= speed * delta;
break;
default:
animation.setup("res/pacman/gh.png", size, Vector2f(0, type * size.y));
Expand All @@ -292,12 +292,12 @@ bool Ghost::move(bool fright)
return 0;
}

void Ghost::die()
void Ghost::die(float delta)
{
Vector2i pos = Vector2i(std::round((this->pos.x - start.x) / tSize.x), std::round((this->pos.y - start.y) / tSize.y));
Vector2i tar = Vector2i{ 9,8 };

if (move()) findPath(tar, pos);
if (move(delta)) findPath(tar, pos);

if (tar == pos) alive = 1; else alive = 0;

Expand Down
19 changes: 8 additions & 11 deletions src/src/Pac_Man.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,11 @@ void Pac_Man::update(Mouse& mouse, RenderWindow& window, state& gameState, Event

if (!gameOver) {
//update the ghosts
blinky.update(pacman.animation.getPosition(), frightened);
blinky.update(pacman.animation.getPosition(), frightened, delta);
pinky.update(pacman.animation.getPosition(), pacman.animation.getRotation(),
blinky.animation.animation.getPosition(), frightened);
inky.update(pacman.animation.getPosition(), pacman.animation.getRotation(), blinky.pos, frightened);
clyde.update(pacman.animation.getPosition(), frightened);
blinky.animation.animation.getPosition(), frightened, delta);
inky.update(pacman.animation.getPosition(), pacman.animation.getRotation(), blinky.pos, frightened, delta);
clyde.update(pacman.animation.getPosition(), frightened, delta);

//frightened mode update
if (frightened) {
Expand Down Expand Up @@ -196,25 +196,22 @@ void Pac_Man::update(Mouse& mouse, RenderWindow& window, state& gameState, Event
}
}




//movement
prevPos = pacman.animation.getPosition();
if (up) {
pacman.animation.move(0, -speed);
pacman.animation.move(0, -speed * delta);
pacman.animation.setRotation(270);
}
else if (down) {
pacman.animation.move(0, speed);
pacman.animation.move(0, speed * delta);
pacman.animation.setRotation(90);
}
else if (left) {
pacman.animation.move(-speed, 0);
pacman.animation.move(-speed * delta, 0);
pacman.animation.setRotation(180);
}
else if (right) {
pacman.animation.move(speed, 0);
pacman.animation.move(speed * delta, 0);
pacman.animation.setRotation(0);
}

Expand Down

0 comments on commit e94f40f

Please sign in to comment.