gdx-studio

#libgdx#java#desktop

git clone https://git.pyrossh.dev/gdx-studio

An IDE for creating Games using libgdx and Java supported on all platforms Android, iOS, Desktop


test/source/Game3d.java
a9b2a64 1
package source;
a9b2a64 2
a62d533 3
import com.badlogic.gdx.scenes.scene2d.Actor;
7c84877 4
import com.badlogic.gdx.graphics.g3d.utils.MeshPartBuilder;
7c84877 5
import com.badlogic.gdx.graphics.g3d.utils.ModelBuilder;
7c84877 6
import com.badlogic.gdx.graphics.VertexAttributes.Usage;
7c84877 7
import com.badlogic.gdx.graphics.g3d.Material;
7c84877 8
import com.badlogic.gdx.graphics.GL20;
7c84877 9
import com.badlogic.gdx.graphics.g3d.attributes.TextureAttribute;
a9b2a64 10
import scene3d.Actor3d;
a9b2a64 11
import scene3d.Camera3d;
a9b2a64 12
import scene3d.actions.Actions3d;
a9b2a64 13
import com.badlogic.gdx.math.MathUtils;
a9b2a64 14
import com.badlogic.gdx.Input.Keys;
a9b2a64 15
import scene2d.*;
a62d533 16
a62d533 17
public class Game3d extends Scene {
a62d533 18
    Actor3d ship;
7c84877 19
    Actor3d skydome;
7c84877 20
    Actor3d knight;
7c84877 21
    Actor3d floor;
7c84877 22
    ModelBuilder builder;
a62d533 23
    
a62d533 24
    public Game3d(){
7c84877 25
       ship = Asset.loadModelObj("ship");//loads an obj model
a62d533 26
       ship.scale(3f);
7c84877 27
       skydome = Asset.loadModel("skydome"); //loads a g3db model
7c84877 28
       builder = new ModelBuilder();
7c84877 29
       builder.begin();
7c84877 30
       MeshPartBuilder part = builder.part("floor", GL20.GL_TRIANGLES, Usage.Position | 
7c84877 31
            Usage.TextureCoordinates | Usage.Normal, new Material());
7c84877 32
       for (float x = -200f; x < 200f; x += 10f) {
7c84877 33
           for (float z = -200f; z < 200f; z += 10f) {
7c84877 34
               part.rect(x, 0, z + 10f, x + 10f, 0, z + 10f, x + 10f, 0, z, x, 0, z, 0, 1, 0);
7c84877 35
           }
7c84877 36
       }
7c84877 37
       floor = new Actor3d(builder.end());
7c84877 38
       floor.materials.get(0).set(TextureAttribute.createDiffuse(Asset.tex("concrete").getTexture()));
a9b2a64 39
       knight = Asset.loadModel("knight");
a9b2a64 40
       knight.setPosition(-20f, 18f, 0f);
a9b2a64 41
       knight.setPitch(-90f);
a9b2a64 42
       addActor3d(floor);
a9b2a64 43
       addActor3d(skydome);
a9b2a64 44
       addActor3d(ship);
a9b2a64 45
       addActor3d(knight);
a9b2a64 46
       stage3d.getCamera().position.set(knight.getX()+ 13f, knight.getY() + 24f, knight.getZ() + 45f);
a9b2a64 47
       //Camera3d.followOffset(20f, 20f, -20f);
a9b2a64 48
      // Camera3d.followActor3d(knight, false);
a9b2a64 49
    }
a9b2a64 50
a9b2a64 51
    float angle, angle2;
a9b2a64 52
    @Override
a9b2a64 53
    public void act(float delta){
a9b2a64 54
        super.act(delta);
a9b2a64 55
        angle = MathUtils.cosDeg(knight.getYaw() - 90); //90 degrees is correction factor
a9b2a64 56
        angle2 = -MathUtils.sinDeg(knight.getYaw() - 90);
a9b2a64 57
        if (upKey) {
a9b2a64 58
            knight.addAction3d(Actions3d.moveBy(angle, 0f, angle2, 1f));
a9b2a64 59
            stage3d.getCamera().translate(angle, 0f, angle2);
a9b2a64 60
        } 
a9b2a64 61
        else if (downKey) {
a9b2a64 62
            knight.addAction3d(Actions3d.moveBy(-angle, 0f, -angle2, 1f));
a9b2a64 63
            stage3d.getCamera().translate(angle, 0f, angle2);
a9b2a64 64
        }
a9b2a64 65
        else if (rightKey) {
a9b2a64 66
            knight.rotateYaw(-2f);
a9b2a64 67
            if(stage3d.getCamera().direction.z > -0.76f)
a9b2a64 68
                Camera3d.rotateBy(-2f, 0f, 0f, 0f);
a9b2a64 69
        } 
a9b2a64 70
        else if (leftKey) {
a9b2a64 71
            knight.rotateYaw(2f);
a9b2a64 72
            if(stage3d.getCamera().direction.z > -0.76f)
a9b2a64 73
                Camera3d.rotateBy(-2f, 0f, 0f, 0f);
a9b2a64 74
        } 
a62d533 75
    }
a62d533 76
a62d533 77
    @Override
a62d533 78
    public void onClick(Actor actor){
a62d533 79
        if(actor.getName().equals("Button1"))
a62d533 80
           ship.addAction3d(Actions3d.moveBy(1f, 0f, 0f, 0.4f));
a62d533 81
    }
a62d533 82
a62d533 83
    @Override
a62d533 84
    public void onTouchDown(Actor actor){}
a62d533 85
a62d533 86
    @Override
a62d533 87
    public void onTouchUp(){}
a62d533 88
a62d533 89
     @Override
a62d533 90
    public void onDragged(){}
a62d533 91
a62d533 92
    @Override
a62d533 93
    public void onGesture(GestureType type){}
a62d533 94
7c84877 95
    @Override
7c84877 96
    public void onKeyTyped(char key){
7c84877 97
    }
7c84877 98
    
a9b2a64 99
    boolean rightKey, leftKey, upKey, downKey, spaceKey;
7c84877 100
    @Override
7c84877 101
    public void onKeyUp(int keycode){
a9b2a64 102
        if (keycode == Keys.LEFT) leftKey = false;
a9b2a64 103
        if (keycode == Keys.RIGHT) rightKey = false;
a9b2a64 104
        if (keycode == Keys.UP) upKey = false;
a9b2a64 105
        if (keycode == Keys.DOWN) downKey = false;
a9b2a64 106
        if (keycode == Keys.SPACE) spaceKey = false;
7c84877 107
    }
7c84877 108
    
7c84877 109
    @Override
7c84877 110
    public void onKeyDown(int keycode){
a9b2a64 111
        if (keycode == Keys.LEFT) leftKey = true;
a9b2a64 112
        if (keycode == Keys.RIGHT) rightKey = true;
a9b2a64 113
        if (keycode == Keys.UP) upKey = true;
a9b2a64 114
        if (keycode == Keys.DOWN) downKey = true;
a9b2a64 115
        if (keycode == Keys.SPACE) spaceKey = true;
7c84877 116
    }
7c84877 117
a62d533 118
    @Override
a62d533 119
    public void onPause(){
a62d533 120
    }
a62d533 121
    
a62d533 122
    @Override
a62d533 123
    public void onResume(){
a62d533 124
    }
a62d533 125
        
a62d533 126
    @Override
a62d533 127
    public void onDispose(){
a62d533 128
    }
a9b2a64 129
}