Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package Game.Screens;
- /**
- *
- */
- import Game.Main;
- import Game.MalwareAttributes.Ability;
- import Game.MalwareAttributes.Effect;
- import Game.MalwareAttributes.InfectionVector;
- import Game.MalwareAttributes.Malware;
- import Game.MalwareAttributes.Time;
- import Game.Rendering.PaintableTexture;
- import Game.Stats.Stats;
- import com.jme3.app.Application;
- import com.jme3.app.state.AbstractAppState;
- //Jmonkey Asset Manager
- import com.jme3.asset.AssetManager;
- import com.jme3.input.InputManager;
- //Jmonkey Input
- import com.jme3.input.MouseInput;
- import com.jme3.input.controls.MouseButtonTrigger;
- import com.jme3.input.controls.ActionListener;
- import com.jme3.input.controls.AnalogListener;
- //Jmonkey Lighting
- import com.jme3.light.DirectionalLight;
- //Jmonkey Material
- import com.jme3.material.Material;
- //Jmonkey Colors and vectors
- import com.jme3.math.ColorRGBA;
- import com.jme3.math.Vector2f;
- import com.jme3.math.Vector3f;
- import com.jme3.renderer.Camera;
- //Jmonkey Shapes
- import com.jme3.scene.Geometry;
- import com.jme3.scene.Node;
- import com.jme3.scene.Spatial;
- //Nifty Stuff
- import de.lessvoid.nifty.Nifty;
- import de.lessvoid.nifty.elements.Element;
- import de.lessvoid.nifty.elements.render.TextRenderer;
- import de.lessvoid.nifty.screen.Screen;
- import de.lessvoid.nifty.screen.ScreenController;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- public class MalwareHub extends AbstractAppState implements ScreenController {
- public MalwareHub() {
- day = Time.getDayRate();
- timekeeper = 0;
- upgradeTimekeeper = 0;
- //We add the functionality necessary to let the player rotate the globe.
- inputManager = Main.getApplication().getInputManager();
- initKeys();
- gameStarted = false;
- }
- @Override
- public void onStartScreen() {
- System.out.println("Screen Started");
- }
- @Override
- public void onEndScreen() {
- System.out.println("Screen Ended");
- }
- @Override
- public void bind(Nifty NIFTY, Screen SCREEN) {
- //Actual Screens
- nifty = NIFTY;
- screen = SCREEN;
- //Correct instantiations of other xml files
- UA = Main.getUpgradeAbility();
- UE = Main.getUpgradeEffect();
- UIV = Main.getUpgradeInfectionVector();
- PH = Main.getPopupHandler();
- //I initialize a lot of Main variables.
- app = Main.getApplication();
- assetManager = app.getAssetManager();
- n = new Node("Globe");
- cam = app.getCamera();
- onMalwareHub = true;
- //<editor-fold desc="Show the beginning popup" defaultstate="collapsed">
- if(!gameStarted){
- System.out.println("Beginning window shown");
- PH.showWindow("Welcome. You have recently posted a piece of malware online that does nothing but will soon be upgraded by you. "
- + "Use these upgrades to increase infectivity, complete new and more powerful effects, and conquer the world.", "");
- }
- //</editor-fold>
- //We're here
- gameStarted = true;
- UpgradeButton=screen.findElementByName("MalwareButton");
- //We are going to create the globe.
- //<editor-fold defaultstate="collapsed" desc="It's 'Global'">
- //In order to do this, first, we need to transform it to fit whatever device they're using.
- //Here we create the actual sphere, with the radius being slightly smaller than the smallest side of whatever device is being used.
- globe = assetManager.loadModel("Models/World/World_Globe.obj");
- globeGeometry = new Geometry();
- //Attach the texture
- globeMaterial = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
- globeMaterial.setTexture("ColorMap", assetManager.loadTexture("Models/World/Globe_Txtr.png"));
- globe.setMaterial(globeMaterial);
- //Set move the globe to the right location and center it.
- globe.setLocalTranslation(0, 1, 0);
- globe.center();
- //Attach the globe, scale it down (cuz its big), then translate the node to the same spot as the globe so we don't start revolving around 0,0,0.
- n.attachChild(globe); //Attach it to the node
- n.scale(0.02f); //Scale the globe down
- n.setLocalTranslation(0, 1, 0);
- //Starring... the globe!
- cam.lookAt(new Vector3f(0, 0, 0), Vector3f.UNIT_Y);
- cam.update();
- //Now we must give it some light!
- //<editor-fold defaultstate="collapsed" desc="Light">
- DirectionalLight sun2 = new DirectionalLight();
- sun2.setDirection(new Vector3f(0, 1, 0).normalizeLocal());
- sun2.setColor(ColorRGBA.White);
- n.addLight(sun2);
- DirectionalLight sun3 = new DirectionalLight();
- sun3.setDirection(new Vector3f(0, 0, 1).normalizeLocal());
- sun3.setColor(ColorRGBA.White);
- n.addLight(sun3);
- DirectionalLight sun4 = new DirectionalLight();
- sun4.setDirection(new Vector3f(1, 0, 0).normalizeLocal());
- sun4.setColor(ColorRGBA.White);
- n.addLight(sun4);
- DirectionalLight sun5 = new DirectionalLight();
- sun5.setDirection(new Vector3f(-1, 0, 0).normalizeLocal());
- sun5.setColor(ColorRGBA.White);
- n.addLight(sun5);
- DirectionalLight sun6 = new DirectionalLight();
- sun6.setDirection(new Vector3f(0, -1, 0).normalizeLocal());
- sun6.setColor(ColorRGBA.White);
- n.addLight(sun6);
- DirectionalLight sun7 = new DirectionalLight();
- sun7.setDirection(new Vector3f(0, 0, -1).normalizeLocal());
- sun7.setColor(ColorRGBA.White);
- n.addLight(sun7);
- //</editor-fold>
- Main.getRN().attachChild(n); //Finally, attach the node to the root node.
- //</editor-fold>
- //Initialize text variables
- //<editor-fold defaultstate="collapsed" desc="Initialize Text Renderers">
- governmentInvestigationText = screen.findElementByName("GovernmentInvestigationText").getRenderer(TextRenderer.class);
- computersInfectedText = screen.findElementByName("InfectedText").getRenderer(TextRenderer.class);
- moneyMadeText = screen.findElementByName("MoneyText").getRenderer(TextRenderer.class);
- destructionDoneText = screen.findElementByName("DestructionText").getRenderer(TextRenderer.class);
- //</editor-fold>
- }
- /**
- * UPDATES INFORMATION IN REALTYME*
- */
- @Override
- public void update(float tpf) {
- timekeeper += tpf;
- //<editor-fold desc="Update Stuff" defaultstate="collapsed">
- //We don't want stuff happenin before the menu has actually loaded, do we?
- if (gameStarted) {
- //<editor-fold desc="Updates on screen variables" defaultstate="true">
- //First we have to convert these doubles into whole numbers, then strings.
- governmentPercent = Math.round(Stats.getInvestigationProgress() * 100);
- destructionDone = Math.round(Stats.getDestruction());
- computersInfected = Math.round(Stats.getInfected());
- moneyMade = Math.round(Stats.getMoney());
- String invString = Long.toString(governmentPercent);
- String desString = Long.toString(destructionDone);
- String infString = Long.toString(computersInfected);
- String monString = Long.toString(moneyMade);
- //Finally, set the text
- governmentInvestigationText.setText(invString + " %");
- computersInfectedText.setText(infString + " Infected");
- moneyMadeText.setText(monString + " $");
- destructionDoneText.setText(desString + " $");
- //</editor-fold>
- //<editor-fold defaultstate="collapsed" desc="Updates stats">
- if (timekeeper >= day) {
- //We update the variables based on the amount of days that have passed
- Stats.update(timekeeper / day);
- timekeeper = 0;
- }
- //</editor-fold>
- //<editor-fold desc="Tracks Development of Upgrades" defaulstate="collapsed">
- if (developing) {
- double timeNeededToFinish;
- upgradeTimekeeper = upgradeTimekeeper + tpf;
- //<editor-fold desc="If it's an ability" defaultstate="collapsed">
- if (abilityDeveloped != null) {
- if (Malware.abilities.contains(abilityDeveloped)) {
- timeNeededToFinish = abilityDeveloped.getDevelopmentTime(abilityDeveloped.getLevel()) * Time.getDayRate() * Stats.getDevTimeMultiplier();
- } else {
- timeNeededToFinish = abilityDeveloped.getDevelopmentTime(abilityDeveloped.getLevel() - 1) * Time.getDayRate() * Stats.getDevTimeMultiplier();
- }
- UA.setProgressBar(upgradeTimekeeper / timeNeededToFinish);
- UA.setProgressBarDescription("Developing...");
- //Finally, if we finish
- if (upgradeTimekeeper >= timeNeededToFinish) {
- //Throw the new abili
- UA.abilityDeveloped(abilityDeveloped, upgradeTitle);
- abilityDeveloped = null;
- developing = false;
- upgradeTimekeeper = 0;
- }
- //</editor-fold>
- //<editor-fold desc="If it's an effect" defaultstate="collapsed">
- } else if (effectDeveloped != null) {
- upgradeTimekeeper = upgradeTimekeeper + tpf;
- if (Malware.effects.contains(effectDeveloped)) {
- timeNeededToFinish = effectDeveloped.getDevelopmentTime(effectDeveloped.getLevel()) * Time.getDayRate() * Stats.getDevTimeMultiplier();
- } else {
- timeNeededToFinish = effectDeveloped.getDevelopmentTime(effectDeveloped.getLevel() - 1) * Time.getDayRate() * Stats.getDevTimeMultiplier();
- }
- UE.setProgressBar(upgradeTimekeeper / timeNeededToFinish);
- UE.setProgressBarDescription("Developing...");
- //Finally, if we finish
- if (upgradeTimekeeper >= timeNeededToFinish) {
- //Throw the new effect back to the screencontroller class to finish things up
- UE.effectDeveloped(effectDeveloped, upgradeTitle);
- effectDeveloped = null;
- developing = false;
- upgradeTitle = false;
- upgradeTimekeeper = 0;
- }
- //</editor-fold>
- //<editor-fold desc="If it's an infection vector" defaultstate="collapsed">
- } else if (infectionVectorDeveloped != null) {
- upgradeTimekeeper = upgradeTimekeeper + tpf;
- if (Malware.infectionVectors.contains(infectionVectorDeveloped)) {
- timeNeededToFinish = infectionVectorDeveloped.getDevelopmentTime(infectionVectorDeveloped.getLevel()) * Time.getDayRate() * Stats.getDevTimeMultiplier();
- } else {
- timeNeededToFinish = infectionVectorDeveloped.getDevelopmentTime(infectionVectorDeveloped.getLevel() - 1) * Time.getDayRate() * Stats.getDevTimeMultiplier();
- }
- UIV.setProgressBar(upgradeTimekeeper / timeNeededToFinish);
- UIV.setProgressBarDescription("Developing...");
- //Finally, if we finish
- if (upgradeTimekeeper >= timeNeededToFinish) {
- //Throw the new infection vector back to the screen controller class to finish things up
- UIV.infectionVectorDeveloped(infectionVectorDeveloped, upgradeTitle);
- infectionVectorDeveloped = null;
- developing = false;
- upgradeTitle = false;
- upgradeTimekeeper = 0;
- }
- }
- //</editor-fold>
- }
- //</editor-fold>
- checkIfGameEnded();
- //</editor-fold>
- } else {
- }
- }
- //<editor-fold desc="Develops required upgrade">
- public void developAbility(Ability a, boolean title) {
- System.out.println("Develop Ability Function Called");
- developing = true;
- upgradeTitle = true;
- abilityDeveloped = a;
- }
- public void developEffect(Effect e, boolean title) {
- developing = true;
- upgradeTitle = true;
- effectDeveloped = e;
- }
- public void developInfectionVector(InfectionVector iv, boolean title) {
- developing = true;
- upgradeTitle = title;
- infectionVectorDeveloped = iv;
- }
- //</editor-fold>
- //<editor-fold defaultstate="collapsed" desc="Called when the player clicks the Malware Button">
- public void loadUpgrades() {
- int x = nifty.getNiftyMouse().getX();
- int y = nifty.getNiftyMouse().getY();
- if(x>=UpgradeButton.getX() &&
- x<=UpgradeButton.getX()+UpgradeButton.getWidth() &&
- y>=UpgradeButton.getY() &&
- y<=UpgradeButton.getY()+UpgradeButton.getHeight()){
- n.detachChild(globe);
- onMalwareHub = false;
- nifty.fromXml("Interface/NiftyGUI/Upgrades/UpgradeEffects.xml", "start", Main.getUpgradeEffect());
- }
- }
- //</editor-fold>
- //<editor-fold defaultstate="collapsed" desc="Input Keys">
- private void initKeys() {
- // The only input we need for android, touch. The engine automatically translates touch events into mouse events for Android, if you were wondering.
- // In fact, the engine can pretty much be used the same way as it were to be used on desktop, except that resolutions are different.
- inputManager.addMapping("Rotate", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
- // Add the names to the touch listener.
- inputManager.addListener(touchMoveListener, new String[]{"Rotate"});
- inputManager.addListener(touchListener, new String[]{"Rotate"});
- }
- //<editor-fold defaultstate="collapsed" desc="Press Listener">
- private ActionListener touchListener = new ActionListener() {
- @Override
- public void onAction(String name, boolean keyPressed, float tpf) {
- //This method is called every time the user either presses the device or stops pressing the device.
- //We need this so we can record an initial position for the mouse.
- if (keyPressed) {
- //I'm going to abuse this a little bit here to make it so that any popups lying around are hidden
- //at the moment the player clicks anywhere on the screen
- if(gameStarted){
- PH.hide();
- }
- //Otherwise this is the main feature
- MousePreviousPosition = inputManager.getCursorPosition().clone();
- applyAnalog = true;
- } else {
- applyAnalog = false;
- }
- if(exit){
- Main.getApplication().stop();
- }
- }
- };
- //</editor-fold>
- //<editor-fold defaultstate="collapsed" desc="Rotate Listener">
- private AnalogListener touchMoveListener = new AnalogListener() {
- @Override
- public void onAnalog(String name, float value, float tpf) {
- if (applyAnalog && onMalwareHub) {
- //This continuously rotates the earth in the direction the user swipes.
- //In order to do this, we must take the previous position on the mouse
- MousePosition = inputManager.getCursorPosition().clone();
- //Rotate along the Y axis based on how far along the x axis the mouse has moved while he's touched the screen.
- n.rotate(0f, (MousePosition.x - MousePreviousPosition.x) / 100, 0f);
- System.out.println(n.getLocalRotation().getY());
- //If the rotation is greater than one or less than negative one (Its made a whole revolution), I set it to zero.
- if (n.getLocalRotation().getY() >= 1 || n.getLocalRotation().getY() <= -1) {
- n.getLocalRotation().set(n.getLocalRotation().getX(), 0, n.getLocalRotation().getZ(), n.getLocalRotation().getW());
- }
- MousePreviousPosition = MousePosition;
- }
- }
- };
- //</editor-fold>
- //</editor-fold>
- //<editor-fold desc="Get Functions" defaultstate="collapsed">
- public boolean isDevelopingUpgrade() {
- return developing;
- }
- //</editor-fold>
- //<editor-fold desc="Display Functions" defaultstate="collapsed">
- public void checkIfGameEnded(){
- //<editor-fold desc="Investigation finished" defaultstate="collapsed">
- if (Stats.getInvestigationProgress() >= 1) {
- PH.showWindow("The investigation has finished. The police have shut down the servers you use to control your hosts. You have been arrested, and have lost.", "");
- exit=true;
- }
- //</editor-fold>
- //<editor-fold desc="5,000,000$ of destruction" defaultstate="collapsed">
- if (Stats.getDestruction() >= 5000000) {
- PH.showWindow("You have destroyed over 5,000,000$ worth of computer files, stored money, and operating systems, and have won.", "");
- exit=true;
- }
- //</editor-fold>
- //<editor-fold desc="2,000,000$ of money" defaultstate="collapsed">
- if (Stats.getMoney() >= 2000000) {
- PH.showWindow("You have profited over 2,000,000$ worth of data and online savings, and have won.", "");
- exit=true;
- }
- //</editor-fold>
- }
- //</editor-fold>
- //<editor-fold desc="Variables" defaultstate="collapsed">
- private Nifty nifty;
- private Screen screen;
- private Application app;
- private AssetManager assetManager;
- private Node n; //Public and static because we need to stop showing the globe in some instances
- private InputManager inputManager;
- private Camera cam;
- private boolean gameStarted;
- private boolean onMalwareHub;
- //The world
- private Spatial globe; //Public and static because we need to not show it in some instances
- private Geometry globeGeometry;
- private Material globeMaterial;
- private PaintableTexture map;
- //Input
- Vector2f MousePreviousPosition;
- Vector2f MousePosition;
- boolean applyAnalog;
- //Timing
- private double timekeeper;
- private double day;
- //Popup
- private Element popup;
- private TextRenderer popupText;
- //Used when developing upgrades
- private float upgradeTimekeeper; //How long has development run?
- private boolean developing; //Are we developing something?
- private boolean upgradeTitle; //Does it have a title or an icon?
- private Ability abilityDeveloped; //The ability being developed
- private Effect effectDeveloped; //Or the effect being developed
- private InfectionVector infectionVectorDeveloped; //Or maybe its an infection vector
- //Nifty Text
- private long governmentPercent;
- private long computersInfected;
- private long moneyMade;
- private long destructionDone;
- private TextRenderer governmentInvestigationText;
- private TextRenderer computersInfectedText;
- private TextRenderer moneyMadeText;
- private TextRenderer destructionDoneText;
- //The only navigation item on the screen at the moment
- private Element UpgradeButton;
- //Other xml files
- private UpgradeAbility UA;
- private UpgradeEffect UE;
- private UpgradeInfectionVector UIV;
- private PopupHandler PH;
- //Should we exit the game?
- private boolean exit;
- //</editor-fold>
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement