1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 
18 package com.replica.replicaisland;
19 
20 public class LevelBuilder extends BaseObject {
21     private final static int THEME_GRASS = 0;
22     private final static int THEME_ISLAND = 1;
23     private final static int THEME_SEWER = 2;
24     private final static int THEME_UNDERGROUND = 3;
25     private final static int THEME_LAB = 4;
26     private final static int THEME_LIGHTING = 5;
27     private final static int THEME_TUTORIAL = 6;
28 
29 
30     private final static int BACKGROUND_SUNSET = 0;
31     private final static int BACKGROUND_ISLAND = 1;
32     private final static int BACKGROUND_SEWER = 2;
33     private final static int BACKGROUND_UNDERGROUND = 3;
34     private final static int BACKGROUND_FOREST = 4;
35     private final static int BACKGROUND_ISLAND2 = 5;
36     private final static int BACKGROUND_LAB = 6;
37 
LevelBuilder()38     public LevelBuilder() {
39         super();
40     }
41 
42     @Override
reset()43     public void reset() {
44     }
45 
46 
buildBackground(int backgroundImage, int levelWidth, int levelHeight)47     public GameObject buildBackground(int backgroundImage, int levelWidth, int levelHeight) {
48         // Generate the scrolling background.
49         TextureLibrary textureLibrary = sSystemRegistry.shortTermTextureLibrary;
50 
51         GameObject background = new GameObject();
52 
53         if (textureLibrary != null) {
54 
55             int backgroundResource = -1;
56 
57             switch (backgroundImage) {
58                 case BACKGROUND_SUNSET:
59                     backgroundResource = R.drawable.background_sunset;
60                     break;
61                 case BACKGROUND_ISLAND:
62                     backgroundResource = R.drawable.background_island;
63                     break;
64                 case BACKGROUND_SEWER:
65                     backgroundResource = R.drawable.background_sewage;
66                     break;
67                 case BACKGROUND_UNDERGROUND:
68                     backgroundResource = R.drawable.background_underground;
69                     break;
70                 case BACKGROUND_FOREST:
71                     backgroundResource = R.drawable.background_grass2;
72                     break;
73                 case BACKGROUND_ISLAND2:
74                     backgroundResource = R.drawable.background_island2;
75                     break;
76                 case BACKGROUND_LAB:
77                     backgroundResource = R.drawable.background_lab01;
78                     break;
79                 default:
80                     assert false;
81             }
82 
83             if (backgroundResource > -1) {
84 
85                 // Background Layer //
86                 RenderComponent backgroundRender = new RenderComponent();
87                 backgroundRender.setPriority(SortConstants.BACKGROUND_START);
88 
89                 ContextParameters params = sSystemRegistry.contextParameters;
90                 // The background image is ideally 1.5 times the size of the largest screen axis
91                 // (normally the width, but just in case, let's calculate it).
92                 final int idealSize = (int)Math.max(params.gameWidth * 1.5f, params.gameHeight * 1.5f);
93                 int width = idealSize;
94                 int height = idealSize;
95 
96                 ScrollerComponent scroller3 =
97                         new ScrollerComponent(0.0f, 0.0f, width, height,
98                             textureLibrary.allocateTexture(backgroundResource));
99                 scroller3.setRenderComponent(backgroundRender);
100 
101                 // Scroll speeds such that the background will evenly match the beginning
102                 // and end of the level.  Don't allow speeds > 1.0, though; that would be faster than
103                 // the foreground, which is disorienting and looks like rotation.
104                 final float scrollSpeedX = Math.min((float)(width - params.gameWidth) / (levelWidth - params.gameWidth), 1.0f);
105                 final float scrollSpeedY = Math.min((float)(height - params.gameHeight) / (levelHeight - params.gameHeight), 1.0f);
106 
107 
108                 scroller3.setScrollSpeed(scrollSpeedX, scrollSpeedY);
109 
110                 backgroundRender.setCameraRelative(false);
111 
112                 background.add(scroller3);
113                 background.add(backgroundRender);
114             }
115         }
116         return background;
117     }
118 
addTileMapLayer(GameObject background, int priority, float scrollSpeed, int width, int height, int tileWidth, int tileHeight, TiledWorld world, int theme)119     public void addTileMapLayer(GameObject background, int priority, float scrollSpeed,
120             int width, int height, int tileWidth, int tileHeight, TiledWorld world,
121             int theme) {
122 
123         int tileMapIndex = 0;
124         switch(theme) {
125             case THEME_GRASS:
126                 tileMapIndex = R.drawable.grass;
127                 break;
128             case THEME_ISLAND:
129                 tileMapIndex = R.drawable.island;
130                 break;
131             case THEME_SEWER:
132                 tileMapIndex = R.drawable.sewage;
133                 break;
134             case THEME_UNDERGROUND:
135                 tileMapIndex = R.drawable.cave;
136                 break;
137             case THEME_LAB:
138                 tileMapIndex = R.drawable.lab;
139                 break;
140             case THEME_LIGHTING:
141                 tileMapIndex = R.drawable.titletileset;
142                 priority = SortConstants.OVERLAY; //hack!
143                 break;
144             case THEME_TUTORIAL:
145                 tileMapIndex = R.drawable.tutorial;
146                 break;
147             default:
148                 assert false;
149         }
150 
151         RenderComponent backgroundRender = new RenderComponent();
152         backgroundRender.setPriority(priority);
153 
154         //Vertex Buffer Code
155         TextureLibrary textureLibrary = sSystemRegistry.shortTermTextureLibrary;
156         TiledVertexGrid bg = new TiledVertexGrid(textureLibrary.allocateTexture(tileMapIndex),
157                 width, height, tileWidth, tileHeight);
158         bg.setWorld(world);
159 
160         //TODO: The map format should really just output independent speeds for x and y,
161         // but as a short term solution we can assume parallax layers lock in the smaller
162         // direction of movement.
163         float xScrollSpeed = 1.0f;
164         float yScrollSpeed = 1.0f;
165 
166         if (world.getWidth() > world.getHeight()) {
167         	xScrollSpeed = scrollSpeed;
168         } else {
169         	yScrollSpeed = scrollSpeed;
170         }
171 
172         ScrollerComponent scroller = new ScrollerComponent(xScrollSpeed, yScrollSpeed,
173                 width, height, bg);
174         scroller.setRenderComponent(backgroundRender);
175 
176         background.add(scroller);
177         background.add(backgroundRender);
178         backgroundRender.setCameraRelative(false);
179     }
180 
181     // This method is a HACK to workaround the stupid map file format.
182     // We want the foreground layer to be render priority FOREGROUND, but
183     // we don't know which is the foreground layer until we've added them all.
184     // So now that we've added them all, find foreground layer and make sure
185     // its render priority is set.
promoteForegroundLayer(GameObject backgroundObject)186 	public void promoteForegroundLayer(GameObject backgroundObject) {
187 		backgroundObject.commitUpdates();	// Make sure layers are sorted.
188 		final int componentCount = backgroundObject.getCount();
189 		for (int x = componentCount - 1; x >= 0; x--) {
190 			GameComponent component = (GameComponent)backgroundObject.get(x);
191 			if (component instanceof RenderComponent) {
192 				RenderComponent render = (RenderComponent)component;
193 				if (render.getPriority() != SortConstants.OVERLAY) {
194 					// found it.
195 					render.setPriority(SortConstants.FOREGROUND);
196 					break;
197 				}
198 			}
199 		}
200 	}
201 
202 }
203