package gui.swing;

import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.Point2D;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import utils.Key;

import logic.WorldObject;
import logic.GameState;

public class SwingGameState extends GameState implements IDraw, KeyListener
{
    Map<Integer, IDraw> _drawableEntities;
    
    private static SwingGameState _instance;
    public static SwingGameState GetInstance()
    {
        if (_instance == null)
            _instance = new SwingGameState();
        return _instance;
    }

    private SwingGameState()
    {
        _drawableEntities = new HashMap<Integer, IDraw>();
        _entities = new HashMap<Integer, WorldObject>();
        
        _objectBuilder = new SwingWorldObjectBuilder();
        _objectBuilder.SetGameState(this);
        
        _objectBuilder.CreatePlayer("test", new Point2D.Double(56, 60));

        for (int x = 0; x < 50; ++x)
        {
            for (int y = 0; y < 50; ++y)
            {
                if (x == 0 || x == 47 || y == 0 || y == 47)
                    _objectBuilder.CreateWall(-1, new Point2D.Double(x * SwingWall.SIZE  + SwingWall.SIZE / 2, y * SwingWall.SIZE  + SwingWall.SIZE / 2));
                else if (x % 2 == 0 && y % 2 == 0)
                    _objectBuilder.CreateWall(1, new Point2D.Double(x * SwingWall.SIZE  + SwingWall.SIZE / 2, y * SwingWall.SIZE  + SwingWall.SIZE / 2));
            }
        }
    }
    
    @Override
    public void AddEntity(WorldObject entity)
    {
        _drawableEntities.put(entity.Guid, (IDraw)entity);
        _entities.put(entity.Guid, entity);
    }

    @Override
    public void Draw(Graphics2D g)
    {
        List<WorldObject> objs = _quadTree.QueryRange(new Rectangle(0, 0, 800, 600));
        
        for (WorldObject wo : objs)
            _drawableEntities.get(wo.Guid).Draw(g);
    }

    @Override
    public void keyTyped(KeyEvent e)
    {
    }

    @Override
    public void keyPressed(KeyEvent e)
    {
        Key k = Key.ToEnum(e.getKeyCode());
        if (k == null)
            return;
        
        _pressedKeys.put(k, true);
    }

    @Override
    public void keyReleased(KeyEvent e)
    {
        Key k = Key.ToEnum(e.getKeyCode());
        if (k == null)
            return;
        
        _pressedKeys.put(k, false);
    }
}

@@ -235,16 +235,41 @@
 til.Map;
+%0Aimport java.util.Random;
 %0A%0Aimport
@@ -1168,16 +1168,46 @@
  y == 47
+ %7C%7C (x %25 2 == 0 && y %25 2 == 0)
 )%0A      
@@ -1371,26 +1371,597 @@
     
+%7D%0A
     
-else if 
+    %7D%0A        %0A        class Pair%0A        %7B%0A            int X;%0A            int Y;%0A        %7D%0A        %0A        Map%3CInteger, Pair%3E map = new HashMap%3CInteger, Pair%3E();%0A        %0A        Random r = new Random();%0A        %0A        while (map.size() != 500)%0A        %7B%0A            int x = r.nextInt(50);%0A            int y = r.nextInt(50);%0A            %0A            int hash = (int)(1.0/2.0 * (x + y) * (x + y + 1) + y);%0A            if (map.containsKey(hash))%0A                continue;%0A            %0A            if (x == 0 %7C%7C y == 0)%0A                continue;%0A            %0A            if (
 (x %25 2
+)
  == 
@@ -1961,29 +1961,31 @@
  2) == 0 && 
+(
 y %25 2
+)
  == 0)%0A     
@@ -1982,32 +1982,360 @@
 = 0)%0A           
+     
+continue;%0A            %0A            if ((x == 1 && y == 1) %7C%7C ((x == 1) && (y == 2)) %7C%7C ((x == 2) && (y == 1)))%0A                continue;%0A            %0A            Pair p = new Pair();%0A            p.X = x;%0A            p.Y = y;%0A            map.put(hash, p);%0A        %7D%0A        %0A        for (Pair p : map.values())%0A        %7B%0A   
          _object
@@ -2367,33 +2367,35 @@
  Point2D.Double(
-x
+p.X
  * SwingWall.SIZ
@@ -2411,33 +2411,35 @@
 gWall.SIZE / 2, 
-y
+p.Y
  * SwingWall.SIZ
@@ -2461,38 +2461,24 @@
 SIZE / 2));%0A
-            %7D%0A
         %7D%0A  

Add basic map generation