author | leseb <leseb> | 2002-04-15 22:40:28 (UTC) |
---|---|---|
committer | leseb <leseb> | 2002-04-15 22:40:28 (UTC) |
commit | a91544d04ed391bbdc0c6f95ff8a80d35190788c (patch) (unidiff) | |
tree | 85dea85fd8a1cdb6d2d18fef57753d0b5e4bd143 /noncore/games/kpacman/fruit.cpp | |
parent | 6396d8b9fca7f3f50010a13a26e4ee9569abefb3 (diff) | |
download | opie-a91544d04ed391bbdc0c6f95ff8a80d35190788c.zip opie-a91544d04ed391bbdc0c6f95ff8a80d35190788c.tar.gz opie-a91544d04ed391bbdc0c6f95ff8a80d35190788c.tar.bz2 |
New directory structure
Diffstat (limited to 'noncore/games/kpacman/fruit.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/games/kpacman/fruit.cpp | 176 |
1 files changed, 176 insertions, 0 deletions
diff --git a/noncore/games/kpacman/fruit.cpp b/noncore/games/kpacman/fruit.cpp new file mode 100644 index 0000000..e6ad2d5 --- a/dev/null +++ b/noncore/games/kpacman/fruit.cpp | |||
@@ -0,0 +1,176 @@ | |||
1 | #include <stdlib.h> | ||
2 | |||
3 | #include "fruit.h" | ||
4 | |||
5 | Fruit::Fruit(Board *b) | ||
6 | { | ||
7 | board = b; | ||
8 | maxPixmaps = 0; | ||
9 | setLevel(0, 0, 0, 0); | ||
10 | } | ||
11 | |||
12 | void Fruit::setEaten(int duration) | ||
13 | { | ||
14 | actualState = eaten; | ||
15 | timeLeft = duration; | ||
16 | actualDirection = X; | ||
17 | } | ||
18 | |||
19 | void Fruit::setLevel(int level, int wDuration, int fDuration, int ticks) | ||
20 | { | ||
21 | actualLevel = level; | ||
22 | waitDuration = wDuration; | ||
23 | fruitDuration = fDuration; | ||
24 | pauseDuration = ticks; | ||
25 | pause = 0; | ||
26 | actualState = inactive; | ||
27 | timeLeft = waitDuration; | ||
28 | lastPosition = OUT; | ||
29 | setPosition(OUT); | ||
30 | setMovement(OUT, OUT, 0); | ||
31 | actualDirection = X; | ||
32 | setMaxPixmaps(maxPixmaps); | ||
33 | } | ||
34 | |||
35 | void Fruit::setMaxPixmaps(int max) | ||
36 | { | ||
37 | maxPixmaps = max; | ||
38 | if (actualLevel-1 < maxPixmaps) | ||
39 | actualPix = actualLevel == 0 ? 0 : actualLevel-1; | ||
40 | else if (maxPixmaps > 0) | ||
41 | actualPix = rand() % maxPixmaps; | ||
42 | else | ||
43 | actualPix = -1; | ||
44 | } | ||
45 | |||
46 | void Fruit::setMovement(int entry, int tunnel, int iq) | ||
47 | { | ||
48 | homePosition = board->position(fruithome); | ||
49 | entryPosition = entry; | ||
50 | tunnelPosition = tunnel; | ||
51 | IQ = iq; | ||
52 | } | ||
53 | |||
54 | void Fruit::setPosition(int pos) | ||
55 | { | ||
56 | board->reset(lastPosition, fruit); | ||
57 | actualPosition = lastPosition = pos; | ||
58 | board->set(actualPosition, fruit); | ||
59 | } | ||
60 | |||
61 | void Fruit::setDirection(int dir) | ||
62 | { | ||
63 | actualDirection = dir; | ||
64 | } | ||
65 | |||
66 | fruitState Fruit::state() | ||
67 | { | ||
68 | return actualState; | ||
69 | } | ||
70 | |||
71 | int Fruit::position() | ||
72 | { | ||
73 | return actualPosition; | ||
74 | } | ||
75 | |||
76 | int Fruit::direction() | ||
77 | { | ||
78 | return actualDirection; | ||
79 | } | ||
80 | |||
81 | bool Fruit::move(bool activate) | ||
82 | { | ||
83 | if (timeLeft > 0) { | ||
84 | timeLeft--; | ||
85 | } | ||
86 | |||
87 | if (actualDirection == X || actualState == inactive) { | ||
88 | if (timeLeft == 0 || (activate && actualState == inactive)) { | ||
89 | if (actualState == inactive) { | ||
90 | if (entryPosition == OUT || tunnelPosition == OUT) { | ||
91 | setPosition(board->position(fruithome)); | ||
92 | } else { | ||
93 | setPosition(entryPosition); | ||
94 | actualDirection = 0; | ||
95 | while (!board->isWay(actualPosition, actualDirection, empty) || | ||
96 | board->isJump(actualPosition, actualDirection)) | ||
97 | actualDirection++; | ||
98 | } | ||
99 | timeLeft = fruitDuration; | ||
100 | setMaxPixmaps(maxPixmaps); | ||
101 | actualState = active; | ||
102 | } else { | ||
103 | actualState = inactive; | ||
104 | setPosition(OUT); | ||
105 | timeLeft = waitDuration; | ||
106 | actualDirection = X; | ||
107 | } | ||
108 | return TRUE; | ||
109 | } | ||
110 | return FALSE; | ||
111 | } | ||
112 | |||
113 | if (pause-- > 0) | ||
114 | return FALSE; | ||
115 | else | ||
116 | pause = pauseDuration; | ||
117 | |||
118 | if (actualPosition == OUT) | ||
119 | return FALSE; | ||
120 | |||
121 | if (actualDirection == X) | ||
122 | return TRUE; | ||
123 | |||
124 | int d = actualDirection; | ||
125 | |||
126 | if (rand() % (int) ((190-IQ)/10) == 0) | ||
127 | if (timeLeft > 0) // coming home or leaving again | ||
128 | d = board->closeup(actualPosition, d, homePosition); | ||
129 | else | ||
130 | d = board->closeup(actualPosition, d, tunnelPosition); | ||
131 | else | ||
132 | do // try new direction, but not the opposite | ||
133 | d = rand() % 4; // direction, to prevent hectic movement. | ||
134 | while (d == board->turn(actualDirection)); | ||
135 | |||
136 | while ((!board->isWay(actualPosition, d, empty) && | ||
137 | !board->isWay(actualPosition, d, tunnel)) || | ||
138 | d == board->turn(actualDirection)) { | ||
139 | if (d != actualDirection) // if new direction is not possible, | ||
140 | d = actualDirection; // try current direction first. | ||
141 | else | ||
142 | d = rand() % 4; | ||
143 | } | ||
144 | |||
145 | actualDirection = d; | ||
146 | actualPosition = board->move(actualPosition, actualDirection); | ||
147 | |||
148 | if (actualPosition == homePosition) { | ||
149 | timeLeft = 0; | ||
150 | } | ||
151 | |||
152 | if (board->isTunnel(actualPosition)) { | ||
153 | setPosition(OUT); | ||
154 | timeLeft = waitDuration; | ||
155 | actualState = inactive; | ||
156 | actualDirection = X; | ||
157 | if (board->tunnels() > 0) { | ||
158 | entryPosition = board->position(tunnel, rand() % board->tunnels()); | ||
159 | tunnelPosition = board->position(tunnel, rand() % board->tunnels()); | ||
160 | } | ||
161 | } | ||
162 | |||
163 | if (actualPosition != lastPosition) { | ||
164 | setPosition(actualPosition); | ||
165 | } | ||
166 | |||
167 | return TRUE; | ||
168 | } | ||
169 | |||
170 | int Fruit::pix() | ||
171 | { | ||
172 | if (actualPosition == OUT || actualState == inactive) | ||
173 | return -1; | ||
174 | else | ||
175 | return actualPix; | ||
176 | } | ||