Diffstat (limited to 'noncore/games/kbill/Monster.cc') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/games/kbill/Monster.cc | 226 |
1 files changed, 226 insertions, 0 deletions
diff --git a/noncore/games/kbill/Monster.cc b/noncore/games/kbill/Monster.cc new file mode 100644 index 0000000..72bf39b --- a/dev/null +++ b/noncore/games/kbill/Monster.cc | |||
@@ -0,0 +1,226 @@ | |||
1 | #include "objects.h" | ||
2 | |||
3 | void Monster::get_border() { | ||
4 | int i=game.RAND(0,3); | ||
5 | if (i%2==0) target_x=game.RAND(0, game.scrwidth-bill.width); | ||
6 | else target_y=game.RAND(0, game.scrheight-bill.height); | ||
7 | switch (i) { | ||
8 | case 0: target_y=-bill.height-16; break; | ||
9 | case 1: target_x=game.scrwidth+1; break; | ||
10 | case 2: target_y=game.scrwidth+1; break; | ||
11 | case 3: target_x=-bill.width-2; break; | ||
12 | } | ||
13 | } | ||
14 | |||
15 | /* Adds a bill to the in state*/ | ||
16 | void Monster::enter(){ | ||
17 | state = IN; | ||
18 | get_border(); | ||
19 | x = target_x; y=target_y; | ||
20 | index = 0; | ||
21 | cels = bill.lcels; | ||
22 | cargo = OS.WINGDOWS; | ||
23 | x_offset = -2; | ||
24 | y_offset = -15; | ||
25 | target_c = game.RAND(0, net.units-1); | ||
26 | target_x = net.computers[target_c].x+net.width-XOFFSET; | ||
27 | target_y = net.computers[target_c].y+YOFFSET; | ||
28 | bill.on_screen++; bill.off_screen--; | ||
29 | } | ||
30 | |||
31 | /* Moves bill toward his target - returns whether or not he moved */ | ||
32 | int Monster::move (int mode) { | ||
33 | int xdist = target_x - x; | ||
34 | int ydist = target_y - y; | ||
35 | int step = step_size(game.level); | ||
36 | int dx, dy; | ||
37 | int signx = xdist >= 0 ? 1 : -1; | ||
38 | int signy = ydist >= 0 ? 1 : -1; | ||
39 | xdist = abs(xdist); | ||
40 | ydist = abs(ydist); | ||
41 | if (!xdist && !ydist) return 0; | ||
42 | else if (xdist<step && ydist<step) { | ||
43 | x = target_x; | ||
44 | y = target_y; | ||
45 | } | ||
46 | else { | ||
47 | dx = (xdist*step*signx)/(xdist+ydist); | ||
48 | dy = (ydist*step*signy)/(xdist+ydist); | ||
49 | switch(mode) { | ||
50 | case SLOW: break; | ||
51 | case FAST: dx = 5*dx/4; dy = 5*dy/4; break; | ||
52 | } | ||
53 | x+=dx; | ||
54 | y+=dy; | ||
55 | if (dx<0) | ||
56 | cels = bill.lcels; | ||
57 | else if (dx>0) | ||
58 | cels = bill.rcels; | ||
59 | } | ||
60 | return 1; | ||
61 | } | ||
62 | |||
63 | void Monster::draw() { | ||
64 | switch (state) { | ||
65 | case IN: | ||
66 | case OUT: | ||
67 | case DYING: draw_std(); break; | ||
68 | case AT: draw_at(); break; | ||
69 | case STRAY: draw_stray(); break; | ||
70 | default: break; | ||
71 | } | ||
72 | } | ||
73 | |||
74 | /* Update Bill's position */ | ||
75 | void Monster::update_in() { | ||
76 | int moved = move(SLOW); | ||
77 | if (!moved && (net.computers[target_c].os != OS.WINGDOWS) | ||
78 | && !(net.computers[target_c].busy)) | ||
79 | { | ||
80 | net.computers[target_c].busy=1; | ||
81 | cels = bill.acels; | ||
82 | index=0; | ||
83 | state = AT; | ||
84 | return; | ||
85 | } | ||
86 | else if (!moved) { | ||
87 | int i; | ||
88 | do { | ||
89 | i=game.RAND(0, net.units-1); | ||
90 | } while (i == target_c); | ||
91 | target_c = i; | ||
92 | target_x = net.computers[target_c].x + net.width-XOFFSET; | ||
93 | target_y = net.computers[target_c].y + YOFFSET; | ||
94 | } | ||
95 | index++; | ||
96 | index%=bill.WCELS; | ||
97 | y_offset+=(8*(index%2)-4); | ||
98 | } | ||
99 | |||
100 | /* Update Bill standing at a computer */ | ||
101 | void Monster::update_at() { | ||
102 | int sys; | ||
103 | if (index==0 && net.computers[target_c].os == OS.OFF) { | ||
104 | index=6; | ||
105 | sys = net.computers[target_c].find_stray(); | ||
106 | if (sys<0) cargo = -1; | ||
107 | else { | ||
108 | cargo = bill.list[sys].cargo; | ||
109 | bill.list[sys].state = OFF; | ||
110 | } | ||
111 | } | ||
112 | else index++; | ||
113 | if (index == 13) { | ||
114 | y_offset = -15; | ||
115 | x_offset = -2; | ||
116 | get_border(); | ||
117 | index = 0; | ||
118 | cels = bill.lcels; | ||
119 | state = OUT; | ||
120 | net.computers[target_c].busy=0; | ||
121 | return; | ||
122 | } | ||
123 | y_offset = bill.height - OS.height; | ||
124 | switch (index) { | ||
125 | case 1 : | ||
126 | case 2 : x -= 8; x_offset +=8; break; | ||
127 | case 3 : x -= 10; x_offset +=10; break; | ||
128 | case 4 : x += 3; x_offset -=3; break; | ||
129 | case 5 : x += 2; x_offset -=2; break; | ||
130 | case 6 : | ||
131 | if (net.computers[target_c].os != OS.OFF) { | ||
132 | net.base--; net.off++; | ||
133 | cargo = net.computers[target_c].os; | ||
134 | } | ||
135 | else { | ||
136 | x-=21; x_offset+=21; | ||
137 | } | ||
138 | net.computers[target_c].os = OS.OFF; | ||
139 | y_offset = -15; | ||
140 | x += 20; | ||
141 | x_offset -=20; | ||
142 | break; | ||
143 | case 7 : sy = y_offset; sx = -2; break; | ||
144 | case 8 : sy = -15; sx = -2; break; | ||
145 | case 9 : sy = -7; sx = -7; x -= 8; x_offset +=8; break; | ||
146 | case 10 : sy = 0; sx = -7; x -= 15; x_offset +=15; break; | ||
147 | case 11 : sy = 0; sx = -7; | ||
148 | net.computers[target_c].os = OS.WINGDOWS; | ||
149 | net.off--; net.win++; | ||
150 | break; | ||
151 | case 12 : x += 11; x_offset -=11; | ||
152 | } | ||
153 | } | ||
154 | |||
155 | /* Updates Bill fleeing with his ill gotten gain */ | ||
156 | void Monster::update_out() { | ||
157 | if (game.INTERSECT(x, y, bill.width, bill.height, 0, 0, game.scrwidth, | ||
158 | game.scrheight)) | ||
159 | { | ||
160 | move(FAST); | ||
161 | index++; | ||
162 | index%=bill.WCELS; | ||
163 | y_offset+=(8*(index%2)-4); | ||
164 | } | ||
165 | else { | ||
166 | state = OFF; | ||
167 | bill.on_screen--; bill.off_screen++; | ||
168 | } | ||
169 | } | ||
170 | |||
171 | |||
172 | /* Updates Bill who is dying */ | ||
173 | void Monster::update_dying() { | ||
174 | if (index < bill.DCELS-1){ | ||
175 | y_offset += (index*GRAVITY); | ||
176 | index++; | ||
177 | } | ||
178 | else { | ||
179 | y+=y_offset; | ||
180 | if (cargo<0 || cargo == OS.WINGDOWS) state = OFF; | ||
181 | else state = STRAY; | ||
182 | bill.on_screen--; | ||
183 | } | ||
184 | } | ||
185 | |||
186 | void Monster::update() { | ||
187 | switch (state) { | ||
188 | case IN: update_in(); break; | ||
189 | case AT: update_at(); break; | ||
190 | case OUT: update_out(); break; | ||
191 | case DYING: update_dying(); break; | ||
192 | default: break; | ||
193 | } | ||
194 | } | ||
195 | |||
196 | int Monster::clicked(int locx, int locy) { | ||
197 | return (locx>x && locx<x+bill.width && locy>y && locy<y+bill.height); | ||
198 | } | ||
199 | |||
200 | int Monster::clickedstray(int locx, int locy) { | ||
201 | return (locx>x && locx<x+OS.width && locy>y && locy<y+OS.height); | ||
202 | } | ||
203 | |||
204 | int Monster::step_size(unsigned int lev) { | ||
205 | return game.MIN(14+lev, 18); | ||
206 | } | ||
207 | |||
208 | void Monster::draw_std() { | ||
209 | if (cargo>=0) | ||
210 | ui.draw(OS.os[cargo], x + x_offset, y + y_offset); | ||
211 | ui.draw(cels[index], x, y); | ||
212 | } | ||
213 | |||
214 | void Monster::draw_at() { | ||
215 | if (index>6 && index<12) | ||
216 | ui.draw(OS.os[0], x + sx, y + sy); | ||
217 | if (cargo>=0) | ||
218 | ui.draw(OS.os[cargo], x + x_offset, y + y_offset); | ||
219 | ui.draw(cels[index], net.computers[target_c].x, | ||
220 | net.computers[target_c].y); | ||
221 | } | ||
222 | |||
223 | void Monster::draw_stray() { | ||
224 | if (game.grabbed==-1 || x != bill.list[game.grabbed].x) | ||
225 | ui.draw(OS.os[cargo], x, y); | ||
226 | } | ||