List of usage examples for com.badlogic.gdx.utils Array toArray
public T[] toArray()
From source file:cocos2d.touch_dispatcher.CCTouchDispatcher.java
License:Open Source License
public void touches(Array<CCTouch> pTouches, CCTouchType touchType) { m_bLocked = true;//from w w w.j a v a 2s.c om // optimization to prevent a mutable copy when it is not necessary int uTargetedHandlersCount = m_pTargetedHandlers.size; int uStandardHandlersCount = m_pStandardHandlers.size; boolean bNeedsMutableSet = (uTargetedHandlersCount > 0 && uStandardHandlersCount > 0); if (bNeedsMutableSet) { CCTouch[] tempArray = new CCTouch[pTouches.size]; tempArray = pTouches.toArray(); //Collections.addAll(pMutableTouches, tempArray); pMutableTouches = new Array<CCTouch>(tempArray); } else { pMutableTouches = pTouches; } CCTouchType sHelper = touchType; // process the target handlers 1st if (uTargetedHandlersCount > 0) { for (CCTouch pTouch : pTouches) { for (CCTouchHandler pcHandler : m_pTargetedHandlers) { CCTargetedTouchHandler pHandler = (CCTargetedTouchHandler) pcHandler; ICCTargetedTouchDelegate pDelegate = (ICCTargetedTouchDelegate) (pHandler.getDelegate()); boolean bClaimed = false; if (sHelper == CCTouchType.Began) { bClaimed = pDelegate.touchBegan(pTouch); if (bClaimed) { pHandler.getClaimedTouches().add(pTouch); } } else { if (pHandler.getClaimedTouches().contains(pTouch)) { // moved ended cancelled bClaimed = true; switch (sHelper) { case Moved: pDelegate.touchMoved(pTouch); break; case Ended: pDelegate.touchEnded(pTouch); pHandler.getClaimedTouches().remove(pTouch); break; case Cancelled: pDelegate.touchCancelled(pTouch); pHandler.getClaimedTouches().remove(pTouch); break; } } } if (bClaimed && pHandler.getIsSwallowsTouches()) { if (bNeedsMutableSet) { pMutableTouches.removeValue(pTouch, true); } break; } } } } // process standard handlers 2nd if (uStandardHandlersCount > 0 && pMutableTouches.size > 0) { for (CCTouchHandler pcHandler : m_pStandardHandlers) { CCStandardTouchHandler pHandler = (CCStandardTouchHandler) pcHandler; ICCStandardTouchDelegate pDelegate = (ICCStandardTouchDelegate) pHandler.getDelegate(); switch (sHelper) { case Began: pDelegate.touchesBegan(pMutableTouches); break; case Moved: pDelegate.touchesMoved(pMutableTouches); break; case Ended: pDelegate.touchesEnded(pMutableTouches); break; case Cancelled: pDelegate.touchesCancelled(pMutableTouches); break; } } } if (bNeedsMutableSet) { pMutableTouches = null; } // // Optimization. To prevent a [handlers copy] which is expensive // the add/removes/quit is done after the iterations // m_bLocked = false; if (m_bToRemove) { m_bToRemove = false; for (int i = 0; i < m_pHandlersToRemove.size; ++i) { forceRemoveDelegate((ICCTouchDelegate) m_pHandlersToRemove.get(i)); } m_pHandlersToRemove.clear(); } if (m_bToAdd) { m_bToAdd = false; for (CCTouchHandler pHandler : m_pHandlersToAdd) { if (pHandler instanceof CCTargetedTouchHandler && pHandler.getDelegate() instanceof ICCTargetedTouchDelegate) { forceAddHandler(pHandler, m_pTargetedHandlers); } else if (pHandler instanceof CCStandardTouchHandler && pHandler.getDelegate() instanceof ICCStandardTouchDelegate) { forceAddHandler(pHandler, m_pStandardHandlers); } else { //CCLog.Log("ERROR: inconsistent touch handler and delegate found in m_pHandlersToAdd of CCTouchDispatcher"); } } m_pHandlersToAdd.clear(); } if (m_bToQuit) { m_bToQuit = false; forceRemoveAllDelegates(); } // for (CCTouchHandler pHandler : m_pTargetedHandlers) // { // ICCTargetedTouchDelegate pDelegate = (ICCTargetedTouchDelegate)pHandler.getDelegate(); // pDelegate.touchBegan(pTouches.get(0)); // } }
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);//w w w.j a va 2s . c o m 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>(); }
From source file:com.ahsgaming.valleyofbones.screens.SPGameSetupScreen.java
License:Apache License
public void setupScreen() { Label gameTypeLbl = new Label("Single Player", getSkin(), "medium"); Table table = new Table(getSkin()); table.setFillParent(true);//from w ww. j a v a 2 s . 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).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 { playerTable.add().padLeft(10).padRight(10); } playerTable.add(new Label(String.format("%s (%d)", p.getPlayerName(), p.getPlayerId()), getSkin())) .expandX().left(); 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) { p.setRace(((SelectBox) actor).getSelection()); } }); Image color = new Image(getSkin().getDrawable("white-hex")); color.setColor(p.getPlayerColor()); playerTable.add(color).expandX(); 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]); p.setPlayerColor(Player.AUTOCOLORS[i]); } }); playerTable.row().padBottom(5).padTop(5); } table.add(playerTable).fillX().colspan(2); table.row(); Table setupTable = new Table(getSkin()); Label mapLbl = new Label("Map:", getSkin()); setupTable.add(mapLbl).left(); 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); config.mapName = mapSelect.getSelection(); mapSelect.addListener(new ChangeListener() { @Override public void changed(ChangeEvent event, Actor actor) { config.mapName = ((SelectBox) actor).getSelection(); needsUpdate = true; } }); mapThumb = new Image(game.getTextureManager().getSpriteFromAtlas("assets", config.mapName)); setupTable.add(mapSelect).left().padBottom(4).padTop(4).fillX(); setupTable.row(); setupTable.add("Rules:").left(); SelectBox ruleSelect = new SelectBox(new String[] { "Classic" }, getSkin()); ruleSelect.setSelection(config.ruleSet); ruleSelect.addListener(new ChangeListener() { @Override public void changed(ChangeEvent event, Actor actor) { config.ruleSet = ((SelectBox) actor).getSelectionIndex(); } }); setupTable.add(ruleSelect).left().padBottom(4).fillX(); setupTable.row(); setupTable.add("Starting Locations:").left(); SelectBox spawnSelect = new SelectBox(new String[] { "Normal", "Inverted", "Random" }, getSkin()); spawnSelect.setSelection(config.spawnType); spawnSelect.addListener(new ChangeListener() { @Override public void changed(ChangeEvent event, Actor actor) { config.spawnType = ((SelectBox) actor).getSelectionIndex(); } }); setupTable.add(spawnSelect).left().padBottom(4).fillX(); setupTable.row(); setupTable.add("First Move:").left(); SelectBox moveSelect = new SelectBox(new String[] { "Random", "P1", "P2" }, getSkin()); moveSelect.setSelection(config.firstMove); moveSelect.addListener(new ChangeListener() { @Override public void changed(ChangeEvent event, Actor actor) { config.firstMove = ((SelectBox) actor).getSelectionIndex(); } }); setupTable.add(moveSelect).left().padBottom(4).fillX(); 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(); SelectBox baseTime = new SelectBox(new String[] { "30", "60", "90" }, getSkin()); timingTable.add(baseTime).expandX(); baseTime.setSelection(Integer.toString(config.baseTimer)); baseTime.addListener(new ChangeListener() { @Override public void changed(ChangeEvent event, Actor actor) { config.baseTimer = Integer.parseInt(((SelectBox) actor).getSelection()); } }); timingTable.add("Action:").left().expandX(); SelectBox actionTime = new SelectBox(new String[] { "0", "15", "30" }, getSkin()); timingTable.add(actionTime).expandX(); actionTime.setSelection(Integer.toString(config.actionBonusTime)); actionTime.addListener(new ChangeListener() { @Override public void changed(ChangeEvent event, Actor actor) { config.actionBonusTime = Integer.parseInt(((SelectBox) actor).getSelection()); } }); timingTable.add("Unit:").left().expandX(); SelectBox unitTime = new SelectBox(new String[] { "0", "3", "5" }, getSkin()); timingTable.add(unitTime).expandX(); unitTime.setSelection(Integer.toString(config.unitBonusTime)); unitTime.addListener(new ChangeListener() { @Override public void changed(ChangeEvent event, Actor actor) { config.unitBonusTime = Integer.parseInt(((SelectBox) actor).getSelection()); } }); setupTable.row().expandX().expandY().top().left(); table.add(setupTable).fill(); Table controlTable = new Table(getSkin()); controlTable.add(mapThumb).colspan(2).row(); isHost = true; TextButton start = new TextButton("Start Game", getSkin()); start.addListener(new ClickListener() { /* (non-Javadoc) * @see com.badlogic.gdx.scenes.scene2d.utils.ClickListener#touchUp(com.badlogic.gdx.scenes.scene2d.InputEvent, float, float, int, int) */ @Override public void touchUp(InputEvent event, float x, float y, int pointer, int button) { super.touchUp(event, x, y, pointer, button); 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() { /* (non-Javadoc) * @see com.badlogic.gdx.scenes.scene2d.utils.ClickListener#touchUp(com.badlogic.gdx.scenes.scene2d.InputEvent, float, float, int, int) */ @Override public void touchUp(InputEvent event, float x, float y, int pointer, int button) { super.touchUp(event, x, y, pointer, button); game.setScreen(game.getMainMenuScreen()); game.closeGame(); } }); controlTable.add(); controlTable.add(cancel).fillX().padTop(4); table.add(controlTable).fillX(); table.row(); }
From source file:com.dongbat.game.buff.effects.DelayBuff.java
@Override public void durationEnd(World world, Entity source, Entity target) { Array<Object> toPass = new Array<Object>(); for (ObjectMap.Entry<String, Object> arg : args) { toPass.add(arg.key);//from w w w .jav a 2 s.c om toPass.add(arg.value); } BuffUtil.addBuff(world, source, target, buffName, duration, 1, toPass.toArray()); }
From source file:org.ams.testapps.paintandphysics.cardhouse.CardHouse.java
License:Open Source License
/** * Creates a bunch of blocks and adds them to the {@link #world}. * They are then merged so they look like the ground. * A body with a {@link ChainShape} is used for physical ground * in order to avoid ghost vertex collisions. */// ww w.j av a 2 s .com private void addGround(TextureRegion textureRegion, int blockCount) { debug("Adding ground."); // prepare visual ground float halfBlockWidth = cardHouseDef.cardHeight * 0.5f; float halfBlockHeight = cardHouseDef.cardHeight * 0.5f; float groundWidth = halfBlockWidth * 2 * blockCount; Array<Vector2> vertices = new Array<Vector2>(); vertices.add(new Vector2(-halfBlockWidth * 1.001f, -halfBlockHeight)); vertices.add(new Vector2(halfBlockWidth * 1.001f, -halfBlockHeight)); vertices.add(new Vector2(halfBlockWidth * 1.001f, halfBlockHeight)); vertices.add(new Vector2(-halfBlockWidth * 1.001f, halfBlockHeight)); Array<OutlinePolygon> outlinePolygons = new Array<OutlinePolygon>(); Array<OutlinePolygon> shadowPolygons = new Array<OutlinePolygon>(); Array<TexturePolygon> texturePolygons = new Array<TexturePolygon>(); for (int i = 0; i < blockCount; i++) { float x = (i + 0.5f) * halfBlockWidth * 2 - groundWidth * 0.5f; Vector2 pos = new Vector2(x, cardHouseDef.groundY - halfBlockHeight); PPPolygon block = addGround(textureRegion, vertices, pos); outlinePolygons.add(block.getOutlinePolygons().first()); shadowPolygons.add(block.getOutlinePolygons().get(1)); texturePolygons.add(block.getTexturePolygon()); block.getTexturePolygon().setColor(cardHouseDef.groundColor); } // merge outlines and align textures OutlineMerger outlineMerger = new OutlineMerger(); outlineMerger.mergeOutlines(outlinePolygons); outlineMerger.mergeOutlines(shadowPolygons); TextureAligner textureAligner = new TextureAligner(); textureAligner.alignTextures(texturePolygons); // prepare physical ground // create box2d body BodyDef bodyDef = new BodyDef(); bodyDef.type = BodyDef.BodyType.StaticBody; bodyDef.active = true; Body chainBody = world.boxWorld.world.createBody(bodyDef); // create box2d fixture ChainShape chainShape = new ChainShape(); Array<Vector2> chainVertices = new Array<Vector2>(true, 1, Vector2.class); chainVertices.addAll(outlinePolygons.first().getMyParents().first().getVertices()); chainShape.createLoop(chainVertices.toArray()); FixtureDef fixtureDef = new FixtureDef(); fixtureDef.shape = chainShape; fixtureDef.friction = cardHouseDef.friction; chainBody.createFixture(fixtureDef); chainShape.dispose(); }
From source file:releasethekraken.path.SeaCreaturePath.java
/** * Constructs a new SeaCreaturePath// w w w . j a v a2 s .com * @param id The path's ID, from the level editor * @param parent The path that leads to this path, or null if this is the first path * @param nextPaths A list of paths that can be gone on after this path. The list is empty if there are none. * @param polyline The Polyline object that holds the path data */ public SeaCreaturePath(int id, SeaCreaturePath parent, Array<SeaCreaturePath> nextPaths, Polyline polyline) { this.id = id; this.parent = parent; this.nextPaths = nextPaths; final float scale = 1 / 16F; //Scale it down to match the world units //Scale the polyline to match the world units. //This has to be done manually because Polyline.setScale() doesn't seem to work. float[] vertices = polyline.getTransformedVertices(); for (int i = 0; i < vertices.length; i++) vertices[i] *= scale; polyline = new Polyline(vertices); this.polyline = polyline; vertices = polyline.getVertices(); //Get an array of vertices, in x1, y1, x2, y2,... format Vector2[] points = new Vector2[vertices.length / 2]; //Make an array of Vector2 objects //Convert the vertices to Vector2 objects for (int i = 0; i < points.length; i++) { float x = vertices[i * 2]; float y = vertices[(i * 2) + 1]; points[i] = new Vector2(x, y); } //TODO: Fix this! //Add additional starting and ending points so that all of the original points are used, due to how catmull rom spline works Array<Vector2> pointsList = new Array<Vector2>(points); Vector2 firstPoint = points[0].cpy().add(points[0].cpy().sub(points[1])); pointsList.insert(0, firstPoint); Vector2 lastPoint = points[points.length - 1].cpy() .add(points[points.length - 1].cpy().sub(points[points.length - 2])); pointsList.insert(pointsList.size, lastPoint); //Make the smooth path with the points this.path = new CatmullRomSpline<Vector2>(pointsList.toArray(), false); Gdx.app.log("SCPath " + this.toString(true), "Points: " + Arrays.toString(points)); }