Android Open Source - Ready-Set-Rogue Prefab Factory






From Project

Back to project page Ready-Set-Rogue.

License

The source code is released under:

GNU General Public License

If you think the Android project Ready-Set-Rogue 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

package com.warsheep.scamp;
//www. ja  v  a2  s .c om
import com.badlogic.ashley.core.Component;
import com.badlogic.ashley.core.Entity;
import com.badlogic.gdx.utils.Json;
import com.badlogic.gdx.utils.JsonValue;
import com.rits.cloning.Cloner;
import com.warsheep.scamp.components.*;

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

public class PrefabFactory {
    private AssetDepot assets = AssetDepot.getInstance();
    private Json json = new Json();
    private Map<String, ArrayList<Component>> prefabs = new HashMap<>();
    private Cloner cloner = new Cloner();
    private CollidableComponent collidable = new CollidableComponent();

    public Entity buildEntity(String handle) {
        if (!this.prefabs.containsKey(handle)) {
            this.loadPrefab(handle);
        }

        Entity e = new Entity();

        for (Component c : this.prefabs.get(handle)) {
            e.add(initializeComponent(c));
        }

        return e;
    }

    private void loadPrefab(String handle) {
        JsonValue data = assets.fetchJson(handle);
        String com = data.get("components").toString();
        ArrayList<Component> c = json.fromJson(ArrayList.class, com);
        prefabs.put(handle, c);
    }

    private Component initializeComponent(Component c) {
        switch (CLASS.valueOf(c.getClass().getSimpleName())) {
            case AIControllableComponent:
                return cloner.deepClone(c);
            case AttackerComponent:
                return cloner.shallowClone(c);
            case CameraComponent:
                return new CameraComponent();
            case CollidableComponent:
                return collidable;
            case ControllableComponent:
                return cloner.shallowClone(c);
            case DamageableComponent:
                return cloner.shallowClone(c);
            case DropComponent:
                return cloner.shallowClone(c);
            case FactionComponent:
                return cloner.shallowClone(c);
            case InventoryComponent:
                return cloner.deepClone(c);
            case LevelComponent:
                return cloner.shallowClone(c);
            case MovementComponent:
                return cloner.shallowClone(c);
            case StateComponent:
                return new StateComponent();
            case TileComponent:
                return cloner.deepClone(c);
            case TransformComponent:
                return cloner.deepClone(c);
            case VisibleComponent:
                return cloner.shallowClone(c);
            default:
                return cloner.shallowClone(c);
        }
    }

    private enum CLASS {
        AIControllableComponent,
        AttackerComponent,
        CameraComponent,
        CollidableComponent,
        ControllableComponent,
        DamageableComponent,
        DropComponent,
        FactionComponent,
        InventoryComponent,
        LevelComponent,
        MovementComponent,
        StateComponent,
        TileComponent,
        TransformComponent,
        VisibleComponent
    }
}




Java Source Code List

com.warsheep.scamp.AssetDepot.java
com.warsheep.scamp.IOSLauncher.java
com.warsheep.scamp.MapImporter.java
com.warsheep.scamp.PrefabFactory.java
com.warsheep.scamp.Scamp.java
com.warsheep.scamp.adt.BSPRectangle.java
com.warsheep.scamp.adt.Container.java
com.warsheep.scamp.adt.Pair.java
com.warsheep.scamp.adt.Room.java
com.warsheep.scamp.algorithms.BSPMapGenerator.java
com.warsheep.scamp.algorithms.Compositor.java
com.warsheep.scamp.android.AndroidLauncher.java
com.warsheep.scamp.client.HtmlLauncher.java
com.warsheep.scamp.components.AIControllableComponent.java
com.warsheep.scamp.components.AttackerComponent.java
com.warsheep.scamp.components.CameraComponent.java
com.warsheep.scamp.components.CollidableComponent.java
com.warsheep.scamp.components.ControllableComponent.java
com.warsheep.scamp.components.DamageableComponent.java
com.warsheep.scamp.components.DropComponent.java
com.warsheep.scamp.components.ECSMapper.java
com.warsheep.scamp.components.FactionComponent.java
com.warsheep.scamp.components.InventoryComponent.java
com.warsheep.scamp.components.LevelComponent.java
com.warsheep.scamp.components.MovementComponent.java
com.warsheep.scamp.components.StateComponent.java
com.warsheep.scamp.components.TileComponent.java
com.warsheep.scamp.components.TransformComponent.java
com.warsheep.scamp.components.VisibleComponent.java
com.warsheep.scamp.desktop.AssetPacker.java
com.warsheep.scamp.desktop.DesktopLauncher.java
com.warsheep.scamp.processors.AIProcessor.java
com.warsheep.scamp.processors.CameraProcessor.java
com.warsheep.scamp.processors.CollisionProcessor.java
com.warsheep.scamp.processors.CombatProcessor.java
com.warsheep.scamp.processors.ControlProcessor.java
com.warsheep.scamp.processors.DeathProcessor.java
com.warsheep.scamp.processors.LevelingProcessor.java
com.warsheep.scamp.processors.MovementProcessor.java
com.warsheep.scamp.processors.StateProcessor.java
com.warsheep.scamp.processors.TileProcessor.java
com.warsheep.scamp.processors.VisibilityProcessor.java
com.warsheep.scamp.screens.MainGameScreen.java
com.warsheep.scamp.screens.MainMenuScreen.java