List of usage examples for com.badlogic.gdx Gdx files
Files files
To view the source code for com.badlogic.gdx Gdx files.
Click Source Link
From source file:com.agateau.ui.UiAssets.java
License:Apache License
public UiAssets() { this.atlas = new StrictTextureAtlas(Gdx.files.internal("ui/uiskin.atlas")); this.skin = new Skin(this.atlas); loadFonts();/*from www . j a v a 2 s . c o m*/ this.skin.load(Gdx.files.internal("ui/uiskin.json")); this.background = this.atlas.findRegion("background"); }
From source file:com.agateau.ui.UiAssets.java
License:Apache License
private BitmapFont loadFont(String name, FreeTypeFontGenerator.FreeTypeFontParameter parameter) { FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal(name)); BitmapFont font = generator.generateFont(parameter); generator.dispose();//from ww w . j a v a 2 s . co m return font; }
From source file:com.agateau.utils.FileUtils.java
License:Apache License
public static FileHandle getUserWritableFile(String name) { FileHandle handle;// w w w.j ava 2 s .c o m if (PlatformUtils.isDesktop()) { handle = Gdx.files.external(".local/share/" + appName + "/" + name); } else { handle = Gdx.files.local(name); } return handle; }
From source file:com.agateau.utils.FileUtils.java
License:Apache License
public static FileHandle getCacheDir() { FileHandle handle;/*from ww w . j av a2 s . c om*/ if (PlatformUtils.isDesktop()) { handle = Gdx.files.external(".cache/" + appName); } else { if (!Gdx.files.isExternalStorageAvailable()) { return null; } handle = Gdx.files.absolute(Gdx.files.getExternalStoragePath() + "/" + appName); } handle.mkdirs(); return handle; }
From source file:com.agateau.utils.FileUtils.java
License:Apache License
public static FileHandle assets(String path) { FileHandle handle = Gdx.files.internal(path); /* // Disabled for now: does not work for desktop releases if (Gdx.app.getType() == ApplicationType.Desktop) { handle = new FileHandle(new File(handle.path())); }/*from ww w .j a v a 2 s .co m*/ */ return handle; }
From source file:com.ahsgaming.starbattle.screens.AbstractScreen.java
License:Apache License
public Skin getSkin() { if (skin == null) { skin = new Skin(Gdx.files.local("assets/" + "newui/uiskin.json")); }/* ww w . jav a 2 s .co m*/ return skin; }
From source file:com.ahsgaming.valleyofbones.ai.AIPlayer.java
License:Apache License
@Override public void update(GameController controller, float delta) { super.update(controller, delta); if (genome == null) { Json json = new Json(); genome = json.fromJson(GenomicAI.class, Gdx.files.internal("ai/xjix3xuv").readString()); Gdx.app.log(LOG, "Loaded ai: " + genome.id); }//from w w w.j a v a 2 s. co m if (controller.getCurrentPlayer().getPlayerId() == getPlayerId()) { timer -= delta; if (timer < 0) { timer = countdown; if (VOBGame.DEBUG_AI) Gdx.app.log(LOG, "start..."); // first, create the visibility matrix if (visibilityMatrix == null) { if (VOBGame.DEBUG_AI) Gdx.app.log(LOG, "...create visibility matrix"); visibilityMatrix = createVisibilityMatrix(controller.getMap(), controller.getUnitsByPlayerId(getPlayerId())); return; } // next, create the value matrix and threat matrix if (valueMatrix == null) { Array<AbstractUnit> visibleUnits = new Array<AbstractUnit>(); for (AbstractUnit unit : controller.getUnits()) { if (visibilityMatrix[(int) (unit.getView().getBoardPosition().y * controller.getMap().getWidth() + unit.getView().getBoardPosition().x)]) { visibleUnits.add(unit); } } if (VOBGame.DEBUG_AI) Gdx.app.log(LOG, "...create value/threat matrices"); valueMatrix = createUnitMatrix(controller.getMap(), visibleUnits, false); threatMatrix = createUnitMatrix(controller.getMap(), visibleUnits, true); return; } // create goalMatrix (based on knowledge of the map) if (goalMatrix == null) { if (VOBGame.DEBUG_AI) Gdx.app.log(LOG, "...create goal matrix"); goalMatrix = createGoalMatrix(controller.getMap(), controller.getUnits()); if (VOBGame.DEBUG_AI) { DecimalFormat formatter = new DecimalFormat("00.0"); for (int y = 0; y < controller.getMap().getHeight(); y++) { if (y % 2 == 1) { System.out.print(" "); } for (int x = 0; x < controller.getMap().getWidth(); x++) { int i = y * controller.getMap().getWidth() + x; float sum = (visibilityMatrix[i] ? goalMatrix[i] + valueMatrix[i] + threatMatrix[i] : 0); System.out.print((sum >= 0 ? " " : "") + formatter.format(sum) + " "); } System.out.println("\n"); } } return; } // move units for (AbstractUnit unit : controller.getUnitsByPlayerId(getPlayerId())) { if (unit.getData().getMovesLeft() < 1) continue; Vector2[] adjacent = HexMap.getAdjacent((int) unit.getView().getBoardPosition().x, (int) unit.getView().getBoardPosition().y); Vector2 finalPos = new Vector2(unit.getView().getBoardPosition()); float finalSum = goalMatrix[(int) finalPos.y * controller.getMap().getWidth() + (int) finalPos.x] + valueMatrix[(int) finalPos.y * controller.getMap().getWidth() + (int) finalPos.x] + threatMatrix[(int) finalPos.y * controller.getMap().getWidth() + (int) finalPos.x]; for (Vector2 v : adjacent) { if (v.x < 0 || v.x >= controller.getMap().getWidth() || v.y < 0 || v.y >= controller.getMap().getHeight()) continue; float curSum = goalMatrix[(int) v.y * controller.getMap().getWidth() + (int) v.x] + valueMatrix[(int) v.y * controller.getMap().getWidth() + (int) v.x] + threatMatrix[(int) v.y * controller.getMap().getWidth() + (int) v.x]; if (curSum > finalSum && controller.isBoardPosEmpty(v)) { finalPos.set(v); finalSum = curSum; } } if (finalPos.x != unit.getView().getBoardPosition().x || finalPos.y != unit.getView().getBoardPosition().y) { // move! Move mv = new Move(); mv.owner = getPlayerId(); mv.turn = controller.getGameTurn(); mv.unit = unit.getId(); mv.toLocation = finalPos; netController.sendAICommand(mv); valueMatrix = null; threatMatrix = null; return; } } // attack Array<AbstractUnit> visibleUnits = new Array<AbstractUnit>(); Array<AbstractUnit> friendlyUnits = controller.getUnitsByPlayerId(this.getPlayerId()); for (AbstractUnit unit : controller.getUnits()) { if (unit.getOwner() == this || unit.getOwner() == null) continue; if (visibilityMatrix[(int) (unit.getView().getBoardPosition().y * controller.getMap().getWidth() + unit.getView().getBoardPosition().x)] && !unit.getData().isInvisible() || controller.getMap().detectorCanSee(this, friendlyUnits, unit.getView().getBoardPosition())) { visibleUnits.add(unit); } } for (AbstractUnit unit : controller.getUnitsByPlayerId(getPlayerId())) { if (unit.getData().getAttacksLeft() < 1) continue; AbstractUnit toAttack = null; float toAttackThreat = 0; for (AbstractUnit o : visibleUnits) { float thisThreat = threatMatrix[controller.getMap().getWidth() * (int) o.getView().getBoardPosition().y + (int) o.getView().getBoardPosition().x]; if (controller.getUnitManager().canAttack(unit, o) && (toAttack == null || thisThreat > toAttackThreat)) { toAttack = o; toAttackThreat = thisThreat; } } if (toAttack != null) { // Gdx.app.log(LOG + "$attack", String.format("%s --> %s", unit.getProtoId(), toAttack.getProtoId())); Attack at = new Attack(); at.owner = getPlayerId(); at.turn = controller.getGameTurn(); at.unit = unit.getId(); at.target = toAttack.getId(); netController.sendAICommand(at); valueMatrix = null; threatMatrix = null; return; } } // build units // TODO build other than marines (ie: implement chromosome 11) int positionToBuild = -1; for (int i = 0; i < valueMatrix.length; i++) { if (visibilityMatrix[i]) { float sum = threatMatrix[i] + valueMatrix[i] + goalMatrix[i]; if ((positionToBuild == -1 || valueMatrix[positionToBuild] + valueMatrix[positionToBuild] + goalMatrix[positionToBuild] < sum)) { // Gdx.app.log(LOG, i + ":" + controller.isBoardPosEmpty( // i % controller.getMap().getWidth(), // i / controller.getMap().getWidth())); if (controller.isBoardPosEmpty(i % controller.getMap().getWidth(), i / controller.getMap().getWidth())) { positionToBuild = i; } } } } Prototypes.JsonProto unitToBuild = null; if (positionToBuild >= 0) { Vector2 buildPosition = new Vector2(positionToBuild % controller.getMap().getWidth(), positionToBuild / controller.getMap().getWidth()); Array<String> protoIds = new Array<String>(); HashMap<String, Float> buildScores = new HashMap<String, Float>(); for (Prototypes.JsonProto proto : Prototypes.getAll(getRace())) { protoIds.add(proto.id); if (proto.cost > 0) { buildScores.put(proto.id, 0f); } } for (AbstractUnit unit : controller.getUnits()) { if (unit.getOwner() == this || !visibilityMatrix[(int) (unit.getView().getBoardPosition().y * controller.getMap().getWidth() + unit.getView().getBoardPosition().x)]) continue; int unitIndex = protoIds.indexOf(unit.getProto().id, false); int unitDistance = controller.getMap().getMapDist(unit.getView().getBoardPosition(), buildPosition); for (String key : buildScores.keySet()) { buildScores.put(key, buildScores.get(key) + ((Array<Float>) genome.chromosomes.get(10).genes.get(key)).get(unitIndex) / unitDistance); } } String maxScore = null; float maxBuildScore = 0; while (unitToBuild == null && buildScores.keySet().size() > 0) { for (String id : buildScores.keySet()) { if (maxScore == null || buildScores.get(id) > buildScores.get(maxScore)) { maxScore = id; if (buildScores.get(id) > maxBuildScore) { maxBuildScore = buildScores.get(id); } } } if (!maxScore.equals("saboteur") && buildScores.get(maxScore) > 0 && buildScores.get(maxScore) >= maxBuildScore - (Float) genome.chromosomes.get(10).genes.get("wait") && getBankMoney() >= Prototypes.getProto(getRace(), maxScore).cost) { unitToBuild = Prototypes.getProto(getRace(), maxScore); } else { buildScores.remove(maxScore); maxScore = null; } } } if (unitToBuild != null) { Build build = new Build(); build.owner = getPlayerId(); build.turn = controller.getGameTurn(); build.building = unitToBuild.id; build.location = new Vector2(positionToBuild % controller.getMap().getWidth(), positionToBuild / controller.getMap().getWidth()); netController.sendAICommand(build); valueMatrix = null; return; } EndTurn et = new EndTurn(); et.owner = getPlayerId(); et.turn = controller.getGameTurn(); netController.sendAICommand(et); } } else { visibilityMatrix = null; valueMatrix = null; threatMatrix = null; goalMatrix = null; } }
From source file:com.ahsgaming.valleyofbones.GameController.java
License:Apache License
/** * Methods//from w ww. ja v a 2 s. co m */ private HexMap loadMap() { // loads the map based on the value in mapName if (mapName == null || mapName.length() == 0) mapName = DEFAULT_MAP; // TODO implement loading of maps //map = new HexMap(this, 19, 13, 2, 4); map = new HexMap(this, Gdx.files.internal("maps/" + mapName + ".json")); return map; }
From source file:com.ahsgaming.valleyofbones.screens.AbstractScreen.java
License:Apache License
public Skin getSkin() { if (skin == null) { skin = new Skin(Gdx.files.internal("uiskin.json")); fontSmall = skin.getFont("small-font"); fontMed = skin.getFont("medium-font"); fontLarge = skin.getFont("large-font"); }//from ww w. ja v a2s. c o m return skin; }
From source file:com.ahsgaming.valleyofbones.screens.MPGameSetupScreen.java
License:Apache License
public void setupScreen() { Label gameTypeLbl = new Label("Multiplayer", getSkin(), "medium"); Table table = new Table(getSkin()); table.setFillParent(true);//www .j av a 2s . com stage.addActor(table); table.add(gameTypeLbl).colspan(2).center(); table.row().minWidth(600); Table playerTable = new Table(getSkin()); playerTable.setBackground(getSkin().getDrawable("default-pane")); playerTable.add().padBottom(10).width(30); playerTable.add().padBottom(10).width(30); playerTable.add(new Label("Name", getSkin(), "small-grey")).padBottom(10); playerTable.add(new Label("Race", getSkin(), "small-grey")).padBottom(10); playerTable.add(new Label("Color", getSkin(), "small-grey")).padBottom(10); playerTable.add(new Label("Ready", getSkin(), "small-grey")).padBottom(10).row(); pList = new Array<Player>(); pList.addAll(game.getPlayers()); for (final Player p : pList) { playerTable.add("P" + ((pList.indexOf(p, true) + 1))).padLeft(10); if (pList.indexOf(p, true) == 0) { Image host = new Image(game.getTextureManager().getSpriteFromAtlas("assets", "king-small")); playerTable.add(host).size(host.getWidth() / VOBGame.SCALE, host.getHeight() / VOBGame.SCALE) .padLeft(10).padRight(10); } else { if (config.isHost) { Image btn = getRemovePlayerButton(p.getPlayerId()); playerTable.add(btn).size(btn.getWidth() / VOBGame.SCALE, btn.getHeight() / VOBGame.SCALE) .padLeft(10).padRight(10); } else { playerTable.add().padLeft(10).padRight(10); } } playerTable.add(new Label(String.format("%s (%d)", p.getPlayerName(), p.getPlayerId()), getSkin())) .left().expandX(); if (p == client.getPlayer() || (config.isHost && p.isAI())) { SelectBox raceSelect = new SelectBox(Prototypes.getRaces().toArray(), getSkin()); playerTable.add(raceSelect).expandX(); raceSelect.setSelection(p.getRace()); raceSelect.addListener(new ChangeListener() { @Override public void changed(ChangeEvent event, Actor actor) { KryoCommon.UpdatePlayer update = new KryoCommon.UpdatePlayer(); update.id = p.getPlayerId(); update.race = ((SelectBox) actor).getSelection(); update.color = 0; while (update.color < Player.AUTOCOLORS.length && !p.getPlayerColor().equals(Player.AUTOCOLORS[update.color])) { update.color++; } update.ready = p.isReady(); if (client instanceof MPGameClient) ((MPGameClient) client).sendPlayerUpdate(update); } }); } else { playerTable.add(p.getRace()); } Image color = new Image(getSkin().getDrawable("white-hex")); color.setColor(p.getPlayerColor()); playerTable.add(color).expandX().width(color.getWidth()); if (p == client.getPlayer() || (config.isHost && p.isAI())) { color.addListener(new ClickListener() { @Override public void clicked(InputEvent event, float x, float y) { super.clicked(event, x, y); int i = 0; while (i < Player.AUTOCOLORS.length && !Player.AUTOCOLORS[i].equals(event.getTarget().getColor())) { i++; } i++; i %= Player.AUTOCOLORS.length; event.getTarget().setColor(Player.AUTOCOLORS[i]); KryoCommon.UpdatePlayer update = new KryoCommon.UpdatePlayer(); update.id = p.getPlayerId(); update.race = p.getRace(); update.color = i; update.ready = p.isReady(); if (client instanceof MPGameClient) ((MPGameClient) client).sendPlayerUpdate(update); } }); } CheckBox chkReady = new CheckBox("", getSkin()); playerTable.add(chkReady).expandX(); chkReady.setChecked(p.isReady()); if (p != client.getPlayer()) chkReady.setDisabled(true); chkReady.addListener(new ChangeListener() { @Override public void changed(ChangeEvent event, Actor actor) { KryoCommon.UpdatePlayer update = new KryoCommon.UpdatePlayer(); update.id = p.getPlayerId(); update.race = p.getRace(); update.color = 0; while (update.color < Player.AUTOCOLORS.length && !p.getPlayerColor().equals(Player.AUTOCOLORS[update.color])) { update.color++; } update.ready = !p.isReady(); if (client instanceof MPGameClient) ((MPGameClient) client).sendPlayerUpdate(update); } }); playerTable.row().padBottom(5).padTop(5); } if (pList.size < 2 && config.isHost) { TextButton addAI = new TextButton(" Add AI Player ", getSkin(), "small"); addAI.addListener(new ClickListener() { @Override public void clicked(InputEvent event, float x, float y) { super.clicked(event, x, y); game.addAIPlayer(); } }); playerTable.add(); playerTable.add(); playerTable.add(addAI).colspan(5).left(); } table.add(playerTable).fillX().colspan(2); table.row(); sList = game.getSpectators(); if (sList.keySet().size() > 0) { table.add("Spectators").colspan(2).left(); table.row(); Table spectatorTable = new Table(getSkin()); spectatorTable.setBackground(getSkin().getDrawable("default-pane")); for (Integer id : sList.keySet()) { if (config.isHost) { Image btn = getRemovePlayerButton(id); spectatorTable.add(btn).size(btn.getWidth() / VOBGame.SCALE, btn.getHeight() / VOBGame.SCALE) .padLeft(10).padRight(10); } spectatorTable.add(sList.get(id)).expandX().left(); spectatorTable.row().expandX().padBottom(5).padTop(5); } table.add(spectatorTable).fillX().colspan(2); table.row(); } ChangeListener updateListener = new ChangeListener() { @Override public void changed(ChangeEvent event, Actor actor) { needsUpdate = true; } }; Table setupTable = new Table(getSkin()); chkSpectators = new CheckBox(" Allow Spectators?", getSkin()); chkSpectators.setChecked(config.allowSpectate); chkSpectators.setDisabled(!config.isHost); chkSpectators.addListener(updateListener); setupTable.add(chkSpectators).colspan(2).left().padTop(4).row(); Label mapLbl = new Label("Map:", getSkin()); setupTable.add(mapLbl).left(); if (config.isHost) { needsUpdate = true; // always send an update on setup, just so we're all on the same page JsonReader reader = new JsonReader(); JsonValue val = reader.parse(Gdx.files.internal("maps/maps.json").readString()); Array<String> maps = new Array<String>(); for (JsonValue v : val) { maps.add(v.asString()); } mapSelect = new SelectBox(maps.toArray(), getSkin()); mapSelect.setSelection(config.mapName); mapSelect.addListener(updateListener); setupTable.add(mapSelect).left().padTop(4).padBottom(4).fillX(); } else { lblMap = new Label(config.mapName, getSkin()); setupTable.add(lblMap).left(); } setupTable.row(); setupTable.add("Rules:").left(); if (config.isHost) { Array<VOBGame.RuleSet> ruleSets = game.getRuleSets(); String[] items = new String[ruleSets.size]; for (int i = 0; i < ruleSets.size; i++) { items[i] = ruleSets.get(i).name; } ruleSelect = new SelectBox(items, getSkin()); setupTable.add(ruleSelect).left().padBottom(4).fillX(); ruleSelect.setSelection(config.ruleSet); ruleSelect.addListener(updateListener); } else { lblRule = new Label(game.getRuleSets().get(config.ruleSet).name, getSkin()); setupTable.add(lblRule).left(); } setupTable.row(); setupTable.add("Starting Locations:").left(); if (config.isHost) { spawnSelect = new SelectBox(spawnTypes, getSkin()); setupTable.add(spawnSelect).left().padBottom(4).fillX(); spawnSelect.setSelection(config.spawnType); spawnSelect.addListener(updateListener); } else { lblSpawn = new Label(spawnTypes[config.spawnType], getSkin()); setupTable.add(lblSpawn).left(); } setupTable.row(); setupTable.add("First Move:").left(); if (config.isHost) { moveSelect = new SelectBox(firstMoves, getSkin()); setupTable.add(moveSelect).left().padBottom(4).fillX(); moveSelect.setSelection(config.firstMove); moveSelect.addListener(updateListener); } else { lblMove = new Label(firstMoves[config.firstMove], getSkin()); setupTable.add(lblMove).left(); } setupTable.row(); setupTable.add("Timing Rules:").colspan(2).left().row(); Table timingTable = new Table(getSkin()); setupTable.add(timingTable).colspan(2).fillX(); timingTable.add("Base:").left().expandX(); if (config.isHost) { baseTime = new SelectBox(new String[] { "30", "60", "90" }, getSkin()); timingTable.add(baseTime).expandX().padLeft(4); baseTime.setSelection(Integer.toString(config.baseTimer)); baseTime.addListener(updateListener); } else { lblBaseTime = new Label(Integer.toString(config.baseTimer), getSkin()); timingTable.add(lblBaseTime).expandX().padLeft(4); } timingTable.add("Action:").left().expandX().padLeft(4); if (config.isHost) { actionTime = new SelectBox(new String[] { "0", "15", "30" }, getSkin()); timingTable.add(actionTime).expandX().padLeft(4); actionTime.setSelection(Integer.toString(config.actionBonusTime)); actionTime.addListener(updateListener); } else { lblActionTime = new Label(Integer.toString(config.actionBonusTime), getSkin()); timingTable.add(lblActionTime).expandX().padLeft(4); } timingTable.add("Unit:").left().expandX().padLeft(4); if (config.isHost) { unitTime = new SelectBox(new String[] { "0", "3", "5" }, getSkin()); timingTable.add(unitTime).expandX().padLeft(4); unitTime.setSelection(Integer.toString(config.unitBonusTime)); unitTime.addListener(updateListener); } else { lblUnitTime = new Label(Integer.toString(config.unitBonusTime), getSkin()); timingTable.add(lblUnitTime).expandX().padLeft(4); } table.add(setupTable).fillX(); Table controlTable = new Table(getSkin()); Sprite mapThumb = game.getTextureManager().getSpriteFromAtlas("assets", config.mapName); if (mapThumb != null) { controlTable.add(new Image(mapThumb)).colspan(2).row(); } if (config.isHost) { isHost = true; TextButton start = new TextButton("Start Game", getSkin()); start.addListener(new ClickListener() { @Override public void clicked(InputEvent event, float x, float y) { super.clicked(event, x, y); if (game.getPlayers().size >= 2) game.sendStartGame(); } }); controlTable.add().expand(); controlTable.add(start).padTop(4).right(); controlTable.row(); } TextButton cancel = new TextButton("Cancel", getSkin(), "cancel"); cancel.addListener(new ClickListener() { @Override public void clicked(InputEvent event, float x, float y) { super.clicked(event, x, y); game.setScreen(game.getMainMenuScreen()); game.closeGame(); } }); controlTable.add(); controlTable.add(cancel).fillX().padTop(4).right(); table.add(controlTable).fillX().row(); if (chatTable == null) { chatTable = new Table(getSkin()); chatScroll = new ScrollPane(chatTable, getSkin()); chatScroll.setFadeScrollBars(false); chatTable.padLeft(20); } table.add(chatScroll).colspan(2).fillX().height(100); table.row(); Table chatBox = new Table(getSkin()); final TextField chatMsgText = new TextField("", getSkin()); if (chatListener != null) { stage.removeListener(chatListener); } stage.addListener(new InputListener() { @Override public boolean keyUp(InputEvent event, int keycode) { if (stage.getKeyboardFocus() == chatMsgText && keycode == Input.Keys.ENTER && chatMsgText.getText().length() > 0) { Gdx.app.log(LOG, "Chat: " + chatMsgText.getText()); client.sendChat(chatMsgText.getText()); chatMsgText.setText(""); return false; } return super.keyUp(event, keycode); } }); chatBox.add(chatMsgText).expandX().fillX(); TextButton chatMsgSend = new TextButton("Send", getSkin(), "small"); chatMsgSend.addListener(new ClickListener() { @Override public void clicked(InputEvent event, float x, float y) { super.clicked(event, x, y); //To change body of overridden methods use File | Settings | File Templates. if (chatMsgText.getText().length() > 0) { Gdx.app.log(LOG, "Chat: " + chatMsgText.getText()); client.sendChat(chatMsgText.getText()); chatMsgText.setText(""); } } }); chatBox.add(chatMsgSend).fillY().fillX(); table.add(chatBox).colspan(2).fillX(); chatHistory = new Array<String>(); }