private void tryMove() { bounds.x += vel.x; fetchCollidableRects(); for (int i = 0; i < r.length; i++) { Rectangle rect = r[i]; if (bounds.overlaps(rect)) { if (vel.x < 0) bounds.x = rect.x + rect.width + 0.01f; else bounds.x = rect.x - bounds.width - 0.01f; vel.x = 0; } } bounds.y += vel.y; fetchCollidableRects(); for (int i = 0; i < r.length; i++) { Rectangle rect = r[i]; if (bounds.overlaps(rect)) { if (vel.y < 0) { bounds.y = rect.y + rect.height + 0.01f; } else bounds.y = rect.y - bounds.height - 0.01f; vel.y = 0; } } pos.x = bounds.x - 0.2f; pos.y = bounds.y - 0.2f; }
public void update(float delta, Array<Block> collisions) { // NOTE: figure out why legs go into block, could be order of checking for collisions/updating // position // position += velocity above collisions before changing the velocity acceleration.y = gravity; if (velocity.y < 0) onGround = false; if (delta > .1f) { // make sure time passed isn't great enough to cause clipping issues when window // moved delta = .02f; } acceleration.scl(delta); velocity.add(acceleration); processCollisions(collisions); if (Math.abs(velocity.x) < .05) velocity.x = 0; else { velocity.x *= friction; } if (velocity.x > maxVel) velocity.x = maxVel; if (velocity.x < -maxVel) velocity.x = -maxVel; position.add(velocity.cpy().scl(delta)); bounds.x = position.x; bounds.y = position.y; stateTime += delta; setPosition(getPosition()); }
public GameScreen(final Drop gam) { this.game = gam; // load the images for the droplet and the bucket, 64x64 pixels each dropImage = new Texture(Gdx.files.internal("droplet.png")); bucketImage = new Texture(Gdx.files.internal("bucket.png")); // load the drop sound effect and the rain background "music" dropSound = Gdx.audio.newSound(Gdx.files.internal("drop.wav")); rainMusic = Gdx.audio.newMusic(Gdx.files.internal("rain.mp3")); rainMusic.setLooping(true); // create the camera and the SpriteBatch camera = new OrthographicCamera(); camera.setToOrtho(false, 800, 480); // create a Rectangle to logically represent the bucket bucket = new Rectangle(); bucket.x = 800 / 2 - 64 / 2; // center the bucket horizontally bucket.y = 20; // bottom left corner of the bucket is 20 pixels above // the bottom screen edge bucket.width = 64; bucket.height = 64; // create the raindrops array and spawn the first raindrop raindrops = new Array<Rectangle>(); spawnRaindrop(); }
@Override public void create() { // load the images for the droplet and the bucket, 64x64 pixels each dropImage = new Texture(Gdx.files.internal("bottle.png")); bucketImage = new Texture(Gdx.files.internal("bucket_black.png")); brokenBucketImage = new Texture(Gdx.files.internal("bottle_broken.png")); // load the drop sound effect and the rain background "music" // dropSound = Gdx.audio.newSound(Gdx.files.internal("drop.wav")); // rainMusic = Gdx.audio.newMusic(Gdx.files.internal("rain.mp3")); // start the playback of the background music immediately // rainMusic.setLooping(true); // rainMusic.play(); // create the camera and the SpriteBatch camera = new OrthographicCamera(); camera.setToOrtho(false, 800, 480); batch = new SpriteBatch(); // create a Rectangle to logically represent the bucket bucket = new Rectangle(); bucket.x = 800 / 2 - 64 / 2; // center the bucket horizontally bucket.y = 20; // bottom left corner of the bucket is 20 pixels above the bottom screen edge bucket.width = 64; bucket.height = 80; // create the raindrops array and spawn the first raindrop raindrops = new Array<Rectangle>(); brokenBottles = new Array<Rectangle>(); spawnRaindrop(); }
@Override public void render() { // clear the screen with a dark blue color. The // arguments to glClearColor are the red, green // blue and alpha component in the range [0,1] // of the color to be used to clear the screen. Gdx.gl.glClearColor(0, 0, 0.2f, 1); Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); // tell the camera to update its matrices. camera.update(); // tell the SpriteBatch to render in the // coordinate system specified by the camera. batch.setProjectionMatrix(camera.combined); // begin a new batch and draw the bucket and // all drops batch.begin(); batch.draw(bucketImage, bucket.x, bucket.y); for (Rectangle raindrop : raindrops) { batch.draw(dropImage, raindrop.x, raindrop.y); } batch.end(); // // process user input if (Gdx.input.isTouched()) { Vector3 touchPos = new Vector3(); touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0); camera.unproject(touchPos); bucket.x = touchPos.x - 48 / 2; } if (Gdx.input.isKeyPressed(Keys.LEFT)) bucket.x -= 200 * Gdx.graphics.getDeltaTime(); if (Gdx.input.isKeyPressed(Keys.RIGHT)) bucket.x += 200 * Gdx.graphics.getDeltaTime(); // make sure the bucket stays within the screen bounds if (bucket.x < 0) bucket.x = 0; if (bucket.x > 800 - 48) bucket.x = 800 - 48; // commit // check if we need to create a new raindrop if (TimeUtils.nanoTime() - lastDropTime > 1000000000) spawnRaindrop(); // move the raindrops, remove any that are beneath the bottom edge of // the screen or that hit the bucket. In the later case we play back // a sound effect as well. Iterator<Rectangle> iter = raindrops.iterator(); while (iter.hasNext()) { Rectangle raindrop = iter.next(); raindrop.y -= 200 * Gdx.graphics.getDeltaTime(); if (raindrop.y + 48 < 0) iter.remove(); if (raindrop.overlaps(bucket)) { dropSound.play(); iter.remove(); } } }
public void update(float delta) { collisionRect.x = position.x; collisionRect.y = position.y; collisionRect.width = size.x; collisionRect.height = size.y; position.add(speed.cpy().scl(delta)); }
private void spawnRaindrop() { Rectangle raindrop = new Rectangle(); raindrop.x = MathUtils.random(0, 800 - 64); raindrop.y = 480; raindrop.width = 32; raindrop.height = 128; raindrops.add(raindrop); lastDropTime = TimeUtils.nanoTime(); }
public ShopTable(float x, float y, float velX, float velY, int width, int height) { super(x, y, velX, velY, width, height); rect.x = x; rect.y = y; rect.width = width; rect.height = height; item1 = new ShopPickup(x + 20, y + 32, 0, 0, 32, 32); item2 = new ShopPickup(x + 52, y + 32, 0, 0, 32, 32); item3 = new ShopPickup(x + 84, y + 32, 0, 0, 32, 32); }
public static long spawnRaindrop(Array<Rectangle> raindrops) { // create a raindrop and randomly place it at the top of the screen Rectangle raindrop = new Rectangle(); raindrop.x = MathUtils.random(0, 800 - 64); raindrop.y = 480; raindrop.width = 64; raindrop.height = 64; raindrops.add(raindrop); return TimeUtils.nanoTime(); }
public boolean collide(Rectangle body) { boolean collide = false; if (body.overlaps(AABB)) { collide = true; float bodyMidX = body.x + (body.width / 2); float bodyMidY = body.y + (body.height / 2); float AABBMidX = AABB.x + (AABB.width / 2); float AABBMidY = AABB.y + (AABB.height / 2); AABB.x -= deltaPos.x; AABB.y -= deltaPos.y; AABB.x += deltaPos.x; if (AABB.overlaps(body)) { if (AABBMidX - deltaPos.x > bodyMidX) { AABB.x = (int) body.x + (int) body.width; } else { AABB.x = body.x - AABB.width; } } AABB.y += deltaPos.y; if (AABB.overlaps(body)) { if (AABBMidY - deltaPos.y > bodyMidY) { AABB.y = body.y + body.height; } else { AABB.y = body.y - AABB.height; } } position.x = AABB.x - 14; position.y = AABB.y - 14; } return collide; }
@Override public void render() { Gdx.gl.glClearColor(0, 0, 0.2f, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); camera.update(); batch.setProjectionMatrix(camera.combined); batch.begin(); batch.draw(bucketImage, bucket.x, bucket.y); for (Rectangle raindrop : raindrops) { batch.draw(dropImage, raindrop.x, raindrop.y); } batch.end(); if (Gdx.input.isTouched()) { Vector3 touchPos = new Vector3(); touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0); camera.unproject(touchPos); bucket.x = touchPos.x - 64 / 2; } if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) bucket.x -= 200 * Gdx.graphics.getDeltaTime(); if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) bucket.x += 200 * Gdx.graphics.getDeltaTime(); // make sure the bucket stays within the screen bounds if (bucket.x < 0) bucket.x = 0; if (bucket.x > 800 - 64) bucket.x = 800 - 64; if (TimeUtils.nanoTime() - lastDropTime > 1000000000) { spawnRaindrop(); } Iterator<Rectangle> iter = raindrops.iterator(); while (iter.hasNext()) { Rectangle raindrop = iter.next(); raindrop.y -= 200 * Gdx.graphics.getDeltaTime(); if (raindrop.y + 64 < 0) iter.remove(); if (raindrop.overlaps(bucket)) { dropSound.play(); iter.remove(); } } }
public void getPlatformCollisions(float xx, float yy) { m_collisionPlatform = null; m_boundingBox.x = this.getBoundingRectangle().getX() + m_boundOffX; m_boundingBox.y = this.getBoundingRectangle().getY(); for (m_iter = 0; m_iter < m_platforms.size(); m_iter++) { PlatformSprite p = m_platforms.get(m_iter); if (p.isCollidable()) { if (Intersector.overlaps(p.getBoundingRectangle(), m_boundingBox)) { // hit a platform m_collisionPlatform = p; break; } } } }
@Override public void create() { dropImage = new Texture(Gdx.files.internal("data/generic_water.png")); bucketImage = new Texture(Gdx.files.internal("data/bucket.png")); camera = new OrthographicCamera(); camera.setToOrtho(false, 800, 480); batch = new SpriteBatch(); bucket = new Rectangle(); bucket.x = 800 / 2 - 48 / 2; // center the bucket horizontally bucket.y = 20; // bottom left corner of the bucket is 20 pixels above // the bottom screen edge bucket.width = 48; bucket.height = 48; raindrops = new Array<Rectangle>(); spawnRaindrop(); System.out.println(dropImage.getWidth() + " " + dropImage.getHeight()); }
/* * The create() method initializes our all of our resources and loads them into memory before the rest of the game is run * This method is the first called after the entry point(based on platform) and is only called once. * This is also a convenient place to put boiler plate code for the rest of the game that we only want ran once on startup. * * NOTE: Gdx.files.internal() defaults to the assets put into the Android project folder and takes String "filename.filetype" as a parameter. */ @Override public void create() { dropImage = new Texture(Gdx.files.internal("droplet.png")); bucketImage = new Texture(Gdx.files.internal("bucket.png")); dropSound = Gdx.audio.newSound(Gdx.files.internal("droplet.mp3")); rainMusic = Gdx.audio.newMusic(Gdx.files.internal("rain.mp3")); rainMusic.setLooping(true); // set rainMusic (rain.mp3) to play over and over again rainMusic.play(); // starts the rainMusic audio camera = new OrthographicCamera(); // initialize camera, part of opengl/opengl-es. /* * sets the camera to 2 dimensional plane. Seems redundant since we are using OrthographicCamera() object. * Note: investigate camera settings */ camera.setToOrtho(false, 800, 400); batch = new SpriteBatch(); // new sprite batch used in render() ie.Draws images. /* * set bucket properties. Use Rectangle object to represent bucket, track position and set size * used for collision detection with raindrop objects * */ bucket = new Rectangle(); bucket.x = 800 / 2 - 64 / 2; bucket.y = 20; bucket.width = 64; bucket.height = 64; /* * initializes Libgdx Array. * Note: Investigate how Libgdx Array is different than regular Java Array. */ raindrops = new Array<Rectangle>(); /* * creates new raindrops to be drawn for our game. This method is called here to * start the first raindrop. */ spawnRaindrop(); }
// This gets called once (Constructor) public GameScreen(Skyfall gameRef) { Gdx.app.log("MY TAG", "in gamescreen constructor"); this.game = gameRef; // These variables will adjust as gameTime passes dropSpeed = 200; pastGameTime = 0; respawnTime = 1000000000; // create a Rectangle to represent the paddle paddle = new Rectangle(); paddle.x = GameUtilities.CENTER_X - 70; // center the paddle horizontally paddle.y = 50; // bottom left corner of the paddle is 20px above the bottom of the screen edge paddle.width = 140; paddle.height = 160; // create the circles array and spawn the first circle circles = new ArrayList<Circle>(); spawnCircle(); }
/** * Obtains the actor at (x,y) with TOLERANCE. * * <p>Creates a square with size = TOLERANCE and checks: * * <p>1. if some vertex from the TOLERANCE square is inside an actor bbox 2. if some actor bbox * vertex is inside the TOLERANCE square */ public InteractiveActor getInteractiveActorAtWithTolerance(float x, float y, float tolerance) { List<SceneLayer> layers = getLayers(); tmpToleranceRect.x = x - tolerance / 2; tmpToleranceRect.y = y - tolerance / 2; tmpToleranceRect.width = tolerance; tmpToleranceRect.height = tolerance; for (SceneLayer layer : layers) { if (!layer.isVisible()) continue; // Obtain actors in reverse (close to camera) for (int l = layer.getActors().size() - 1; l >= 0; l--) { BaseActor a = layer.getActors().get(l); if (a instanceof InteractiveActor && ((InteractiveActor) a).hasInteraction()) { if (a.hit(x, y) || a.hit(tmpToleranceRect.x, tmpToleranceRect.y) || a.hit(tmpToleranceRect.x + tmpToleranceRect.width, tmpToleranceRect.y) || a.hit(tmpToleranceRect.x, tmpToleranceRect.y + tmpToleranceRect.height) || a.hit( tmpToleranceRect.x + tmpToleranceRect.width, tmpToleranceRect.y + tmpToleranceRect.height)) return (InteractiveActor) a; float[] verts = a.getBBox().getTransformedVertices(); for (int i = 0; i < verts.length; i += 2) { float vx = verts[i]; float vy = verts[i + 1]; if (tmpToleranceRect.contains(vx, vy)) return (InteractiveActor) a; } } } } return null; }
@Override public void create() { batch = new SpriteBatch(); camera = new OrthographicCamera(); camera.setToOrtho(false, 800, 480); // img = new Texture("badlogic.jpg"); dropImage = new Texture(Gdx.files.internal("droplet.png")); bucketImage = new Texture(Gdx.files.internal("bucket.png")); bucket = new Rectangle(); bucket.x = 800 / 2 - 64 / 2; bucket.y = 20; bucket.width = 64; bucket.width = 64; raindrops = new Array<Rectangle>(); spawnRaindrop(); dropSound = Gdx.audio.newSound(Gdx.files.internal("drop.wav")); rainMusic = Gdx.audio.newMusic(Gdx.files.internal("rain.mp3")); rainMusic.setLooping(true); rainMusic.play(); }
private void handleCollision(Rectangle col) { float temp = position.x; collisionArea.x = oldPosition.x; // just x if (collisionArea.overlaps(col) == false) { position.x = oldPosition.x; return; } collisionArea.x = temp; temp = position.y; collisionArea.y = oldPosition.y; // just y if (collisionArea.overlaps(col) == false) { position.y = oldPosition.y; return; } // both x and y collisionArea.x = oldPosition.y; position.x = oldPosition.x; position.y = oldPosition.y; if (collisionArea.overlaps(col)) { System.out.println("ERROR! collision"); } }
public Rectangle getCollisionArea() { collisionArea.x = position.x; collisionArea.y = position.y; return collisionArea; }
public void setY(float y) { loc.y = y; }
public void update(float delta) { position.add(speed * delta, 0); hitbox.x = position.x; hitbox.y = position.y; }
public void updateStartPosition(Platform platform) { Rectangle platformArea = platform.getArea(); area.x = platformArea.x + (platformArea.width - area.width) / 2; area.y = platformArea.y + platformArea.height; }
public void hitTopWall() { area.y -= (area.y + area.height) - GameBoard.HEIGHT; hitObjectUp(); }
private void updateAABB() { AABB.x = position.x + 14; AABB.y = position.y + 14; }
public void update(float deltaTime) { area.x = area.x + deltaTime * HORIZONTAL_SPEED * horizontalDirection.getMultiplier(); area.y = area.y + deltaTime * VERTICAL_SPEED * verticalDirection.getMultiplier(); }
public void updateBounds() { recBounds.x = fX - fWidth / 2; recBounds.y = fY - fHeight / 2; recBounds.width = fWidth; recBounds.height = fHeight; }
@Override public void draw(SpriteBatch batch, float parentAlpha) { if (widget == null) return; validate(); // Setup transform for this group. applyTransform(batch, computeTransform()); if (scrollX) hKnobBounds.x = hScrollBounds.x + (int) ((hScrollBounds.width - hKnobBounds.width) * getScrollPercentX()); if (scrollY) vKnobBounds.y = vScrollBounds.y + (int) ((vScrollBounds.height - vKnobBounds.height) * (1 - getScrollPercentY())); // Calculate the widget's position depending on the scroll state and available widget area. float y = widgetAreaBounds.y; if (!scrollY) y -= (int) maxY; else y -= (int) (maxY - visualAmountY); if (!fadeScrollBars && scrollbarsOnTop && scrollX) { float scrollbarHeight = 0; if (style.hScrollKnob != null) scrollbarHeight = style.hScrollKnob.getMinHeight(); if (style.hScroll != null) scrollbarHeight = Math.max(scrollbarHeight, style.hScroll.getMinHeight()); y += scrollbarHeight; } float x = widgetAreaBounds.x; if (scrollX) x -= (int) visualAmountX; widget.setPosition(x, y); if (widget instanceof Cullable) { widgetCullingArea.x = -widget.getX() + widgetAreaBounds.x; widgetCullingArea.y = -widget.getY() + widgetAreaBounds.y; widgetCullingArea.width = widgetAreaBounds.width; widgetCullingArea.height = widgetAreaBounds.height; ((Cullable) widget).setCullingArea(widgetCullingArea); } // Caculate the scissor bounds based on the batch transform, the available widget area and the // camera transform. We need to // project those to screen coordinates for OpenGL ES to consume. getStage().calculateScissors(widgetAreaBounds, scissorBounds); // Draw the background ninepatch. Color color = getColor(); batch.setColor(color.r, color.g, color.b, color.a * parentAlpha); if (style.background != null) style.background.draw(batch, 0, 0, getWidth(), getHeight()); batch.flush(); // Enable scissors for widget area and draw the widget. if (ScissorStack.pushScissors(scissorBounds)) { drawChildren(batch, parentAlpha); ScissorStack.popScissors(); } // Render scrollbars and knobs on top. batch.setColor( color.r, color.g, color.b, color.a * parentAlpha * Interpolation.fade.apply(fadeAlpha / fadeAlphaSeconds)); if (scrollX && scrollY) { if (style.corner != null) { style.corner.draw( batch, hScrollBounds.x + hScrollBounds.width, hScrollBounds.y, vScrollBounds.width, vScrollBounds.y); } } if (scrollX) { if (style.hScroll != null) style.hScroll.draw( batch, hScrollBounds.x, hScrollBounds.y, hScrollBounds.width, hScrollBounds.height); if (style.hScrollKnob != null) style.hScrollKnob.draw( batch, hKnobBounds.x, hKnobBounds.y, hKnobBounds.width, hKnobBounds.height); } if (scrollY) { if (style.vScroll != null) style.vScroll.draw( batch, vScrollBounds.x, vScrollBounds.y, vScrollBounds.width, vScrollBounds.height); if (style.vScrollKnob != null) style.vScrollKnob.draw( batch, vKnobBounds.x, vKnobBounds.y, vKnobBounds.width, vKnobBounds.height); } resetTransform(batch); }
public void layout() { final Drawable bg = style.background; final Drawable hScrollKnob = style.hScrollKnob; final Drawable vScrollKnob = style.vScrollKnob; float bgLeftWidth = 0, bgRightWidth = 0, bgTopHeight = 0, bgBottomHeight = 0; if (bg != null) { bgLeftWidth = bg.getLeftWidth(); bgRightWidth = bg.getRightWidth(); bgTopHeight = bg.getTopHeight(); bgBottomHeight = bg.getBottomHeight(); } float width = getWidth(); float height = getHeight(); float scrollbarHeight = 0; if (hScrollKnob != null) scrollbarHeight = hScrollKnob.getMinHeight(); if (style.hScroll != null) scrollbarHeight = Math.max(scrollbarHeight, style.hScroll.getMinHeight()); float scrollbarWidth = 0; if (vScrollKnob != null) scrollbarWidth = vScrollKnob.getMinWidth(); if (style.vScroll != null) scrollbarWidth = Math.max(scrollbarWidth, style.vScroll.getMinWidth()); // Get available space size by subtracting background's padded area. areaWidth = width - bgLeftWidth - bgRightWidth; areaHeight = height - bgTopHeight - bgBottomHeight; if (widget == null) return; // Get widget's desired width. float widgetWidth, widgetHeight; if (widget instanceof Layout) { Layout layout = (Layout) widget; widgetWidth = layout.getPrefWidth(); widgetHeight = layout.getPrefHeight(); } else { widgetWidth = widget.getWidth(); widgetHeight = widget.getHeight(); } // Determine if horizontal/vertical scrollbars are needed. scrollX = forceScrollX || (widgetWidth > areaWidth && !disableX); scrollY = forceScrollY || (widgetHeight > areaHeight && !disableY); boolean fade = fadeScrollBars; if (!fade) { // Check again, now taking into account the area that's taken up by any enabled scrollbars. if (scrollY) { areaWidth -= scrollbarWidth; if (!scrollX && widgetWidth > areaWidth && !disableX) { scrollX = true; } } if (scrollX) { areaHeight -= scrollbarHeight; if (!scrollY && widgetHeight > areaHeight && !disableY) { scrollY = true; areaWidth -= scrollbarWidth; } } } // Set the widget area bounds. widgetAreaBounds.set(bgLeftWidth, bgBottomHeight, areaWidth, areaHeight); if (fade) { // Make sure widget is drawn under fading scrollbars. if (scrollX) areaHeight -= scrollbarHeight; if (scrollY) areaWidth -= scrollbarWidth; } else { if (scrollbarsOnTop) { // Make sure widget is drawn under non-fading scrollbars. if (scrollX) widgetAreaBounds.height += scrollbarHeight; if (scrollY) widgetAreaBounds.width += scrollbarWidth; } else { // Offset widget area y for horizontal scrollbar. if (scrollX) { if (hScrollOnBottom) { widgetAreaBounds.y += scrollbarHeight; } else { widgetAreaBounds.y = 0; } } // Offset widget area x for vertical scrollbar. if (scrollY) { if (vScrollOnRight) { widgetAreaBounds.x = 0; } else { widgetAreaBounds.x += scrollbarWidth; } } } } // If the widget is smaller than the available space, make it take up the available space. widgetWidth = disableX ? width : Math.max(areaWidth, widgetWidth); widgetHeight = disableY ? height : Math.max(areaHeight, widgetHeight); maxX = widgetWidth - areaWidth; maxY = widgetHeight - areaHeight; if (fade) { // Make sure widget is drawn under fading scrollbars. if (scrollX) maxY -= scrollbarHeight; if (scrollY) maxX -= scrollbarWidth; } scrollX(MathUtils.clamp(amountX, 0, maxX)); scrollY(MathUtils.clamp(amountY, 0, maxY)); // Set the bounds and scroll knob sizes if scrollbars are needed. if (scrollX) { if (hScrollKnob != null) { float hScrollHeight = style.hScroll != null ? style.hScroll.getMinHeight() : hScrollKnob.getMinHeight(); // the small gap where the two scroll bars intersect might have to flip from right to left float boundsX, boundsY; if (vScrollOnRight) { boundsX = bgLeftWidth; } else { boundsX = bgLeftWidth + scrollbarWidth; } // bar on the top or bottom if (hScrollOnBottom) { boundsY = bgBottomHeight; } else { boundsY = height - bgTopHeight - hScrollHeight; } hScrollBounds.set(boundsX, boundsY, areaWidth, hScrollHeight); hKnobBounds.width = Math.max( hScrollKnob.getMinWidth(), (int) (hScrollBounds.width * areaWidth / widgetWidth)); hKnobBounds.height = hScrollKnob.getMinHeight(); hKnobBounds.x = hScrollBounds.x + (int) ((hScrollBounds.width - hKnobBounds.width) * getScrollPercentX()); hKnobBounds.y = hScrollBounds.y; } else { hScrollBounds.set(0, 0, 0, 0); hKnobBounds.set(0, 0, 0, 0); } } if (scrollY) { if (vScrollKnob != null) { float vScrollWidth = style.vScroll != null ? style.vScroll.getMinWidth() : vScrollKnob.getMinWidth(); // the small gap where the two scroll bars intersect might have to flip from bottom to top float boundsX, boundsY; if (hScrollOnBottom) { boundsY = height - bgTopHeight - areaHeight; } else { boundsY = bgBottomHeight; } // bar on the left or right if (vScrollOnRight) { boundsX = width - bgRightWidth - vScrollWidth; } else { boundsX = bgLeftWidth; } vScrollBounds.set(boundsX, boundsY, vScrollWidth, areaHeight); vKnobBounds.width = vScrollKnob.getMinWidth(); vKnobBounds.height = Math.max( vScrollKnob.getMinHeight(), (int) (vScrollBounds.height * areaHeight / widgetHeight)); if (vScrollOnRight) { vKnobBounds.x = width - bgRightWidth - vScrollKnob.getMinWidth(); } else { vKnobBounds.x = bgLeftWidth; } vKnobBounds.y = vScrollBounds.y + (int) ((vScrollBounds.height - vKnobBounds.height) * (1 - getScrollPercentY())); } else { vScrollBounds.set(0, 0, 0, 0); vKnobBounds.set(0, 0, 0, 0); } } widget.setSize(widgetWidth, widgetHeight); if (widget instanceof Layout) ((Layout) widget).validate(); }
public void setPosition(Vector2 position) { this.position = position; bounds.x = position.x; bounds.y = position.y; }
@Override public void render(float delta) { // clear the screen with a dark blue color. The // arguments to glClearColor are the red, green // blue and alpha component in the range [0,1] // of the color to be used to clear the screen. Gdx.gl.glClearColor(0, 0, 0.2f, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); // tell the camera to update its matrices. camera.update(); // tell the SpriteBatch to render in the // coordinate system specified by the camera. game.batch.setProjectionMatrix(camera.combined); // Implement pause/resume if (Gdx.input.isKeyJustPressed(Keys.ESCAPE)) { if (state == State.RUN) { setGameState(State.PAUSE); } else { setGameState(State.RUN); } } // begin a new batch and draw the bucket and // all drops game.batch.begin(); game.font.draw(game.batch, "Drops Collected: " + dropsGathered, 0, 480); game.batch.draw(bucketImage, bucket.x, bucket.y); for (Rectangle raindrop : raindrops) { game.batch.draw(dropImage, raindrop.x, raindrop.y); } game.batch.end(); // Implement pause/resume switch (state) { case RUN: // process user input if (Gdx.input.isTouched()) { Vector3 touchPos = new Vector3(); touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0); camera.unproject(touchPos); bucket.x = touchPos.x - 64 / 2; } if (Gdx.input.isKeyPressed(Keys.LEFT)) bucket.x -= 200 * Gdx.graphics.getDeltaTime(); if (Gdx.input.isKeyPressed(Keys.RIGHT)) bucket.x += 200 * Gdx.graphics.getDeltaTime(); // make sure the bucket stays within the screen bounds if (bucket.x < 0) bucket.x = 0; if (bucket.x > 800 - 64) bucket.x = 800 - 64; // check if we need to create a new raindrop if (TimeUtils.nanoTime() - lastDropTime > 1000000000) spawnRaindrop(); // move the raindrops, remove any that are beneath the bottom edge of // the screen or that hit the bucket. In the later case we increase the // value our drops counter and add a sound effect. Iterator<Rectangle> iter = raindrops.iterator(); while (iter.hasNext()) { Rectangle raindrop = iter.next(); raindrop.y -= 200 * Gdx.graphics.getDeltaTime(); if (raindrop.y + 64 < 0) iter.remove(); if (raindrop.overlaps(bucket)) { dropsGathered++; dropSound.play(); iter.remove(); } } break; case PAUSE: break; } // end switch }