package cc.clv.BlueRobe.graphics;

import com.badlogic.gdx.graphics.g3d.ModelInstance;

import java.util.ArrayList;

import cc.clv.BlueRobe.engine.Ground;
import cc.clv.BlueRobe.engine.GroundBlock;
import cc.clv.BlueRobe.engine.GroundLine;
import rx.Observable;
import rx.functions.Action1;
import rx.functions.Func1;

/**
 * Created by slightair on 15/02/22.
 */
public class Groundlayouter {

    private final Ground ground;
    private final LineLayouter lineLayouter = new LineLayouter();
    private int firstLineIndex = 0;

    @lombok.Getter
    private final ArrayList<ModelInstance> blockInstances = new ArrayList<ModelInstance>();

    public Groundlayouter(Ground ground) {
        ground.getNewLines().subscribe(lineLayouter);
        this.ground = ground;
    }

    public void layoutGround() {
        blockInstances.clear();

        Observable.from(ground.getLines()).subscribe(lineLayouter);
    }

    class LineLayouter implements Action1<GroundLine> {

        class ModelInstanceCreator implements Func1<GroundBlock, GroundBlockModelInstance> {

            @Override
            public GroundBlockModelInstance call(GroundBlock groundBlock) {
                return GroundBlockModelInstance.create(groundBlock);
            }
        }

        class BlockLayouter implements Action1<GroundBlockModelInstance> {

            @Override
            public void call(GroundBlockModelInstance modelInstance) {
                GroundBlock groundBlock = modelInstance.getGroundBlock();

                int lineIndex = groundBlock.getLineIndex() - firstLineIndex;
                float z = (lineIndex - Ground.NUM_LINES / 2) * GroundBlockModel.SIZE;
                float y = -GroundBlockModel.HEIGHT / 2;
                float x = (groundBlock.getIndex() - GroundLine.NUM_BLOCKS / 2)
                        * GroundBlockModel.SIZE;

                modelInstance.transform.translate(x, y, z);
                blockInstances.add(modelInstance);
            }
        }

        @Override
        public void call(GroundLine groundLine) {
            Observable.from(groundLine.getBlocks())
                    .map(new ModelInstanceCreator())
                    .subscribe(new BlockLayouter());
        }
    }
}

@@ -98,21 +98,22 @@
 va.util.
-Array
+Linked
 List;%0A%0Ai
@@ -488,16 +488,122 @@
 uter();%0A
+    private final int capacity = GroundLine.NUM_BLOCKS * Ground.NUM_LINES;%0A    private int lineCount = 0;%0A
     priv
@@ -668,21 +668,22 @@
 e final 
-Array
+Linked
 List%3CMod
@@ -719,13 +719,14 @@
 new 
-Array
+Linked
 List
@@ -1710,16 +1710,17 @@
 oat z = 
+-
 (lineInd
@@ -2050,16 +2050,20 @@
 nces.add
+Last
 (modelIn
@@ -2075,100 +2075,347 @@
 e);%0A
-            %7D%0A        %7D%0A%0A        @Override%0A        public void call(GroundLine groundLine) %7B
+%0A                if (blockInstances.size() %3E capacity) %7B%0A                    blockInstances.removeFirst();%0A                %7D%0A            %7D%0A        %7D%0A%0A        @Override%0A        public void call(GroundLine groundLine) %7B%0A            lineCount++;%0A            if (lineCount %3E Ground.NUM_LINES) %7B%0A                firstLineIndex++;%0A            %7D%0A
 %0A   

Truncate block model instances