Java Code Examples for com.badlogic.gdx.math.Rectangle#overlaps()

The following examples show how to use com.badlogic.gdx.math.Rectangle#overlaps() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CheckCollision.java    From buffer_bci with GNU General Public License v3.0 6 votes vote down vote up
public boolean thisAndAll (Rectangle testRect) {
	numFood = food.getNum();
	for (int i = 0; i < numFood; i++) {
		foodRect = foodList.get(i);
		if (foodRect.overlaps(testRect)) return true;
	}

	tmpX = (int)testRect.x;
	tmpY = (int)testRect.y;
	if (wallCollidesWith(tmpX, tmpY)) return true;

	bodyLength = worm.getBodyLength();
	for (int i = 0; i < bodyLength; i++) {
		bodySegment = worm.getBodySegment(i);
		bodyRect.x = bodySegment.x;
		bodyRect.y = bodySegment.y;
		bodyRect.width = Level.SIZE;
		bodyRect.height = Level.SIZE;
		if (testRect.overlaps(bodyRect)) return true;
	}
	return false;
}
 
Example 2
Source File: DeckBuilderScreen.java    From Cardshifter with Apache License 2.0 5 votes vote down vote up
public boolean checkCardDrop(CardViewSmall cardView) {
	Table table = (Table)cardView.getActor();
	Vector2 stageLoc = table.localToStageCoordinates(new Vector2());
	Rectangle tableRect = new Rectangle(stageLoc.x, stageLoc.y, table.getWidth(), table.getHeight());
	
	Vector2 stageLocCardList = this.cardsInDeckList.localToStageCoordinates(new Vector2(this.cardsInDeckList.getX(), this.cardsInDeckList.getY()));
	Vector2 modifiedSLCL = new Vector2(stageLocCardList.x, stageLocCardList.y - this.screenHeight/2);
	Rectangle deckRect = new Rectangle(modifiedSLCL.x, modifiedSLCL.y, this.cardsInDeckList.getWidth(), this.screenHeight);
	
	if (tableRect.overlaps(deckRect)) {
		this.addEntity(cardView);
		return true;
	}
	
	return false;
	
	//these can be used to double check the location of the rectangles
	/*
	Image squareImage = new Image(new Texture(Gdx.files.internal("cardbg.png")));
	squareImage.setPosition(modifiedSLCL.x, modifiedSLCL.y);
	squareImage.setSize(deckRect.width, deckRect.height);
	this.game.stage.addActor(squareImage);
	*/
	/*
	Image squareImage = new Image(new Texture(Gdx.files.internal("cardbg.png")));
	squareImage.setPosition(stageLoc.x, stageLoc.y);
	squareImage.setSize(tableRect.width, tableRect.height);
	this.game.stage.addActor(squareImage);
	*/
}
 
