View Javadoc

1   package erland.game.tileadventure;
2   /*
3    * Copyright (C) 2004 Erland Isaksson (erland_i@hotmail.com)
4    *
5    * This program is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU General Public License
7    * as published by the Free Software Foundation; either version 2
8    * of the License, or (at your option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with this program; if not, write to the Free Software
17   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18   *
19   */
20  
21  import erland.game.GamePanelInterface;
22  import erland.game.FpsCounter;
23  import erland.game.GameEnvironmentInterface;
24  import erland.game.tileadventure.rect.RectBlockContainerData;
25  import erland.game.tileadventure.isodiamond.IsoDiamondBlockContainerData;
26  
27  import java.awt.event.KeyListener;
28  import java.awt.event.KeyAdapter;
29  import java.awt.event.KeyEvent;
30  import java.awt.*;
31  
32  /***
33   * This is the class that controls the drawing on the screen and
34   * receives the commands from the user
35   */
36  public class TileAdventureMain implements GamePanelInterface {
37      /*** Block container for the tile map */
38      protected BlockContainerData cont;
39      /*** Indicates if the game should be quit */
40      protected boolean bQuit;
41      /*** Key listener that listens on keyboard command */
42      protected KeyListener keyListener;
43      /*** FPS counter object */
44      protected FpsCounter fps;
45      /*** Game environment object */
46      protected GameEnvironmentInterface environment;
47      /*** Indicates if cheat mode is activated */
48      protected boolean cheatMode;
49  
50      /*** TileAdventure model object that implements all game logic */
51      private TileAdventureModelInterface model;
52      /*** Indicates if the screen should be cleared next time */
53      private boolean bClearScreen = true;
54  
55      /***
56       * Creates a TileAdventureMain object using the specified model
57       * @param model The model that implements the game logic
58       */
59      public TileAdventureMain(TileAdventureModelInterface model) {
60          this.model = model;
61      }
62      public boolean isExit()
63      {
64          return bQuit;
65      }
66  
67      public void setCheatmode(boolean enable)
68      {
69          cheatMode = enable;
70          if(!enable) {
71              cheatMode = false;
72          }
73      }
74      public void exit()
75      {
76          environment.getScreenHandler().getContainer().removeKeyListener(keyListener);
77      }
78  
79      public void init(GameEnvironmentInterface environment)
80      {
81          this.environment = environment;
82          bQuit = false;
83          cheatMode = false;
84  
85          keyListener = new KeyAdapter() {
86              public void keyPressed(KeyEvent e) {
87                  if(e.getKeyCode() == KeyEvent.VK_ESCAPE) {
88                      bQuit = true;
89                  }else if(e.getKeyCode() == KeyEvent.VK_LEFT) {
90                      model.startMoveLeft();
91                  }else if(e.getKeyCode() == KeyEvent.VK_RIGHT) {
92                      model.startMoveRight();
93                  }else if(e.getKeyCode() == KeyEvent.VK_UP) {
94                      model.startMoveUp();
95                  }else if(e.getKeyCode() == KeyEvent.VK_DOWN) {
96                      model.startMoveDown();
97                  }else if(e.getKeyCode() == KeyEvent.VK_F1) {
98                      model.start();
99                  }else if(e.getKeyCode() == KeyEvent.VK_SPACE) {
100                     model.jump();
101                 }
102                 if(cheatMode) {
103                     if(e.getKeyCode()==KeyEvent.VK_1) {
104                         //model.setCheatModeParameter("ALLCARSSHOWSERVERCAR","NONE");
105                     }
106                 }
107             }
108             public void keyReleased(KeyEvent e) {
109                 if(e.getKeyCode() == KeyEvent.VK_LEFT) {
110                     model.stopMoveLeft();
111                 }else if(e.getKeyCode() == KeyEvent.VK_RIGHT) {
112                     model.stopMoveRight();
113                 }else if(e.getKeyCode() == KeyEvent.VK_UP) {
114                     model.stopMoveUp();
115                 }else if(e.getKeyCode() == KeyEvent.VK_DOWN) {
116                     model.stopMoveDown();
117                 }
118             }
119         };
120         environment.getScreenHandler().setCursor(null);
121         environment.getScreenHandler().getContainer().requestFocus();
122         environment.getScreenHandler().getContainer().addKeyListener(keyListener);
123 
124         fps = new FpsCounter(50);
125         cont = ((TileGameEnvironmentInterface)(environment.getCustomEnvironment())).createBlockContainer(20, 20, 10,10, 10);
126 
127         model.init(environment,cont);
128 
129         environment.getScreenHandler().getContainer().setBackground(Color.black);
130     }
131 
132     private int getMinPosY() {
133         return cont.getPositionY(0,0,cont.getSizeZ());
134     }
135     private void updateScrollOffset(GameObject obj) {
136         /*
137         // Scroll map
138         int scrollBorderLeft = cont.getSquareSizeX()*3;
139         int scrollBorderRight = cont.getSquareSizeX()*4;
140         int scrollBorderUp = cont.getSquareSizeY()*3;
141         int scrollBorderDown = cont.getSquareSizeY()*4;
142         if(obj.getPixelPosX()<(cont.getScrollingOffsetX()+scrollBorderLeft)) {
143             cont.setScrollingOffsetX(obj.getPixelPosX()-scrollBorderLeft);
144         }
145         if(obj.getPixelPosX()>(cont.getScrollingOffsetX()+cont.getDrawingSizeX()-scrollBorderRight)) {
146             cont.setScrollingOffsetX(obj.getPixelPosX()-cont.getDrawingSizeX()+scrollBorderRight);
147         }
148         if(obj.getPixelPosY()<(cont.getScrollingOffsetY()+scrollBorderUp)) {
149             cont.setScrollingOffsetY(obj.getPixelPosY()-scrollBorderUp);
150         }
151         if(obj.getPixelPosY()>(cont.getScrollingOffsetY()+cont.getDrawingSizeY()-scrollBorderDown)) {
152             cont.setScrollingOffsetY(obj.getPixelPosY()-cont.getDrawingSizeY()+scrollBorderDown);
153         }
154 
155         // Make sure we don't scroll the map to much
156         if(cont.getScrollingOffsetX()<0) {
157             cont.setScrollingOffsetX(0);
158         }
159         if(cont.getScrollingOffsetX()>cont.getScrollingSizeX()-cont.getDrawingSizeX()) {
160             cont.setScrollingOffsetX(cont.getScrollingSizeX()-cont.getDrawingSizeX());
161         }
162         if(cont.getScrollingOffsetY()<getMinPosY()) {
163             cont.setScrollingOffsetY(getMinPosY());
164         }
165         if(cont.getScrollingOffsetY()>cont.getScrollingSizeY()-cont.getDrawingSizeY()) {
166             cont.setScrollingOffsetY(cont.getScrollingSizeY()-cont.getDrawingSizeY());
167         }
168         */
169     }
170     public void update() {
171         if(model.isInitialized()) {
172             model.update();
173             updateScrollOffset(model.getPlayerObject());
174         }
175     }
176 
177     public void draw()
178     {
179         Graphics g = environment.getScreenHandler().getCurrentGraphics();
180         if(bClearScreen) {
181             g.clearRect(0,0,environment.getScreenHandler().getWidth(),environment.getScreenHandler().getHeight());
182         }else {
183             g.clearRect(0,0,environment.getScreenHandler().getWidth(),cont.getOffsetY()-1);
184         }
185         fps.update();
186         fps.draw(g,Color.red, 10,10);
187         g.setColor(Color.white);
188         g.drawString(model.getInfoString(),50,10);
189         g.setColor(Color.white);
190         if(cheatMode) {
191             g.setColor(Color.white);
192             g.drawString("CHEATMODE",80,10);
193         }
194         g.setClip(cont.getOffsetX(),cont.getOffsetY(),cont.getDrawingSizeX(),cont.getDrawingSizeY());
195         if(model.isInitialized()) {
196             model.getMap().draw(g);
197             bClearScreen = true;
198             if(!model.isStarted()) {
199                 bClearScreen = true;
200                 g.setColor(Color.white);
201                 g.clearRect(50,50,400,150);
202                 g.drawString("Press space to start game",100,100);
203                 if(model.isMultiplayer()) {
204                     if(model.getNoOfHumanPlayers()>1) {
205                         g.drawString("Currently "+(model.getNoOfHumanPlayers()-1)+" other human player(s) connected",100,120);
206                     }else {
207                         g.drawString("Only you and computer contolled cars are connected",100,120);
208                     }
209                     g.drawString("You can wait for more human players to connect",100,140);
210                 }
211             }
212         }else {
213             g.setColor(Color.white);
214             g.clearRect(50,50,400,150);
215             g.drawString("Loading game data, please wait...",100,100);
216         }
217     }
218 }