arcade/ArcadeGame

From ggc

Jump to: navigation, search

001 package arcade;
002 
003 import java.awt.geom.Point2D;
004 import java.util.TreeSet;
005 
006 import fang.StringSprite;
007 
008 /**
009  * This class displays any number of games
010  * and lets the player choose which one to 
011  * play.  ArcadeGames can also be nested
012  * which means that they can be added to other
013  * ArcadeGames.
014  @author jam
015  *
016  */
017 public class ArcadeGame
018       extends ArcadeLevel
019 {
020   /**all of the games to choose from in this arcade
021    * in alphabetical order*/
022   private TreeSet<ArcadeLevel> levels;
023   private String name;
024   /**
025    * displays the names of the games
026    */
027   public void startLevel()
028   {
029     if(name==nullname=getClass().getCanonicalName();
030     if(levels==null)
031     {
032       levels=new TreeSet<ArcadeLevel>();
033     }
034     if(getParent()==nullsetParent(this);
035     double i=0;
036     for(ArcadeLevel level: levels)
037     {
038       StringSprite name=new StringSprite(level.toString());
039       name.setLineHeight(1.0/levels.size());
040       if(name.getWidth()>0.9name.setWidth(0.9);
041       //name.leftJustify();
042       //name.topJustify();
043       name.setLocation(0.5(i+0.5)/levels.size());
044       canvas.addSprite(name);
045       i++;
046     }
047   }
048 
049   /**this method must be called before the level
050    * starts.  Call this method to add a game that
051    * could be played in the arcade.  To convert a
052    * game that extends GameLevel to one that extends
053    * ArcadeLevel, just change what it extends and
054    * optionally override the toString method.
055    @param levelsToAdd the level(s) to add
056    */
057   public void addArcadeLevel(ArcadeLevel ... levelsToAdd)
058   {
059     if(levels==nulllevels=new TreeSet<ArcadeLevel>();
060     for(ArcadeLevel level: levelsToAdd)
061     {
062       levels.add(level);
063       level.setParent(this);
064     }
065   }
066 
067   /**
068    * gets the location of a click to determine
069    * which game to launch.  The games are vertically
070    * separated.  The click location is turned into
071    * an index into the set of games by scaling
072    * the y location of the click.
073    */
074   public void advanceFrame(double elapsed)
075   {
076     Point2D.Double click=getPlayer().getMouse().getClickLocation();
077     if(click!=null)
078     {
079       int position=(int)(click.y*levels.size());
080       System.out.println("position is "+position);
081       ArcadeLevel nextLevel=
082           levels.toArray(new ArcadeLevel[0])[position];
083       nextLevel.setParent(this);
084       finishLevel();
085       setNextLevel(nextLevel);
086       System.out.println("finished");
087     }
088   }
089 
090   /**this method must be called before the level
091    * starts.  Call this method to change the
092    default name of this arcade.
093    @param arcadeName the name of this arcade
094    */
095   public void setName(String arcadeName)
096   {
097     name=arcadeName;
098   }
099 
100   /**
101    * returns the name of this arcade
102    */
103   public String toString()
104   {
105     if(name==nullreturn getClass().getCanonicalName();
106     return name;
107   }
108 }
109 


Download/View arcade/ArcadeGame.java





Views
Personal tools
Add to 
del.icio.usAdd to 
diggAdd to 
FacebookAdd to 
favoritesAdd to 
GoogleAdd to 
MySpaceAdd to 
PrintAdd to 
SlashdotAdd to 
StumbleUponAdd to 
Twitter