Example 3
Source File: GigaGal.java    From ud406 with MIT License 4 votes vote down vote up
public void update(float delta, Array<Platform> platforms) {

        lastFramePosition.set(position);
        velocity.y -= Constants.GRAVITY;
        position.mulAdd(velocity, delta);

        if (position.y < Constants.KILL_PLANE) {
            init();
        }

        // Land on/fall off platforms
        if (jumpState != Enums.JumpState.JUMPING) {
            if (jumpState != JumpState.RECOILING) {
                jumpState = Enums.JumpState.FALLING;
            }

            for (Platform platform : platforms) {
                if (landedOnPlatform(platform)) {
                    jumpState = Enums.JumpState.GROUNDED;
                    velocity.y = 0;
                    velocity.x = 0;
                    position.y = platform.top + Constants.GIGAGAL_EYE_HEIGHT;
                }
            }
        }

        // Collide with enemies

        Rectangle gigaGalBounds = new Rectangle(
                position.x - Constants.GIGAGAL_STANCE_WIDTH / 2,
                position.y - Constants.GIGAGAL_EYE_HEIGHT,
                Constants.GIGAGAL_STANCE_WIDTH,
                Constants.GIGAGAL_HEIGHT);

        for (Enemy enemy : level.getEnemies()) {
            Rectangle enemyBounds = new Rectangle(
                    enemy.position.x - Constants.ENEMY_COLLISION_RADIUS,
                    enemy.position.y - Constants.ENEMY_COLLISION_RADIUS,
                    2 * Constants.ENEMY_COLLISION_RADIUS,
                    2 * Constants.ENEMY_COLLISION_RADIUS
            );
            if (gigaGalBounds.overlaps(enemyBounds)) {

                if (position.x < enemy.position.x) {
                    recoilFromEnemy(Direction.LEFT);
                } else {
                    recoilFromEnemy(Direction.RIGHT);
                }
            }
        }

        // Move left/right
        if (jumpState != JumpState.RECOILING) {
            if (Gdx.input.isKeyPressed(Keys.LEFT)) {
                moveLeft(delta);
            } else if (Gdx.input.isKeyPressed(Keys.RIGHT)) {
                moveRight(delta);
            } else {
                walkState = Enums.WalkState.NOT_WALKING;
            }
        }

        // Jump
        if (Gdx.input.isKeyPressed(Keys.Z)) {
            switch (jumpState) {
                case GROUNDED:
                    startJump();
                    break;
                case JUMPING:
                    continueJump();
            }
        } else {
            endJump();
        }


        // TODO: Check to see if shoot key has been pressed
        // You can make it whatever you want, but I've been using the 'x' key
        // You'll want to use Gdx.input.isKeyJustPressed()


            // TODO: Create a Vector2 to hold the position of a new bullet


            // TODO: Set the bullet's position in the case where GigaGal is facing right
            // bullet position =  GigaGal's position + cannon offset
            // TODO: Set the bullet's position in the case where GigaGal is facing left
            // In this case we need to negate the x component of the cannon offset

            // TODO: Ask the Level to spawn the bullet


    }
 
Example 4
Source File: GigaGal.java    From ud406 with MIT License 4 votes vote down vote up
public void update(float delta, Array<Platform> platforms) {

        lastFramePosition.set(position);
        velocity.y -= Constants.GRAVITY;
        position.mulAdd(velocity, delta);

        if (position.y < Constants.KILL_PLANE) {
            init();
        }

        // Land on/fall off platforms
        if (jumpState != Enums.JumpState.JUMPING) {
            // TODO: If GigaGal is not RECOILING, set FALLING jump state
            if (jumpState != JumpState.RECOILING) {
                jumpState = Enums.JumpState.FALLING;
            }

            for (Platform platform : platforms) {
                if (landedOnPlatform(platform)) {
                    jumpState = Enums.JumpState.GROUNDED;
                    velocity.y = 0;
                    // TODO: Zero horizontal velocity
                    velocity.x = 0;
                    position.y = platform.top + Constants.GIGAGAL_EYE_HEIGHT;
                }
            }
        }

        // Collide with enemies

        Rectangle gigaGalBounds = new Rectangle(
                position.x - Constants.GIGAGAL_STANCE_WIDTH / 2,
                position.y - Constants.GIGAGAL_EYE_HEIGHT,
                Constants.GIGAGAL_STANCE_WIDTH,
                Constants.GIGAGAL_HEIGHT);

        for (Enemy enemy : level.getEnemies()) {
            Rectangle enemyBounds = new Rectangle(
                    enemy.position.x - Constants.ENEMY_COLLISION_RADIUS,
                    enemy.position.y - Constants.ENEMY_COLLISION_RADIUS,
                    2 * Constants.ENEMY_COLLISION_RADIUS,
                    2 * Constants.ENEMY_COLLISION_RADIUS
            );
            if (gigaGalBounds.overlaps(enemyBounds)) {

                if (position.x < enemy.position.x) {
                    recoilFromEnemy(Direction.LEFT);
                } else {
                    recoilFromEnemy(Direction.RIGHT);
                }
            }
        }

        // Move left/right
        // TODO: Disable left/right movement if RECOILING
        if (jumpState != JumpState.RECOILING) {
            if (Gdx.input.isKeyPressed(Keys.LEFT)) {
                moveLeft(delta);
            } else if (Gdx.input.isKeyPressed(Keys.RIGHT)) {
                moveRight(delta);
            } else {
                walkState = Enums.WalkState.NOT_WALKING;
            }
        }

        // Jump
        if (Gdx.input.isKeyPressed(Keys.Z)) {
            switch (jumpState) {
                case GROUNDED:
                    startJump();
                    break;
                case JUMPING:
                    continueJump();
            }
        } else {
            endJump();
        }


    }
 
