View Javadoc

1   /* $Id: World.java 22 2008-12-30 17:33:44Z mihlon $ */
2   
3   //////////////////////////////////////////////////////////////////////////////
4   //                                                                          //
5   //   This program is free software: you can redistribute it and/or modify   //
6   //   it under the terms of the GNU General Public License as published by   //
7   //   the Free Software Foundation, either version 3 of the License, or      //
8   //   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, see <http://www.gnu.org/licenses/>.  //
17  //                                                                          //
18  //////////////////////////////////////////////////////////////////////////////
19  
20  package cs.pancava.caltha.worlds;
21  
22  import java.util.ArrayList;
23  
24  /**
25   * <p><b>Tato trida obsahuje jednotlive elementy prave vytvoreneho sveta.</b></p>
26   *
27   * @author Milan Vaclavik<br />
28   * @version $Revision: 22 $<br />
29   * $LastChangedBy: mihlon $
30   */
31  public class World
32  {
33      /**
34       * Identifikuje objekt jako mistnost.
35       */
36      public static final int WORLD_ROOM = 1;
37  
38      /**
39       * Identifikuje objekt jako trasu mezi dvema objekty (mistnostmi).
40       */
41      public static final int WORLD_ROUTE = 2;
42  
43      /**
44       * Seznam vsech mistnosti ve vytvorenem svete.
45       */
46      private ArrayList<GameEntity>roomsAL = new ArrayList<GameEntity>();
47  
48      /**
49       * Seznam vsech tras mezi dvema objekty (mistnostmi) ve vytvorenem svete.
50       */
51      private ArrayList<GameEntity>routesAL = new ArrayList<GameEntity>();
52  
53      /**
54       * Nazev vytvoreneho sveta.
55       */
56      private String worldNameS;
57  
58      /**
59       * Konstruktor.
60       * @param n String : Nazev vytvareneho sveta.
61       */
62      public World(final String n)
63      {
64          this.worldNameS = n;
65  
66          // Vynulovani citace -> zacali jsme tvorit novy svet.
67          GameEntity.resetCounter();
68      }
69  
70      /**
71       * Vlozi vytvorenou herni instanci sveta do daneho seznamu, obsahuji jednotlive
72       * elementy vytvoreneho sveta.
73       * @param ge GameEntity : Vkladana instance herniho sveta.
74       */
75      public final void addGameEntity(final GameEntity ge)
76      {
77          switch (ge.getType())
78          {
79              case World.WORLD_ROOM:
80                                      this.roomsAL.add(ge);
81                                      break;
82              case World.WORLD_ROUTE:
83                                      this.routesAL.add(ge);
84                                      break;
85              default:
86                       // TODO: Neznamy objekt, proto ho nevlozime do zadneho seznamu, vadi to, kdyz se pouze takto osetri tato udalost ?
87                       break;
88          }
89      }
90  
91      /**
92       * Vrati pole vsech mistnosti (objektu) ve vytvorenem svete.
93       * @return ArratList : Pole vsech obektu ve vytvorenem svete.
94       */
95      public final ArrayList<GameEntity> getWorldRoomsAL()
96      {
97          return this.roomsAL;
98      }
99  
100     /**
101      * Vrati pole vsech spoju (cest) ve vytvorenem svete.
102      * @return ArratList : Pole vsech obektu ve vytvorenem svete.
103      */
104     public final ArrayList<GameEntity> getWorldRoutesAL()
105     {
106         return this.routesAL;
107     }
108 
109     /**
110      * Vrati objekt ze seznamu urceny indexem.
111      * @param indexObektu int : Index objektu ze seznamu, ktery budeme vracet.
112      * @return GameEntity : Prvek ze seznamu, ktery vracime
113      */
114     public final GameEntity getWorldRoomsAL(final int indexObektu)
115     {
116         if (!this.roomsAL.isEmpty() && indexObektu > -1 && indexObektu < this.roomsAL.size())
117         {
118             return this.roomsAL.get(indexObektu);
119         }
120 
121         // Seznam je prazdny a nebo jsme zadali spatny rozsah indexu.
122         return null;
123     }
124 
125     /**
126      * Vrati objekt ze seznamu urceny indexem.
127      * @param indexObektu int : Index objektu ze seznamu, ktery budeme vracet.
128      * @return GameEntity : Prvek ze seznamu, ktery vracime
129      */
130     public final GameEntity getWorldRoutesAL(final int indexObektu)
131     {
132         if (!this.routesAL.isEmpty() && indexObektu > -1 && indexObektu < this.routesAL.size())
133         {
134             return this.routesAL.get(indexObektu);
135         }
136 
137         // Seznam je prazdny a nebo jsme zadali spatny rozsah indexu.
138         return null;
139     }
140 
141     /**
142      * Aktualizuje lokaci konkretniho objektu.
143      * @param ge GameEntity: Herni objekt, ktery budeme aktualizovat.
144      * @param index : Index obektu, u ktereho provadime aktualizaci lokace.
145      * @param x     : X-ova souradnice nove pozice.
146      * @param y     : Y-ova souradnice nove pozice.
147      */
148     public final void updateGameEntityLocation(final GameEntity ge, final int index, final int x, final int y)
149     {
150         switch (ge.getType())
151         {
152             case World.WORLD_ROOM:
153                                     if (!this.roomsAL.isEmpty() && index > -1 && index < this.roomsAL.size())
154                                     {
155                                         this.roomsAL.get(index).setLocationX(x);
156                                         this.roomsAL.get(index).setLocationY(y);
157                                     }
158                                     break;
159             default:
160                      // TODO: Neznamy objekt, proto ho nevlozime do zadneho seznamu, vadi to, kdyz se pouze takto osetri tato udalost ?
161                      break;
162         }
163     }
164 
165     /**
166      * Pokud smazeme nejakou mistnost, tak ztratime i index do seznamu mistnost -> do seznamu se take pristupuje primo
167      * s pomoci indexu. Proto posuneme indexy u zbyvajicich mistnosti.
168      * @param odstranenyIndex int : Od jake pozice budeme posouvat indexy.
169      */
170     public void updateRooms(final int odstranenyIndex)
171     {
172         final int pocet = roomsAL.size();
173 
174         // Indexy se nebudou posouvat, pokud odstranujeme posledni prvek a nebo kdyz je zadany index mimo interval
175         if (odstranenyIndex < pocet && odstranenyIndex > -1)
176         {
177             for (int i = odstranenyIndex; i < pocet; i = i + 1)
178             {
179                 this.getWorldRoomsAL(i).downID();
180             }
181         }
182     }
183 }