/** * @param xCor left coordinate of intersection * @param yCor right coordinate of intersection * @return PolygonRegion of intersection piece which lies on intersection with coordinates xCor and yCor, null if no game piece lies on that space */ private PolygonRegion getIntersectionPiece(int xCor, int yCor) { for (PolygonRegion pr : villages) { float xV0 = pr.getVertices()[0]; float yV0 = pr.getVertices()[1]; float xPos = (xCor * BASE); float yPos = -1 * (yCor * LENGTH / 2); if (xV0 == xPos - PIECEBASE / 2.0 && yV0 == yPos - PIECEBASE / 2.0) { return pr; } } return null; }
/** * @param xCorFirst left coordinate of first intersection * @param yCorFirst right coordinate of first intersection * @param xCorSecond left coordinate of second intersection * @param yCorSecond right coordinate of second intersection * @return PolygonRegion of edge piece which lies between (xCorFirst,yCorFirst) and (xCorSecond,yCorSecon), null if no game piece lies on that space */ private PolygonRegion getEdgePiece(int xCorFirst, int yCorFirst, int xCorSecond, int yCorSecond) { for (PolygonRegion pr : edgeUnits) { float xVM = pr.getVertices()[0]; float yVM = pr.getVertices()[1]; float xCorM = (float) (Math.min(xCorFirst, xCorSecond) + (Math.max(xCorFirst, xCorSecond) - Math.min(xCorFirst, xCorSecond)) / 2.0); float yCorM = (float) (Math.min(yCorFirst, yCorSecond) + (Math.max(yCorFirst, yCorSecond) - Math.min(yCorFirst, yCorSecond)) / 2.0); if (xVM == xCorM && yVM == yCorM) { return pr; } } return null; }
@Override public void draw (Batch batch, float parentAlpha) { batch.end(); Gdx.gl.glLineWidth(10); for (PolygonRegion region : polygonRegions) { this.batch.begin(); this.batch.draw(region, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); this.batch.end(); Polyline line = new Polyline(region.getVertices()); line.setScale(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); shapeRenderer.begin(ShapeRenderer.ShapeType.Line); shapeRenderer.setProjectionMatrix(batch.getProjectionMatrix()); shapeRenderer.setColor(Color.BROWN); shapeRenderer.polyline(line.getTransformedVertices()); shapeRenderer.end(); } batch.begin(); }
protected int apply (float[] vertices, int vertexStartingIndex, AttributeOffsets offsets, int vertexSize) { final PolygonRegion region = this.region; final TextureRegion tRegion = region.getRegion(); if (!sizeSet && region != null) { width = tRegion.getRegionWidth(); height = tRegion.getRegionHeight(); } float color = this.color; for (int i = 0, v = vertexStartingIndex + offsets.color0; i < numVertices; i++, v += vertexSize) { vertices[v] = color; } float[] textureCoords = region.getTextureCoords(); for (int i = 0, v = vertexStartingIndex + offsets.textureCoordinate0, n = textureCoords.length; i < n; i += 2, v += vertexSize) { vertices[v] = textureCoords[i]; vertices[v + 1] = textureCoords[i + 1]; } return 0; // handled by subclass }
PolygonRegion createCircleRegion (float degrees) { final int steps = Math.abs((int)degrees); if (steps < 3) { return new PolygonRegion(new TextureRegion(emptyTexture), new float[0], new short[0]); } float[] vertecies = new float[steps * 2]; vertecies[0] = 0; vertecies[1] = 0; for (int i = 2; i < (vertecies.length); i += 2) { float subDegrees = degrees * ((float)i - 2) / (vertecies.length - 4); vertecies[i] = (float)Math.cos(Math.toRadians(subDegrees)); vertecies[i + 1] = (float)Math.sin(Math.toRadians(subDegrees)); } short[] triangles = new short[(steps - 2) * 3]; for (int i = 0; i < (steps - 2) * 3; i += 3) { triangles[i] = 0; triangles[i + 1] = (short)((i / 3) + 1); triangles[i + 2] = (short)((i / 3) + 2); } return new PolygonRegion(new TextureRegion(emptyTexture), vertecies, triangles); }
/** Creates a new AssetManager with all default loaders. */ public AssetManager (FileHandleResolver resolver) { setLoader(BitmapFont.class, new BitmapFontLoader(resolver)); setLoader(Music.class, new MusicLoader(resolver)); setLoader(Pixmap.class, new PixmapLoader(resolver)); setLoader(Sound.class, new SoundLoader(resolver)); setLoader(TextureAtlas.class, new TextureAtlasLoader(resolver)); setLoader(Texture.class, new TextureLoader(resolver)); setLoader(Skin.class, new SkinLoader(resolver)); setLoader(ParticleEffect.class, new ParticleEffectLoader(resolver)); setLoader(PolygonRegion.class, new PolygonRegionLoader(resolver)); setLoader(I18NBundle.class, new I18NBundleLoader(resolver)); setLoader(Model.class, ".g3dj", new G3dModelLoader(new JsonReader(), resolver)); setLoader(Model.class, ".g3db", new G3dModelLoader(new UBJsonReader(), resolver)); setLoader(Model.class, ".obj", new ObjLoader(resolver)); executor = new AsyncExecutor(1); }
@Override public void create () { texture = new Texture(Gdx.files.internal("data/tree.png")); PolygonRegionLoader loader = new PolygonRegionLoader(); region = loader.load(new TextureRegion(texture), Gdx.files.internal("data/tree.psh")); // create a region from an arbitrary set of vertices (a triangle in this case) region2 = new PolygonRegion(new TextureRegion(texture), new float[] {0, 0, 100, 100, 0, 100}, new short[] {0, 1, 2}); camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); camera.position.x = 0; camera.position.y = 0; batch = new PolygonSpriteBatch(); debugRenderer = new PolygonRegionDebugRenderer(); Gdx.input.setInputProcessor(this); }
/** * removes polygon of village at given coordinates from the board * * @param xCor left coordinate of intersection * @param yCor right coordinate of intersection * @return true if a village was removed from the board */ private boolean removeVillage(int xCor, int yCor) { PolygonRegion village = getIntersectionPiece(xCor, yCor); if (village != null) { villages.remove(village); return true; } return false; }
/** * renders the village with appropriate color and kind at the given position. If position is already occupied, the currently placed village will be removed and replaced * * @param position of intersection to update * @param color of player who owns the new Village * @param kind of new Village */ public void updateIntersection(CoordinatePair position, PlayerColor color, VillageKind kind) { int offsetX = position.getLeft(); int offsetY = position.getRight(); // Removes village on given coordinate removeVillage(offsetX, offsetY); PolygonRegion village = null; switch (kind) { case CITY: village = gamePieces.createCity(offsetX, offsetY, BASE, LENGTH, PIECEBASE, color); break; case SCIENCE_METROPOLIS: village = gamePieces.createMetropolis(offsetX, offsetY, BASE, LENGTH, PIECEBASE, kind); break; case SETTLEMENT: village = gamePieces.createSettlement(offsetX, offsetY, BASE, LENGTH, PIECEBASE, color); break; case TRADE_METROPOLIS: village = gamePieces.createMetropolis(offsetX, offsetY, BASE, LENGTH, PIECEBASE, kind); break; case POLITICS_METROPOLIS: village = gamePieces.createMetropolis(offsetX, offsetY, BASE, LENGTH, PIECEBASE, kind); break; default: break; } if (village != null) villages.add(village); }
/** * renders the road with appropriate color and position. If edge is already occupied, it removes current edge piece and replaces it with this one * * @param firstCoordinate end point of edge * @param secondCoordinate other end point of edge * @param kind of new edge unit (SHIP or ROAD) * @param color of player who owns the new edge unit */ public void updateEdge(CoordinatePair firstCoordinate, CoordinatePair secondCoordinate, EdgeUnitKind kind, PlayerColor color) { if (firstCoordinate == null || secondCoordinate == null) { return; } int xCorFirst = firstCoordinate.getLeft(); int yCorFirst = firstCoordinate.getRight(); int xCorSecond = secondCoordinate.getLeft(); int yCorSecond = secondCoordinate.getRight(); System.out.println("Road placed at " + firstCoordinate + " " + secondCoordinate); // removes edge on given coordinate removeEdgeUnit(xCorFirst, yCorFirst, xCorSecond, yCorSecond); PolygonRegion edgeUnit = null; switch (kind) { case ROAD: edgeUnit = gamePieces.createRoad(xCorFirst, yCorFirst, xCorSecond, yCorSecond, BASE, LENGTH, PIECEBASE, color); break; case SHIP: edgeUnit = gamePieces.createShip(xCorFirst, yCorFirst, xCorSecond, yCorSecond, BASE, LENGTH, PIECEBASE, color); break; default: break; } if (edgeUnit != null) edgeUnits.add(edgeUnit); }
/** * Re-color a given region * @param color New Color for the Region * @param region Region to re-color */ private void setRegionColor(Color color, AssetMap.Region region) { Pixmap pix = new Pixmap(1, 1, Pixmap.Format.RGBA8888); pix.setColor(color); pix.fill(); Texture regionTexture = new Texture(pix); PolygonRegion polygonRegion = regionMap.get(region); polygonRegion.getRegion().setTexture(regionTexture); }
private void initPolygonRegions() { polygonRegions = new ArrayList<>(assetMap.getRegions().size()); for (AssetMap.Region region : assetMap.getRegions()) { PolygonRegion polyReg = new PolygonRegion(new TextureRegion(texture), region.getVertices(), new EarClippingTriangulator().computeTriangles(region.getVertices()).toArray()); regionMap.put(region, polyReg); regionNameMap.put(region.getName(), region); polygonRegions.add(polyReg); } }
@Override protected int apply (float[] vertices, int vertexStartingIndex, AttributeOffsets offsets, int vertexSize) { super.apply(vertices, vertexStartingIndex, offsets, vertexSize); final PolygonRegion region = this.region; final TextureRegion tRegion = region.getRegion(); final float originX = this.originX; final float originY = this.originY; final float scaleX = this.scaleX; final float scaleY = this.scaleY; final float[] regionVertices = region.getVertices(); final float worldOriginX = x + originX; final float worldOriginY = y + originY; final float sX = width / tRegion.getRegionWidth(); final float sY = height / tRegion.getRegionHeight(); final float cos = MathUtils.cosDeg(rotation); final float sin = MathUtils.sinDeg(rotation); float fx, fy; for (int i = 0, v = vertexStartingIndex + offsets.position, n = regionVertices.length; i < n; i += 2, v += vertexSize) { fx = (regionVertices[i] * sX - originX) * scaleX; fy = (regionVertices[i + 1] * sY - originY) * scaleY; vertices[v] = cos * fx - sin * fy + worldOriginX; vertices[v + 1] = sin * fx + cos * fy + worldOriginY; } return numVertices; }
private void drawHoldBeam(Vector2 from, Vector2 to, float orgSize, float dstSize) { Vector2 delta = from.cpy().sub(to); float w = Math.max(orgSize, dstSize); float h = delta.len(); float tw = holdBG.getRegionWidth(); float th = holdBG.getRegionHeight(); float factorScale = (tw / w) * 0.5f; float topFactor = Math.max(dstSize - orgSize, 0f) * factorScale; float botFactor = Math.max(orgSize - dstSize, 0f) * factorScale; float[] points = { topFactor, 0f, botFactor, th, tw - botFactor, th, tw - topFactor, 0f }; PolygonRegion clamped = new PolygonRegion(holdBG, points, triangles); spriteBatch.draw(clamped, from.x - w * 0.5f, from.y, w * 0.5f, 0f, w, h, 1f, 1f, delta.angle() + 90); }
public PolySpatial(PolygonRegion region, Body body, Color color) { this(region,color); mBody = body; mTmp.set(mBody.getPosition()); mSprite.setOrigin(0, 0); }
public Map<AssetMap.Region, PolygonRegion> getRegionMap() { return regionMap; }
public List<PolygonRegion> getPolygonRegions() { return polygonRegions; }
protected PolygonRegion createdPolygonRegion(TextureRegion pixel, float[] vertices) { return new PolygonRegion(pixel, vertices, new EarClippingTriangulator().computeTriangles(vertices) .toArray()); }
@Override public void render (float delta) { Gdx.gl.glClearColor(1, 1, 1, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); PolygonRegion outerRegion = createCircleRegion(percentage % 2 < 1 ? (percentage % 1) * 360 : 1 * 360); PolygonRegion innerRegion = createCircleRegion(percentage % 2 > 1 ? (percentage % 1) * 360 : 0); Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); camera.viewportWidth = Gdx.graphics.getWidth(); camera.viewportHeight = Gdx.graphics.getHeight(); camera.update(); final int ringRadius = 200; final int innerRingRadius = 180; final int innerRingWidth = 20; final Color outerColor = new Color(0.97f, 0.44f, 0f, 1f); final Color innerColor = Color.WHITE; String percentageString = (Math.round((((float)percentage % 1)) * 10)) * 10 + "%"; polygonBatch.setProjectionMatrix(camera.combined); polygonBatch.begin(); polygonBatch.setColor(outerColor); polygonBatch.draw(outerRegion, 0, 0, ringRadius, ringRadius); polygonBatch.setColor(innerColor); polygonBatch.draw(innerRegion, 0, 0, innerRingRadius, innerRingRadius); polygonBatch.setColor(outerColor); polygonBatch.draw(innerRegion, 0, 0, innerRingRadius - innerRingWidth, innerRingRadius - innerRingWidth); font.setColor(innerColor); font.getData().setScale(1f); font.setColor(Color.WHITE); drawStringCentered(polygonBatch, 0, 0, percentageString); font.getData().setScale(1f); font.setColor(Color.GRAY); drawStringCentered(polygonBatch, 0, -300, percentage % 2 < 1 ? "Fetching Assets..." : "Recieving Game State..."); polygonBatch.end(); }
@Override public void create() { super.create(); executor = new AsyncExecutor(1); // create a string of generally-overlapping polygons, will draw in // blue GeoTester.randomPolys(3, 40, 80, new Vector2(100, 300), blue); float s = 10; Polygon p0 = new Polygon(new float[] { // north-west, low, north-east 0, 3 * s, 0, 2 * s, 2 * s, 0, 3 * s, 0, 4.5f * s, 2 * s, 6 * s, 0, 7 * s, 0, 9 * s, 2 * s, 9 * s, 3 * s, // north-east, high, north-west 8 * s, 3 * s, 6.5f * s, 1 * s, 5 * s, 3 * s, 4 * s, 3 * s, 2.5f * s, s, 1 * s, 3 * s }); blue.add(p0); // merge them into a single polygon, will draw in red for (Polygon bp : blue) { GeometryUtils.merge(geo, bp); } Geometry collapsed = GeometryUtils.collapse(geo); Polygon p = GeometryUtils.jtsCoordsToGdx(collapsed.getCoordinates()); red.add(p); triangles = GeometryUtils.triangulate(collapsed); Gdx.app.error("GeoTester", "ready to display triangles worth " + triangles.length + " vertices"); // use the polygon to clip a randomly-generated texture textureSolid = new Texture(GeoTester.randomPixmap(100, 100, null), false); PolygonRegion polyReg = new PolygonRegion(new TextureRegion( textureSolid), p.getVertices(), triangles); poly = new PolygonSprite(polyReg); poly.setOrigin(p.getVertices()[0], p.getVertices()[1]); polyBatch = new PolygonSpriteBatch(); // prepare rendering aids shapeRenderer = new ShapeRenderer(); Gdx.input.setInputProcessor(this); }
public PolySpatial(PolygonRegion region, Color color) { mSprite = new PolygonSprite(region); mSprite.setColor(color); mSprite.setSize(region.getRegion().getRegionWidth()/PIXELS_PER_METER,region.getRegion().getRegionHeight()/PIXELS_PER_METER); }
/** * removes polygon of edge unit at given coordinates from the board * * @param xCorFirst left coordinate of first intersection * @param yCorFirst right coordinate of first intersection * @param xCorSecond left coordinate of second intersection * @param yCorSecond right coordinate of second intersection * @return true if an edge unit was removed from the board */ public boolean removeEdgeUnit(int xCorFirst, int yCorFirst, int xCorSecond, int yCorSecond) { PolygonRegion edgePiece = getEdgePiece(xCorFirst, yCorFirst, xCorSecond, yCorSecond); if (edgePiece != null) { edgeUnits.remove(edgePiece); return true; } return false; }