Update ooo320-m1
[ooovba.git] / goodies / source / inv / invader.cxx
blobc3aa086cd361d746aa08c90caf936704c2074367
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: invader.cxx,v $
10 * $Revision: 1.12 $
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 "invader.hxx"
34 #include "monst.hxx"
35 #include "shapes.hxx"
36 #include "expl.hxx"
37 #include "invader.hrc"
38 #include "strings.hrc"
39 #include "score.hxx"
40 #include <svtools/solar.hrc>
41 #include <cppuhelper/servicefactory.hxx>
42 #include <comphelper/processfactory.hxx>
43 #include <vcl/msgbox.hxx>
44 #include <vcl/virdev.hxx>
46 MyWindow::MyWindow(Window* pParent, ResMgr *pResMgr ) :
47 FloatingWindow(pParent, WB_3DLOOK | WB_CLOSEABLE | WB_MOVEABLE ),
48 pRes(pResMgr),
49 ProgStatus(FALSE),
50 bEndLevel(TRUE),
51 bFightDest(FALSE),
52 bTimeHigh(TRUE),
53 bPause(FALSE),
54 bAuseModus(FALSE),
55 bWaitDlg(FALSE),
56 StartPhase(1),
57 nLevel(1),
58 nScore(0L),
59 nHighScore(0L),
60 nFighter(3),
61 nTimeOut(TIMEHIGH),
62 nAuseCount(0),
63 pBitWelcome2(0L),
64 pBitHeros(0L),
65 pBitStarWars(0L),
66 pGegner(0L),
67 pFighter(0L),
68 pMunition(0L),
69 pExplosion(0L),
70 pBombe(0L),
71 pWall(0L),
72 pScoreWindow(0L),
73 pVirtualDevice(0L),
74 pPauseWindow(0L),
75 pBox(0L),
76 nDirection(0L),
77 bMouseMooving(FALSE)
79 pBitWelcome2 = ImplLoadImage( WELCOME2, GetResMgr() );
80 pBitHeros = ImplLoadImage( HEROS, GetResMgr() );
81 pBitStarWars = ImplLoadImage( STARWARS, GetResMgr() );
82 pBombe = new Bombe(GetResMgr());
83 pMunition = new Munition(GetResMgr());
84 pExplosion = new Explosion(GetResMgr());
85 pWall = new Wall(GetResMgr());
86 pScoreWindow = new ScoreWindow(this,WB_BORDER, GetResMgr());
87 pPauseWindow = new Window(this, 0);
89 SetOutputSizePixel(Size(WINWIDTH,WINHEIGHT));
90 SetText(String(ResId(STR_APP_TITLE,*GetResMgr())));
92 pPauseWindow->SetPosSizePixel(Point(100,190),Size(500,50));
93 pPauseWindow->SetBackground();
95 pScoreWindow->SetPosSizePixel(Point(0,0),Size(WINWIDTH,22));
96 Size aSize(WINWIDTH,WINHEIGHT);
97 pFighter = new Fighter(aSize, GetResMgr());
98 pGegner = new Gegner(pFighter, pBombe, GetResMgr());
100 pVirtualDevice = new VirtualDevice(*this);
101 pVirtualDevice->SetOutputSizePixel(Size( WINWIDTH, WINHEIGHT ));
103 SetBackground();
105 aPaintTimer = Timer();
106 aPaintTimer.SetTimeoutHdl(LINK(this, MyWindow, PaintTimer));
107 aPaintTimer.SetTimeout(nTimeOut);
108 aPaintTimer.Stop();
110 aWaitDlgTimer = Timer();
111 aWaitDlgTimer.SetTimeoutHdl(LINK(this, MyWindow, StartDlgTimer));
112 aWaitDlgTimer.SetTimeout(10);
113 aWaitDlgTimer.Stop();
115 aPointer = GetPointer();
117 Show();
121 MyWindow::~MyWindow()
123 aPaintTimer.Stop();
124 delete pVirtualDevice;
125 delete pGegner;
126 delete pFighter;
127 delete pMunition;
128 delete pExplosion;
129 delete pBombe;
130 delete pWall;
131 delete pBitHeros;
132 delete pBitStarWars;
133 delete pBitWelcome2;
134 delete pScoreWindow;
135 delete pPauseWindow;
138 void MyWindow::Paint(const Rectangle&)
140 if(bWaitDlg)
141 return;
143 Size aVSize = pVirtualDevice->GetOutputSizePixel();
145 if (!ProgStatus)
147 if (StartPhase < 3)
149 pScoreWindow->Hide();
150 bMouseMooving = FALSE;
152 if( StartPhase == 1 )
154 const Font aOldFont( GetFont() );
155 Font aFont( aOldFont );
157 SetFillColor( COL_BLACK );
158 DrawRect(Rectangle(Point(0,0),Point(640,480)));
160 DrawImage(Point(55,78), *pBitStarWars);
161 DrawImage(Point(53,207), *pBitHeros);
162 DrawImage(Point(470,400), *(pFighter->pFight1));
163 DrawImage(Point(460,290), *(pMunition->pMunition1));
164 DrawImage(Point(480,320), *(pMunition->pMunition1));
165 DrawImage(Point(470,250), *(pMunition->pMunition1));
167 aFont.SetColor(COL_WHITE);
168 aFont.SetFillColor(COL_BLACK);
169 SetFont( aFont );
171 DrawText(Point(175,170),String(ResId(STR_CHOOSEHERO, *GetResMgr())));
172 DrawText(Point(98,208),String(ResId(STR_NAME1, *GetResMgr())));
173 DrawText(Point(98,252),String(ResId(STR_NAME2, *GetResMgr())));
174 DrawText(Point(98,296),String(ResId(STR_NAME3, *GetResMgr())));
175 DrawText(Point(98,340),String(ResId(STR_NAME4, *GetResMgr())));
176 DrawText(Point(98,384),String(ResId(STR_NAME5, *GetResMgr())));
178 SetFont( aOldFont );
180 else if(StartPhase == 2)
182 SetFillColor( COL_WHITE );
183 DrawRect(Rectangle(Point(0,0),Point(640,480)));
184 DrawImage(Point(19,147), *pBitWelcome2 );
188 else if(!bPause)
190 aPaintTimer.Start();
192 Point aPoint(0,20);
194 pVirtualDevice->SetFillColor( COL_WHITE );
195 pVirtualDevice->SetLineColor();
196 pVirtualDevice->DrawRect(Rectangle(Point(0,0), Point(aVSize.Width(),aVSize.Height())));
198 if(!bStartLevel)
200 if ((bMouseMooving)&&(nDirection > pFighter->GetPoint().X()+32))
202 pFighter->Move(1);
203 pFighter->Move(1);
204 pGegner->SetAuseMode(FALSE);
205 nAuseCount = 0;
207 else if ((bMouseMooving)&&(nDirection < pFighter->GetPoint().X()))
209 pFighter->Move(-1);
210 pFighter->Move(-1);
211 pGegner->SetAuseMode(FALSE);
212 nAuseCount = 0;
214 nAuseCount++;
215 if(nAuseCount > AUSEMODE)
216 pGegner->SetAuseMode(TRUE);
217 pWall->Paint(*pVirtualDevice);
218 pGegner->DrawGegner(pVirtualDevice,&aPoint);
219 pFighter->Paint(*pVirtualDevice);
220 pMunition->Paint(*pVirtualDevice);
221 pBombe->Paint(*pVirtualDevice);
222 pExplosion->Paint(*pVirtualDevice);
224 else
226 bMouseMooving = FALSE;
227 Font aOldFont = pVirtualDevice->GetFont();
228 Font aFont = aOldFont;
229 switch(nStartLevel)
231 case 0:
232 aFont.SetColor(COL_RED);
233 break;
234 case 1:
235 aFont.SetColor(COL_YELLOW);
236 break;
237 case 2:
238 aFont.SetColor(COL_GREEN);
239 break;
241 pVirtualDevice->SetFont(*&aFont);
242 String aString = String(ResId(STR_LEVELSTART, *GetResMgr()));
243 aString += ' ';
244 aString += String::CreateFromInt32(nLevel);
245 if(nStartLevel < 3)
246 pVirtualDevice->DrawText(Point(LEVELTEXTX,LEVELTEXTY),aString);
247 nStartLevel++;
248 if(nStartLevel > 3)
250 bStartLevel = FALSE;
251 bEndLevel = FALSE;
252 bFightDest = FALSE;
253 aPaintTimer.SetTimeout(nTimeOut);
255 pVirtualDevice->SetFont(*&aOldFont);
259 DrawOutDev( Point( 0, 22), aVSize, Point( 0, 22), aVSize, *pVirtualDevice);
261 if(!bStartLevel)
262 Kollision();
264 else pPauseWindow->DrawText(Point(20,20),String(ResId(STR_PAUSE, *GetResMgr())));
267 void MyWindow::KeyInput( const KeyEvent& rKEvent)
269 if(bEndLevel || bFightDest)
271 rKEvent.GetKeyCode().GetCode();
272 FloatingWindow::KeyInput(rKEvent);
274 return;
277 bMouseMooving = FALSE;
278 switch(rKEvent.GetKeyCode().GetCode())
280 case KEY_LEFT:
281 pFighter->Move(-1);
282 nAuseCount = 0;
283 pGegner->SetAuseMode(FALSE);
284 break;
285 case KEY_RIGHT:
286 pFighter->Move(1);
287 nAuseCount = 0;
288 pGegner->SetAuseMode(FALSE);
289 break;
290 case KEY_SPACE:
291 pMunition->Start(pFighter->GetPoint());
292 break;
293 case KEY_L:
294 if(nLevel > 1)
295 return;
296 if(bTimeHigh)
298 bTimeHigh = FALSE;
299 nTimeOut = TIMELOW;
301 else
303 bTimeHigh = TRUE;
304 nTimeOut = TIMEHIGH;
306 aPaintTimer.SetTimeout(nTimeOut);
307 break;
308 case KEY_ADD:
309 if(nLevel > 1)
310 return;
311 nTimeOut++;
312 aPaintTimer.SetTimeout(nTimeOut);
313 break;
314 case KEY_SUBTRACT:
315 if(nLevel > 1)
316 return;
317 nTimeOut--;
318 if(nTimeOut <= 0)
319 nTimeOut = 1;
320 aPaintTimer.SetTimeout(nTimeOut);
321 aPaintTimer.Start();
322 break;
323 case KEY_P:
324 if(!ProgStatus)
325 return;
326 if(bPause)
328 aPaintTimer.Start();
329 bPause = FALSE;
330 SetBackground();
331 pPauseWindow->Hide();
333 else
335 aPaintTimer.Stop();
336 bPause = TRUE;
337 SetBackground( Wallpaper( COL_WHITE ) );
338 pPauseWindow->Show();
339 pPauseWindow->DrawText(Point(20,20),String(ResId(STR_PAUSE, *GetResMgr())));
341 break;
342 default:
343 FloatingWindow::KeyInput(rKEvent);
347 IMPL_LINK( MyWindow, PaintTimer, Timer*, EMPTYARG)
350 Invalidate();
352 return 0;
355 IMPL_LINK( MyWindow, StartDlgTimer, Timer*, EMPTYARG )
357 bWaitDlg = TRUE;
359 aWaitDlgTimer.Stop();
361 String aString;
362 if(!bFightDest)
364 aString = String(ResId(STR_GRAT1,*GetResMgr()));
365 aString += String::CreateFromInt32(nLevel);
366 aString += String(ResId(STR_GRAT2,*GetResMgr()));
368 pBox = new MessBox(this,WinBits(WB_OK),String(ResId(STR_APP_TITLE,*GetResMgr())),aString);
369 PlaceDialog(pBox);
370 ProgStatus=FALSE;
371 SetBackground(Wallpaper( Color( COL_WHITE ) ) );
373 pBox->Execute();
375 SetBackground();
376 ProgStatus=TRUE;
377 delete pBox;
378 nLevel++;
379 GrabFocus();
381 else
383 nFighter--;
384 pScoreWindow->SetLives(nFighter);
385 if(nFighter > 0)
387 aString = String(ResId(STR_FIGHTDEST1,*GetResMgr()));
388 aString += String::CreateFromInt32(nFighter);
389 aString += String(ResId(STR_FIGHTDEST2,*GetResMgr()));
391 else
393 aString = String(ResId(STR_GAMEOVER,*GetResMgr()));
394 ProgStatus = FALSE;
395 nLevel = 1;
396 nFighter = 3;
397 pGegner->SetRandWert(200);
398 pGegner->SetDown(10);
400 if(bTimeHigh)
401 nTimeOut = TIMEHIGH;
402 else
403 nTimeOut = TIMELOW;
406 pBox = new MessBox(this,WinBits(WB_OK),String(ResId(STR_APP_TITLE,*GetResMgr())),aString);
407 PlaceDialog(pBox);
408 BOOL aDummyStatus = ProgStatus;
409 ProgStatus=FALSE;
410 SetBackground(Wallpaper( Color( COL_WHITE ) ) );
412 pBox->Execute();
414 SetBackground();
415 ProgStatus=aDummyStatus;
416 delete pBox;
417 GrabFocus();
419 if(!ProgStatus)
421 StartPhase = 1;
422 if (nScore > nHighScore)
424 String aHSString;
425 aHSString = String(ResId(STR_HIGHSCORE1,*GetResMgr()));
426 aHSString += String::CreateFromInt32(nScore);
427 aHSString += String(ResId(STR_HIGHSCORE2,*GetResMgr()));
428 aHSString += String::CreateFromInt32(nHighScore);
429 aHSString += String(ResId(STR_HIGHSCORE3,*GetResMgr()));
430 nHighScore = nScore;
431 pBox = new MessBox(this,WinBits(WB_OK),String(ResId(STR_APP_TITLE,*GetResMgr())),aHSString);
432 PlaceDialog(pBox);
433 Paint(Rectangle(Point(0,0),Point(640,480)));
434 SetBackground(Wallpaper( Color( COL_WHITE ) ) );
435 pBox->Execute();
436 SetBackground();
437 delete pBox;
438 GrabFocus();
440 nScore = 0;
445 InitLevel();
446 Invalidate();
448 bWaitDlg = FALSE;
450 return 0;
453 void MyWindow::MouseButtonDown(const MouseEvent& rMEvt)
455 MessBox* pMessBox = 0L;
457 if (!ProgStatus)
459 TheHero = 0;
461 if (StartPhase == 1)
463 if ((rMEvt.GetPosPixel().X() >= 57) && (rMEvt.GetPosPixel().X() <=90))
465 if ((rMEvt.GetPosPixel().Y() >= 211) && (rMEvt.GetPosPixel().Y() <= 244))
466 TheHero = STR_NAME1;
467 else if ((rMEvt.GetPosPixel().Y() >= 255) && (rMEvt.GetPosPixel().Y() <= 288))
468 TheHero = STR_NAME2;
469 else if ((rMEvt.GetPosPixel().Y() >= 299) && (rMEvt.GetPosPixel().Y() <= 332))
470 TheHero = STR_NAME3;
471 else if ((rMEvt.GetPosPixel().Y() >= 343) && (rMEvt.GetPosPixel().Y() <= 376))
472 TheHero = STR_NAME4;
473 else if ((rMEvt.GetPosPixel().Y() >= 387) && (rMEvt.GetPosPixel().Y() <= 420))
474 TheHero = STR_NAME5;
476 if (TheHero)
478 SetPointer(aPointer);
479 switch (TheHero)
481 case STR_NAME1:
483 pMessBox = new MessBox(this,WinBits(WB_OK),String(ResId(STR_APP_TITLE,*GetResMgr())),String(ResId(STR_HERO1, *GetResMgr())));
485 break;
486 case STR_NAME2:
488 pMessBox = new MessBox(this,WinBits(WB_OK),String(ResId(STR_APP_TITLE,*GetResMgr())), String(ResId(STR_HERO2, *GetResMgr())));
490 break;
491 case STR_NAME3:
493 pMessBox = new MessBox(this,WinBits(WB_OK),String(ResId(STR_APP_TITLE,*GetResMgr())), String(ResId(STR_HERO3, *GetResMgr())));
495 break;
496 case STR_NAME4:
498 pMessBox = new MessBox(this,WinBits(WB_OK),String(ResId(STR_APP_TITLE,*GetResMgr())), String(ResId(STR_HERO4, *GetResMgr())));
500 break;
501 case STR_NAME5:
503 pMessBox = new MessBox(this,WinBits(WB_OK),String(ResId(STR_APP_TITLE,*GetResMgr())), String(ResId(STR_HERO5, *GetResMgr())));
505 break;
507 PlaceDialog(pMessBox);
508 pMessBox->Execute();
509 delete pMessBox;
510 StartPhase++;
511 pScoreWindow->SetHero(TheHero);
512 Invalidate();
515 else if (StartPhase == 2)
517 ProgStatus = TRUE;
518 StartPhase++;
519 pScoreWindow->ShowMe();
520 InitLevel();
521 Invalidate();
522 aPaintTimer.Start();
525 else
527 if((!bStartLevel) && (!bPause))
528 pMunition->Start(pFighter->GetPoint());
532 void MyWindow::MouseMove(const MouseEvent& rMEvt)
534 long Hero;
536 if ((!ProgStatus) && ( StartPhase == 1 ))
538 Hero = 0;
539 if ((rMEvt.GetPosPixel().X() >= 57) && (rMEvt.GetPosPixel().X() <=90))
541 if ((rMEvt.GetPosPixel().Y() >= 211) && (rMEvt.GetPosPixel().Y() <= 244))
542 Hero = 1;
543 else if ((rMEvt.GetPosPixel().Y() >= 255) && (rMEvt.GetPosPixel().Y() <= 288))
544 Hero = 2;
545 else if ((rMEvt.GetPosPixel().Y() >= 299) && (rMEvt.GetPosPixel().Y() <= 332))
546 Hero = 3;
547 else if ((rMEvt.GetPosPixel().Y() >= 343) && (rMEvt.GetPosPixel().Y() <= 376))
548 Hero = 4;
549 else if ((rMEvt.GetPosPixel().Y() >= 387) && (rMEvt.GetPosPixel().Y() <= 420))
550 Hero = 5;
552 if ((Hero) && (GetPointer() != POINTER_REFHAND)) SetPointer(POINTER_REFHAND);
553 else if ((!Hero) && (GetPointer() != aPointer)) SetPointer(aPointer);
555 else if ( ProgStatus )
557 nDirection = rMEvt.GetPosPixel().X();
558 bMouseMooving = TRUE;
562 BOOL MyWindow::Close()
564 Hide();
565 #ifdef TEST
566 pApplication->Quit();
567 #else
568 delete this;
569 #endif
571 return TRUE;
574 void MyWindow::PlaceDialog(MessBox* pMessBox)
576 if(pMessBox)
578 Point aPos = GetPosPixel();
579 Size aSize = GetSizePixel();
580 Size aBoxSize = pMessBox->GetOutputSizePixel();
581 aSize = Size(aSize.Width()/2 - aBoxSize.Width()/2,
582 aSize.Height()/2 - aBoxSize.Height()/2);
583 pMessBox->SetPosPixel(
584 Point(aPos.X()+aSize.Width(),aPos.Y()+aSize.Height()));
588 #ifdef TEST
589 void MyApp::Main()
591 ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >
592 xMSF = cppu::createRegistryServiceFactory(
593 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "applicat.rdb" ) ), sal_True );
595 ::comphelper::setProcessServiceFactory( xMSF );
597 ByteString aRes = "tfu";
598 ResMgr *pMyResMgr = ResMgr::CreateResMgr(aRes.GetBuffer());
600 MyWindow aWindow( NULL, pMyResMgr);
602 #ifdef TEST
603 aWindow.setApplication(this);
604 #endif
606 Execute();
608 delete pMyResMgr;
611 MyApp aMyApp;
612 #else
614 #ifdef WNT
615 extern "C" void __cdecl StartInvader(Window* pParent , ResMgr* pRes)
616 #else
617 extern "C" void StartInvader(Window* pParent, ResMgr* pRes )
618 #endif
620 MyWindow *pInvaderWin = new MyWindow(pParent, pRes);
621 pInvaderWin->Show();
623 #endif