<commit_before>/*
 * Copyright 2018 MovingBlocks
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.destinationsol.game.screens;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import org.destinationsol.Const;
import org.destinationsol.GameOptions;
import org.destinationsol.SolApplication;
import org.destinationsol.assets.Assets;
import org.destinationsol.common.SolColor;
import org.destinationsol.common.SolMath;
import org.destinationsol.game.DebugOptions;
import org.destinationsol.game.FactionManager;
import org.destinationsol.game.HardnessCalc;
import org.destinationsol.game.Hero;
import org.destinationsol.game.SolCam;
import org.destinationsol.game.SolGame;
import org.destinationsol.game.SolObject;
import org.destinationsol.game.context.Context;
import org.destinationsol.game.item.Gun;
import org.destinationsol.game.item.ItemManager;
import org.destinationsol.game.item.Shield;
import org.destinationsol.game.item.SolItem;
import org.destinationsol.game.planet.Planet;
import org.destinationsol.game.ship.ShipAbility;
import org.destinationsol.game.ship.SolShip;
import org.destinationsol.ui.DisplayDimensions;
import org.destinationsol.ui.FontSize;
import org.destinationsol.ui.SolInputManager;
import org.destinationsol.ui.SolUiBaseScreen;
import org.destinationsol.ui.SolUiControl;
import org.destinationsol.ui.SolUiScreen;
import org.destinationsol.ui.UiDrawer;
import org.destinationsol.ui.nui.screens.ConsoleScreen;
import org.destinationsol.ui.nui.screens.UIShipControlsScreen;
import org.terasology.gestalt.assets.ResourceUrn;
import org.terasology.nui.asset.UIElement;

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

public class MainGameScreen extends SolUiBaseScreen {
    // TODO: Rename!
    private static final float ICON_SZ = .03f;
    private static final float BAR_SZ = ICON_SZ * 5;
    private static final int MAX_ICON_COUNT = 3;
    static final float CELL_SZ = .17f;
    private static final float H_PAD = .005f;
    private static final float V_PAD = H_PAD;
    static final float HELPER_ROW_1 = 1 - 3f * CELL_SZ;
    private static final float HELPER_ROW_2 = HELPER_ROW_1 - .5f * CELL_SZ;
    private static final float HELPER_ROW_3 = HELPER_ROW_2 - .5f * CELL_SZ;

    private final ShipUiControl shipControl;
    private final SolUiControl freeCamControl;
    private final SolUiControl pauseControl;
    private final CameraKeyboardControl cameraControl;

    private final ZoneNameAnnouncer zoneNameAnnouncer;
    private final BorderDrawer borderDrawer;

    private final TextureAtlas.AtlasRegion lifeTexture;
    private final TextureAtlas.AtlasRegion infinityTexture;
    private final TextureAtlas.AtlasRegion waitTexture;
    private final TextureAtlas.AtlasRegion compassTexture;

    private final Color myCompassTint;
    private final TextPlace myLifeTp;
    private final TextPlace myRepairsExcessTp;
    private final TextPlace myShieldLifeTp;
    private final TextPlace myG1AmmoTp;
    private final TextPlace myG1AmmoExcessTp;
    private final TextPlace myG2AmmoTp;
    private final TextPlace myG2AmmoExcessTp;
    private final TextPlace myChargesExcessTp;
    private final TextPlace myMoneyExcessTp;
    private final SolApplication solApplication;
    private final Context context;

    private List<SolUiScreen> gameOverlayScreens = new ArrayList<>();
    private List<WarnDrawer> warnDrawers = new ArrayList<>();

    MainGameScreen(RightPaneLayout rightPaneLayout, Context context) {
        DisplayDimensions displayDimensions = SolApplication.displayDimensions;

        this.context = context;
        solApplication = context.get(SolApplication.class);
        GameOptions gameOptions = solApplication.getOptions();

        switch (gameOptions.controlType) {
            case KEYBOARD:
                UIShipControlsScreen shipControlsScreen =
                        (UIShipControlsScreen) solApplication.getNuiManager().createScreen("engine:uiShipControlsScreen");
                solApplication.getNuiManager().pushScreen(shipControlsScreen);
                shipControl = shipControlsScreen;
                break;
            case MOUSE:
                shipControl = new ShipMouseControl();
                break;
            case CONTROLLER:
                shipControl = new ShipControllerControl(solApplication);
                break;
            case MIXED:
            default:
                shipControl = new ShipMixedControl(solApplication, controls);
                break;
        }

        boolean mobile = solApplication.isMobile();
        float lastCol = displayDimensions.getRatio() - MainGameScreen.CELL_SZ;
        // No button, since on mobile, it should be ideally controlled straightly by dragging.
        freeCamControl = new SolUiControl(null, false, gameOptions.getKeyFreeCameraMovement());
        controls.add(freeCamControl);
        pauseControl = new SolUiControl(null, true, gameOptions.getKeyPause());
        controls.add(pauseControl);
        cameraControl = new CameraKeyboardControl(gameOptions, controls);

        // possible warning messages in order of importance, so earlier one will be drawn on the center
        warnDrawers.add(new SunWarnDrawer());
        warnDrawers.add(new DmgWarnDrawer());
        warnDrawers.add(new CollisionWarnDrawer());
        warnDrawers.add(new NoShieldWarn());
        warnDrawers.add(new NoArmorWarn());
        warnDrawers.add(new EnemyWarn());

        zoneNameAnnouncer = new ZoneNameAnnouncer();
        borderDrawer = new BorderDrawer();

        lifeTexture = Assets.getAtlasRegion("engine:iconLife");
        infinityTexture = Assets.getAtlasRegion("engine:iconInfinity");
        waitTexture = Assets.getAtlasRegion("engine:iconWait");

        compassTexture = Assets.getAtlasRegion("engine:uiCompass");
        myCompassTint = SolColor.col(1, 0);

        myLifeTp = new TextPlace(SolColor.W50);
        myRepairsExcessTp = new TextPlace(SolColor.WHITE);
        myShieldLifeTp = new TextPlace(SolColor.W50);
        myG1AmmoTp = new TextPlace(SolColor.W50);
        myG1AmmoExcessTp = new TextPlace(SolColor.WHITE);
        myG2AmmoTp = new TextPlace(SolColor.W50);
        myG2AmmoExcessTp = new TextPlace(SolColor.WHITE);
        myChargesExcessTp = new TextPlace(SolColor.WHITE);
        myMoneyExcessTp = new TextPlace(SolColor.WHITE);
    }

    public static Rectangle btn(float x, float y, boolean halfHeight) {
        float gap = .01f;
        float cellH = CELL_SZ;
        if (halfHeight) {
            cellH /= 2;
        }
        return new Rectangle(x + gap, y + gap, CELL_SZ - gap * 2, cellH - gap * 2);
    }

    private void maybeDrawHeight(UiDrawer drawer) {
        SolGame game = solApplication.getGame();
        Planet np = game.getPlanetManager().getNearestPlanet();
        SolCam cam = context.get(SolCam.class);
        Vector2 camPos = cam.getPosition();
        if (np != null && np.getPosition().dst(camPos) < np.getFullHeight()) {
            drawHeight(drawer, np, camPos, cam.getAngle());
        }
    }

    private void drawHeight(UiDrawer drawer, Planet np, Vector2 camPos, float camAngle) {
        float toPlanet = camPos.dst(np.getPosition());
        toPlanet -= np.getGroundHeight();<commit_msg>fix: fix LoadingScreen not being removed<commit_after>/*
 * Copyright 2018 MovingBlocks
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.destinationsol.game.screens;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import org.destinationsol.Const;
import org.destinationsol.GameOptions;
import org.destinationsol.SolApplication;
import org.destinationsol.assets.Assets;
import org.destinationsol.common.SolColor;
import org.destinationsol.common.SolMath;
import org.destinationsol.game.DebugOptions;
import org.destinationsol.game.FactionManager;
import org.destinationsol.game.HardnessCalc;
import org.destinationsol.game.Hero;
import org.destinationsol.game.SolCam;
import org.destinationsol.game.SolGame;
import org.destinationsol.game.SolObject;
import org.destinationsol.game.context.Context;
import org.destinationsol.game.item.Gun;
import org.destinationsol.game.item.ItemManager;
import org.destinationsol.game.item.Shield;
import org.destinationsol.game.item.SolItem;
import org.destinationsol.game.planet.Planet;
import org.destinationsol.game.ship.ShipAbility;
import org.destinationsol.game.ship.SolShip;
import org.destinationsol.ui.DisplayDimensions;
import org.destinationsol.ui.FontSize;
import org.destinationsol.ui.SolInputManager;
import org.destinationsol.ui.SolUiBaseScreen;
import org.destinationsol.ui.SolUiControl;
import org.destinationsol.ui.SolUiScreen;
import org.destinationsol.ui.UiDrawer;
import org.destinationsol.ui.nui.screens.ConsoleScreen;
import org.destinationsol.ui.nui.screens.UIShipControlsScreen;
import org.terasology.gestalt.assets.ResourceUrn;
import org.terasology.nui.asset.UIElement;

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

public class MainGameScreen extends SolUiBaseScreen {
    // TODO: Rename!
    private static final float ICON_SZ = .03f;
    private static final float BAR_SZ = ICON_SZ * 5;
    private static final int MAX_ICON_COUNT = 3;
    static final float CELL_SZ = .17f;
    private static final float H_PAD = .005f;
    private static final float V_PAD = H_PAD;
    static final float HELPER_ROW_1 = 1 - 3f * CELL_SZ;
    private static final float HELPER_ROW_2 = HELPER_ROW_1 - .5f * CELL_SZ;
    private static final float HELPER_ROW_3 = HELPER_ROW_2 - .5f * CELL_SZ;

    private final ShipUiControl shipControl;
    private final SolUiControl freeCamControl;
    private final SolUiControl pauseControl;
    private final CameraKeyboardControl cameraControl;

    private final ZoneNameAnnouncer zoneNameAnnouncer;
    private final BorderDrawer borderDrawer;

    private final TextureAtlas.AtlasRegion lifeTexture;
    private final TextureAtlas.AtlasRegion infinityTexture;
    private final TextureAtlas.AtlasRegion waitTexture;
    private final TextureAtlas.AtlasRegion compassTexture;

    private final Color myCompassTint;
    private final TextPlace myLifeTp;
    private final TextPlace myRepairsExcessTp;
    private final TextPlace myShieldLifeTp;
    private final TextPlace myG1AmmoTp;
    private final TextPlace myG1AmmoExcessTp;
    private final TextPlace myG2AmmoTp;
    private final TextPlace myG2AmmoExcessTp;
    private final TextPlace myChargesExcessTp;
    private final TextPlace myMoneyExcessTp;
    private final SolApplication solApplication;
    private final Context context;

    private List<SolUiScreen> gameOverlayScreens = new ArrayList<>();
    private List<WarnDrawer> warnDrawers = new ArrayList<>();

    MainGameScreen(RightPaneLayout rightPaneLayout, Context context) {
        DisplayDimensions displayDimensions = SolApplication.displayDimensions;

        this.context = context;
        solApplication = context.get(SolApplication.class);
        GameOptions gameOptions = solApplication.getOptions();

        switch (gameOptions.controlType) {
            case KEYBOARD:
                UIShipControlsScreen shipControlsScreen =
                        (UIShipControlsScreen) solApplication.getNuiManager().createScreen("engine:uiShipControlsScreen");
                shipControl = shipControlsScreen;
                break;
            case MOUSE:
                shipControl = new ShipMouseControl();
                break;
            case CONTROLLER:
                shipControl = new ShipControllerControl(solApplication);
                break;
            case MIXED:
            default:
                shipControl = new ShipMixedControl(solApplication, controls);
                break;
        }

        boolean mobile = solApplication.isMobile();
        float lastCol = displayDimensions.getRatio() - MainGameScreen.CELL_SZ;
        // No button, since on mobile, it should be ideally controlled straightly by dragging.
        freeCamControl = new SolUiControl(null, false, gameOptions.getKeyFreeCameraMovement());
        controls.add(freeCamControl);
        pauseControl = new SolUiControl(null, true, gameOptions.getKeyPause());
        controls.add(pauseControl);
        cameraControl = new CameraKeyboardControl(gameOptions, controls);

        // possible warning messages in order of importance, so earlier one will be drawn on the center
        warnDrawers.add(new SunWarnDrawer());
        warnDrawers.add(new DmgWarnDrawer());
        warnDrawers.add(new CollisionWarnDrawer());
        warnDrawers.add(new NoShieldWarn());
        warnDrawers.add(new NoArmorWarn());
        warnDrawers.add(new EnemyWarn());

        zoneNameAnnouncer = new ZoneNameAnnouncer();
        borderDrawer = new BorderDrawer();

        lifeTexture = Assets.getAtlasRegion("engine:iconLife");
        infinityTexture = Assets.getAtlasRegion("engine:iconInfinity");
        waitTexture = Assets.getAtlasRegion("engine:iconWait");

        compassTexture = Assets.getAtlasRegion("engine:uiCompass");
        myCompassTint = SolColor.col(1, 0);

        myLifeTp = new TextPlace(SolColor.W50);
        myRepairsExcessTp = new TextPlace(SolColor.WHITE);
        myShieldLifeTp = new TextPlace(SolColor.W50);
        myG1AmmoTp = new TextPlace(SolColor.W50);
        myG1AmmoExcessTp = new TextPlace(SolColor.WHITE);
        myG2AmmoTp = new TextPlace(SolColor.W50);
        myG2AmmoExcessTp = new TextPlace(SolColor.WHITE);
        myChargesExcessTp = new TextPlace(SolColor.WHITE);
        myMoneyExcessTp = new TextPlace(SolColor.WHITE);
    }

    @Override
    public void onAdd(SolApplication solApplication) {
        super.onAdd(solApplication);
        if (solApplication.getOptions().controlType == GameOptions.ControlType.KEYBOARD) {
            UIShipControlsScreen uiControls = (UIShipControlsScreen) shipControl;
            if (!solApplication.getNuiManager().hasScreen(uiControls)) {
                solApplication.getNuiManager().pushScreen(uiControls);
            }
        }
    }

    public static Rectangle btn(float x, float y, boolean halfHeight) {
        float gap = .01f;
        float cellH = CELL_SZ;
        if (halfHeight) {
            cellH /= 2;
        }
        return new Rectangle(x + gap, y + gap, CELL_SZ - gap * 2, cellH - gap * 2);
    }

    private void maybeDrawHeight(UiDrawer drawer) {
        SolGame game = solApplication.getGame();
        Planet np = game.getPlanetManager().getNearestPlanet();
        SolCam cam = context.get(SolCam.class);
        Vector2 camPos = cam.getPosition();
        if (np != null && np.getPosition().dst(camPos) < np.getFullHeight()) {
            drawHeight(drawer, np, camPos, cam.getAngle());
        }
    }

    private void drawHeight(UiDrawer drawer, Planet np, Vector2 camPos, float camAngle) {
        float toPlanet = camPos.dst(np.getPosition());
        toPlanet -= np.getGroundHeight();
{'fraction_non_alphanumeric': 0.08252067645969634, 'fraction_numerical': 0.006542402172571287, 'mean_word_length': 3.5070931849791376, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2016947', 'n_tokens_mistral': 4863, 'n_tokens_neox': 4633, 'n_words': 1121}