Example 5
Source File: GigaGal.java    From ud406 with MIT License 4 votes vote down vote up
public void update(float delta, Array<Platform> platforms) {

        lastFramePosition.set(position);
        velocity.y -= Constants.GRAVITY;
        position.mulAdd(velocity, delta);

        if (position.y < Constants.KILL_PLANE) {
            init();
        }

        // Land on/fall off platforms
        if (jumpState != Enums.JumpState.JUMPING) {

            jumpState = Enums.JumpState.FALLING;


            for (Platform platform : platforms) {
                if (landedOnPlatform(platform)) {
                    jumpState = Enums.JumpState.GROUNDED;
                    velocity.y = 0;

                    position.y = platform.top + Constants.GIGAGAL_EYE_HEIGHT;
                }
            }
        }

        // Collide with enemies

        Rectangle gigaGalBounds = new Rectangle(
                position.x - Constants.GIGAGAL_STANCE_WIDTH / 2,
                position.y - Constants.GIGAGAL_EYE_HEIGHT,
                Constants.GIGAGAL_STANCE_WIDTH,
                Constants.GIGAGAL_HEIGHT);

        for (Enemy enemy : level.getEnemies()) {
            Rectangle enemyBounds = new Rectangle(
                    enemy.position.x - Constants.ENEMY_COLLISION_RADIUS,
                    enemy.position.y - Constants.ENEMY_COLLISION_RADIUS,
                    2 * Constants.ENEMY_COLLISION_RADIUS,
                    2 * Constants.ENEMY_COLLISION_RADIUS
            );
            if (gigaGalBounds.overlaps(enemyBounds)) {

                if (position.x < enemy.position.x) {
                    recoilFromEnemy(Direction.LEFT);
                } else {
                    recoilFromEnemy(Direction.RIGHT);
                }
            }
        }

        // Move left/right

        if (Gdx.input.isKeyPressed(Keys.LEFT)) {
            moveLeft(delta);
        } else if (Gdx.input.isKeyPressed(Keys.RIGHT)) {
            moveRight(delta);
        } else {
            walkState = Enums.WalkState.NOT_WALKING;
        }


        // Jump
        if (Gdx.input.isKeyPressed(Keys.Z)) {
            switch (jumpState) {
                case GROUNDED:
                    startJump();
                    break;
                case JUMPING:
                    continueJump();
            }
        } else {
            endJump();
        }


    }
 
