package com.mygdx.game;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.mygdx.handlers.GameStateManager;
import com.mygdx.handlers.MyInput;
import com.mygdx.handlers.MyInputProcessor;
import com.mygdx.handlers.NetworkManager;
import com.mygdx.net.NetworkInterface;

import java.util.HashMap;

public class MyGame extends ApplicationAdapter
{
    public static final int V_WIDTH = 320 * 2;
    public static final int V_HEIGHT = 240 * 2;
    public static final int SCALE = 2;

    private SpriteBatch spriteBatch;
    private OrthographicCamera camera;
    private OrthographicCamera hudCamera;
    private GameStateManager gameStateManager;

    private NetworkManager networkManager;
    private Thread networkThread;

    public MyGame(HashMap<NetworkManager.ConnectionMode, NetworkInterface> networkImpls)
    {
        networkManager = new NetworkManager(networkImpls);
    }

    @Override
    public void create()
    {
        Gdx.input.setInputProcessor(new MyInputProcessor());
        spriteBatch = new SpriteBatch();
        gameStateManager = new GameStateManager(this, networkManager);

        // This must be done in MyGame.create()! putting this in render() will lock up the game.
        networkThread = new Thread(networkManager);
        networkThread.start();
        boolean success = networkManager.initialize(true,
                                                    NetworkManager.ConnectionMode.WIFI_LAN,
                                                    null);

        if (!success)
        {
            System.out.println("NET: Initialize failed.");
        }
    }

    @Override
    public void render()
    {
        gameStateManager.update(Gdx.graphics.getDeltaTime());
        gameStateManager.render();
        MyInput.update();

        /*
        try
        {
            Thread.sleep(1000);
        }
        catch(InterruptedException e)
        {

        }*/
    }

    public SpriteBatch getSpriteBatch()
    {
        return spriteBatch;
    }

    public OrthographicCamera getCamera()
    {
        return camera;
    }

    public OrthographicCamera getHudCamera()
    {
        return hudCamera;
    }
}

@@ -194,16 +194,57 @@
 eBatch;%0A
+import com.badlogic.gdx.utils.TimeUtils;%0A
 import c
@@ -817,16 +817,88 @@
 nager;%0A%0A
+    private double accumulator;%0A    private float step = 1.0f / 60.0f;%0A%0A
     priv
@@ -1890,24 +1890,25 @@
 )%0A    %7B%0A
+%0A
         
 gameStat
@@ -1903,59 +1903,286 @@
     
-gameStateManager.update(Gdx.graphics.getDeltaTime()
+// This makes the game run at/close to 60 frams a sec.        side note (1/60 = 0.0166666)%0A        accumulator += Gdx.graphics.getDeltaTime();%0A        while (accumulator %3E step)%0A        %7B%0A        accumulator -= step;%0A        %7D%0A%0A        gameStateManager.update((float)accumulator
 );%0A 

fix time steps added