Diffstat (limited to 'noncore/games/kpacman/pacman.cpp') (more/less context) (show whitespace changes)
-rw-r--r-- | noncore/games/kpacman/pacman.cpp | 147 |
1 files changed, 147 insertions, 0 deletions
diff --git a/noncore/games/kpacman/pacman.cpp b/noncore/games/kpacman/pacman.cpp new file mode 100644 index 0000000..40f60a8 --- a/dev/null +++ b/noncore/games/kpacman/pacman.cpp | |||
@@ -0,0 +1,147 @@ | |||
1 | #include "pacman.h" | ||
2 | #include "board.h" | ||
3 | |||
4 | Pacman::Pacman(Board *b) | ||
5 | { | ||
6 | board = b; | ||
7 | setDemo(FALSE); | ||
8 | setAlive(0); | ||
9 | actualPosition = lastPosition = OUT; | ||
10 | mouthPosition = 0; | ||
11 | lastPix = 0; | ||
12 | maxPixmaps = 0; | ||
13 | } | ||
14 | |||
15 | void Pacman::setMaxPixmaps(int max) | ||
16 | { | ||
17 | if (actualDirection == X && lastPix >= 0) { | ||
18 | actualDirection = lastPix / (maxPixmaps/4); | ||
19 | if (max < maxPixmaps) | ||
20 | mouthPosition = 0; | ||
21 | else | ||
22 | mouthPosition = lastPix % (maxPixmaps/4); | ||
23 | maxPixmaps = max; | ||
24 | |||
25 | lastPix = pix(); | ||
26 | |||
27 | actualDirection = X; | ||
28 | } else | ||
29 | maxPixmaps = max; | ||
30 | } | ||
31 | |||
32 | void Pacman::setAlive(int ticks) | ||
33 | { | ||
34 | actualState = alive; | ||
35 | pauseDuration = ticks; | ||
36 | pause = 0; | ||
37 | } | ||
38 | |||
39 | void Pacman::setPosition(int pos) | ||
40 | { | ||
41 | board->reset(lastPosition, pacman); | ||
42 | actualPosition = lastPosition = pos; | ||
43 | board->set(actualPosition, pacman); | ||
44 | mouthPosition = 0; | ||
45 | } | ||
46 | |||
47 | void Pacman::setDirection(int dir, bool forced) | ||
48 | { | ||
49 | if (forced || | ||
50 | board->isWay(actualPosition, dir, empty) || | ||
51 | board->isWay(actualPosition, dir, tunnel)) { | ||
52 | if (dir == X) | ||
53 | lastPix = pix(); | ||
54 | actualDirection = dir; | ||
55 | nextDirection = X; | ||
56 | } else | ||
57 | nextDirection = dir; | ||
58 | } | ||
59 | |||
60 | void Pacman::setDemo(bool yes) | ||
61 | { | ||
62 | demo = yes; | ||
63 | } | ||
64 | |||
65 | pacmanState Pacman::state() | ||
66 | { | ||
67 | return actualState; | ||
68 | } | ||
69 | |||
70 | int Pacman::position() | ||
71 | { | ||
72 | return actualPosition; | ||
73 | } | ||
74 | |||
75 | int Pacman::direction() | ||
76 | { | ||
77 | return actualDirection; | ||
78 | } | ||
79 | |||
80 | bool Pacman::move() | ||
81 | { | ||
82 | if (pause-- > 0) | ||
83 | return FALSE; | ||
84 | else | ||
85 | pause = pauseDuration; | ||
86 | |||
87 | if (actualDirection == X || actualPosition == OUT) | ||
88 | return FALSE; | ||
89 | |||
90 | lastPosition = actualPosition; | ||
91 | |||
92 | if (demo) { | ||
93 | int d = actualDirection; | ||
94 | |||
95 | do // try new direction, but not the opposite | ||
96 | d = rand() % 4; // direction, to prevent hectic movement. | ||
97 | while (d == board->turn(actualDirection)); | ||
98 | |||
99 | while (!board->isWay(actualPosition, d, empty) && | ||
100 | !board->isWay(actualPosition, d, tunnel)) { | ||
101 | if (d != actualDirection) // if new actualDirection is not possible, | ||
102 | d = actualDirection; // try current actualDirection first. | ||
103 | else | ||
104 | d = rand() % 4; | ||
105 | } | ||
106 | |||
107 | actualDirection = d; | ||
108 | actualPosition = board->move(actualPosition, actualDirection); | ||
109 | |||
110 | } else { | ||
111 | |||
112 | if (nextDirection != X) | ||
113 | if (board->isWay(actualPosition, nextDirection, empty) || | ||
114 | board->isWay(actualPosition, nextDirection, tunnel)) { | ||
115 | actualDirection = nextDirection; | ||
116 | nextDirection = X; | ||
117 | } | ||
118 | |||
119 | if (board->isWay(actualPosition, actualDirection, empty) || | ||
120 | board->isWay(actualPosition, actualDirection, tunnel)) | ||
121 | actualPosition = board->move(actualPosition, actualDirection); | ||
122 | } | ||
123 | |||
124 | if (actualPosition != lastPosition) { | ||
125 | board->reset(lastPosition, pacman); | ||
126 | board->set(actualPosition, pacman); | ||
127 | |||
128 | if (++mouthPosition >= (maxPixmaps/4)) | ||
129 | mouthPosition = 0; | ||
130 | } | ||
131 | return TRUE; | ||
132 | } | ||
133 | |||
134 | int Pacman::pix() | ||
135 | { | ||
136 | if (actualPosition != OUT && maxPixmaps > 0) | ||
137 | switch (actualDirection) { | ||
138 | case N : return ((maxPixmaps/4)*0)+mouthPosition; | ||
139 | case E : return ((maxPixmaps/4)*1)+mouthPosition; | ||
140 | case S : return ((maxPixmaps/4)*2)+mouthPosition; | ||
141 | case W : return ((maxPixmaps/4)*3)+mouthPosition; | ||
142 | case X : return lastPix; | ||
143 | } | ||
144 | |||
145 | return -1; | ||
146 | } | ||
147 | |||