Example 6
Source File: GigaGal.java    From ud406 with MIT License 4 votes vote down vote up
public void update(float delta, Array<Platform> platforms) {

        lastFramePosition.set(position);
        velocity.y -= Constants.GRAVITY;
        position.mulAdd(velocity, delta);

        if (position.y < Constants.KILL_PLANE) {
            init();
        }

        // Land on/fall off platforms
        if (jumpState != Enums.JumpState.JUMPING) {
            // TODO: If GigaGal is not RECOILING, set FALLING jump state

            jumpState = Enums.JumpState.FALLING;


            for (Platform platform : platforms) {
                if (landedOnPlatform(platform)) {
                    jumpState = Enums.JumpState.GROUNDED;
                    velocity.y = 0;
                    // TODO: Zero horizontal velocity

                    position.y = platform.top + Constants.GIGAGAL_EYE_HEIGHT;
                }
            }
        }

        // Collide with enemies

        Rectangle gigaGalBounds = new Rectangle(
                position.x - Constants.GIGAGAL_STANCE_WIDTH / 2,
                position.y - Constants.GIGAGAL_EYE_HEIGHT,
                Constants.GIGAGAL_STANCE_WIDTH,
                Constants.GIGAGAL_HEIGHT);

        for (Enemy enemy : level.getEnemies()) {
            Rectangle enemyBounds = new Rectangle(
                    enemy.position.x - Constants.ENEMY_COLLISION_RADIUS,
                    enemy.position.y - Constants.ENEMY_COLLISION_RADIUS,
                    2 * Constants.ENEMY_COLLISION_RADIUS,
                    2 * Constants.ENEMY_COLLISION_RADIUS
            );
            if (gigaGalBounds.overlaps(enemyBounds)) {

                if (position.x < enemy.position.x) {
                    recoilFromEnemy(Direction.LEFT);
                } else {
                    recoilFromEnemy(Direction.RIGHT);
                }
            }
        }

        // Move left/right
        // TODO: Disable left/right movement if RECOILING

        if (Gdx.input.isKeyPressed(Keys.LEFT)) {
            moveLeft(delta);
        } else if (Gdx.input.isKeyPressed(Keys.RIGHT)) {
            moveRight(delta);
        } else {
            walkState = Enums.WalkState.NOT_WALKING;
        }


        // Jump
        if (Gdx.input.isKeyPressed(Keys.Z)) {
            switch (jumpState) {
                case GROUNDED:
                    startJump();
                    break;
                case JUMPING:
                    continueJump();
            }
        } else {
            endJump();
        }


    }
 
Example 7
Source File: GigaGal.java    From ud406 with MIT License 4 votes vote down vote up
public void update(float delta, Array<Platform> platforms) {

        lastFramePosition.set(position);
        velocity.y -= Constants.GRAVITY;
        position.mulAdd(velocity, delta);

        if (position.y < Constants.KILL_PLANE) {
            init();
        }

        // Land on/fall off platforms
        if (jumpState != Enums.JumpState.JUMPING) {
            if (jumpState != JumpState.RECOILING) {
                jumpState = Enums.JumpState.FALLING;
            }

            for (Platform platform : platforms) {
                if (landedOnPlatform(platform)) {
                    jumpState = Enums.JumpState.GROUNDED;
                    velocity.y = 0;
                    velocity.x = 0;
                    position.y = platform.top + Constants.GIGAGAL_EYE_HEIGHT;
                }
            }
        }

        // Collide with enemies

        Rectangle gigaGalBounds = new Rectangle(
                position.x - Constants.GIGAGAL_STANCE_WIDTH / 2,
                position.y - Constants.GIGAGAL_EYE_HEIGHT,
                Constants.GIGAGAL_STANCE_WIDTH,
                Constants.GIGAGAL_HEIGHT);

        for (Enemy enemy : level.getEnemies()) {
            Rectangle enemyBounds = new Rectangle(
                    enemy.position.x - Constants.ENEMY_COLLISION_RADIUS,
                    enemy.position.y - Constants.ENEMY_COLLISION_RADIUS,
                    2 * Constants.ENEMY_COLLISION_RADIUS,
                    2 * Constants.ENEMY_COLLISION_RADIUS
            );
            if (gigaGalBounds.overlaps(enemyBounds)) {

                if (position.x < enemy.position.x) {
                    recoilFromEnemy(Direction.LEFT);
                } else {
                    recoilFromEnemy(Direction.RIGHT);
                }
            }
        }

        // Move left/right
        if (jumpState != JumpState.RECOILING) {
            if (Gdx.input.isKeyPressed(Keys.LEFT)) {
                moveLeft(delta);
            } else if (Gdx.input.isKeyPressed(Keys.RIGHT)) {
                moveRight(delta);
            } else {
                walkState = Enums.WalkState.NOT_WALKING;
            }
        }

        // Jump
        if (Gdx.input.isKeyPressed(Keys.Z)) {
            switch (jumpState) {
                case GROUNDED:
                    startJump();
                    break;
                case JUMPING:
                    continueJump();
            }
        } else {
            endJump();
        }



    }
 
