Android Open Source - Roll-Ball-Roll Player






From Project

Back to project page Roll-Ball-Roll.

License

The source code is released under:

GNU General Public License

If you think the Android project Roll-Ball-Roll 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) 2014 Lewis Chun//  w w w .j a va  2  s  .c o  m
 * Android game in which the user controls a ball to get a high score.
 *
 * This file is part of Roll Ball Roll.
 *
 * Roll Ball Roll 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 com.zwac035.finalprogram;

import com.jme3.bullet.PhysicsSpace;
import com.jme3.bullet.collision.PhysicsCollisionObject;
import com.jme3.bullet.collision.shapes.SphereCollisionShape;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.shape.Sphere;

/**
 *
 * @author Lewis Chun
 */
public class Player {
    private static Player instance = null;
    private RigidBodyControl playerControl;
    private Geometry playerGeom;
    private int collectedTriangles;
    private String skinName;
    
    private Player(){ 
    }
    
    public static Player getInstance(){
        if(instance == null){
            instance = new Player();
        }
        return instance;
    }
    
    public void loadPlayer(Node root, PhysicsSpace physics){
        // Set up the ball controlled by the user
        collectedTriangles = 0;
        // As this is a singleton class, nothing gets deallocated until the
        // process is destroyed, so we only have to load everything the first
        // time the game is initialised.
        if(playerGeom == null){
            Sphere playerSphere = new Sphere(20, 20, 1);
            playerGeom = new Geometry("Sphere", playerSphere);
            playerGeom.setName("ball");
            playerGeom.setLocalTranslation(0, 0, 1);
            playerControl = new RigidBodyControl(new SphereCollisionShape(1), 1f);
            // Set the sphere to collide with the boxesNode and the invisible planes
            playerControl.setCollideWithGroups(PhysicsCollisionObject.COLLISION_GROUP_03 | 
                                                PhysicsCollisionObject.COLLISION_GROUP_02 | 
                                                PhysicsCollisionObject.COLLISION_GROUP_01);
            playerGeom.addControl(playerControl);
        } else {
            setEnableMovement(true);
        }
        
        setMaterial();
        
        root.attachChild(playerGeom);
        physics.add(playerGeom);
        
    }
    
    public RigidBodyControl getPhysicsControl(){
        return playerControl;
    }
    
    public Geometry getGeometry(){
        return playerGeom;
    }
    
    public void moveTo(float x, float y, float z){
        playerControl.setEnabled(false);
        playerGeom.setLocalTranslation(x, y, z);
        playerControl.setEnabled(true);
    }
    
    public void setEnableMovement(boolean move){
        playerControl.setAngularVelocity(Vector3f.ZERO);
        playerControl.setLinearVelocity(Vector3f.ZERO);
        playerControl.clearForces();
        playerControl.setEnabled(move);
    }

    public Vector3f getLocation(){
        return playerGeom.getLocalTranslation();
    }
    
    public void increaseCollectedTriangles(int value){
        collectedTriangles += value;
    }
    
    public int getCollectedTriangles(){
        return collectedTriangles;
    }
    
    public void setSkin(String s){
        skinName = s;
    }
    
    private void setMaterial(){
        Material sphereMat = new Material(Res.assets, "Common/MatDefs/Light/Lighting.j3md");
        sphereMat.setBoolean("UseMaterialColors",true); 
        if(skinName.equals("Red")) {
            sphereMat.setColor("Diffuse", ColorRGBA.Red);
            sphereMat.setColor("Ambient", ColorRGBA.Red.mult(ColorRGBA.Black).mult(0.5f));
            sphereMat.setColor("Specular", ColorRGBA.White);
        } else if(skinName.equals("Blue")) {
            sphereMat.setColor("Diffuse", ColorRGBA.Blue);
            sphereMat.setColor("Ambient", ColorRGBA.Blue.mult(ColorRGBA.Black).mult(0.5f));
            sphereMat.setColor("Specular", ColorRGBA.White);
        } else if(skinName.equals("Green")) {
            sphereMat.setColor("Diffuse", ColorRGBA.Green);
            sphereMat.setColor("Ambient", ColorRGBA.Green.mult(ColorRGBA.Black).mult(0.5f));
            sphereMat.setColor("Specular", ColorRGBA.White);
        } else if(skinName.equals("Basket")) {
            sphereMat.setTexture("DiffuseMap", Res.basketBallTexture);
            sphereMat.setColor("Diffuse", ColorRGBA.White);
            sphereMat.setColor("Ambient", ColorRGBA.White);
            sphereMat.setColor("Specular", ColorRGBA.White);
        } else if(skinName.equals("Beach")) {
            sphereMat.setTexture("DiffuseMap", Res.beachBallTexture);
            sphereMat.setColor("Diffuse", ColorRGBA.White);
            sphereMat.setColor("Ambient", ColorRGBA.White);
            sphereMat.setColor("Specular", ColorRGBA.White);
        }
        playerGeom.setMaterial(sphereMat);
    }
    
}




Java Source Code List

com.zwac035.finalprogram.Database.java
com.zwac035.finalprogram.EnterNameActivity.java
com.zwac035.finalprogram.FloorType.java
com.zwac035.finalprogram.ForceChangeListener.java
com.zwac035.finalprogram.GameCamera.java
com.zwac035.finalprogram.GamePauseState.java
com.zwac035.finalprogram.GameRunningState.java
com.zwac035.finalprogram.HighScoreScreenActivity.java
com.zwac035.finalprogram.InfoScreenActivity.java
com.zwac035.finalprogram.Level.java
com.zwac035.finalprogram.MainActivity.java
com.zwac035.finalprogram.Main.java
com.zwac035.finalprogram.Player.java
com.zwac035.finalprogram.Res.java
com.zwac035.finalprogram.ShopActivity.java
com.zwac035.finalprogram.StartScreenActivity.java
com.zwac035.finalprogram.TutorialState.java
com.zwac035.finalprogram.box.BoxControl.java
com.zwac035.finalprogram.box.BoxType.java
com.zwac035.finalprogram.box.ExplodingBox.java
com.zwac035.finalprogram.box.HeavyBox.java
com.zwac035.finalprogram.box.MagnetBox.java
com.zwac035.finalprogram.box.NormalBoxControl.java
com.zwac035.finalprogram.box.Triangle.java
com.zwac035.finalprogram.managers.BoxManager.java
com.zwac035.finalprogram.managers.BoxPool.java
com.zwac035.finalprogram.managers.FloorManager.java
com.zwac035.finalprogram.managers.FloorPool.java
com.zwac035.finalprogram.managers.LevelChangeManager.java
com.zwac035.finalprogram.managers.LevelChangeObservable.java
com.zwac035.finalprogram.managers.LevelChangeObserver.java
com.zwac035.finalprogram.managers.LevelManager.java
com.zwac035.finalprogram.managers.ManagerFacade.java
com.zwac035.finalprogram.managers.Manager.java
com.zwac035.finalprogram.managers.TriangleManager.java
com.zwac035.finalprogram.managers.TrianglePool.java