merge the formfield patch from ooo-build
[ooovba.git] / goodies / source / inv / monst.cxx
blob117946a278f3c150cc68765b69b92db4eabae40c
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: monst.cxx,v $
10 * $Revision: 1.7 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_goodies.hxx"
33 #include <stdlib.h>
34 #include "monst.hxx"
35 #include "invader.hrc"
36 #include "expl.hxx"
37 #include "shapes.hxx"
38 #include <vcl/outdev.hxx>
39 #include <tools/time.hxx>
41 Gegner::Gegner(Fighter* pFig, Bombe* pBom, ResMgr* pRes) :
42 GegnerListe(0,0),
43 pBitMonst1(0L),
44 pBitMonst2(0L),
45 pBitMonst3(0L),
46 pBitMonst4(0L),
47 pBitMonst1b(0L),
48 pBitMonst2b(0L),
49 pBitMonst3b(0L),
50 pBitMonst4b(0L),
51 pBitMonst5(0L),
52 pBitMonst5a(0L),
53 pBitMonst5b(0L),
54 pBombe(pBom),
55 pFighter(pFig),
56 bDown(FALSE),
57 bLeft(TRUE),
58 bAuseMode(FALSE),
59 nDown(MOVEY)
61 pBitMonst1 = ImplLoadImage( MONSTER1, pRes );
62 pBitMonst2 = ImplLoadImage( MONSTER2, pRes );
63 pBitMonst3 = ImplLoadImage( MONSTER3, pRes );
64 pBitMonst4 = ImplLoadImage( MONSTER4, pRes );
65 pBitMonst1b = ImplLoadImage( MONSTER1B, pRes );
66 pBitMonst2b = ImplLoadImage( MONSTER2B, pRes );
67 pBitMonst3b = ImplLoadImage( MONSTER3B, pRes );
68 pBitMonst4b = ImplLoadImage( MONSTER4B, pRes );
69 pBitMonst5 = ImplLoadImage( MONSTER5, pRes );
70 pBitMonst5a = ImplLoadImage( MONSTER5A, pRes );
71 pBitMonst5b = ImplLoadImage( MONSTER5B, pRes );
73 aOutSize = pBitMonst1->GetSizePixel();
75 SetRandWert( 100 );
78 Gegner::~Gegner()
80 ClearAll();
82 delete pBitMonst1;
83 delete pBitMonst2;
84 delete pBitMonst3;
85 delete pBitMonst4;
86 delete pBitMonst1b;
87 delete pBitMonst2b;
88 delete pBitMonst3b;
89 delete pBitMonst4b;
90 delete pBitMonst5;
91 delete pBitMonst5a;
92 delete pBitMonst5b;
95 void Gegner::InsertGegner(long nType, long x, long y)
97 Gegner_Impl* pWork = new Gegner_Impl();
99 pWork->aType = (enum GegnerType)nType;
100 pWork->aMode = MOVE1;
101 pWork->aXY = Point(x,y);
102 pWork->aX = x;
103 pWork->nHits = 0;
104 switch(pWork->aType)
106 case GEGNER1:
107 pWork->nPoints = 50;
108 pWork->nMaxHits = 1;
109 break;
110 case GEGNER2:
111 pWork->nPoints = 75;
112 pWork->nMaxHits = 2;
113 break;
114 case GEGNER3:
115 pWork->nPoints = 150;
116 pWork->nMaxHits = 3;
117 break;
118 case GEGNER4:
119 pWork->nPoints = 225;
120 pWork->nMaxHits = 5;
121 break;
122 case GEGNER5:
123 pWork->nPoints = 500;
124 pWork->nMaxHits = 3;
125 pWork->aMode = HIDE;
126 break;
129 Insert(pWork);
132 void Gegner::Move()
134 BOOL bNextDown = FALSE;
135 unsigned long i;
136 for(i=0; i<Count(); i++)
138 if(bDown)
140 SetGegnerPos(i,Point(GegnerX(i),GegnerY(i)+nDown));
143 else if(bLeft)
145 SetGegnerPos(i,Point(GegnerX(i)+MOVEX,GegnerY(i)));
146 if(GegnerX(i)+MOVEX+aOutSize.Width() > nMaxX)
147 bNextDown = TRUE;
149 else
151 SetGegnerPos(i,Point(GegnerX(i)-MOVEX,GegnerY(i)));
152 if(GegnerX(i)-MOVEX <= 0)
153 bNextDown = TRUE;
158 if(bDown)
160 if(bLeft)
161 bLeft = FALSE;
162 else
163 bLeft = TRUE;
166 bDown = FALSE;
167 if(bNextDown)
168 bDown = TRUE;
171 void Gegner::DrawGegner(OutputDevice* pDev,Point* pStart)
174 Time aTime;
175 srand(aTime.GetTime() % 1000);
177 nMaxX = pDev->GetOutputSizePixel().Width()-pStart->X();
178 unsigned long i;
179 for(i=0; i<Count();i++)
181 switch(GegType(i))
183 case GEGNER1:
184 if(GegMode(i) == MOVE1)
186 pDev->DrawImage(Point(pStart->X()+GegnerX(i),
187 pStart->Y()+GegnerY(i)),*pBitMonst1);
188 SetMode(i,MOVE2);
190 else if(GegMode(i) == MOVE2)
192 pDev->DrawImage(Point(pStart->X()+GegnerX(i),
193 pStart->Y()+GegnerY(i)),*pBitMonst1b);
194 SetMode(i,MOVE1);
196 break;
197 case GEGNER2:
198 if(GegMode(i) == MOVE1)
200 pDev->DrawImage(Point(pStart->X()+GegnerX(i),
201 pStart->Y()+GegnerY(i)),*pBitMonst2);
202 SetMode(i,MOVE2);
204 else if(GegMode(i) == MOVE2)
206 pDev->DrawImage(Point(pStart->X()+GegnerX(i),
207 pStart->Y()+GegnerY(i)),*pBitMonst2b);
208 SetMode(i,MOVE1);
210 break;
211 case GEGNER3:
212 if(GegMode(i) == MOVE1)
214 pDev->DrawImage(Point(pStart->X()+GegnerX(i),
215 pStart->Y()+GegnerY(i)),*pBitMonst3);
216 SetMode(i,MOVE2);
218 else if(GegMode(i) == MOVE2)
220 pDev->DrawImage(Point(pStart->X()+GegnerX(i),
221 pStart->Y()+GegnerY(i)),*pBitMonst3b);
222 SetMode(i,MOVE1);
224 break;
225 case GEGNER4:
226 if(GegMode(i) == MOVE1)
228 pDev->DrawImage(Point(pStart->X()+GegnerX(i),
229 pStart->Y()+GegnerY(i)),*pBitMonst4);
230 SetMode(i,MOVE2);
232 else if(GegMode(i) == MOVE2)
234 pDev->DrawImage(Point(pStart->X()+GegnerX(i),
235 pStart->Y()+GegnerY(i)),*pBitMonst4b);
236 SetMode(i,MOVE1);
238 break;
239 case GEGNER5:
240 if(GegMode(i) == MOVE1)
242 pDev->DrawImage(Point(pStart->X()+GegnerX(i),
243 pStart->Y()+GegnerY(i)),*pBitMonst5);
244 DecDelay(i);
245 if(!GetDelay(i))
247 SetDelay(i);
248 SetMode(i,MOVE2);
251 if(GegMode(i) == MOVE2)
253 pDev->DrawImage(Point(pStart->X()+GegnerX(i),
254 pStart->Y()+GegnerY(i)),*pBitMonst5a);
255 DecDelay(i);
256 if(!GetDelay(i))
258 SetDelay(i);
259 SetMode(i,MOVE3);
262 if(GegMode(i) == MOVE3)
264 pDev->DrawImage(Point(pStart->X()+GegnerX(i),
265 pStart->Y()+GegnerY(i)),*pBitMonst5b);
266 DecDelay(i);
267 pBombe->InsertBombe(Point(GegnerX(i),
268 GegnerY(i)+aOutSize.Height()/2));
269 if(!GetDelay(i))
271 SetDelay(i);
272 SetMode(i,MOVE4);
275 if(GegMode(i) == MOVE4)
277 pDev->DrawImage(Point(pStart->X()+GegnerX(i),
278 pStart->Y()+GegnerY(i)),*pBitMonst5a);
279 DecDelay(i);
280 if(!GetDelay(i))
282 SetDelay(i);
283 if ( rand() % 5 < 2 )
284 SetMode(i,MOVE3);
285 else
286 SetMode(i,MOVE5);
289 if(GegMode(i) == MOVE5)
291 pDev->DrawImage(Point(pStart->X()+GegnerX(i),
292 pStart->Y()+GegnerY(i)),*pBitMonst5);
293 DecDelay(i);
294 if(!GetDelay(i))
296 if ( rand() % 5 < 2 )
298 SetMode(i,MOVE1);
299 SetDelay(i);
301 else
302 SetMode(i,HIDE);
305 break;
309 SetKoll(i,Rectangle(Point(GegnerX(i)+KOLLXY,GegnerY(i)+KOLLXY),
310 Point(GegnerX(i)+aOutSize.Width()-KOLLXY,
311 GegnerY(i)+aOutSize.Height()-KOLLXY)));
313 if(bAuseMode && GegMode(i) == MOVE1)
315 if(GegnerX(i) < pFighter->GetHalf() &&
316 GegnerX(i)+aOutSize.Width() > pFighter->GetHalf())
317 pBombe->InsertBombe(Point(pFighter->GetPoint().X(),
318 GegnerY(i)+aOutSize.Height()/2));
320 else
322 int ran = rand();
324 int nScaledLimit;
325 // NOTE: the two expressions are the same in floatingpoint but not in integer
327 int nRandMax = RAND_MAX;
328 if ( nRandMax < 32767 )
329 nScaledLimit = GetRandWert() / ( 32767 / nRandMax );
330 else
331 nScaledLimit = GetRandWert() * ( nRandMax / 32767);
333 if(GegType(i) != GEGNER5)
335 if(ran < nScaledLimit )
336 pBombe->InsertBombe(Point(GegnerX(i),
337 GegnerY(i)+aOutSize.Height()/2));
339 else if(GegMode(i) == HIDE)
341 if(ran < (nScaledLimit *3) /2)
343 SetMode(i,MOVE1);
344 SetDelay(i);
350 Move();
353 long Gegner::Kollision(Rectangle& rRect, Explosion* pExpl)
355 long nWert = -1;
357 Rectangle aWork;
359 unsigned long i;
360 for( i=0; i<Count();i++)
362 aWork = GegnerKoll(i);
363 if((aWork.Left() <= rRect.Left() && aWork.Right() >= rRect.Right()) &&
364 (aWork.Top() <= rRect.Top() && aWork.Bottom() >= rRect.Bottom()) &&
365 GegMode(i) != DELETED)
367 nWert = 0;
368 if(GegnerDest(i))
370 SetMode(i,DELETED);
371 if(nWert == -1)
372 nWert = GegnerPoints(i);
373 else
374 nWert += GegnerPoints(i);
377 pExpl->InsertExpl(GegnerPos(i));
382 return nWert;
385 BOOL Gegner::GegnerDest(long nWert)
387 GegnerHit(nWert);
388 if(GetObject(nWert)->nHits >= GetObject(nWert)->nMaxHits)
389 return TRUE;
391 return FALSE;
394 Rectangle Gegner::GetKoll(long nWert)
396 return Rectangle(Point(GegnerX(nWert)+aOutSize.Width()/2,
397 GegnerY(nWert)+aOutSize.Height()),
398 Point(GegnerX(nWert)+aOutSize.Width()/2,
399 GegnerY(nWert)+aOutSize.Height()));
402 BOOL Gegner::RemoveGegner()
404 for(long i=Count()-1; i>=0; i--)
406 Gegner_Impl* pWork = GetObject(i);
408 if(pWork->aMode == DELETED)
410 Remove(pWork);
411 delete pWork;
414 if(Count())
415 return FALSE;
416 else
417 return TRUE;
420 void Gegner::ClearAll()
422 unsigned long i;
423 for( i=0; i<Count(); i++ )
424 delete GetObject(i);
426 Clear();