Example 8
Source File: GigaGal.java    From ud406 with MIT License 4 votes vote down vote up
public void update(float delta, Array<Platform> platforms) {

        lastFramePosition.set(position);
        velocity.y -= Constants.GRAVITY;
        position.mulAdd(velocity, delta);

        if (position.y < Constants.KILL_PLANE) {
            init();
        }

        // Land on/fall off platforms
        if (jumpState != Enums.JumpState.JUMPING) {

            jumpState = Enums.JumpState.FALLING;


            for (Platform platform : platforms) {
                if (landedOnPlatform(platform)) {
                    jumpState = Enums.JumpState.GROUNDED;
                    velocity.y = 0;

                    position.y = platform.top + Constants.GIGAGAL_EYE_HEIGHT;
                }
            }
        }

        // Collide with enemies

        // TODO: Define GigaGal bounding rectangle
        // Use GigaGal's constants for height and stance width

        Rectangle gigaGalBounds = new Rectangle(
                position.x - Constants.GIGAGAL_STANCE_WIDTH / 2,
                position.y - Constants.GIGAGAL_EYE_HEIGHT,
                Constants.GIGAGAL_STANCE_WIDTH,
                Constants.GIGAGAL_HEIGHT);

        for (Enemy enemy : level.getEnemies()) {
            // TODO: Define enemy bounding rectangle
            // You'll want to define an enemy collision radius constant
            Rectangle enemyBounds = new Rectangle(
                    enemy.position.x - Constants.ENEMY_COLLISION_RADIUS,
                    enemy.position.y - Constants.ENEMY_COLLISION_RADIUS,
                    2 * Constants.ENEMY_COLLISION_RADIUS,
                    2 * Constants.ENEMY_COLLISION_RADIUS
            );

            // TODO: If GigaGal overlaps an enemy, log the direction from which she hit it
            // Use Rectangle.overlaps() to make it easy!

            if (gigaGalBounds.overlaps(enemyBounds)) {
                if (position.x < enemy.position.x) {
                    Gdx.app.log(TAG, "Hit an enemy from the left");
                } else {
                    Gdx.app.log(TAG, "Hit an enemy from the right");
                }
            }
        }

        // Move left/right

        if (Gdx.input.isKeyPressed(Keys.LEFT)) {
            moveLeft(delta);
        } else if (Gdx.input.isKeyPressed(Keys.RIGHT)) {
            moveRight(delta);
        } else {
            walkState = Enums.WalkState.NOT_WALKING;
        }


        // Jump
        if (Gdx.input.isKeyPressed(Keys.Z)) {
            switch (jumpState) {
                case GROUNDED:
                    startJump();
                    break;
                case JUMPING:
                    continueJump();
            }
        } else {
            endJump();
        }


    }
 
