00001 #include "pacman.h"
00002
00003 Pacman::Pacman(Board *b)
00004 {
00005 board = b;
00006 setDemo(FALSE);
00007 setAlive(0);
00008 actualPosition = lastPosition = OUT;
00009 mouthPosition = 0;
00010 lastPix = 0;
00011 maxPixmaps = 0;
00012 }
00013
00014 void Pacman::setMaxPixmaps(int max)
00015 {
00016 if (actualDirection == X && lastPix >= 0) {
00017 actualDirection = lastPix / (maxPixmaps/4);
00018 if (max < maxPixmaps)
00019 mouthPosition = 0;
00020 else
00021 mouthPosition = lastPix % (maxPixmaps/4);
00022 maxPixmaps = max;
00023
00024 lastPix = pix();
00025
00026 actualDirection = X;
00027 } else
00028 maxPixmaps = max;
00029 }
00030
00031 void Pacman::setAlive(int ticks)
00032 {
00033 actualState = alive;
00034 pauseDuration = ticks;
00035 pause = 0;
00036 }
00037
00038 void Pacman::setPosition(int pos)
00039 {
00040 board->reset(lastPosition, pacman);
00041 actualPosition = lastPosition = pos;
00042 board->set(actualPosition, pacman);
00043 mouthPosition = 0;
00044 }
00045
00046 void Pacman::setDirection(int dir, bool forced)
00047 {
00048 if (forced ||
00049 board->isWay(actualPosition, dir, empty) ||
00050 board->isWay(actualPosition, dir, tunnel)) {
00051 if (dir == X)
00052 lastPix = pix();
00053 actualDirection = dir;
00054 nextDirection = X;
00055 } else
00056 nextDirection = dir;
00057 }
00058
00059 void Pacman::setDemo(bool yes)
00060 {
00061 demo = yes;
00062 }
00063
00064 pacmanState Pacman::state()
00065 {
00066 return actualState;
00067 }
00068
00069 int Pacman::position()
00070 {
00071 return actualPosition;
00072 }
00073
00074 int Pacman::direction()
00075 {
00076 return actualDirection;
00077 }
00078
00079 bool Pacman::move()
00080 {
00081 if (pause-- > 0)
00082 return FALSE;
00083 else
00084 pause = pauseDuration;
00085
00086 if (actualDirection == X || actualPosition == OUT)
00087 return FALSE;
00088
00089 lastPosition = actualPosition;
00090
00091 if (demo) {
00092 int d = actualDirection;
00093
00094 do
00095 d = rand() % 4;
00096 while (d == board->turn(actualDirection));
00097
00098 while (!board->isWay(actualPosition, d, empty) &&
00099 !board->isWay(actualPosition, d, tunnel)) {
00100 if (d != actualDirection)
00101 d = actualDirection;
00102 else
00103 d = rand() % 4;
00104 }
00105
00106 actualDirection = d;
00107 actualPosition = board->move(actualPosition, actualDirection);
00108
00109 } else {
00110
00111 if (nextDirection != X)
00112 if (board->isWay(actualPosition, nextDirection, empty) ||
00113 board->isWay(actualPosition, nextDirection, tunnel)) {
00114 actualDirection = nextDirection;
00115 nextDirection = X;
00116 }
00117
00118 if (board->isWay(actualPosition, actualDirection, empty) ||
00119 board->isWay(actualPosition, actualDirection, tunnel))
00120 actualPosition = board->move(actualPosition, actualDirection);
00121 }
00122
00123 if (actualPosition != lastPosition) {
00124 board->reset(lastPosition, pacman);
00125 board->set(actualPosition, pacman);
00126
00127 if (++mouthPosition >= (maxPixmaps/4))
00128 mouthPosition = 0;
00129 }
00130 return TRUE;
00131 }
00132
00133 int Pacman::pix()
00134 {
00135 if (actualPosition != OUT && maxPixmaps > 0)
00136 switch (actualDirection) {
00137 case N : return ((maxPixmaps/4)*0)+mouthPosition;
00138 case E : return ((maxPixmaps/4)*1)+mouthPosition;
00139 case S : return ((maxPixmaps/4)*2)+mouthPosition;
00140 case W : return ((maxPixmaps/4)*3)+mouthPosition;
00141 case X : return lastPix;
00142 }
00143
00144 return -1;
00145 }
00146