1   import java.awt.*;
2   import java.awt.event.*;
3   import javax.swing.*;
4   import java.applet.*;
5   import java.awt.image.*;
6   import java.awt.geom.*;
7   import java.net.*;
8   import java.io.*;
9   import java.util.*;
10  
11  /**
12   * Simple class for displaying an image
13   * 
14   * @see Game
15   * @version 1
16   * @author Matt, Sam
17   */
18  class Splash extends Component
19  {
20      /**
21       * The Image to display
22       */
23      Image img;
24      /**
25       * Construct a new Splash with the image.
26       * 
27       * @param img the Image to paint
28       */
29      Splash(Image img)
30      {
31          this.img = img;
32      }
33      /**
34       * Draw the image
35       * 
36       * @param g the Graphics
37       */
38      public void paint(Graphics g)
39      {
40          g.drawImage(img, 0, 0, this);
41      }
42  }
43  
44  /*
45   * <applet code="Game" width = 640 height = 450>
46   * </applet>
47   */
48  /**
49   * Main class to handle the switching between game modes, and initiation of the game.
50   * 
51   * @see Grid
52   * @see Battle
53   * @see Shop
54   * @see PlayerLoader
55   * @see PlayerSaver
56   * @see MapLoader
57   * @version 500
58   * @author Matt, Sam
59   */
60  public class Game extends Applet implements ActionListener, BattleListener, BattleEndListener, RedirectListener, ShopEndListener, GameOverListener, SaveListener
61  {
62      Grid grid;
63      Battle battle;
64      Shop shop;
65      MapLoader mp;
66      Splash sp;
67      Panel spp;
68      Button bn, bl, bname, ok;
69      TextField tname;
70      TextArea savedata;
71      boolean num;
72      int win;
73  
74      Vector mc;
75      // Need one for each type to instanciate the images
76      // STUPID Applet Security Model!!
77      // Should be able to read from directory that exists in!!
78      Person p;
79      Trainer t;
80      Player pl;
81      Grass g;
82      Rock r;
83      Floor f;
84      Building b;
85      Wall w;
86      Redirect re;
87      Image /*pf, pef, tef,*/ sh, splash, gover, gwin;
88      ImageMap[] monsters;
89      Graphics graph;
90  
91      /**
92       * Starts the game displaying the default Splash Screen to the user.
93       */
94      public void init()
95      {
96          num = true;
97          win = -1;
98          setLayout(new BorderLayout());
99  
100         setUpImages();
101 
102         shop = new Shop(pl, sh);
103         shop.addShopEndListener(this);
104         shop.addSaveListener(this);
105 
106         sp = new Splash(splash);
107         spp = new Panel();
108         bn = new Button("New Game");
109         bn.addActionListener(this);
110         bl = new Button("Load Game");
111         bl.addActionListener(this);
112         ok = new Button("OK");
113         ok.addActionListener(this);
114         spp.add(bn);
115         spp.add(bl);
116         add(sp, BorderLayout.CENTER);
117         add(spp, BorderLayout.SOUTH);
118 
119         bname = new Button("Play");
120         bname.addActionListener(this);
121         tname = new TextField(15);
122         tname.addActionListener(this);
123 
124         savedata = new TextArea();
125 
126     }
127 
128     /**
129      * Handle a battleBegin Event, ie pause Grid,
130      * display the Battle system and move the MapObjects
131      * between the two.
132      * 
133      * @see MapObject
134      * @see Grid
135      * @see Battle
136      * @see Player
137      * @see People
138      * @see Monster
139      * 
140      * @param e the BattleEvent
141      */
142     public void battleBegin(BattleEvent e)
143     {
144         MapObject m = e.getOpponent();
145         grid.transferFocus();
146 
147         // IF'd b/c using varying images
148         if(m.getType() == MapObject.PERSON)
149         {
150             battle = new Battle(e.getPlayer(), e.getOpponent(), monsters);
151         }
152         else if(m.getType() == MapObject.TRAINER)
153         {
154             battle = new Battle(e.getPlayer(), e.getOpponent(), monsters);
155         }
156         else if(m.getType() == MapObject.MONSTER)
157         {
158             battle = new Battle(e.getPlayer(), e.getOpponent(), monsters);
159         }
160         battle.addBattleEndListener(this);
161         battle.addGameOverListener(this);
162         removeAll();
163         add(battle, BorderLayout.CENTER);
164         battle.requestFocus();
165         battle.setSize(getWidth(), getHeight());
166         battle.paintAll(graph);
167     }
168     /**
169      * Handle a battleEnd Event, ie unpause Grid,
170      * display the Grid system, destroy Battle system and move the MapObjects
171      * between the two.
172      * 
173      * @see MapObject
174      * @see Grid
175      * @see Battle
176      * @see Player
177      * @see People
178      * @see Monster
179      * 
180      * @param e the BattleEndEvent
181      */
182     public void battleEnd(BattleEndEvent e)
183     {
184         pl = e.getPlayer();
185         pl.stopBattle();
186         battle.transferFocus();
187         grid.setPlayer(pl);
188         grid.fillMonsters();
189         removeAll();
190         add(grid, BorderLayout.CENTER);
191         grid.requestFocus();
192         grid.setSize(getWidth(), getHeight());
193         grid.paintAll(graph);
194     }
195     /**
196      * Handle a shopEnd Event, ie display the Shop, and move 
197      * Player between the two
198      * 
199      * @see MapObject
200      * @see Grid
201      * @see Battle
202      * @see Player
203      * @see People
204      * @see Monster
205      * 
206      * @param e the ShopEndEvent
207      */
208     public void shopEnd(ShopEndEvent e)
209     {
210         shop.transferFocus();
211         pl = e.getPlayer();
212         pl.stopBattle();
213         grid.fillMonsters();
214         grid.setPlayer(pl);
215         removeAll();
216         add(grid, BorderLayout.CENTER);
217         grid.requestFocus();
218         grid.setSize(getWidth(), getHeight());
219         grid.paintAll(graph);
220     }
221     /**
222      * Handle a redirect event, ie load the new map
223      * specified in the RedirectEvent.  If the map specified
224      * is Shop load the Shop system.
225      * 
226      * @see MapObject
227      * @see Grid
228      * @see RedirectEvent
229      * @see MapLoader
230      * 
231      * @param e the RedirectEvent
232      */
233     public void redirectFired(RedirectEvent e)
234     {
235         String name = e.getMapName();
236         pl = e.getPlayer();
237         pl.stopBattle();
238         if(name.equalsIgnoreCase("ItemShop"))
239         {
240             shop = new Shop(pl, sh);
241             shop.addShopEndListener(this);
242             shop.addSaveListener(this);
243             pl.battle();
244             removeAll();
245             add(shop, BorderLayout.CENTER);
246             shop.requestFocus();
247             shop.setSize(getWidth(), getHeight());
248             shop.paintAll(graph);
249             return;
250         }
251         else if(name.equalsIgnoreCase("Winner"))
252         {
253             sp = new Splash(gwin);
254             removeAll();
255             add(sp, BorderLayout.CENTER);
256             add(ok, BorderLayout.SOUTH);
257             win = 0;
258             paintAll(graph);
259             return;
260         }
261         try
262         {
263             URL url = new URL(getCodeBase(), "maps/" + name + ".mml");
264             URLConnection uc = url.openConnection();
265             BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream()));
266             mp = new MapLoader(in);
267         }
268         catch(Exception ee) { ee.printStackTrace(); }
269         synchronized(grid)
270         {
271             grid.destroy();
272             removeAll();
273             grid = mp.getGrid();
274             grid.addBattleListener(this);
275             grid.addRedirectListener(this);
276             grid.init();
277             grid.setPlayer(pl);
278             add(grid, BorderLayout.CENTER);
279             grid.requestFocus();
280             grid.setSize(getWidth(), getHeight());
281             grid.paintAll(graph);
282         }
283     }
284     /**
285      * Handle a GameOver Event, ie display the Game Over splash
286      * and offer the player the option to rebegin.
287      * 
288      * @see MapObject
289      * @see Grid
290      * @see Battle
291      * @see Player
292      * @see People
293      * @see Monster
294      * 
295      * @param e the GameOverEvent
296      */
297     public void gameOver(GameOverEvent e)
298     {
299         removeAll();
300         sp = new Splash(gover);
301         spp.removeAll();
302         spp.add(bn);
303         spp.add(bl);
304         add(sp, BorderLayout.CENTER);
305         add(spp, BorderLayout.SOUTH);
306         sp.setSize(getWidth(), getHeight());
307         paintAll(graph);
308     }
309     /**
310      * Handle a SaveGame Event, ie take the Player
311      * specified in SaveEvent and pass it to PlayerSaver
312      * then place this text in a TextArea for the user to save it.
313      * 
314      * @see MapObject
315      * @see PlayerSaver
316      * @see SaveEvent
317      * @see Player
318      * @see People
319      * @see Monster
320      * 
321      * @param e the SaveEvent
322      */
323     public void saveGame(SaveEvent e)
324     {
325         savedata.setEditable(false);
326         removeAll();
327         PlayerSaver ps = new PlayerSaver(e.getPlayer());
328         savedata.setText(ps.toString());
329         add(savedata, BorderLayout.CENTER);
330         add(ok, BorderLayout.SOUTH);
331         add(new Label("Copy the text below and save it in a text file for loading later", Label.CENTER), BorderLayout.NORTH);
332         paintAll(graph);
333     }
334     /**
335      * Handle Button clicks to load, start a new game, etc.
336      * 
337      * @param e the ActionEvent
338      */
339     public void actionPerformed(ActionEvent e)
340     {
341         if(e.getSource() == bn)
342         {
343             removeAll();
344             spp = new Panel();
345             spp.add(new Label("Please Enter Your Name"));
346             spp.add(tname);
347             spp.add(bname);
348             add(sp, BorderLayout.CENTER);
349             add(spp, BorderLayout.SOUTH);
350             tname.requestFocus();
351             paintAll(graph);
352         }
353         else if(e.getSource() == bname || e.getSource() == tname)
354         {
355             String name = tname.getText();
356             if(name.equals(""))
357                 name = "Player1";
358             // Setup pl for def temp
359 
360             loadPlayerFile("Default");
361 
362             pl.setName(name);
363             removeAll();
364             try
365             {
366                 URL url = new URL(getCodeBase(), "maps/Saphire.mml");
367                 URLConnection uc = url.openConnection();
368                 BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream()));
369                 mp = new MapLoader(in);
370             }
371             catch(Exception ee) { ee.printStackTrace(); }
372             initGrid();
373         }
374         else if(e.getSource() == ok)
375         {
376             removeAll();
377             savedata.setText("");
378             if(win == 0)
379             {
380                 savedata.setEditable(false);
381                 removeAll();
382                 PlayerSaver ps = new PlayerSaver(pl);
383                 savedata.setText(ps.toString());
384                 add(savedata, BorderLayout.CENTER);
385                 add(ok, BorderLayout.SOUTH);
386                 add(new Label("Copy the text below and save it in a text file for loading later", Label.CENTER), BorderLayout.NORTH);
387                 paintAll(graph);
388                 win = 1;
389             }
390             else if(win == 1)
391             {
392                 removeAll();
393                 spp.removeAll();
394                 sp = new Splash(splash);
395                 spp.add(bn);
396                 spp.add(bl);
397                 add(sp, BorderLayout.CENTER);
398                 add(spp, BorderLayout.SOUTH);
399                 win = -1;
400                 paintAll(graph);
401             }
402             else
403             {
404                 add(shop, BorderLayout.CENTER);
405                 shop.requestFocus();
406                 shop.setSize(getWidth(), getHeight());
407                 shop.repaint();
408             }
409         }
410         else if(e.getSource() == bl)
411         {
412             if(num)
413             {
414                 savedata.setEditable(true);
415                 removeAll();
416                 savedata.setText("");
417                 add(savedata, BorderLayout.CENTER);
418                 add(bl, BorderLayout.SOUTH);
419                 add(new Label("Paste the text you copied and saved earlier below", Label.CENTER), BorderLayout.NORTH);
420                 paintAll(graph);
421                 num = false;
422                 savedata.requestFocus();
423             }
424             else
425             {
426                 try
427                 {
428                     BufferedReader in = new BufferedReader(new StringReader(savedata.getText()));
429                     PlayerLoader pload = new PlayerLoader(in);
430                     pl = pload.getPlayer();
431                 }
432                 catch(Exception ee) 
433                 {
434                     removeAll();
435                     spp.removeAll();
436                     spp.add(bn);
437                     spp.add(bl);
438                     add(sp, BorderLayout.CENTER);
439                     add(spp, BorderLayout.SOUTH);
440                     add(new Label("Invalid Player Save File", Label.CENTER), BorderLayout.NORTH);
441                     paintAll(graph);
442                     return;
443                 }
444 
445                 num = true;
446                 removeAll();
447                 try
448                 {
449                     URL url = new URL(getCodeBase(), "maps/Saphire.mml");
450                     URLConnection uc = url.openConnection();
451                     BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream()));
452                     mp = new MapLoader(in);
453                 }
454                 catch(Exception ee) { ee.printStackTrace(); }
455                 initGrid();
456             }
457         }
458     }
459     /**
460      * Draw the Components
461      */
462     public void paint(Graphics g)
463     {
464         g.drawString("Loading...", getWidth()/2, getHeight()/2);
465         this.graph = g;
466         super.paint(g);
467     }
468     /**
469      * Load all the required Images and instantiate the
470      * classes that depend on them.
471      */
472     public void setUpImages()
473     {
474         Image[] img = new Image[4];
475         img[0] = getImage(getCodeBase(), "images/PersonF-N.gif");
476         img[1] = getImage(getCodeBase(), "images/PersonB-N.gif");
477         img[2] = getImage(getCodeBase(), "images/PersonL-N.gif");
478         img[3] = getImage(getCodeBase(), "images/PersonR-N.gif");
479         p = new Person();
480         p.setImage(img);
481 
482         img[0] = getImage(getCodeBase(), "images/TrainerF.gif");
483         img[1] = getImage(getCodeBase(), "images/TrainerB.gif");
484         img[2] = getImage(getCodeBase(), "images/TrainerL.gif");
485         img[3] = getImage(getCodeBase(), "images/TrainerR.gif");
486         t = new Trainer();
487         t.setImage(img);
488 
489         img[0] = getImage(getCodeBase(), "images/PersonF-Still.gif");
490         img[1] = getImage(getCodeBase(), "images/PersonB-Still.gif");
491         img[2] = getImage(getCodeBase(), "images/PersonL-Still.gif");
492         img[3] = getImage(getCodeBase(), "images/PersonR-Still.gif");
493         pl = new Player();
494         pl.setImage(img);
495 
496         img = new Image[1];
497         img[0] = getImage(getCodeBase(), "images/Grass.gif");
498         g = new Grass();
499         g.setImage(img);
500 
501         img[0] = getImage(getCodeBase(), "images/Rock.gif");
502         r = new Rock();
503         r.setImage(img);
504 
505         img[0] = getImage(getCodeBase(), "images/Floor.gif");
506         f = new Floor();
507         f.setImage(img);
508 
509         img[0] = getImage(getCodeBase(), "images/Building.gif");
510         b = new Building();
511         b.setImage(img);
512 
513         img[0] = getImage(getCodeBase(), "images/Wall.gif");
514         w = new Wall();
515         w.setImage(img);
516 
517         img[0] = getImage(getCodeBase(), "images/Redirect.gif");
518         re = new Redirect();
519         re.setImage(img);
520 
521         monsters = new ImageMap[20];
522         monsters[0] = new ImageMap("Alligatrix-B", getImage(getCodeBase(), "images/Alligatrix-B.jpg"));
523         monsters[1] = new ImageMap("Alligatrix-F", getImage(getCodeBase(), "images/Alligatrix-F.jpg"));
524 
525         monsters[2] = new ImageMap("Eurekasaur-B", getImage(getCodeBase(), "images/Eurekasaur-B.jpg"));
526         monsters[3] = new ImageMap("Eurekasaur-F", getImage(getCodeBase(), "images/Eurekasaur-F.jpg"));
527 
528         monsters[4] = new ImageMap("Flamesaur-B", getImage(getCodeBase(), "images/Flamesaur-B.jpg"));
529         monsters[5] = new ImageMap("Flamesaur-F", getImage(getCodeBase(), "images/Flamesaur-F.jpg"));
530 
531         monsters[6] = new ImageMap("Groundyx-B", getImage(getCodeBase(), "images/Groundyx-B.jpg"));
532         monsters[7] = new ImageMap("Groundyx-F", getImage(getCodeBase(), "images/Groundyx-F.jpg"));
533 
534         monsters[8] = new ImageMap("MoPyro-B", getImage(getCodeBase(), "images/MoPyro-B.jpg"));
535         monsters[9] = new ImageMap("MoPyro-F", getImage(getCodeBase(), "images/MoPyro-F.jpg"));
536 
537         monsters[10] = new ImageMap("Rhono-B", getImage(getCodeBase(), "images/Rhono-B.jpg"));
538         monsters[11] = new ImageMap("Rhono-F", getImage(getCodeBase(), "images/Rhono-F.jpg"));
539 
540         monsters[12] = new ImageMap("Sharkasaur-B", getImage(getCodeBase(), "images/Sharkasaur-B.jpg"));
541         monsters[13] = new ImageMap("Sharkasaur-F", getImage(getCodeBase(), "images/Sharkasaur-F.jpg"));
542 
543         monsters[14] = new ImageMap("Shockachu-B", getImage(getCodeBase(), "images/Shockachu-B.jpg"));
544         monsters[15] = new ImageMap("Shockachu-F", getImage(getCodeBase(), "images/Shockachu-F.jpg"));
545 
546         monsters[16] = new ImageMap("Smelltor-B", getImage(getCodeBase(), "images/Smelltor-B.jpg"));
547         monsters[17] = new ImageMap("Smelltor-F", getImage(getCodeBase(), "images/Smelltor-F.jpg"));
548 
549         monsters[18] = new ImageMap("Voltasaur-B", getImage(getCodeBase(), "images/Voltasaur-B.jpg"));
550         monsters[19] = new ImageMap("Voltasaur-F", getImage(getCodeBase(), "images/Voltasaur-F.jpg"));
551 
552         //pf = getImage(getCodeBase(), "images/PlayerFight.gif");
553         //pef = getImage(getCodeBase(), "images/PersonFight.gif");
554         //tef = getImage(getCodeBase(), "images/TrainerFight.gif");
555         splash = getImage(getCodeBase(), "images/Splash.jpg");
556         gover = getImage(getCodeBase(), "images/GameOver.jpg");
557         sh = getImage(getCodeBase(), "images/Shop.gif");
558         gwin = getImage(getCodeBase(), "images/Win.jpg");
559 
560     }
561     /**
562      * Get the Grid ready to be redisplayed
563      * after it was removed, ie add the Monsters.
564      */
565     public void initGrid()
566     {
567         grid = mp.getGrid();
568         grid.addBattleListener(this);
569         grid.addRedirectListener(this);
570         add(grid, BorderLayout.CENTER);
571         grid.setSize(getWidth(), getHeight());
572         grid.init();
573         grid.setPlayer(pl);
574         grid.requestFocus();
575     }
576     /**
577      * Handle the loading of a Player file.
578      * 
579      * @see Player
580      * @see PlayerLoader
581      */
582     public void loadPlayerFile(String file)
583     {
584         try
585         {
586             URL url = new URL(getCodeBase(), "maps/" + file + ".pml");
587             URLConnection uc = url.openConnection();
588             BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream()));
589             PlayerLoader pload = new PlayerLoader(in);
590             pl = pload.getPlayer();
591         }
592         catch(Exception ee) { ee.printStackTrace(); }
593     }
594 }