View Javadoc
1   package de.jos.game.objects.screen;
2   
3   import java.awt.Graphics2D;
4   import java.util.ArrayList;
5   import java.util.HashMap;
6   import java.util.List;
7   import java.util.Map;
8   
9   import org.apache.commons.lang.StringUtils;
10  import org.apache.commons.logging.Log;
11  import org.apache.commons.logging.LogFactory;
12  
13  import com.golden.gamedev.GameEngine;
14  import com.golden.gamedev.object.AnimatedSprite;
15  import com.golden.gamedev.object.GameFont;
16  
17  import de.jos.game.objects.ResourceContainer;
18  import de.jos.game.xml.Combination;
19  import de.jos.game.xml.Level;
20  
21  /***
22   * 
23   * @author root
24   * 
25   */
26  public class CombinationScreen extends MultiPageScreen {
27  
28    private static Log log = LogFactory.getLog(CombinationScreen.class);
29  
30    private static final int COMBINATIONS_PER_PAGE = 4;
31  
32    private Map<Integer, List<CombinationInfoLine>> combinationPagesMap = new HashMap<Integer, List<CombinationInfoLine>>();
33  
34    private Level level = null;
35  
36    public CombinationScreen(GameEngine parent, Level level, ResourceContainer resourceContainer) {
37      super(parent, resourceContainer, null, Screen.GAME);
38      this.level = level;
39    }
40  
41    public void initResources() {
42      super.initResources();
43  
44      int numberOfPages = ((level.getAccumulatedCombinations().size() - 1) / COMBINATIONS_PER_PAGE) + 1;
45  
46      setNumberOfPages(numberOfPages);
47  
48      log.info("Combination-Pages : " + numberOfPages);
49  
50      // init the combinationsPagesMap
51      // TODO das hier ist die number of pages
52      for (int i = 0; i < numberOfPages; i++) {
53        List<CombinationInfoLine> combinations = new ArrayList<CombinationInfoLine>();
54        combinationPagesMap.put(Integer.valueOf(i + 1), combinations);
55      }
56  
57      ResourceContainer resourceContainer = getResourceContainer();
58  
59      int index = 1;
60      for (Combination tmpCombination : level.getAccumulatedCombinations()) {
61        CombinationInfoLine infoLine = new CombinationInfoLine();
62        infoLine.setLevelString(String.valueOf(tmpCombination.getLevelId()));
63        infoLine.setSpriteColor1(resourceContainer.createCanisterSprite(tmpCombination.getColors().getColor1()));
64        infoLine.setSpriteColor2(resourceContainer.createCanisterSprite(tmpCombination.getColors().getColor2()));
65        infoLine.setSpriteColor3(resourceContainer.createCanisterSprite(tmpCombination.getColors().getColor3()));
66        infoLine.setDescription(tmpCombination.getAction().getDescription());
67        infoLine.setDescriptionShort(tmpCombination.getAction().getDescriptionShort());
68        infoLine.setIndex((index - 1) % COMBINATIONS_PER_PAGE);
69        infoLine.setFont(resourceContainer.getGameFont());
70        infoLine.init();
71  
72        int pageIndex = ((index - 1) / COMBINATIONS_PER_PAGE) + 1;
73        combinationPagesMap.get(Integer.valueOf(pageIndex)).add(infoLine);
74        index++;
75      }
76    }
77  
78    public void update(long elapsedTime) {
79      super.update(elapsedTime);
80    }
81  
82    @Override
83    public void executePagingAction() {
84    // nicht zu tun
85    }
86  
87    public void render(Graphics2D graphics) {
88      getResourceContainer().getBackground().render(graphics);
89  
90      List<CombinationInfoLine> combinations = combinationPagesMap.get(Integer.valueOf(getCurrentPage()));
91  
92      for (CombinationInfoLine tmpInfoLine : combinations) {
93        tmpInfoLine.render(graphics);
94      }
95  
96      getFramework().render(graphics);
97    }
98  
99    private static final class CombinationInfoLine {
100 
101     private static final int OFFSET_Y = 60;
102 
103     private static final int OFFSET_X_COLOR1 = 80;
104     private static final int OFFSET_X_COLOR2 = 120;
105     private static final int OFFSET_X_COLOR3 = 160;
106 
107     private AnimatedSprite spriteColor1 = null;
108     private AnimatedSprite spriteColor2 = null;
109     private AnimatedSprite spriteColor3 = null;
110     private String levelString = null;
111     private String description = null;
112     private String descriptionShort = null;
113 
114     private GameFont font = null;
115 
116     private int index = -1;
117 
118     public void init() {
119       int y = getIndex() * 60 + OFFSET_Y;
120       getSpriteColor1().setX(OFFSET_X_COLOR1);
121       getSpriteColor1().setY(y);
122       getSpriteColor1().setFrame(32);
123       getSpriteColor2().setX(OFFSET_X_COLOR2);
124       getSpriteColor2().setY(y);
125       getSpriteColor2().setFrame(32);
126       getSpriteColor3().setX(OFFSET_X_COLOR3);
127       getSpriteColor3().setY(y);
128       getSpriteColor3().setFrame(32);
129     }
130 
131     public void render(Graphics2D graphics) {
132       getSpriteColor1().render(graphics);
133       getSpriteColor2().render(graphics);
134       getSpriteColor3().render(graphics);
135 
136       int y = getIndex() * 60 + OFFSET_Y;
137       getFont().drawString(graphics, getLevelString(), 20, y);
138       getFont().drawString(graphics, StringUtils.upperCase(getDescriptionShort()), 230, y);
139       getFont().drawString(graphics, StringUtils.upperCase(getDescription()), 230, y + 20);
140     }
141 
142     private String getDescription() {
143       return description;
144     }
145 
146     public void setIndex(int index) {
147       this.index = index;
148     }
149 
150     private void setDescription(String description) {
151       this.description = description;
152     }
153 
154     private String getLevelString() {
155       return levelString;
156     }
157 
158     private void setLevelString(String levelString) {
159       this.levelString = levelString;
160     }
161 
162     private AnimatedSprite getSpriteColor1() {
163       return spriteColor1;
164     }
165 
166     private void setSpriteColor1(AnimatedSprite spriteColor1) {
167       this.spriteColor1 = spriteColor1;
168     }
169 
170     private AnimatedSprite getSpriteColor2() {
171       return spriteColor2;
172     }
173 
174     private void setSpriteColor2(AnimatedSprite spriteColor2) {
175       this.spriteColor2 = spriteColor2;
176     }
177 
178     private AnimatedSprite getSpriteColor3() {
179       return spriteColor3;
180     }
181 
182     private void setSpriteColor3(AnimatedSprite spriteColor3) {
183       this.spriteColor3 = spriteColor3;
184     }
185 
186     private int getIndex() {
187       return index;
188     }
189 
190     private GameFont getFont() {
191       return font;
192     }
193 
194     private void setFont(GameFont font) {
195       this.font = font;
196     }
197 
198     public String getDescriptionShort() {
199       return descriptionShort;
200     }
201 
202     public void setDescriptionShort(String descriptionShort) {
203       this.descriptionShort = descriptionShort;
204     }
205 
206   }
207 
208 }