gdx-studio
git clone https://git.pyrossh.dev/gdx-studio
An IDE for creating Games using libgdx and Java supported on all platforms Android, iOS, Desktop
src/scene2d/Asset.java
| a9b2a64 | 1 | package scene2d; |
| a9b2a64 | 2 | import scene3d.Actor3d; |
| a9b2a64 | 3 | |
| a62d533 | 4 | import com.badlogic.gdx.Gdx; |
| a62d533 | 5 | import com.badlogic.gdx.assets.AssetManager; |
| a62d533 | 6 | import com.badlogic.gdx.assets.loaders.resolvers.InternalFileHandleResolver; |
| a62d533 | 7 | import com.badlogic.gdx.audio.Music; |
| a62d533 | 8 | import com.badlogic.gdx.audio.Sound; |
| a62d533 | 9 | import com.badlogic.gdx.graphics.g2d.Animation; |
| a62d533 | 10 | import com.badlogic.gdx.graphics.g2d.BitmapFont; |
| a62d533 | 11 | import com.badlogic.gdx.graphics.g2d.TextureAtlas; |
| a62d533 | 12 | import com.badlogic.gdx.graphics.g2d.TextureRegion; |
| a62d533 | 13 | import com.badlogic.gdx.graphics.g3d.Model; |
| a62d533 | 14 | import com.badlogic.gdx.maps.tiled.TiledMap; |
| a62d533 | 15 | import com.badlogic.gdx.maps.tiled.TmxMapLoader; |
| a62d533 | 16 | import com.badlogic.gdx.scenes.scene2d.ui.Skin; |
| a9b2a64 | 17 | import com.badlogic.gdx.utils.Array; |
| a62d533 | 18 | import com.badlogic.gdx.utils.ArrayMap; |
| a62d533 | 19 | import com.badlogic.gdx.utils.JsonValue; |
| a62d533 | 20 | |
| a62d533 | 21 | /** Automatic Assets Loading for the Game |
| a62d533 | 22 | * <p> |
| a62d533 | 23 | * This class automatically loads all the assets in the prescribed folders into the appropriate |
| a62d533 | 24 | * class types. All This can be accessed using an neat api. |
| a62d533 | 25 | * |
| a62d533 | 26 | * <p> |
| a62d533 | 27 | * <b>#Important </b> <br> |
| a62d533 | 28 | * All asset files must be lowercase only.. otherwise it causes problems with android <br> |
| a62d533 | 29 | * 1. All Assets are to be stored in the assets directory <br> |
| a62d533 | 30 | * 2. Automatic Asset Loading the directory Structure should be like this <br> |
| a62d533 | 31 | * |
| a62d533 | 32 | * <p> |
| a9b2a64 | 33 | * icon.png --- your game icon to be displayed on the window<br> |
| a9b2a64 | 34 | * atlas/ --- all your Texture Atlas files .atlas and .png go here<br> |
| a9b2a64 | 35 | * font/ --- all your BitmapFont files .fnt and .png go here<br> |
| a9b2a64 | 36 | * music/ --- all your Music files .mp3 go here<br> |
| a9b2a64 | 37 | * sound/ --- all your Music files .mp3 go here<br> |
| a9b2a64 | 38 | * particle/ --- all your Particle files .part go here<br> |
| a9b2a64 | 39 | * map/ --- all your TMX map files along with tilesets go here<br> |
| a9b2a64 | 40 | * pack/ --- all your image files which are to be packed are to be stored here<br> |
| a62d533 | 41 | * so that they are automatically packed by the texture packer and stored in |
| a62d533 | 42 | * the atlas folder |
| a62d533 | 43 | * |
| a62d533 | 44 | * <p> |
| a62d533 | 45 | * All the assets are read from their particular folders and an asset.json file is created with |
| a62d533 | 46 | * the filenames: |
| a62d533 | 47 | * ex: asset.json |
| a62d533 | 48 | * {
|
| a62d533 | 49 | * "font": "arial.fnt, font1", |
| a62d533 | 50 | * "atlas": "image.atlas", |
| a62d533 | 51 | * "sound": "click.mp3,gg.mp3", |
| a62d533 | 52 | * "music": "title.mp3,"bg.mp3", |
| a62d533 | 53 | * } |
| a62d533 | 54 | * All assets are accessed this way,<br> |
| a62d533 | 55 | * <pre> |
| a62d533 | 56 | * <code> |
| a62d533 | 57 | * |
| a62d533 | 58 | * //To load TextureRegion |
| a62d533 | 59 | * TextureRegion cat = Asset.tex("cat");
|
| a62d533 | 60 | * |
| a62d533 | 61 | * //To load Animation |
| a62d533 | 62 | * Animation catAnim = Asset.anim("cat");
|
| a62d533 | 63 | * |
| a62d533 | 64 | * //To load BitmapFont |
| a62d533 | 65 | * BitmapFont font1 = Asset.font("font1");
|
| a62d533 | 66 | * |
| a62d533 | 67 | * //The music and sound files are automatically cached and can be played by invoking: |
| a62d533 | 68 | * Asset.musicPlay("musicname");
|
| a62d533 | 69 | * Asset.soundPlay("soundname");
|
| a62d533 | 70 | * |
| a62d533 | 71 | * //The asset functions will return null for Font, TextureRegion and Animation if the asset cannot be found |
| a62d533 | 72 | * </code> |
| a62d533 | 73 | </pre> |
| a62d533 | 74 | * </p> |
| a62d533 | 75 | * @author pyros2097 */ |
| a62d533 | 76 | |
| a62d533 | 77 | public final class Asset {
|
| a62d533 | 78 | private static AssetManager assetMan = new AssetManager(); |
| a62d533 | 79 | |
| a62d533 | 80 | public static Skin skin; |
| a62d533 | 81 | public final static ArrayMap<String, String> assetMap = new ArrayMap<String, String>(); |
| a62d533 | 82 | public final static ArrayMap<String, Music> musicMap = new ArrayMap<String, Music>(); |
| a62d533 | 83 | public final static ArrayMap<String, Sound> soundMap = new ArrayMap<String, Sound>(); |
| a62d533 | 84 | public final static ArrayMap<String, BitmapFont> fontMap = new ArrayMap<String, BitmapFont>(); |
| a62d533 | 85 | public final static ArrayMap<String, TextureRegion> texMap = new ArrayMap<String, TextureRegion>(); |
| a9b2a64 | 86 | public final static ArrayMap<String, TextureRegion> particleMap = new ArrayMap<String, TextureRegion>(); |
| a9b2a64 | 87 | public final static Array<String> modelMap = new Array<String>(); |
| a62d533 | 88 | |
| a62d533 | 89 | private static Music currentMusic = null; |
| a62d533 | 90 | public static String currentMusicName = ""; //Only file name no prefix or suffix |
| a62d533 | 91 | private static Sound currentSound = null; |
| a62d533 | 92 | |
| a62d533 | 93 | private static boolean readinglock = false; |
| a62d533 | 94 | private static boolean updatinglock = false; |
| a62d533 | 95 | |
| a62d533 | 96 | /* This is to be used by Gdx Studio */ |
| a9b2a64 | 97 | public static String basePath = ""; |
| a62d533 | 98 | |
| a62d533 | 99 | public static boolean musicOn = true; |
| a62d533 | 100 | public static boolean soundOn = false; |
| a62d533 | 101 | public static boolean loadAsynchronous = true; |
| a62d533 | 102 | |
| a62d533 | 103 | private static void clear(){
|
| a62d533 | 104 | assetMap.clear(); |
| a62d533 | 105 | fontMap.clear(); |
| a62d533 | 106 | texMap.clear(); |
| a9b2a64 | 107 | particleMap.clear(); |
| a62d533 | 108 | musicMap.clear(); |
| a62d533 | 109 | soundMap.clear(); |
| a9b2a64 | 110 | modelMap.clear(); |
| a62d533 | 111 | } |
| a62d533 | 112 | |
| a62d533 | 113 | public static void load(){
|
| a62d533 | 114 | if(loadAsynchronous) |
| a62d533 | 115 | loadNonBlocking(); |
| a62d533 | 116 | //else |
| a62d533 | 117 | // loadBlocking(); |
| a62d533 | 118 | } |
| a62d533 | 119 | /* |
| a62d533 | 120 | * This is a blocking load call blocks the display until assets are all loaded. |
| a62d533 | 121 | */ |
| a62d533 | 122 | public static void loadBlocking(){
|
| a62d533 | 123 | clear(); |
| a62d533 | 124 | readinglock = true; |
| a62d533 | 125 | updatinglock = true; |
| a62d533 | 126 | loadAssets(); |
| a62d533 | 127 | assetMan.finishLoading(); |
| a62d533 | 128 | assetMan.setLoader(TiledMap.class, new TmxMapLoader(new InternalFileHandleResolver())); |
| a62d533 | 129 | getAssets(); |
| a62d533 | 130 | } |
| a62d533 | 131 | |
| a62d533 | 132 | /* |
| a62d533 | 133 | * This is a non-blocking load call that allows you to display other things while the load is going |
| a62d533 | 134 | * on. This is called in the act method of SplashScene so that it is runs in the background |
| a62d533 | 135 | * once the assets are all loaded it will automatically stop and call SplashScene.onAssetsLoaded() |
| a62d533 | 136 | */ |
| a62d533 | 137 | private static boolean loadNonBlocking(){
|
| a62d533 | 138 | if(!readinglock){
|
| a62d533 | 139 | clear(); |
| a62d533 | 140 | loadAssets(); |
| a62d533 | 141 | readinglock = true; |
| a62d533 | 142 | } |
| a62d533 | 143 | // once update returns true then condition is satisfied and the lock stops update call |
| a62d533 | 144 | if(!updatinglock) |
| a62d533 | 145 | if(assetMan.update()){
|
| a62d533 | 146 | assetMan.setLoader(TiledMap.class, new TmxMapLoader(new InternalFileHandleResolver())); |
| a62d533 | 147 | getAssets(); |
| a62d533 | 148 | updatinglock = true; |
| a9b2a64 | 149 | if(Scene.splashDuration != 0) |
| a9b2a64 | 150 | Scene.nextSceneWithDelay(Scene.splashDuration); |
| a62d533 | 151 | else |
| a9b2a64 | 152 | Scene.nextScene(); |
| a62d533 | 153 | } |
| a62d533 | 154 | return updatinglock; |
| a62d533 | 155 | } |
| a62d533 | 156 | |
| a62d533 | 157 | public static void loadAssets(){
|
| a9b2a64 | 158 | JsonValue sv = Scene.jsonReader.parse(Gdx.files.internal(Asset.basePath+"asset")); |
| a62d533 | 159 | for(JsonValue jv: sv.iterator()) |
| a62d533 | 160 | assetMap.put(jv.name, jv.asString()); |
| a62d533 | 161 | for(String font: assetMap.get("font").split(",")){
|
| a62d533 | 162 | if(!font.isEmpty()) |
| a62d533 | 163 | assetMan.load(basePath+"font/"+font, BitmapFont.class); |
| a62d533 | 164 | } |
| a62d533 | 165 | for(String atlas: assetMap.get("atlas").split(",")){
|
| a62d533 | 166 | if(!atlas.isEmpty()) |
| a62d533 | 167 | assetMan.load(basePath+"atlas/"+atlas, TextureAtlas.class); |
| a62d533 | 168 | } |
| a62d533 | 169 | for(String sound: assetMap.get("sound").split(",")){
|
| a62d533 | 170 | if(!sound.isEmpty()) |
| a62d533 | 171 | assetMan.load(basePath+"sound/"+sound, Sound.class); |
| a62d533 | 172 | } |
| a62d533 | 173 | for(String music: assetMap.get("music").split(",")){
|
| a62d533 | 174 | if(!music.isEmpty()) |
| a62d533 | 175 | assetMan.load(basePath+"music/"+music, Music.class); |
| a62d533 | 176 | } |
| a62d533 | 177 | assetMan.load(basePath+"skin/uiskin.json", Skin.class); |
| a62d533 | 178 | } |
| a62d533 | 179 | |
| a62d533 | 180 | public static void getAssets(){
|
| a62d533 | 181 | for(String font: assetMap.get("font").split(",")){
|
| a62d533 | 182 | if(!font.isEmpty()) |
| a62d533 | 183 | fontMap.put(ext(font), assetMan.get(basePath+"font/"+font, BitmapFont.class)); |
| a62d533 | 184 | } |
| a62d533 | 185 | for(String atlas: assetMap.get("atlas").split(",")){
|
| a62d533 | 186 | if(!atlas.isEmpty()) |
| a62d533 | 187 | for(TextureAtlas.AtlasRegion ar: assetMan.get(basePath+"atlas/"+atlas, |
| a62d533 | 188 | TextureAtlas.class).getRegions()) |
| a62d533 | 189 | texMap.put(ar.name, ar); |
| a62d533 | 190 | } |
| a62d533 | 191 | for(String sound: assetMap.get("sound").split(",")){
|
| a62d533 | 192 | if(!sound.isEmpty()) |
| a62d533 | 193 | soundMap.put(ext(sound), assetMan.get(basePath+"sound/"+sound, Sound.class)); |
| a62d533 | 194 | } |
| a62d533 | 195 | for(String music: assetMap.get("music").split(",")){
|
| a62d533 | 196 | if(!music.isEmpty()) |
| a62d533 | 197 | musicMap.put(ext(music), assetMan.get(basePath+"music/"+music, Music.class)); |
| a62d533 | 198 | } |
| a62d533 | 199 | skin = assetMan.get(basePath+"skin/uiskin.json", Skin.class); |
| a9b2a64 | 200 | if(Scene.configJson.getBoolean("showFPS"))
|
| a9b2a64 | 201 | Scene.setupFps(); |
| a62d533 | 202 | } |
| a62d533 | 203 | |
| a62d533 | 204 | private static String ext(String file){
|
| a62d533 | 205 | return file.substring(0, file.indexOf("."));
|
| a62d533 | 206 | } |
| a62d533 | 207 | |
| a62d533 | 208 | public static void save(){
|
| a9b2a64 | 209 | Gdx.files.local(Asset.basePath+"asset").writeString(Scene.json.toJson(assetMap, ArrayMap.class, String.class), false); |
| a62d533 | 210 | } |
| a62d533 | 211 | |
| a62d533 | 212 | /*********************************************************************************************************** |
| a62d533 | 213 | * Music Related Global Functions * |
| a62d533 | 214 | ************************************************************************************************************/ |
| a62d533 | 215 | /** Plays the music file which was dynamically loaded if it is present otherwise logs the name |
| a62d533 | 216 | * @param filename The Music File name only |
| a62d533 | 217 | * @ex <code>music("title")</code>
|
| a62d533 | 218 | * */ |
| a62d533 | 219 | public static void musicPlay(String filename){
|
| a62d533 | 220 | if(Config.isMusic){
|
| a62d533 | 221 | if(currentMusic != null) |
| a62d533 | 222 | if(currentMusic.isPlaying()) |
| a62d533 | 223 | if(currentMusicName == filename) |
| a62d533 | 224 | return; |
| a62d533 | 225 | else |
| a62d533 | 226 | musicStop(); |
| a62d533 | 227 | if(musicMap.containsKey(filename)){
|
| a9b2a64 | 228 | Scene.log("Music: playing "+filename);
|
| a62d533 | 229 | currentMusic = musicMap.get(filename);//Gdx.audio.newMusic(Gdx.files.internal("music/"+filename));
|
| a62d533 | 230 | currentMusic.setVolume(Config.volMusic); |
| a62d533 | 231 | currentMusic.setLooping(true); |
| a62d533 | 232 | currentMusic.play(); |
| a62d533 | 233 | currentMusicName = filename; |
| a62d533 | 234 | } |
| a62d533 | 235 | else{
|
| a9b2a64 | 236 | Scene.log("Music File Not Found: "+filename);
|
| a62d533 | 237 | } |
| a62d533 | 238 | } |
| a62d533 | 239 | } |
| a62d533 | 240 | |
| a62d533 | 241 | /** Pauses the current music file being played */ |
| a62d533 | 242 | public static void musicPause(){
|
| a62d533 | 243 | if(currentMusic != null) |
| a62d533 | 244 | if(currentMusic.isPlaying()){
|
| a9b2a64 | 245 | Scene.log("Music: pausing "+currentMusicName);
|
| a62d533 | 246 | currentMusic.pause(); |
| a62d533 | 247 | } |
| a62d533 | 248 | } |
| a62d533 | 249 | |
| a62d533 | 250 | /** Resumes the current music file being played */ |
| a62d533 | 251 | public static void musicResume(){
|
| a62d533 | 252 | if(currentMusic != null) |
| a62d533 | 253 | if(!currentMusic.isPlaying()){
|
| a9b2a64 | 254 | Scene.log("Music: resuming "+currentMusicName);
|
| a62d533 | 255 | currentMusic.play(); |
| a62d533 | 256 | } |
| a62d533 | 257 | else |
| a62d533 | 258 | musicPlay(currentMusicName); |
| a62d533 | 259 | } |
| a62d533 | 260 | |
| a62d533 | 261 | /** Stops the current music file being played */ |
| a62d533 | 262 | public static void musicStop(){
|
| a62d533 | 263 | if(currentMusic != null){
|
| a9b2a64 | 264 | Scene.log("Music: stoping "+currentMusicName);
|
| a62d533 | 265 | currentMusic.stop(); |
| a62d533 | 266 | currentMusic = null; |
| a62d533 | 267 | } |
| a62d533 | 268 | } |
| a62d533 | 269 | |
| a62d533 | 270 | /** Sets the volume music file being played */ |
| a62d533 | 271 | public static void musicVolume(){
|
| a62d533 | 272 | if(currentMusic != null); |
| a62d533 | 273 | currentMusic.setVolume(Config.volMusic); |
| a62d533 | 274 | } |
| a62d533 | 275 | |
| a62d533 | 276 | /** Disoposes the current music file being played */ |
| a62d533 | 277 | public static void musicDispose(){
|
| a62d533 | 278 | if(currentMusic != null); |
| a62d533 | 279 | currentMusic.dispose(); |
| a62d533 | 280 | } |
| a62d533 | 281 | |
| a62d533 | 282 | /*********************************************************************************************************** |
| a62d533 | 283 | * Sound Related Global Functions * |
| a62d533 | 284 | ************************************************************************************************************/ |
| a62d533 | 285 | /** Plays the sound file which was dynamically loaded if it is present otherwise logs the name |
| a62d533 | 286 | * @param filename The Sound File name only |
| a62d533 | 287 | * @ex <code>soundPlay("bang")</code>
|
| a62d533 | 288 | * */ |
| a62d533 | 289 | public static void soundPlay(String filename){
|
| a62d533 | 290 | if(Config.isSound){
|
| a62d533 | 291 | if(soundMap.containsKey(filename)){
|
| a62d533 | 292 | currentSound = soundMap.get(filename); |
| a62d533 | 293 | long id = currentSound.play(Config.volSound); |
| a62d533 | 294 | currentSound.setLooping(id, false); |
| a62d533 | 295 | currentSound.setPriority(id, 99); |
| a9b2a64 | 296 | Scene.log("Sound:"+"Play "+ filename);
|
| a62d533 | 297 | } |
| a62d533 | 298 | else{
|
| a9b2a64 | 299 | Scene.log("Music File Not Found: "+filename);
|
| a62d533 | 300 | } |
| a62d533 | 301 | } |
| a62d533 | 302 | } |
| a62d533 | 303 | |
| a62d533 | 304 | /** Plays the sound file "click" */ |
| a62d533 | 305 | public static void soundClick(){
|
| a62d533 | 306 | if(Config.isSound && soundMap.containsKey("click")){
|
| a62d533 | 307 | currentSound = soundMap.get("click");
|
| a62d533 | 308 | long id = currentSound.play(Config.volSound); |
| a62d533 | 309 | currentSound.setLooping(id, false); |
| a62d533 | 310 | currentSound.setPriority(id, 99); |
| a9b2a64 | 311 | Scene.log("Sound:"+"Play "+ "click");
|
| a62d533 | 312 | } |
| a62d533 | 313 | } |
| a62d533 | 314 | |
| a62d533 | 315 | /** Pauses the current sound file being played */ |
| a62d533 | 316 | public static void soundPause(){
|
| a9b2a64 | 317 | Scene.log("Sound:"+"Pausing");
|
| a62d533 | 318 | if(currentSound != null) |
| a62d533 | 319 | currentSound.pause(); |
| a62d533 | 320 | } |
| a62d533 | 321 | |
| a62d533 | 322 | /** Resumes the current sound file being played */ |
| a62d533 | 323 | public static void soundResume(){
|
| a9b2a64 | 324 | Scene.log("Sound:"+"Resuming");
|
| a62d533 | 325 | if(currentSound != null) |
| a62d533 | 326 | currentSound.resume(); |
| a62d533 | 327 | } |
| a62d533 | 328 | |
| a62d533 | 329 | /** Stops the current sound file being played */ |
| a62d533 | 330 | public static void soundStop(){
|
| a9b2a64 | 331 | Scene.log("Sound:"+"Stopping");
|
| a62d533 | 332 | if(currentSound != null) |
| a62d533 | 333 | currentSound.stop(); |
| a62d533 | 334 | } |
| a62d533 | 335 | |
| a62d533 | 336 | /** Disposes the current sound file being played */ |
| a62d533 | 337 | public static void soundDispose(){
|
| a9b2a64 | 338 | Scene.log("Sound:"+"Disposing Sound");
|
| a62d533 | 339 | if(currentSound != null) |
| a62d533 | 340 | currentSound.dispose(); |
| a62d533 | 341 | } |
| a62d533 | 342 | |
| a62d533 | 343 | |
| a62d533 | 344 | /*********************************************************************************************************** |
| a62d533 | 345 | * BitmapFont Related Functions * |
| a62d533 | 346 | ************************************************************************************************************/ |
| a62d533 | 347 | /** If key is present returns the BitmapFont that was dynamically loaded |
| a62d533 | 348 | * else returns null |
| a62d533 | 349 | * @param fontname The BitmapFont name |
| a62d533 | 350 | * @return BitmapFont or null |
| a62d533 | 351 | * @ex font("font1") or font("arial")
|
| a62d533 | 352 | * */ |
| a62d533 | 353 | public static BitmapFont font(String fontname){
|
| a62d533 | 354 | if(fontMap.containsKey(fontname)){
|
| a62d533 | 355 | return fontMap.get(fontname); |
| a62d533 | 356 | } |
| a62d533 | 357 | else{
|
| a9b2a64 | 358 | Scene.log("Font File Not Found: "+fontname);
|
| a62d533 | 359 | return null; |
| a62d533 | 360 | } |
| a62d533 | 361 | } |
| a62d533 | 362 | |
| a62d533 | 363 | /*********************************************************************************************************** |
| a62d533 | 364 | * Texture Related Functions * |
| a62d533 | 365 | ************************************************************************************************************/ |
| a62d533 | 366 | /** If key is present returns the TextureRegion that was loaded from all the atlases |
| a62d533 | 367 | * else returns null |
| a62d533 | 368 | * @param textureregionName The TextureRegion name |
| a62d533 | 369 | * @return TextureRegion or null |
| a62d533 | 370 | * */ |
| a62d533 | 371 | public static TextureRegion tex(String textureregionName){
|
| a62d533 | 372 | if(texMap.containsKey(textureregionName)){
|
| a62d533 | 373 | return texMap.get(textureregionName); |
| a62d533 | 374 | } |
| a62d533 | 375 | else{
|
| a9b2a64 | 376 | Scene.log("TextureRegion Not Found: "+textureregionName);
|
| a62d533 | 377 | return null; |
| a62d533 | 378 | } |
| a62d533 | 379 | } |
| a62d533 | 380 | |
| a62d533 | 381 | /*********************************************************************************************************** |
| a62d533 | 382 | * Animation Related Functions * |
| a62d533 | 383 | ************************************************************************************************************/ |
| a62d533 | 384 | private static Animation anim(String texName, int numberOfFrames, int hOffset) {
|
| a62d533 | 385 | // Key frames list |
| a62d533 | 386 | TextureRegion[] keyFrames = new TextureRegion[numberOfFrames]; |
| a62d533 | 387 | TextureRegion texture = Asset.tex(texName); |
| a62d533 | 388 | int width = texture.getRegionWidth() / numberOfFrames; |
| a62d533 | 389 | int height = texture.getRegionHeight(); |
| a62d533 | 390 | // Set key frames (each comes from the single texture) |
| a62d533 | 391 | for (int i = 0; i < numberOfFrames; i++) |
| a62d533 | 392 | keyFrames[i] = new TextureRegion(texture, width * i, hOffset, width, height); |
| a62d533 | 393 | Animation animation = new Animation(1f/numberOfFrames, keyFrames); |
| a62d533 | 394 | return animation; |
| a62d533 | 395 | } |
| a62d533 | 396 | |
| a62d533 | 397 | private static Animation anim(String texName, int numberOfFrames, float duration, int hOffset) {
|
| a62d533 | 398 | // Key frames list |
| a62d533 | 399 | TextureRegion[] keyFrames = new TextureRegion[numberOfFrames]; |
| a62d533 | 400 | TextureRegion texture = Asset.tex(texName); |
| a62d533 | 401 | int width = texture.getRegionWidth() / numberOfFrames; |
| a62d533 | 402 | int height = texture.getRegionHeight(); |
| a62d533 | 403 | // Set key frames (each comes from the single texture) |
| a62d533 | 404 | for (int i = 0; i < numberOfFrames; i++) |
| a62d533 | 405 | keyFrames[i] = new TextureRegion(texture, width * i, hOffset, width, height); |
| a62d533 | 406 | Animation animation = new Animation(duration, keyFrames); |
| a62d533 | 407 | return animation; |
| a62d533 | 408 | } |
| a62d533 | 409 | |
| a62d533 | 410 | /** |
| a62d533 | 411 | * Get animation from single textureRegion which contains all frames |
| a62d533 | 412 | * (It is like a single png which has all the frames). Each frames' width should be same. |
| a62d533 | 413 | * <p> |
| a62d533 | 414 | * @param texName |
| a62d533 | 415 | * the name of the texture region |
| a62d533 | 416 | * @param numberOfFrames |
| a62d533 | 417 | * number of frames of the texture Region |
| a62d533 | 418 | * @return animation created |
| a62d533 | 419 | * |
| a62d533 | 420 | * */ |
| a62d533 | 421 | public static Animation anim(String texName, int numberOfFrames) {
|
| a62d533 | 422 | return anim(texName, numberOfFrames, 0); |
| a62d533 | 423 | } |
| a62d533 | 424 | |
| a62d533 | 425 | /** |
| a62d533 | 426 | * Get animation from single textureRegion which contains all frames |
| a62d533 | 427 | * (It is like a single png which has all the frames). Each frames' width should be same. |
| a62d533 | 428 | * <p> |
| a62d533 | 429 | * @param texName |
| a62d533 | 430 | * the name of the texture region |
| a62d533 | 431 | * @param numberOfFrames |
| a62d533 | 432 | * number of frames of the texture Region |
| a62d533 | 433 | * @param duration |
| a62d533 | 434 | * each frame duration on play |
| a62d533 | 435 | * @return animation created |
| a62d533 | 436 | * |
| a62d533 | 437 | * */ |
| a62d533 | 438 | public static Animation anim(String texName, int numberOfFrames, float duration) {
|
| a62d533 | 439 | return anim(texName, numberOfFrames, duration, 0); |
| a62d533 | 440 | } |
| a62d533 | 441 | |
| a62d533 | 442 | /** |
| a62d533 | 443 | * There is only single texture region which contains all frames |
| a62d533 | 444 | * (It is like a single png which has all the frames). |
| a62d533 | 445 | * The texture regions consists of both rows and columns |
| a62d533 | 446 | * <p> |
| a62d533 | 447 | * |
| a62d533 | 448 | * @param textureAtlas |
| a62d533 | 449 | * atlas which contains the single animation texture |
| a62d533 | 450 | * @param animationName |
| a62d533 | 451 | * name of the animation in atlas |
| a62d533 | 452 | * @param numberOfFrames |
| a62d533 | 453 | * number of frames of the animation |
| a62d533 | 454 | * @param numberOfMaximumFramesInTheSheet |
| a62d533 | 455 | * maximum number of frame in a row in the sheet |
| a62d533 | 456 | * @param numberOfRows |
| a62d533 | 457 | * number of rows that the sheet contains |
| a62d533 | 458 | * @param indexOfAnimationRow |
| a62d533 | 459 | * the row index (starts from 0) that desired animation exists |
| a62d533 | 460 | * @param frameDuration |
| a62d533 | 461 | * each frame duration on play |
| a62d533 | 462 | * @return animation created |
| a62d533 | 463 | * |
| a62d533 | 464 | * */ |
| a62d533 | 465 | public static Animation[] anim(String texName, int rows, int cols, float duration) {
|
| a62d533 | 466 | TextureRegion texture = Asset.tex(texName); |
| a62d533 | 467 | // Set key frames (each comes from the single texture) |
| a62d533 | 468 | /*for (int i = 0; i < numberOfFrames; i++) {
|
| a62d533 | 469 | keyFrames[i] = new TextureRegion( |
| a62d533 | 470 | textureRegion, |
| a62d533 | 471 | (textureRegion.getRegionWidth() / numberOfMaximumFramesInTheSheet) |
| a62d533 | 472 | * i, textureRegion.getRegionHeight() / numberOfRows |
| a62d533 | 473 | * indexOfAnimationRow, |
| a62d533 | 474 | textureRegion.getRegionWidth() |
| a62d533 | 475 | / numberOfMaximumFramesInTheSheet, |
| a62d533 | 476 | textureRegion.getRegionHeight() / numberOfRows); |
| a62d533 | 477 | }*/ |
| a62d533 | 478 | int height = texture.getRegionHeight()/rows; |
| a62d533 | 479 | Animation[] animations = new Animation[rows]; |
| a62d533 | 480 | for(int i=0;i<rows;i++) |
| a62d533 | 481 | animations[i] = anim(texName, cols, i*height); |
| a62d533 | 482 | return animations; |
| a62d533 | 483 | } |
| a62d533 | 484 | |
| a62d533 | 485 | /*********************************************************************************************************** |
| a62d533 | 486 | * TMX MAP Related Functions * |
| a62d533 | 487 | ************************************************************************************************************/ |
| a62d533 | 488 | /* |
| a62d533 | 489 | * Loads a Tmx map by specifying the map/level no |
| a62d533 | 490 | * eg: loadTmx(4) -> returns the TiledMap "map/level4.tmx" |
| a62d533 | 491 | * |
| a62d533 | 492 | * Note: Tmx Maps must be loaded and unloaded manually as they may take a lot of time to load |
| a62d533 | 493 | */ |
| a62d533 | 494 | public static TiledMap map(int i){
|
| a62d533 | 495 | assetMan.setLoader(TiledMap.class, new TmxMapLoader(new InternalFileHandleResolver())); |
| a62d533 | 496 | assetMan.load(basePath+"map/level"+i+".tmx", TiledMap.class); |
| a62d533 | 497 | assetMan.finishLoading(); |
| a62d533 | 498 | return assetMan.get(basePath+"map/level"+i+".tmx", TiledMap.class); |
| a62d533 | 499 | } |
| a62d533 | 500 | |
| a62d533 | 501 | /* |
| a62d533 | 502 | * unloads a Tmx map by specifying the map/level no |
| a62d533 | 503 | * eg: unloadTmx(4) -> unloads the TiledMap "map/level4.tmx" |
| a62d533 | 504 | * |
| a62d533 | 505 | * Note: Tmx Maps must be unloaded manually |
| a62d533 | 506 | */ |
| a62d533 | 507 | public static void unloadmap(int i){
|
| a62d533 | 508 | assetMan.unload(basePath+"map/level"+i+".tmx"); |
| a62d533 | 509 | } |
| a62d533 | 510 | |
| a62d533 | 511 | /* |
| 7c84877 | 512 | * Load a G3db model from the model directory |
| a62d533 | 513 | * @param modelName The name of the modelFile |
| a62d533 | 514 | * @example loadModel("ship");
|
| a62d533 | 515 | */ |
| a62d533 | 516 | public static Actor3d loadModel(String modelName){
|
| 7c84877 | 517 | assetMan.load(basePath+"model/"+modelName+".g3db", Model.class); |
| 7c84877 | 518 | assetMan.finishLoading(); |
| 7c84877 | 519 | return new Actor3d(assetMan.get(basePath+"model/"+modelName+".g3db", Model.class)); |
| 7c84877 | 520 | } |
| 7c84877 | 521 | |
| 7c84877 | 522 | /* |
| 7c84877 | 523 | * Unload a G3db model which was previously loaded in the assetManager |
| 7c84877 | 524 | * @param modelName The name of the modelFile that was loaded |
| 7c84877 | 525 | * @example unloadModel("ship");
|
| 7c84877 | 526 | */ |
| 7c84877 | 527 | public static void unloadModel(String modelName){
|
| 7c84877 | 528 | assetMan.unload(basePath+"model/"+modelName+".g3db"); |
| 7c84877 | 529 | } |
| 7c84877 | 530 | |
| 7c84877 | 531 | /* |
| 7c84877 | 532 | * Load a Obj model from the model directory |
| 7c84877 | 533 | * @param modelName The name of the modelFile |
| 7c84877 | 534 | * @example loadModel("ship");
|
| 7c84877 | 535 | */ |
| 7c84877 | 536 | public static Actor3d loadModelObj(String modelName){
|
| a62d533 | 537 | assetMan.load(basePath+"model/"+modelName+".obj", Model.class); |
| a62d533 | 538 | assetMan.finishLoading(); |
| a62d533 | 539 | return new Actor3d(assetMan.get(basePath+"model/"+modelName+".obj", Model.class)); |
| a62d533 | 540 | } |
| a62d533 | 541 | |
| a62d533 | 542 | /* |
| 7c84877 | 543 | * Unload a Obj model which was previously loaded in the assetManager |
| a62d533 | 544 | * @param modelName The name of the modelFile that was loaded |
| a62d533 | 545 | * @example unloadModel("ship");
|
| a62d533 | 546 | */ |
| 7c84877 | 547 | public static void unloadModelObj(String modelName){
|
| 7c84877 | 548 | assetMan.unload(basePath+"model/"+modelName+".obj"); |
| a62d533 | 549 | } |
| a62d533 | 550 | |
| a62d533 | 551 | /*********************************************************************************************************** |
| a62d533 | 552 | * LOG Related Functions * |
| a62d533 | 553 | ************************************************************************************************************/ |
| a62d533 | 554 | /* |
| a62d533 | 555 | * Logs all the assets that are loaded and cached |
| a62d533 | 556 | */ |
| a62d533 | 557 | public static void logAll(){
|
| a62d533 | 558 | logTextures(); |
| a62d533 | 559 | logFonts(); |
| a62d533 | 560 | logSounds(); |
| a62d533 | 561 | logMusics(); |
| a62d533 | 562 | } |
| a62d533 | 563 | |
| a62d533 | 564 | /* |
| a62d533 | 565 | * Logs all the TextureRegions that are loaded and cached |
| a62d533 | 566 | */ |
| a62d533 | 567 | public static void logTextures(){
|
| a9b2a64 | 568 | Scene.log("BEGIN logging Textures------------------");
|
| a62d533 | 569 | for(String na: texMap.keys()) |
| a9b2a64 | 570 | Scene.log(na); |
| a9b2a64 | 571 | Scene.log("END logging Textures------------------");
|
| a62d533 | 572 | } |
| a62d533 | 573 | |
| a62d533 | 574 | /* |
| a62d533 | 575 | * Logs all the BitmapFonts that are loaded and cached |
| a62d533 | 576 | */ |
| a62d533 | 577 | public static void logFonts(){
|
| a9b2a64 | 578 | Scene.log("BEGIN logging Fonts------------------");
|
| a62d533 | 579 | for(String na: fontMap.keys()) |
| a9b2a64 | 580 | Scene.log(na); |
| a9b2a64 | 581 | Scene.log("END logging Fonts------------------");
|
| a62d533 | 582 | } |
| a62d533 | 583 | |
| a62d533 | 584 | /* |
| a62d533 | 585 | * Logs all the Sounds that are loaded and cached |
| a62d533 | 586 | */ |
| a62d533 | 587 | public static void logSounds(){
|
| a9b2a64 | 588 | Scene.log("BEGIN logging Sounds------------------");
|
| a62d533 | 589 | for(String na: soundMap.keys()) |
| a9b2a64 | 590 | Scene.log(na); |
| a9b2a64 | 591 | Scene.log("END logging Sounds------------------");
|
| a62d533 | 592 | } |
| a62d533 | 593 | |
| a62d533 | 594 | /* |
| a62d533 | 595 | * Logs all the Music that are loaded and cached |
| a62d533 | 596 | */ |
| a62d533 | 597 | public static void logMusics(){
|
| a9b2a64 | 598 | Scene.log("BEGIN logging Musics------------------");
|
| a62d533 | 599 | for(String na: musicMap.keys()) |
| a9b2a64 | 600 | Scene.log(na); |
| a9b2a64 | 601 | Scene.log("END logging Musics------------------");
|
| a62d533 | 602 | } |
| a62d533 | 603 | |
| a62d533 | 604 | /* |
| a62d533 | 605 | * Unloads and disposes all the resources except for Tmx Maps |
| a62d533 | 606 | * This is called by Sink.exit(); |
| a62d533 | 607 | */ |
| a62d533 | 608 | public static void unloadAll(){
|
| a62d533 | 609 | assetMan.dispose(); |
| a62d533 | 610 | } |
| a62d533 | 611 | |
| a62d533 | 612 | /* |
| a62d533 | 613 | * If using in eclipse set basePath to ./bin/ |
| a62d533 | 614 | */ |
| a62d533 | 615 | public static void setBasePath(String path){
|
| a62d533 | 616 | basePath = path; |
| a62d533 | 617 | } |
| a62d533 | 618 | } |