13.07.2015 Views

29 The Power of Inheritance and Polymorphism

29 The Power of Inheritance and Polymorphism

29 The Power of Inheritance and Polymorphism

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Implementation: DungeonItems 1061}int range = fPos.Distance(p->Where());return (range < 7);<strong>The</strong> Advance() function determines the change in x, y coords that will bringthe Ghost closer to the Player.void Ghost::Advance(){Player *p = fD->Human();Pt p1 = p->Where();int dx, dy;dx = dy = 0;if(p1.X() > fPos.X()) dx = 1;elseif(p1.X() < fPos.X()) dx = -1;if(p1.Y() > fPos.Y()) dy = 1;elseif(p1.Y() < fPos.Y()) dy = -1;}Move(Pt(fPos.X() + dx, fPos.Y() + dy));W<strong>and</strong>erer<strong>The</strong> W<strong>and</strong>erer::CanDetect() function uses the Dungeon::ClearLineOfSight()member function to determine whether the Player object is visible. This functioncall also fills in the array fPath with the points that will have to be crossed.int W<strong>and</strong>erer::CanDetect(){Player *p = fD->Human();returnfD->ClearLineOfSight(fPos, p->Where(), 10, fPath);}<strong>The</strong> Advance() function moves one step along the path:void W<strong>and</strong>erer::Advance(){Move(fPath[0]);}<strong>The</strong> NormalMove() function tries moving in the same direction as before.Directions are held by storing the delta-x <strong>and</strong> delta-y values in fLastX <strong>and</strong> fLastYdata members (initialized to zero in the constructor). If movement in that generaldirection is blocked, a new direction is picked r<strong>and</strong>omly.void W<strong>and</strong>erer::NormalMove(){int x = fPos.X();int y = fPos.Y();

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!