-
Notifications
You must be signed in to change notification settings - Fork 0
Run First Scene
Radomiej edited this page Aug 15, 2016
·
12 revisions
- Before start you must setup in all platforms run scene. Example for Desktop it look this:
import org.javity.engine.ext.JavityApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
import com.badlogic.gdx.backends.lwjgl.LwjglGraphics;
import pl.radomiej.test.Scene0Bulider;
public class DesktopLauncher {
public static void main (String[] arg) {
LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
new LwjglApplication(new JavityApplication(new Scene0Bulider()), config);
}
}- Create SceneBulider in core project which allow you to create prefabs object and instantiate them on the scene:
package pl.radomiej.test;
import org.javity.components.Camera;
import org.javity.components.RectangleCollider;
import org.javity.components.Rigidbody;
import org.javity.components.SpineRenderer;
import org.javity.components.SpriteRenderer;
import org.javity.engine.CustomScene;
import org.javity.engine.JGameObject;
import org.javity.engine.JGameObjectImpl;
import org.javity.engine.Scene;
import org.javity.engine.SceneBulider;
import org.javity.engine.JSceneManager;
import com.badlogic.gdx.math.Vector2;
public class Scene0Bulider implements SceneBulider {
@Override
public Scene getScene() {
Scene scene = new CustomScene();
JGameObject ground = scene.instantiateGameObject(new Vector2(250, 0));
ground.addComponent(new SpriteRenderer("badlogic.jpg"));
ground.addComponent(new Rigidbody(true));
ground.addComponent(new RectangleCollider(1000000, 10));
ground.getTransform().setScale(new Vector2(1000, 0.1f));
;
JGameObject logo = scene.instantiateGameObject(new Vector2(100, 200));
logo.addComponent(new SpriteRenderer("resources/atlas/images.atlas#babel"));
logo.addComponent(new Rigidbody());
logo.addComponent(new RectangleCollider());
logo.addComponent(new ChangeScene());
JGameObject logo3 = scene.instantiateGameObject(new Vector2(50, 250));
logo3.addComponent(new SpineRenderer("resources/animations/skeleton.json#arrow"));
logo3.addComponent(new Rigidbody());
logo3.addComponent(new RectangleCollider());
logo3.addComponent(new TransformTest());
logo3.addComponent(new Camera());
JGameObject logo2 = scene.instantiateGameObject(new Vector2(250, 100));
logo2.addComponent(new SpriteRenderer("badlogic.jpg"));
logo2.addComponent(new Rigidbody(true));
logo2.addComponent(new RectangleCollider());
MyComponent myComponent = new MyComponent();
// myComponent.parent = logo;
logo2.addComponent(myComponent);
logo2.getTransform().setParent(logo3);
return scene;
}
}
This above code emulated manual scene creation via Unity Editor. You can use Prefab class to create custom new GameObject prefabs and add to them new components and set custom values to variable in them:
GameObject logo = Prefabs.createGameObject();
Prefabs.addComponent(logo, new SpriteRenderer("resources/atlas/images.atlas#babel"));
Prefabs.addComponent(logo, new Rigidbody());
Prefabs.addComponent(logo, new RectangleCollider());
Prefabs.addComponent(logo, new Camera());You must create scene object too, and put all GameObject which you need to one:
Scene scene = new CustomScene();
scene.instantiateGameObject(logo, new Vector2(100, 400));
scene.instantiateGameObject(logo2, new Vector2(250, 300));
scene.instantiateGameObject(logo3, new Vector2(50, 500));
scene.instantiateGameObject(ground, new Vector2(250, 0));Next parse scene to json string and return it(you don`t have must parse scene to json but it is recommended for validate good code structure in development mode):
String sceneJson = SceneManager.saveToJson(scene);
return sceneJson;Go to Basic Concepts for more details.