Advertisement
Guest User

Malware Hub

a guest
Jun 12th, 2014
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 20.41 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package Game.Screens;
  6.  
  7. /**
  8.  *
  9.  */
  10. import Game.Main;
  11. import Game.MalwareAttributes.Ability;
  12. import Game.MalwareAttributes.Effect;
  13. import Game.MalwareAttributes.InfectionVector;
  14. import Game.MalwareAttributes.Malware;
  15. import Game.MalwareAttributes.Time;
  16. import Game.Rendering.PaintableTexture;
  17. import Game.Stats.Stats;
  18. import com.jme3.app.Application;
  19. import com.jme3.app.state.AbstractAppState;
  20.  
  21. //Jmonkey Asset Manager
  22. import com.jme3.asset.AssetManager;
  23. import com.jme3.input.InputManager;
  24. //Jmonkey Input
  25. import com.jme3.input.MouseInput;
  26. import com.jme3.input.controls.MouseButtonTrigger;
  27. import com.jme3.input.controls.ActionListener;
  28. import com.jme3.input.controls.AnalogListener;
  29. //Jmonkey Lighting
  30. import com.jme3.light.DirectionalLight;
  31. //Jmonkey Material
  32. import com.jme3.material.Material;
  33. //Jmonkey Colors and vectors
  34. import com.jme3.math.ColorRGBA;
  35. import com.jme3.math.Vector2f;
  36. import com.jme3.math.Vector3f;
  37. import com.jme3.renderer.Camera;
  38. //Jmonkey Shapes
  39. import com.jme3.scene.Geometry;
  40. import com.jme3.scene.Node;
  41. import com.jme3.scene.Spatial;
  42.  
  43. //Nifty Stuff
  44. import de.lessvoid.nifty.Nifty;
  45. import de.lessvoid.nifty.elements.Element;
  46. import de.lessvoid.nifty.elements.render.TextRenderer;
  47. import de.lessvoid.nifty.screen.Screen;
  48. import de.lessvoid.nifty.screen.ScreenController;
  49. import java.util.logging.Level;
  50. import java.util.logging.Logger;
  51.  
  52. public class MalwareHub extends AbstractAppState implements ScreenController {
  53.  
  54.     public MalwareHub() {
  55.  
  56.         day = Time.getDayRate();
  57.         timekeeper = 0;
  58.         upgradeTimekeeper = 0;
  59.         //We add the functionality necessary to let the player rotate the globe.
  60.         inputManager = Main.getApplication().getInputManager();
  61.         initKeys();
  62.  
  63.         gameStarted = false;
  64.  
  65.  
  66.     }
  67.  
  68.     @Override
  69.     public void onStartScreen() {
  70.         System.out.println("Screen Started");
  71.     }
  72.  
  73.     @Override
  74.     public void onEndScreen() {
  75.         System.out.println("Screen Ended");
  76.     }
  77.  
  78.     @Override
  79.     public void bind(Nifty NIFTY, Screen SCREEN) {
  80.         //Actual Screens
  81.         nifty = NIFTY;
  82.         screen = SCREEN;
  83.  
  84.         //Correct instantiations of other xml files
  85.         UA = Main.getUpgradeAbility();
  86.         UE = Main.getUpgradeEffect();
  87.         UIV = Main.getUpgradeInfectionVector();
  88.         PH = Main.getPopupHandler();
  89.        
  90.         //I initialize a lot of Main variables.
  91.         app = Main.getApplication();
  92.         assetManager = app.getAssetManager();
  93.         n = new Node("Globe");
  94.         cam = app.getCamera();
  95.         onMalwareHub = true;
  96.        
  97.         //<editor-fold desc="Show the beginning popup" defaultstate="collapsed">
  98.         if(!gameStarted){      
  99.             System.out.println("Beginning window shown");
  100.             PH.showWindow("Welcome. You have recently posted a piece of malware online that does nothing but will soon be upgraded by you. "
  101.                   + "Use these upgrades to increase infectivity, complete new and more powerful effects, and conquer the world.", "");
  102.         }
  103.         //</editor-fold>
  104.        
  105.         //We're here
  106.         gameStarted = true;
  107.        
  108.         UpgradeButton=screen.findElementByName("MalwareButton");
  109.        
  110.         //We are going to create the globe.
  111.         //<editor-fold defaultstate="collapsed" desc="It's 'Global'">        
  112.         //In order to do this, first, we need to transform it to fit whatever device they're using.        
  113.         //Here we create the actual sphere, with the radius being slightly smaller than the smallest side of whatever device is being used.
  114.         globe = assetManager.loadModel("Models/World/World_Globe.obj");
  115.  
  116.         globeGeometry = new Geometry();
  117.  
  118.         //Attach the texture
  119.         globeMaterial = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  120.         globeMaterial.setTexture("ColorMap", assetManager.loadTexture("Models/World/Globe_Txtr.png"));
  121.         globe.setMaterial(globeMaterial);
  122.  
  123.         //Set move the globe to the right location and center it.
  124.         globe.setLocalTranslation(0, 1, 0);
  125.         globe.center();
  126.  
  127.         //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.
  128.         n.attachChild(globe); //Attach it to the node
  129.         n.scale(0.02f); //Scale the globe down    
  130.         n.setLocalTranslation(0, 1, 0);
  131.  
  132.         //Starring... the globe!
  133.         cam.lookAt(new Vector3f(0, 0, 0), Vector3f.UNIT_Y);
  134.         cam.update();
  135.  
  136.         //Now we must give it some light!
  137.         //<editor-fold defaultstate="collapsed" desc="Light">
  138.         DirectionalLight sun2 = new DirectionalLight();
  139.         sun2.setDirection(new Vector3f(0, 1, 0).normalizeLocal());
  140.         sun2.setColor(ColorRGBA.White);
  141.         n.addLight(sun2);
  142.  
  143.         DirectionalLight sun3 = new DirectionalLight();
  144.         sun3.setDirection(new Vector3f(0, 0, 1).normalizeLocal());
  145.         sun3.setColor(ColorRGBA.White);
  146.         n.addLight(sun3);
  147.  
  148.         DirectionalLight sun4 = new DirectionalLight();
  149.         sun4.setDirection(new Vector3f(1, 0, 0).normalizeLocal());
  150.         sun4.setColor(ColorRGBA.White);
  151.         n.addLight(sun4);
  152.  
  153.         DirectionalLight sun5 = new DirectionalLight();
  154.         sun5.setDirection(new Vector3f(-1, 0, 0).normalizeLocal());
  155.         sun5.setColor(ColorRGBA.White);
  156.         n.addLight(sun5);
  157.  
  158.         DirectionalLight sun6 = new DirectionalLight();
  159.         sun6.setDirection(new Vector3f(0, -1, 0).normalizeLocal());
  160.         sun6.setColor(ColorRGBA.White);
  161.         n.addLight(sun6);
  162.  
  163.         DirectionalLight sun7 = new DirectionalLight();
  164.         sun7.setDirection(new Vector3f(0, 0, -1).normalizeLocal());
  165.         sun7.setColor(ColorRGBA.White);
  166.         n.addLight(sun7);
  167.         //</editor-fold>
  168.  
  169.         Main.getRN().attachChild(n); //Finally, attach the node to the root node.
  170.  
  171.         //</editor-fold>
  172.        
  173.         //Initialize text variables
  174.         //<editor-fold defaultstate="collapsed" desc="Initialize Text Renderers">
  175.         governmentInvestigationText = screen.findElementByName("GovernmentInvestigationText").getRenderer(TextRenderer.class);
  176.         computersInfectedText = screen.findElementByName("InfectedText").getRenderer(TextRenderer.class);
  177.         moneyMadeText = screen.findElementByName("MoneyText").getRenderer(TextRenderer.class);
  178.         destructionDoneText = screen.findElementByName("DestructionText").getRenderer(TextRenderer.class);
  179.         //</editor-fold>
  180.        
  181.  
  182.     }
  183.  
  184.     /**
  185.      * UPDATES INFORMATION IN REALTYME*
  186.      */
  187.     @Override
  188.     public void update(float tpf) {
  189.         timekeeper += tpf;
  190.        
  191.         //<editor-fold desc="Update Stuff" defaultstate="collapsed">
  192.         //We don't want stuff happenin before the menu has actually loaded, do we?
  193.         if (gameStarted) {
  194.  
  195.             //<editor-fold desc="Updates on screen variables" defaultstate="true">
  196.             //First we have to convert these doubles into whole numbers, then strings.
  197.             governmentPercent = Math.round(Stats.getInvestigationProgress() * 100);
  198.             destructionDone = Math.round(Stats.getDestruction());
  199.             computersInfected = Math.round(Stats.getInfected());
  200.             moneyMade = Math.round(Stats.getMoney());
  201.  
  202.             String invString = Long.toString(governmentPercent);
  203.             String desString = Long.toString(destructionDone);
  204.             String infString = Long.toString(computersInfected);
  205.             String monString = Long.toString(moneyMade);
  206.  
  207.             //Finally, set the text
  208.             governmentInvestigationText.setText(invString + " %");
  209.             computersInfectedText.setText(infString + " Infected");
  210.             moneyMadeText.setText(monString + " $");
  211.             destructionDoneText.setText(desString + " $");
  212.             //</editor-fold>
  213.  
  214.             //<editor-fold defaultstate="collapsed" desc="Updates stats">
  215.             if (timekeeper >= day) {
  216.                 //We update the variables based on the amount of days that have passed
  217.                 Stats.update(timekeeper / day);
  218.                 timekeeper = 0;
  219.  
  220.             }
  221.             //</editor-fold>
  222.  
  223.             //<editor-fold desc="Tracks Development of Upgrades" defaulstate="collapsed">
  224.             if (developing) {
  225.  
  226.                 double timeNeededToFinish;
  227.                 upgradeTimekeeper = upgradeTimekeeper + tpf;
  228.  
  229.                 //<editor-fold desc="If it's an ability" defaultstate="collapsed">
  230.                 if (abilityDeveloped != null) {
  231.                     if (Malware.abilities.contains(abilityDeveloped)) {
  232.                         timeNeededToFinish = abilityDeveloped.getDevelopmentTime(abilityDeveloped.getLevel()) * Time.getDayRate() * Stats.getDevTimeMultiplier();
  233.                     } else {
  234.                         timeNeededToFinish = abilityDeveloped.getDevelopmentTime(abilityDeveloped.getLevel() - 1) * Time.getDayRate() * Stats.getDevTimeMultiplier();
  235.                     }
  236.                     UA.setProgressBar(upgradeTimekeeper / timeNeededToFinish);
  237.                     UA.setProgressBarDescription("Developing...");
  238.                     //Finally, if we finish
  239.                     if (upgradeTimekeeper >= timeNeededToFinish) {
  240.                         //Throw the new abili
  241.                         UA.abilityDeveloped(abilityDeveloped, upgradeTitle);
  242.  
  243.                         abilityDeveloped = null;
  244.                         developing = false;
  245.                         upgradeTimekeeper = 0;
  246.                     }
  247.                     //</editor-fold>
  248.  
  249.                 //<editor-fold desc="If it's an effect" defaultstate="collapsed">
  250.                 } else if (effectDeveloped != null) {
  251.                     upgradeTimekeeper = upgradeTimekeeper + tpf;
  252.                     if (Malware.effects.contains(effectDeveloped)) {
  253.                         timeNeededToFinish = effectDeveloped.getDevelopmentTime(effectDeveloped.getLevel()) * Time.getDayRate() * Stats.getDevTimeMultiplier();
  254.                     } else {
  255.                         timeNeededToFinish = effectDeveloped.getDevelopmentTime(effectDeveloped.getLevel() - 1) * Time.getDayRate() * Stats.getDevTimeMultiplier();
  256.                     }
  257.                     UE.setProgressBar(upgradeTimekeeper / timeNeededToFinish);
  258.                     UE.setProgressBarDescription("Developing...");
  259.                     //Finally, if we finish
  260.                     if (upgradeTimekeeper >= timeNeededToFinish) {
  261.                         //Throw the new effect back to the screencontroller class to finish things up
  262.                         UE.effectDeveloped(effectDeveloped, upgradeTitle);
  263.  
  264.                         effectDeveloped = null;
  265.                         developing = false;
  266.                         upgradeTitle = false;
  267.                         upgradeTimekeeper = 0;
  268.                     }
  269.  
  270.                     //</editor-fold>
  271.  
  272.                 //<editor-fold desc="If it's an infection vector" defaultstate="collapsed">
  273.                 } else if (infectionVectorDeveloped != null) {
  274.                     upgradeTimekeeper = upgradeTimekeeper + tpf;
  275.                     if (Malware.infectionVectors.contains(infectionVectorDeveloped)) {
  276.                         timeNeededToFinish = infectionVectorDeveloped.getDevelopmentTime(infectionVectorDeveloped.getLevel()) * Time.getDayRate() * Stats.getDevTimeMultiplier();
  277.                     } else {
  278.                         timeNeededToFinish = infectionVectorDeveloped.getDevelopmentTime(infectionVectorDeveloped.getLevel() - 1) * Time.getDayRate() * Stats.getDevTimeMultiplier();
  279.                     }
  280.                     UIV.setProgressBar(upgradeTimekeeper / timeNeededToFinish);
  281.                     UIV.setProgressBarDescription("Developing...");
  282.                     //Finally, if we finish
  283.                     if (upgradeTimekeeper >= timeNeededToFinish) {
  284.                         //Throw the new infection vector back to the screen controller class to finish things up
  285.                         UIV.infectionVectorDeveloped(infectionVectorDeveloped, upgradeTitle);
  286.  
  287.                         infectionVectorDeveloped = null;
  288.                         developing = false;
  289.                         upgradeTitle = false;
  290.                         upgradeTimekeeper = 0;
  291.                     }
  292.                 }
  293.                 //</editor-fold>
  294.  
  295.             }
  296.             //</editor-fold>        
  297.  
  298.  
  299.             checkIfGameEnded();
  300.         //</editor-fold>
  301.  
  302.         } else {
  303.         }
  304.     }
  305.  
  306.     //<editor-fold desc="Develops required upgrade">
  307.     public void developAbility(Ability a, boolean title) {
  308.         System.out.println("Develop Ability Function Called");
  309.         developing = true;
  310.         upgradeTitle = true;
  311.         abilityDeveloped = a;
  312.     }
  313.  
  314.     public void developEffect(Effect e, boolean title) {
  315.         developing = true;
  316.         upgradeTitle = true;
  317.         effectDeveloped = e;
  318.     }
  319.  
  320.     public void developInfectionVector(InfectionVector iv, boolean title) {
  321.         developing = true;
  322.         upgradeTitle = title;
  323.         infectionVectorDeveloped = iv;
  324.     }
  325.     //</editor-fold>
  326.  
  327.     //<editor-fold defaultstate="collapsed" desc="Called when the player clicks the Malware Button">
  328.     public void loadUpgrades() {
  329.         int x = nifty.getNiftyMouse().getX();
  330.         int y = nifty.getNiftyMouse().getY();
  331.         if(x>=UpgradeButton.getX() &&
  332.            x<=UpgradeButton.getX()+UpgradeButton.getWidth() &&
  333.            y>=UpgradeButton.getY() &&
  334.            y<=UpgradeButton.getY()+UpgradeButton.getHeight()){
  335.             n.detachChild(globe);
  336.             onMalwareHub = false;
  337.             nifty.fromXml("Interface/NiftyGUI/Upgrades/UpgradeEffects.xml", "start", Main.getUpgradeEffect());
  338.         }
  339.     }
  340.     //</editor-fold>
  341.  
  342.     //<editor-fold defaultstate="collapsed" desc="Input Keys">
  343.     private void initKeys() {
  344.         // The only input we need for android, touch. The engine automatically translates touch events into mouse events for Android, if you were wondering.
  345.         // 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.
  346.         inputManager.addMapping("Rotate", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
  347.  
  348.         // Add the names to the touch listener.
  349.         inputManager.addListener(touchMoveListener, new String[]{"Rotate"});
  350.         inputManager.addListener(touchListener, new String[]{"Rotate"});
  351.  
  352.     }
  353.     //<editor-fold defaultstate="collapsed" desc="Press Listener">
  354.     private ActionListener touchListener = new ActionListener() {
  355.         @Override
  356.         public void onAction(String name, boolean keyPressed, float tpf) {
  357.             //This method is called every time the user either presses the device or stops pressing the device.
  358.             //We need this so we can record an initial position for the mouse.
  359.             if (keyPressed) {
  360.                 //I'm going to abuse this a little bit here to make it so that any popups lying around are hidden
  361.                 //at the moment the player clicks anywhere on the screen
  362.                 if(gameStarted){
  363.                     PH.hide();
  364.                 }
  365.                 //Otherwise this is the main feature
  366.                 MousePreviousPosition = inputManager.getCursorPosition().clone();
  367.                 applyAnalog = true;
  368.             } else {
  369.                 applyAnalog = false;
  370.             }
  371.             if(exit){
  372.                 Main.getApplication().stop();
  373.             }
  374.         }
  375.     };
  376.     //</editor-fold>
  377.     //<editor-fold defaultstate="collapsed" desc="Rotate Listener">
  378.     private AnalogListener touchMoveListener = new AnalogListener() {
  379.         @Override
  380.         public void onAnalog(String name, float value, float tpf) {
  381.             if (applyAnalog && onMalwareHub) {
  382.                 //This continuously rotates the earth in the direction the user swipes.
  383.                 //In order to do this, we must take the previous position on the mouse
  384.                 MousePosition = inputManager.getCursorPosition().clone();
  385.  
  386.                 //Rotate along the Y axis based on how far along the x axis the mouse has moved while he's touched the screen.
  387.                 n.rotate(0f, (MousePosition.x - MousePreviousPosition.x) / 100, 0f);
  388.                 System.out.println(n.getLocalRotation().getY());
  389.  
  390.                 //If the rotation is greater than one or less than negative one (Its made a whole revolution), I set it to zero.
  391.                 if (n.getLocalRotation().getY() >= 1 || n.getLocalRotation().getY() <= -1) {
  392.                     n.getLocalRotation().set(n.getLocalRotation().getX(), 0, n.getLocalRotation().getZ(), n.getLocalRotation().getW());
  393.                 }
  394.  
  395.  
  396.  
  397.                 MousePreviousPosition = MousePosition;
  398.             }
  399.         }
  400.     };
  401.     //</editor-fold>
  402.     //</editor-fold>
  403.  
  404.     //<editor-fold desc="Get Functions" defaultstate="collapsed">
  405.     public boolean isDevelopingUpgrade() {
  406.         return developing;
  407.     }
  408.     //</editor-fold>
  409.    
  410.     //<editor-fold desc="Display Functions" defaultstate="collapsed">
  411.     public void checkIfGameEnded(){
  412.        
  413.             //<editor-fold desc="Investigation finished" defaultstate="collapsed">
  414.             if (Stats.getInvestigationProgress() >= 1) {                
  415.                 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.", "");
  416.                 exit=true;
  417.             }
  418.             //</editor-fold>
  419.            
  420.             //<editor-fold desc="5,000,000$ of destruction" defaultstate="collapsed">
  421.             if (Stats.getDestruction() >= 5000000) {                
  422.                 PH.showWindow("You have destroyed over 5,000,000$ worth of computer files, stored money, and operating systems, and have won.", "");
  423.                 exit=true;
  424.             }            
  425.             //</editor-fold>
  426.            
  427.             //<editor-fold desc="2,000,000$ of money" defaultstate="collapsed">
  428.             if (Stats.getMoney() >= 2000000) {                
  429.                 PH.showWindow("You have profited over 2,000,000$ worth of data and online savings, and have won.", "");
  430.                 exit=true;
  431.             }        
  432.             //</editor-fold>
  433.            
  434.     }
  435.     //</editor-fold>
  436.    
  437.    
  438.     //<editor-fold desc="Variables" defaultstate="collapsed">
  439.     private Nifty nifty;
  440.     private Screen screen;
  441.     private Application app;
  442.     private AssetManager assetManager;
  443.     private Node n; //Public and static because we need to stop showing the globe in some instances
  444.     private InputManager inputManager;
  445.     private Camera cam;
  446.     private boolean gameStarted;
  447.     private boolean onMalwareHub;
  448.    
  449.     //The world
  450.     private Spatial globe; //Public and static because we need to not show it in some instances
  451.     private Geometry globeGeometry;
  452.     private Material globeMaterial;
  453.     private PaintableTexture map;
  454.    
  455.     //Input
  456.     Vector2f MousePreviousPosition;
  457.     Vector2f MousePosition;
  458.     boolean applyAnalog;
  459.    
  460.     //Timing
  461.     private double timekeeper;
  462.     private double day;
  463.    
  464.     //Popup
  465.     private Element popup;
  466.     private TextRenderer popupText;
  467.    
  468.     //Used when developing upgrades
  469.     private float upgradeTimekeeper; //How long has development run?
  470.     private boolean developing; //Are we developing something?
  471.     private boolean upgradeTitle; //Does it have a title or an icon?
  472.     private Ability abilityDeveloped; //The ability being developed
  473.     private Effect effectDeveloped; //Or the effect being developed
  474.     private InfectionVector infectionVectorDeveloped; //Or maybe its an infection vector
  475.    
  476.     //Nifty Text
  477.     private long governmentPercent;
  478.     private long computersInfected;
  479.     private long moneyMade;
  480.     private long destructionDone;
  481.     private TextRenderer governmentInvestigationText;
  482.     private TextRenderer computersInfectedText;
  483.     private TextRenderer moneyMadeText;
  484.     private TextRenderer destructionDoneText;
  485.    
  486.     //The only navigation item on the screen at the moment
  487.     private Element UpgradeButton;
  488.    
  489.     //Other xml files
  490.     private UpgradeAbility UA;
  491.     private UpgradeEffect UE;
  492.     private UpgradeInfectionVector UIV;
  493.     private PopupHandler PH;
  494.    
  495.     //Should we exit the game?
  496.     private boolean exit;
  497.     //</editor-fold>
  498. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement