package com.fundynamic.d2tm.game.state;

import com.fundynamic.d2tm.Game;
import com.fundynamic.d2tm.game.Viewport;
import com.fundynamic.d2tm.game.drawing.DrawableViewPort;
import com.fundynamic.d2tm.game.event.QuitGameKeyListener;
import com.fundynamic.d2tm.game.map.Map;
import com.fundynamic.d2tm.game.math.Random;
import com.fundynamic.d2tm.game.math.Vector2D;
import com.fundynamic.d2tm.game.terrain.TerrainFactory;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;

import java.util.ArrayList;
import java.util.List;

public class PlayingState {

    private final TerrainFactory terrainFactory;

    private Map map;
    private Graphics graphics;

    private List<DrawableViewPort> drawableViewPorts = new ArrayList<>();

    private boolean initialized;


    public PlayingState(GameContainer gameContainer, TerrainFactory terrainFactory) throws SlickException {
        this.terrainFactory = terrainFactory;
        this.graphics = gameContainer.getGraphics();

        Input input = gameContainer.getInput();
        input.addKeyListener(new QuitGameKeyListener(gameContainer));

        // on map load...
        this.map = new Map(terrainFactory, 64, 64);
    }

    public void init() throws SlickException {
        // HACK HACK: we call init in render, and we only want to do it once so we check here. Ugly because now we check
        // every iteration if we should do this...
        if (!initialized) {
            this.map.init();
            initialized = true;

            try {
                Vector2D viewPortDrawingPosition = Vector2D.zero();
                final Viewport newViewport;
                newViewport = new Viewport(Game.SCREEN_WIDTH, Game.SCREEN_HEIGHT, this.map);
                drawableViewPorts.add(new DrawableViewPort(newViewport, viewPortDrawingPosition, Vector2D.zero(), graphics));
            } catch (SlickException e) {
                throw new IllegalStateException("Unable to create new viewport!");
            }
        }
    }

    public void update() {
        for (DrawableViewPort drawableViewPort : drawableViewPorts) {
            drawableViewPort.update();
        }
    }

    public void render() throws SlickException {
        for (DrawableViewPort drawableViewPort : drawableViewPorts) {
            drawableViewPort.render();
        }
        this.graphics.drawString("Drawing " + drawableViewPorts.size() + " viewports.", 10, 30);
    }

}

@@ -163,24 +163,93 @@
 leViewPort;%0A
+import com.fundynamic.d2tm.game.event.DrawableViewPortMoverListener;%0A
 import com.f
@@ -287,32 +287,32 @@
 ameKeyListener;%0A
-
 import com.fundy
@@ -925,16 +925,47 @@
 alized;%0A
+    private final Input input;%0A
 %0A%0A    pu
@@ -1174,22 +1174,16 @@
         
-Input 
 input = 
@@ -1904,33 +1904,33 @@
                 
-d
+D
 rawableViewPorts
@@ -1932,14 +1932,28 @@
 Port
-s.add(
+ drawableViewPort = 
 new 
@@ -2033,16 +2033,223 @@
 raphics)
+;%0A%0A                // Add listener for this viewport%0A                input.addMouseListener(new DrawableViewPortMoverListener(drawableViewPort, 5.0F));%0A%0A                drawableViewPorts.add(drawableViewPort
 );%0A     
@@ -2705,24 +2705,24 @@
 t.render();%0A
+
         %7D%0A  
@@ -2723,105 +2723,8 @@
   %7D%0A
-        this.graphics.drawString(%22Drawing %22 + drawableViewPorts.size() + %22 viewports.%22, 10, 30);%0A
     

add DrawableViewPort (mouse) listener to listeners so it actually interacts