Example 9
Source File: GigaGal.java    From ud406 with MIT License 4 votes vote down vote up
public void update(float delta, Array<Platform> platforms) {

        lastFramePosition.set(position);
        velocity.y -= Constants.GRAVITY;
        position.mulAdd(velocity, delta);

        if (position.y < Constants.KILL_PLANE) {
            init();
        }

        // Land on/fall off platforms
        if (jumpState != Enums.JumpState.JUMPING) {
            if (jumpState != JumpState.RECOILING) {
                jumpState = Enums.JumpState.FALLING;
            }

            for (Platform platform : platforms) {
                if (landedOnPlatform(platform)) {
                    jumpState = Enums.JumpState.GROUNDED;
                    velocity.y = 0;
                    velocity.x = 0;
                    position.y = platform.top + Constants.GIGAGAL_EYE_HEIGHT;
                }
            }
        }

        // Collide with enemies

        Rectangle gigaGalBounds = new Rectangle(
                position.x - Constants.GIGAGAL_STANCE_WIDTH / 2,
                position.y - Constants.GIGAGAL_EYE_HEIGHT,
                Constants.GIGAGAL_STANCE_WIDTH,
                Constants.GIGAGAL_HEIGHT);

        for (Enemy enemy : level.getEnemies()) {
            Rectangle enemyBounds = new Rectangle(
                    enemy.position.x - Constants.ENEMY_COLLISION_RADIUS,
                    enemy.position.y - Constants.ENEMY_COLLISION_RADIUS,
                    2 * Constants.ENEMY_COLLISION_RADIUS,
                    2 * Constants.ENEMY_COLLISION_RADIUS
            );
            if (gigaGalBounds.overlaps(enemyBounds)) {

                if (position.x < enemy.position.x) {
                    recoilFromEnemy(Direction.LEFT);
                } else {
                    recoilFromEnemy(Direction.RIGHT);
                }
            }
        }

        // Move left/right
        if (jumpState != JumpState.RECOILING) {
            if (Gdx.input.isKeyPressed(Keys.LEFT)) {
                moveLeft(delta);
            } else if (Gdx.input.isKeyPressed(Keys.RIGHT)) {
                moveRight(delta);
            } else {
                walkState = Enums.WalkState.NOT_WALKING;
            }
        }

        // Jump
        if (Gdx.input.isKeyPressed(Keys.Z)) {
            switch (jumpState) {
                case GROUNDED:
                    startJump();
                    break;
                case JUMPING:
                    continueJump();
            }
        } else {
            endJump();
        }



    }
 
Example 10
Source File: GigaGal.java    From ud406 with MIT License 4 votes vote down vote up
public void update(float delta, Array<Platform> platforms) {

        lastFramePosition.set(position);
        velocity.y -= Constants.GRAVITY;
        position.mulAdd(velocity, delta);

        if (position.y < Constants.KILL_PLANE) {
            init();
        }

        // Land on/fall off platforms
        if (jumpState != Enums.JumpState.JUMPING) {
            if (jumpState != JumpState.RECOILING) {
                jumpState = Enums.JumpState.FALLING;
            }

            for (Platform platform : platforms) {
                if (landedOnPlatform(platform)) {
                    jumpState = Enums.JumpState.GROUNDED;
                    velocity.y = 0;
                    velocity.x = 0;
                    position.y = platform.top + Constants.GIGAGAL_EYE_HEIGHT;
                }
            }
        }

        // Collide with enemies

        Rectangle gigaGalBounds = new Rectangle(
                position.x - Constants.GIGAGAL_STANCE_WIDTH / 2,
                position.y - Constants.GIGAGAL_EYE_HEIGHT,
                Constants.GIGAGAL_STANCE_WIDTH,
                Constants.GIGAGAL_HEIGHT);

        for (Enemy enemy : level.getEnemies()) {
            Rectangle enemyBounds = new Rectangle(
                    enemy.position.x - Constants.ENEMY_COLLISION_RADIUS,
                    enemy.position.y - Constants.ENEMY_COLLISION_RADIUS,
                    2 * Constants.ENEMY_COLLISION_RADIUS,
                    2 * Constants.ENEMY_COLLISION_RADIUS
            );
            if (gigaGalBounds.overlaps(enemyBounds)) {

                if (position.x < enemy.position.x) {
                    recoilFromEnemy(Direction.LEFT);
                } else {
                    recoilFromEnemy(Direction.RIGHT);
                }
            }
        }

        // Move left/right
        if (jumpState != JumpState.RECOILING) {
            if (Gdx.input.isKeyPressed(Keys.LEFT)) {
                moveLeft(delta);
            } else if (Gdx.input.isKeyPressed(Keys.RIGHT)) {
                moveRight(delta);
            } else {
                walkState = Enums.WalkState.NOT_WALKING;
            }
        }

        // Jump
        if (Gdx.input.isKeyPressed(Keys.Z)) {
            switch (jumpState) {
                case GROUNDED:
                    startJump();
                    break;
                case JUMPING:
                    continueJump();
            }
        } else {
            endJump();
        }



    }
 
