Android Open Source - SpaceRagdoll Controller






From Project

Back to project page SpaceRagdoll.

License

The source code is released under:

GNU General Public License

If you think the Android project SpaceRagdoll listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*******************************************************************************
 * Copyright (C) 2015 Tuukka Ruhanen//from ww w . java  2 s. c  o m
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *******************************************************************************/
package fi.karu.spaceragdoll.controller;

import java.util.HashMap;
import java.util.Map;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.math.Rectangle;

import fi.karu.spaceragdoll.Ragdoll;
import fi.karu.spaceragdoll.Space;

/**
 * This class changes the space's and the bird's
 * parameters based on the input events from the
 * GameScreen.
 */
public class Controller {

    private Space space;
    private Rectangle throttleButton = new Rectangle(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    private int throttlePointer = -1;
    private int swingPointer = -1;
    private int swingX;
    private int swingY;

    enum Keys {
        THROTTLE, SWING, RELEASE
    }

    static Map<Keys, Boolean> keys = new HashMap<Controller.Keys, Boolean>();
    static {
        keys.put(Keys.THROTTLE, false);
        keys.put(Keys.SWING, false);
        keys.put(Keys.RELEASE, false);
    };

    public Controller(Space space) {
        this.space = space;
    }

    // ** Key presses and touches **************** //

    public void touchDown(int x, int y, int pointer) {
        if (throttlePointer == -1 && space.isReleased()) { // && throttleButton.contains(x, y)
            throttlePointer = pointer;
            keys.put(Keys.THROTTLE, true);
        } else if (swingPointer == -1 && !space.isReleased()) {
            swingPointer = pointer;
            keys.put(Keys.SWING, true);
        }
    }

    public void touchUp(int pointer) {
        if (pointer == throttlePointer) {
            throttlePointer = -1;
            keys.put(Keys.THROTTLE, false);
        } else if (pointer == swingPointer) {
            swingPointer = -1;
            keys.put(Keys.SWING, false);
            keys.put(Keys.RELEASE, true);
        }
    }

    public void touchDragged(int x, int y, int pointer) {
        if (pointer == swingPointer) {
            swingX = x;
            swingY = y;
        }
    }

    /** The main update method **/
    public void update() {
        processInput();
    }

    /** Change Bird's state and parameters based on input controls **/
    private void processInput() {
        if (keys.get(Keys.RELEASE)) {
            space.releaseBird();
            keys.put(Keys.RELEASE, false);
        } else if (keys.get(Keys.SWING)) {
            space.setBirdSwing(swingX, swingY);
        } else if (space.isReleased()) {
            if (keys.get(Keys.THROTTLE)) {
                space.setBirdState(Ragdoll.State.ON);
            } else {
                space.setBirdState(Ragdoll.State.OFF);
            }
        }
    }
}




Java Source Code List

fi.karu.spacebird_android.SpaceRagdollActivity.java
fi.karu.spaceragdoll.GameScreen.java
fi.karu.spaceragdoll.Planet.java
fi.karu.spaceragdoll.Ragdoll.java
fi.karu.spaceragdoll.SpaceRagdoll.java
fi.karu.spaceragdoll.SpaceRenderer.java
fi.karu.spaceragdoll.Space.java
fi.karu.spaceragdoll.controller.Controller.java
fi.karu.spaceragdoll_desktop.SpaceRagdollDesktop.java