Example 11
Source File: GigaGal.java    From ud406 with MIT License 4 votes vote down vote up
public void update(float delta, Array<Platform> platforms) {

        lastFramePosition.set(position);
        velocity.y -= Constants.GRAVITY;
        position.mulAdd(velocity, delta);

        if (position.y < Constants.KILL_PLANE) {
            init();
        }

        // Land on/fall off platforms
        if (jumpState != Enums.JumpState.JUMPING) {

            jumpState = Enums.JumpState.FALLING;


            for (Platform platform : platforms) {
                if (landedOnPlatform(platform)) {
                    jumpState = Enums.JumpState.GROUNDED;
                    velocity.y = 0;

                    position.y = platform.top + Constants.GIGAGAL_EYE_HEIGHT;
                }
            }
        }

        // Collide with enemies

        Rectangle gigaGalBounds = new Rectangle(
                position.x - Constants.GIGAGAL_STANCE_WIDTH / 2,
                position.y - Constants.GIGAGAL_EYE_HEIGHT,
                Constants.GIGAGAL_STANCE_WIDTH,
                Constants.GIGAGAL_HEIGHT);

        for (Enemy enemy : level.getEnemies()) {
            Rectangle enemyBounds = new Rectangle(
                    enemy.position.x - Constants.ENEMY_COLLISION_RADIUS,
                    enemy.position.y - Constants.ENEMY_COLLISION_RADIUS,
                    2 * Constants.ENEMY_COLLISION_RADIUS,
                    2 * Constants.ENEMY_COLLISION_RADIUS
            );
            if (gigaGalBounds.overlaps(enemyBounds)) {

                if (position.x < enemy.position.x) {
                    recoilFromEnemy(Direction.LEFT);
                } else {
                    recoilFromEnemy(Direction.RIGHT);
                }
            }
        }

        // Move left/right

        if (Gdx.input.isKeyPressed(Keys.LEFT)) {
            moveLeft(delta);
        } else if (Gdx.input.isKeyPressed(Keys.RIGHT)) {
            moveRight(delta);
        } else {
            walkState = Enums.WalkState.NOT_WALKING;
        }


        // Jump
        if (Gdx.input.isKeyPressed(Keys.Z)) {
            switch (jumpState) {
                case GROUNDED:
                    startJump();
                    break;
                case JUMPING:
                    continueJump();
            }
        } else {
            endJump();
        }


    }
 
Example 12
Source File: GigaGal.java    From ud406 with MIT License 4 votes vote down vote up
public void update(float delta, Array<Platform> platforms) {

        lastFramePosition.set(position);
        velocity.y -= Constants.GRAVITY;
        position.mulAdd(velocity, delta);

        if (position.y < Constants.KILL_PLANE) {
            init();
        }

        // Land on/fall off platforms
        if (jumpState != Enums.JumpState.JUMPING) {
            if (jumpState != JumpState.RECOILING) {
                jumpState = Enums.JumpState.FALLING;
            }

            for (Platform platform : platforms) {
                if (landedOnPlatform(platform)) {
                    jumpState = Enums.JumpState.GROUNDED;
                    velocity.y = 0;
                    velocity.x = 0;
                    position.y = platform.top + Constants.GIGAGAL_EYE_HEIGHT;
                }
            }
        }

        // Collide with enemies

        Rectangle gigaGalBounds = new Rectangle(
                position.x - Constants.GIGAGAL_STANCE_WIDTH / 2,
                position.y - Constants.GIGAGAL_EYE_HEIGHT,
                Constants.GIGAGAL_STANCE_WIDTH,
                Constants.GIGAGAL_HEIGHT);

        for (Enemy enemy : level.getEnemies()) {
            Rectangle enemyBounds = new Rectangle(
                    enemy.position.x - Constants.ENEMY_COLLISION_RADIUS,
                    enemy.position.y - Constants.ENEMY_COLLISION_RADIUS,
                    2 * Constants.ENEMY_COLLISION_RADIUS,
                    2 * Constants.ENEMY_COLLISION_RADIUS
            );
            if (gigaGalBounds.overlaps(enemyBounds)) {

                if (position.x < enemy.position.x) {
                    recoilFromEnemy(Direction.LEFT);
                } else {
                    recoilFromEnemy(Direction.RIGHT);
                }
            }
        }

        // Move left/right
        if (jumpState != JumpState.RECOILING) {
            if (Gdx.input.isKeyPressed(Keys.LEFT)) {
                moveLeft(delta);
            } else if (Gdx.input.isKeyPressed(Keys.RIGHT)) {
                moveRight(delta);
            } else {
                walkState = Enums.WalkState.NOT_WALKING;
            }
        }

        // Jump
        if (Gdx.input.isKeyPressed(Keys.Z)) {
            switch (jumpState) {
                case GROUNDED:
                    startJump();
                    break;
                case JUMPING:
                    continueJump();
            }
        } else {
            endJump();
        }



    }
 
Example 13
Source File: GameScreen.java    From Cardshifter with Apache License 2.0 4 votes vote down vote up
public boolean checkCardDrop(CardViewSmall cardView) {
	Table table = (Table)cardView.getActor();
	Vector2 stageLoc = table.localToStageCoordinates(new Vector2());
	Rectangle tableRect = new Rectangle(stageLoc.x, stageLoc.y, table.getWidth(), table.getHeight());

	for (Container<Actor> actor : this.holders.values()) {
		if (actor.getName() == "Battlefield") {
			Vector2 stageBattlefieldLoc = actor.localToStageCoordinates(new Vector2(actor.getActor().getX(), actor.getActor().getY()));
			Vector2 modifiedSBL = new Vector2(stageBattlefieldLoc.x - actor.getWidth()/2, stageBattlefieldLoc.y - actor.getHeight()/2);
			Rectangle deckRect = new Rectangle(modifiedSBL.x, modifiedSBL.y, actor.getWidth() * 0.8f, actor.getHeight());
			
			//uncomment this to see the bug where battlefields pop up in strange places
			/*
			Image squareImage = new Image(new Texture(Gdx.files.internal("cardbg.png")));
			squareImage.setPosition(modifiedSBL.x, modifiedSBL.y);
			squareImage.setSize(deckRect.width, deckRect.height);
			this.game.stage.addActor(squareImage);
			*/
			
			if (tableRect.overlaps(deckRect)) {
				//this.addEntity(cardView);
				System.out.println("target found!");
				return true;
			}
		}
	}
	
	return false;
	
	//these can be used to double check the location of the rectangles
	/*
	Image squareImage = new Image(new Texture(Gdx.files.internal("cardbg.png")));
	squareImage.setPosition(modifiedSBL.x, modifiedSBL.y);
	squareImage.setSize(deckRect.width, deckRect.height);
	this.game.stage.addActor(squareImage);
	*/
	/*
	Image squareImage = new Image(new Texture(Gdx.files.internal("cardbg.png")));
	squareImage.setPosition(stageLoc.x, stageLoc.y);
	squareImage.setSize(tableRect.width, tableRect.height);
	this.game.stage.addActor(squareImage);
	*/
}