Advertisement
Guest User

Untitled

a guest
Oct 29th, 2013
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 26.41 KB | None | 0 0
  1. package md.obama.game;
  2.  
  3. import java.awt.Container;
  4. import java.awt.event.MouseEvent;
  5. import java.awt.event.MouseListener;
  6. import java.awt.event.MouseMotionListener;
  7. import java.io.File;
  8. import java.text.DecimalFormat;
  9. import java.util.Formatter;
  10. import java.util.Scanner;
  11.  
  12. import javax.swing.JOptionPane;
  13. import javax.swing.JPanel;
  14.  
  15. import md.obama.game.entities.buildings.Booster;
  16. import md.obama.game.entities.buildings.Building;
  17. import md.obama.game.entities.buildings.MetalExtractor;
  18. import md.obama.game.entities.buildings.SolarArray;
  19. import md.obama.game.gfx.Colours;
  20. import md.obama.game.gfx.Font;
  21. import md.obama.game.gfx.Screen;
  22. import md.obama.game.level.GenerateLevel;
  23. import md.obama.game.level.Level;
  24.  
  25. public class GameHandler extends JPanel implements MouseListener, MouseMotionListener{
  26.  
  27.     /**
  28.      * Really
  29.      */
  30.     private static final long serialVersionUID = 1L;
  31.     private static final byte GRIDMAX = 14;
  32.     private static final byte GRIDMIN = 0;
  33.    
  34.    
  35.     private final Container cc = getRootPane();
  36.     private boolean can_generate = true;
  37.     GenerateLevel genLvl = new GenerateLevel();
  38.    
  39.     //Metal
  40.     public double metalBank=8;
  41.     public float metalPerSecond=1;
  42.     public int metalClicked=0;
  43.     public int metalPerClick=1;
  44.     public int metalFromClicking=0;
  45.     public int metalFromBuildings=0;
  46.    
  47.     public int totalTicks=0;
  48.    
  49.     //Energy
  50.     public double energyMax=0; //Max allowed energy
  51.     public double energyUse=0; //Current energy consumption
  52.    
  53.     public Level level;
  54.     public Screen screen;
  55.    
  56.    
  57.     //debug vars
  58.     //private boolean skip_ask = false;
  59.     private boolean skip_cost = false;
  60.     private boolean debug_help = false;
  61.    
  62.     //buying / placing things
  63.     private boolean placing = false;
  64.     private boolean moving = false;
  65.     private short place_id = 0;
  66.     int mouse_y = 200;
  67.     int mouse_x = 200;
  68.     private byte place_sprite_x=0, place_sprite_y=0;
  69.     int place_color = Colours.get(000, 222, 444, 333);
  70.    
  71.     //stats
  72.     int stat_id;
  73.     int stat_lvl;
  74.     int stat_x;
  75.     int stat_y;
  76.     double stat_meCost;
  77.     double stat_enCost;
  78.     double stat_enUsed;
  79.     double stat_product;
  80.     double stat_productB;
  81.     float stat_pboost;
  82.     boolean show_stats = false;
  83.     Building stat_ding;
  84.    
  85.     DecimalFormat df = new DecimalFormat("###.##");
  86.    
  87.    
  88.     @Override
  89.     public void mousePressed(MouseEvent e) {
  90.         int x = e.getX();
  91.         int y = e.getY();
  92.         int gridX = Math.round(x/16/2)-12;
  93.         int gridY = Math.round(y/16/2)-1;
  94.         if(e.getButton() == MouseEvent.BUTTON3){
  95.             System.out.printf("Coords: %s,%s  tiled: %s,%s Grid: %s,%s\n", x, y, x/8, y/8, gridX, gridY);
  96.             placing = false;
  97.             moving = false;
  98.            
  99.            
  100.         }
  101.         else if(placing == false){
  102.             if(boxed(x,y,1, 456, 400, 40)){     //Click the ore for metal
  103.                 metalBank += metalPerClick;
  104.                 metalFromClicking += metalPerClick;
  105.                 metalClicked++;
  106.                 System.out.printf("Total metal! %s, Metal clicked %s.\n", ((long)Math.floor(metalBank)), metalClicked);
  107.             }
  108.            
  109.             if(boxed(x,y,1, 500, 400, 20) && debug_help){    //Just increases metal per click.
  110.                 metalPerClick++;
  111.                 metalPerSecond+=5;
  112.                 System.out.printf("Metal per click %s, Metal from clicking %s.\n", metalPerClick, metalFromClicking);
  113.             }
  114.            
  115.            /* if(mouse_x >= 388 && mouse_y >=34
  116.             && mouse_x <=863 && mouse_y <=498){               //Clicking a building
  117.                 if(Building.build[gridX][gridY] != null){
  118.                     Building ding = Building.build[gridX][gridY];
  119.                     updateEnergy();
  120.                     if(ding.id==1){                              //Metal extractor
  121.                         clickExtractor(ding, false);
  122.                     } else if (ding.id == 2) { // Booster
  123.                         clickBooster(ding, false);
  124.                     } else if (ding.id == 3) { // Solar Array
  125.                         clickSolarArray(ding, false);
  126.                     }  
  127.                 }
  128.             }*/
  129.            
  130.             gridX = Math.round(x/32)-12;
  131.             gridY = Math.round(y/32)-1;
  132.            
  133.             if(gridX >=GRIDMIN && gridY >= GRIDMIN && gridX <=GRIDMAX && gridY <= GRIDMAX){
  134.                 if(moving){
  135.                     if(Building.build[gridX][gridY] == null){ //if it is empty
  136.                         Building.build[gridX][gridY] = Building.build[stat_x][stat_y];
  137.                         Building.build[stat_x][stat_y] = null;
  138.                         Building ding = Building.build[gridX][gridY];
  139.                         ding.setGrid(gridX,gridY);
  140.                        
  141.                         updateStats(ding, gridX, gridY);
  142.                         show_stats = true;
  143.                         moving = false;
  144.                     }
  145.                 } //if we're not moving and we click another building
  146.                 else if(Building.build[gridX][gridY] != null){
  147.                     Building ding = Building.build[gridX][gridY];   //clicking to show stats
  148.                    
  149.                     updateStats(ding, gridX, gridY);
  150.                    
  151.                     show_stats = true;
  152.                 }
  153.                
  154.             }
  155.            
  156.            
  157.            
  158.             if(show_stats){
  159.                 if(boxed(x,y,0,375,115,20)){ //UPGRADE BUTTON
  160.                     updateEnergy();
  161.                     if(stat_ding.id==1){            //Metal extractor
  162.                         clickExtractor(stat_ding);
  163.                     } else if (stat_ding.id == 2) { // Booster
  164.                         clickBooster(stat_ding);
  165.                     } else if (stat_ding.id == 3) { // Solar Array
  166.                         clickSolarArray(stat_ding);
  167.                     }
  168.                     updateStats(stat_ding, stat_ding.gridX, stat_ding.gridY);
  169.                 }
  170.                 else if(boxed(x,y,140,375,65,20)){ //MOVE BUTTON
  171.                     moving = true;
  172.                     Building ding = Building.build[stat_x][stat_y];
  173.                     place_id = ding.id;
  174.                     place_sprite_x = (byte) ding.x;
  175.                     place_sprite_y = (byte) ding.y;
  176.                     place_color = ding.getTileColour();
  177.                    
  178.                 }
  179.                
  180.                 else if(boxed(x,y,240,375,70,20)){ //SELL BUTTON
  181.                     if(JOptionPane.showConfirmDialog(cc, "Sell building?")==0){
  182.                         double enCost = stat_ding.getEnergyCost();
  183.                         double enUsed = stat_ding.getEnergyUsed();
  184.                         int lvl = stat_ding.getUpgrade();
  185.                        
  186.                         metalBank += ((enUsed*enCost)*2)*lvl;
  187.                        
  188.                         Building.build[stat_x][stat_y] = null;
  189.                         tick(false); //update the metal bank on screen
  190.                     }
  191.                 }
  192.             }
  193.         }
  194.         else{ //Place buildings
  195.             if(placing){
  196.                 gridX = Math.round(x/32)-12;
  197.                 gridY = Math.round(y/32)-1;
  198.                 int xx = (gridX*2)+24;
  199.                 int yy = (gridY*2)+1;
  200.                 if(gridX >=0 && gridY >= 0 && gridX <=14 && gridY <= 14){
  201.                     if(place_id==1){//Metal Extractor
  202.                         if(Building.build[gridX][gridY] == null){
  203.                             if(level.getTile(xx,yy).getId()==5 && level.getTile(xx+1,yy).getId()==6 &&
  204.                             level.getTile(xx,yy+1).getId()==7 && level.getTile(xx+1,yy+1).getId()==8){
  205.                                 new MetalExtractor(null, 1, place_sprite_x, place_sprite_y,gridX,gridY, place_color);
  206.                                 placing = false;
  207.                                 metalBank -= 10;
  208.                                 updateEnergy();
  209.                         }
  210.                         }
  211.                     }
  212.                     else if(place_id==2){ //Booster
  213.                         if(Building.build[gridX][gridY] == null){
  214.                             if(level.getTile(xx,yy).getId()==2 && level.getTile(xx+1,yy).getId()==2 &&
  215.                             level.getTile(xx,yy+1).getId()==2 && level.getTile(xx+1,yy+1).getId()==2){
  216.                                 new Booster(null, 2, place_sprite_x, place_sprite_y,gridX,gridY, place_color);
  217.                                 placing = false;
  218.                                 metalBank -= 20;
  219.                                 updateEnergy();
  220.                             }
  221.                         }
  222.                     }
  223.                     else if(place_id==3){ //Solar Array
  224.                         if(Building.build[gridX][gridY] == null){
  225.                             if(level.getTile(xx,yy).getId()==2 && level.getTile(xx+1,yy).getId()==2 &&
  226.                             level.getTile(xx,yy+1).getId()==2 && level.getTile(xx+1,yy+1).getId()==2){
  227.                                 new SolarArray(null, 3, place_sprite_x, place_sprite_y,gridX,gridY, place_color);
  228.                                 placing = false;
  229.                                 metalBank -= 8;
  230.                                 updateEnergy();
  231.                         }
  232.                         }
  233.                     }
  234.                     if(can_generate) can_generate = false;
  235.                 }
  236.             }
  237.         }
  238.     }
  239.    
  240.     private void updateStats(Building ding, int gridX, int gridY) {
  241.         float boost;
  242.         stat_ding = ding;
  243.         stat_id = ding.id;
  244.         stat_lvl = ding.getUpgrade();
  245.         stat_x = gridX;
  246.         stat_y = gridY;
  247.         stat_meCost = ding.getMetalCost();
  248.         stat_enCost = ding.getEnergyCost();
  249.         stat_enUsed = ding.getEnergyUsed();
  250.         stat_product = getProduct(ding);
  251.         boost = 1;
  252.         if(stat_x != 14) {boost = getBoost(stat_x+1,stat_y,boost);}
  253.         if(stat_x != 1)  {boost = getBoost(stat_x-1,stat_y,boost);}
  254.         if(stat_y != 14) {boost = getBoost(stat_x,stat_y+1,boost);}
  255.         if(stat_y != 1)  {boost = getBoost(stat_x,stat_y-1,boost);}
  256.         stat_productB = stat_product*boost;
  257.         stat_pboost = boost;
  258.         if(stat_id==3){        //Solar array receives half the benefit of boosters
  259.             stat_productB/=2;
  260.             stat_pboost/=2;
  261.         }
  262.        
  263.     }
  264.  
  265.     private double getProduct(Building ding) {
  266.         double it;
  267.         switch (ding.id){
  268.             default:
  269.             case 1:
  270.                 it = ((MetalExtractor)ding).getMetalGen();
  271.                 break;
  272.             case 2:
  273.                 it = ((Booster)ding).getBoosting();
  274.                 break;
  275.             case 3:
  276.                 it = ((SolarArray)ding).getEnergy();
  277.                 break;
  278.         }
  279.         return it;
  280.     }
  281.  
  282.     private void clickSolarArray(Building ding) {
  283.         double meCost = ((SolarArray) ding).getMetalCost();
  284.         double toBoost;
  285.         if (this.metalBank >= meCost || skip_cost){ //If we have enough metal, or if debug is enabled
  286.             if(!skip_cost){
  287.                 this.metalBank -= meCost;
  288.                 } //Don't apply the cost if debug
  289.              meCost = (meCost+2)*1.28; //Increase the cost for the next upgrade
  290.              toBoost = (((SolarArray) ding).getEnergy()+1)*1.02; //What we will upgrade it by
  291.     ((SolarArray) ding).setEnergy(toBoost);
  292.     ((SolarArray) ding).setMetalCost(meCost);
  293.     ((SolarArray) ding).setUpgrade((((SolarArray) ding).getUpgrade() + 1));
  294.     System.out.println("Upgraded Solar Array to "
  295.             + df.format(((SolarArray) ding).getEnergy())
  296.             + " level: "
  297.             + ((SolarArray) ding).getUpgrade()
  298.             + " metalCost: "
  299.             + df.format(meCost));
  300.     tick(false);
  301.         }
  302.     }
  303.  
  304.     private void clickExtractor(Building ding) {
  305.         double meCost = ((MetalExtractor) ding).getMetalCost();
  306.         double enCost = ((MetalExtractor) ding).getEnergyCost();
  307.         double enUsed = ((MetalExtractor) ding).getEnergyUsed();
  308.         double toGen;
  309.         double enBoth = enCost + enUsed;
  310.         if ((this.metalBank >= meCost && (this.energyUse + enCost <= this.energyMax))|| skip_cost){ //If we have enough metal, or if debug is enabled
  311.             if(!skip_cost){
  312.                 this.metalBank -= meCost;
  313.                 ((MetalExtractor) ding).setEnergyUsed(enBoth);
  314.                 } //Don't apply the cost if debug
  315.              meCost = (meCost+2)*1.21; //Increase the cost for the next upgrade
  316.              enCost = (enCost)*1.075;
  317.              toGen = ((((MetalExtractor) ding).getMetalGen()+0.70)*1.065f); //What we will upgrade it to
  318.     ((MetalExtractor) ding).setMetalGen(toGen);
  319.     ((MetalExtractor) ding).setMetalCost(meCost);
  320.     ((MetalExtractor) ding).setEnergyCost(enCost);
  321.     ((MetalExtractor) ding).setUpgrade((((MetalExtractor) ding).getUpgrade() + 1));
  322.     System.out.println("Upgraded Metal Extractor to "
  323.             + df.format(((MetalExtractor) ding).getMetalGen())
  324.             + " level: "
  325.             + ((MetalExtractor) ding).getUpgrade()
  326.             + " metalCost: "
  327.             + df.format(meCost));
  328.     tick(false);
  329.         }
  330.        
  331.     }
  332.  
  333.     private void clickBooster(Building ding) {
  334.         double meCost = ((Booster) ding).getMetalCost();
  335.         double enCost = ((Booster) ding).getEnergyCost();
  336.         double enUsed = ((Booster) ding).getEnergyUsed();
  337.         float toBoost;
  338.         double enBoth = enCost + enUsed;
  339.         if ((this.metalBank >= meCost && (this.energyUse + enCost <= this.energyMax))|| skip_cost){ //If we have enough metal, or if debug is enabled
  340.             if(!skip_cost){
  341.                 this.metalBank -= meCost;
  342.                 ((Booster) ding).setEnergyUsed(enBoth);
  343.                 } //Don't apply the cost if debug
  344.              meCost = (meCost+3)*1.32; //Increase the cost for the next upgrade
  345.              enCost = (enCost)*1.125;
  346.              toBoost = (float) (((Booster) ding).getBoosting()*1.11);
  347.             ((Booster) ding).setBoosting(toBoost);
  348.             ((Booster) ding).setMetalCost(meCost);
  349.             ((Booster) ding).setEnergyCost(enCost);
  350.             ((Booster) ding).setUpgrade((((Booster) ding).getUpgrade() + 1));
  351.             System.out.println("Upgraded Booster to "
  352.                     + df.format(((Booster) ding).getBoosting()) + " level: "
  353.                     + ((Booster) ding).getUpgrade()
  354.                     + " metalCost: "
  355.                     + df.format(meCost));
  356.            
  357.             tick(false);
  358.         }
  359.        
  360.     }
  361.  
  362.     public void tick(boolean add){ //Update bank every second.
  363.         this.metalPerSecond = 0;
  364.         double income = getIncome();
  365.         this.metalPerSecond += income;
  366.         if(add){
  367.             this.metalBank += this.metalPerSecond;
  368.             this.metalFromBuildings += this.metalPerSecond;
  369.             this.totalTicks++;
  370.             if(this.totalTicks>=300) { //Save game every 5 minutes
  371.                 saveGame();
  372.                 this.totalTicks=0;
  373.             }
  374.         }
  375.        
  376.         updateEnergy();
  377.     }
  378.    
  379.  
  380.  
  381.     private boolean boxed(int x, int y, int xp, int yp, int xl, int yl) {
  382.         if (x < xp || x > (xp+xl))
  383.             return false;
  384.         if (y < yp || y > (yp+yl)) {
  385.             return false;
  386.         } else
  387.             return true;
  388.     }
  389.    
  390.     public float getIncome(){
  391.         float metal=0, boost;
  392.         double exMetal;
  393.         for(int xx=0; xx<15; xx++){
  394.             for(int yy=0; yy<15; yy++){
  395.                 if(Building.build[xx][yy]!=null){
  396.                     Building ding = Building.build[xx][yy];
  397.                     if(ding.id==1){
  398.                         exMetal = ((MetalExtractor) ding).metalGen;
  399.                         boost = 1;
  400.                         if(xx != 14) {boost = getBoost(xx+1,yy,boost);}
  401.                         if(xx != 1)  {boost = getBoost(xx-1,yy,boost);}
  402.                         if(yy != 14) {boost = getBoost(xx,yy+1,boost);}
  403.                         if(yy != 1)  {boost = getBoost(xx,yy-1,boost);}
  404.                         exMetal *= boost;
  405.                         metal+=exMetal;
  406.                         //System.out.println("boosting by "+ boost);
  407.                     }
  408.                 }
  409.             }
  410.         }
  411.         //System.out.println("it is by "+ metal);
  412.         return metal;
  413.     }
  414.    
  415.     public void updateEnergy(){
  416.         float boost;
  417.         double enUse=0, exEnergy=0, energy=0;
  418.         for(int xx=0; xx<15; xx++){
  419.             for(int yy=0; yy<15; yy++){
  420.                 if(Building.build[xx][yy]!=null){
  421.                     Building ding = Building.build[xx][yy];
  422.                     if(ding.id==1) enUse+= ((MetalExtractor)ding).getEnergyUsed();
  423.                     if(ding.id==2) enUse+= ((Booster)ding).getEnergyUsed();
  424.                     if(ding.id==3){ //Solar Array
  425.                         exEnergy=((SolarArray) ding).getEnergy();
  426.                         boost = 1;
  427.                         if(xx != 14) {boost = getBoost(xx+1,yy,boost);}
  428.                         if(xx != 1)  {boost = getBoost(xx-1,yy,boost);}
  429.                         if(yy != 14) {boost = getBoost(xx,yy+1,boost);}
  430.                         if(yy != 1)  {boost = getBoost(xx,yy-1,boost);}
  431.                         if(boost!=0){
  432.                             boost/=2; //Solar arrays gain half the benefit of boosters.
  433.                             exEnergy *= boost;
  434.                         }
  435.                         energy+=exEnergy;
  436.                     }
  437.                 }
  438.             }
  439.         }
  440.         this.energyMax = energy;
  441.         this.energyUse = enUse;
  442.     }
  443.    
  444.    
  445.     private static float getBoost(int xx, int yy, float boost) {
  446.         if(xx >=0 && yy >= 0 && xx <=14 && yy <= 14){
  447.             if(Building.build[xx][yy]!=null){
  448.                 Building ding2 = Building.build[xx][yy];
  449.                 if(ding2.id==2){
  450.                     boost+=((Booster) ding2).getBoosting();
  451.                 }
  452.             }
  453.         }
  454.         return boost;
  455.     }
  456.    
  457.    
  458.     public void buyExtractor(){
  459.         //location 17,13
  460.         if(metalBank>=10 && (energyUse + 2 <= energyMax)){
  461.             placing = true;
  462.             place_id = 1;
  463.             place_sprite_x=17;
  464.             place_sprite_y=13;
  465.             place_color = Colours.get(000, 222, 444, 333);
  466.         }
  467.     }
  468.    
  469.     public void buyBooster(){
  470.         //location 17,13
  471.         if(metalBank>=20 && (energyUse + 1 <= energyMax)){
  472.             placing = true;
  473.             place_id = 2;
  474.             place_sprite_x=17;
  475.             place_sprite_y=13;
  476.             place_color = Colours.get(000, 220, 440, 333);
  477.         }
  478.     }
  479.    
  480.     public void buySolarArray(){
  481.         //location 17,13
  482.         if(metalBank>=8){
  483.             placing = true;
  484.             place_id = 3;
  485.             place_sprite_x=17;
  486.             place_sprite_y=17;
  487.             place_color = Colours.get(000, 113, 225, -1);
  488.         }
  489.     }
  490.    
  491.     public void sendGenerate(){
  492.         if(can_generate){
  493.             genLvl.generateLevel(level);
  494.             System.out.println("Generated land");
  495.         }
  496.     }
  497.    
  498.     public void showStats(){
  499.         int xx = Math.round((mouse_x/32))-12;
  500.         int yy = Math.round(mouse_y/32)-1;
  501.         int lvl;
  502.         double meCost, enCost, enUsed;
  503.         if(Building.build[xx][yy] != null){
  504.             Building ding = Building.build[xx][yy];
  505.             if(ding.id==1){
  506.                 double metal=0;
  507.                 float boost;
  508.                 metal = ((MetalExtractor) ding).metalGen;
  509.                 lvl = ((MetalExtractor) ding).getUpgrade();
  510.                 meCost = ((MetalExtractor) ding).getMetalCost();
  511.                 enCost = ((MetalExtractor) ding).getEnergyCost();
  512.                 enUsed = ((MetalExtractor) ding).getEnergyUsed();
  513.                 boost = 1;
  514.                 if(xx != 14) {boost = getBoost(xx+1,yy,boost);}
  515.                 if(xx != 1)  {boost = getBoost(xx-1,yy,boost);}
  516.                 if(yy != 14) {boost = getBoost(xx,yy+1,boost);}
  517.                 if(yy != 1)  {boost = getBoost(xx,yy-1,boost);}
  518.                 metal *= boost;
  519.  
  520.                 JOptionPane.showMessageDialog(cc, "Level: " + lvl
  521.                         + "\nMetal Cost: " + df.format(meCost)
  522.                         + "\nEnergy Cost: " + df.format(enCost)
  523.                         + "\nEnergy Used: " + df.format(enUsed)
  524.                         + "\nProducing metal: " + df.format(metal)
  525.                         + "\nBoost multiplier: " + df.format(boost));
  526.             }
  527.             if(ding.id==2){
  528.                 float boost = ((Booster) ding).boosting;
  529.                 lvl = ((Booster) ding).getUpgrade();
  530.                 meCost = ((Booster) ding).getMetalCost();
  531.                 enCost = ((Booster) ding).getEnergyCost();
  532.                 enUsed = ((Booster) ding).getEnergyUsed();
  533.                
  534.                 JOptionPane.showMessageDialog(cc, "Level: " + lvl
  535.                         + "\nMetal Cost: " + df.format(meCost)
  536.                         + "\nEnergy Cost: " + df.format(enCost)
  537.                         + "\nEnergy Used: " + df.format(enUsed)
  538.                         + "\nBoosting by: " + df.format(1+boost)); 
  539.             }
  540.             if(ding.id==3){
  541.                 double energy;
  542.                 float boost;
  543.                 lvl = ((SolarArray) ding).getUpgrade();
  544.                 meCost = ((SolarArray) ding).getMetalCost();
  545.                 energy = ((SolarArray) ding).energy;
  546.                 boost = 1;
  547.                 if(xx != 14) {boost = getBoost(xx+1,yy,boost);}
  548.                 if(xx != 1)  {boost = getBoost(xx-1,yy,boost);}
  549.                 if(yy != 14) {boost = getBoost(xx,yy+1,boost);}
  550.                 if(yy != 1)  {boost = getBoost(xx,yy-1,boost);}
  551.                 energy *= boost;
  552.                
  553.                 JOptionPane.showMessageDialog(cc, "Level: " + lvl
  554.                         + "\nMetal Cost: " + df.format(meCost)
  555.                         + "\nEnergy Produced: " + df.format(energy));
  556.             }
  557.         }
  558.     }
  559.    
  560.    
  561.     public void saveGame(){
  562.         //level.setImagePath("/levels/savegame.png");
  563.         level.saveLevelToFile();
  564.        
  565.         try{
  566.             //String path = System.getProperty("user.home")+"\\MCsavegame.txt";
  567.              String path = "/savegame.txt";
  568.             File f = new File(path);
  569.            
  570.             if(!f.exists() && !f.isDirectory()) { f.createNewFile(); }
  571.            
  572.             Formatter file = new Formatter(f);
  573.             String line = metalBank +"]";
  574.             for(int xx=0; xx<16; xx++){
  575.                 for(int yy=0; yy<16; yy++){
  576.                     if(Building.build[xx][yy]!=null){
  577.                         Building ding = Building.build[xx][yy];
  578.                         line += "="+ding.id+"-"; //ID of the building
  579.                         line += xx + "-"; //x coordinate on grid
  580.                         line += yy + "-"; //y coordinate on grid
  581.                         line += ding.getUpgrade() + "-"; //level of building
  582.                         line += ding.getMetalCost() + "-"; //cost to upgrade
  583.                         line += ding.getEnergyCost() + "-"; //cost to upgrade in energy
  584.                         line += ding.getEnergyUsed() + "-"; //current energy consumption
  585.                         if(ding.id==1){
  586.                             line += ((MetalExtractor)ding).getMetalGen() + "-";
  587.                         }
  588.                         if(ding.id==2){
  589.                             line += ((Booster)ding).getBoosting() + "-";
  590.                         }
  591.                         if(ding.id==3){
  592.                             line += ((SolarArray)ding).getEnergy() + "-"; //energy production
  593.                         }
  594.                     }
  595.                 }
  596.             }
  597.        
  598.             file.format("%s", line);
  599.             file.close();     //close file
  600.             System.out.println("Saved game");
  601.         }
  602.         catch (Exception e){
  603.             System.out.println("[WARNING] Problem saving the game");
  604.         }
  605.  
  606.     }
  607.    
  608.     public void loadGame(){
  609.         level.loadLevelFromFile();
  610.        
  611.         try{
  612.             //String path = System.getProperty("user.home")+"\\MCsavegame.txt";
  613.              String path = "/savegame.txt";
  614.             Scanner file = new Scanner(new File(path));
  615.             String line = file.next();
  616.            
  617.             String[] bank = line.split("]");
  618.             String[] buildings = bank[1].split("=");
  619.             String[] attributes = new String[8];
  620.             metalBank = Double.parseDouble(bank[0]);
  621.             int x, y, lvl;
  622.             double enCost, enUsed, meCost, product;
  623.             Building ding;
  624.             if(buildings.length>=1) can_generate = false;
  625.             for(int xx=0; xx<16; xx++){
  626.                 for(int yy=0; yy<16; yy++){
  627.                     Building.build[xx][yy] = null;
  628.                 }
  629.             }
  630.             for(int i=1; i<buildings.length; i++){
  631.                 attributes = buildings[i].split("-");
  632.                 x = Integer.parseInt(attributes[1]);          //x on grid
  633.                 y = Integer.parseInt(attributes[2]);          //y on grid
  634.                 lvl = Integer.parseInt(attributes[3]);        //level
  635.                 meCost = Double.parseDouble(attributes[4]);   //Cost of building
  636.                 enCost = Double.parseDouble(attributes[5]);   //Cost in energy of building
  637.                 enUsed = Double.parseDouble(attributes[6]);   //Total energy used
  638.                 product = Double.parseDouble(attributes[7]);  //Either energy, metal or boost multiplier
  639.                 switch (attributes[0]){
  640.                     default:
  641.                     case "1":
  642.                         ding = new MetalExtractor(null, 1, 17, 13 ,x ,y ,Colours.get(000, 222, 444, 333));
  643.                         ding.setUpgrade(lvl);
  644.                         ding.setMetalCost(meCost);
  645.                         ding.setEnergyCost(enCost);
  646.                         ding.setEnergyUsed(enUsed);
  647.                         ((MetalExtractor) ding).setMetalGen(product);
  648.                         break;
  649.                     case "2":
  650.                         ding = new Booster(null, 2, 17, 13,x ,y ,Colours.get(000, 220, 440, 333));
  651.                         ding.setUpgrade(lvl);
  652.                         ding.setMetalCost(meCost);
  653.                         ding.setEnergyCost(enCost);
  654.                         ding.setEnergyUsed(enUsed);
  655.                         ((Booster) ding).setBoosting((float) product);
  656.                         break;
  657.                     case "3":
  658.                         ding = new SolarArray(null, 3, 17, 17,x ,y ,Colours.get(000, 113, 225, -1));
  659.                         ding.setUpgrade(lvl);
  660.                         ding.setMetalCost(meCost);
  661.                         ding.setEnergyCost(enCost);
  662.                         ding.setEnergyUsed(enUsed);
  663.                         ((SolarArray) ding).setEnergy(product);
  664.                         break;
  665.                 }
  666.             }
  667.            
  668.             file.close(); //close file
  669.         }
  670.         catch (Exception e){
  671.             System.out.println("[WARNING] Could not load the game.");
  672.         }
  673.     }
  674.    
  675.     public String makeString(Double make){
  676.         if(make>=1000000000) {
  677.             return df.format(make/1000000000)+"b"; }
  678.         else if(make>=1000000) {
  679.                 return df.format(make/1000000)+"m"; }
  680.             else if(make>=1000) {
  681.                 return df.format(make/1000)+"k"; }
  682.         return df.format(make);
  683.     }
  684.    
  685.     public void render(Screen screen){
  686.         //System.out.println("test");
  687.         for(int xx=0; xx<16; xx++){
  688.             for(int yy=0; yy<16; yy++){
  689.                 if(Building.build[xx][yy]!=null){
  690.                     Building ding = Building.build[xx][yy];
  691.                     ding.render(screen);
  692.                 }
  693.             }
  694.         }
  695.         int gridX, gridY;
  696.         if(placing || moving){
  697.             if(mouse_x >= 388 && mouse_y >=34
  698.             && mouse_x <=863 && mouse_y <=498){
  699.                 gridX = (Math.round(mouse_x/32)*16);
  700.                 gridY = Math.round(mouse_y/32)*16-8;
  701.                 screen.render(gridX, gridY, place_sprite_x + place_sprite_y * 32, place_color, 0x00, 1);
  702.                 screen.render(gridX+8, gridY, (place_sprite_x+1) + place_sprite_y * 32, place_color, 0x00, 1);
  703.                 screen.render(gridX, gridY+8, place_sprite_x + (place_sprite_y+1) * 32, place_color, 0x00, 1);
  704.                 screen.render(gridX+8, gridY+8, (place_sprite_x+1) + (place_sprite_y+1) * 32, place_color, 0x00, 1);
  705.             }
  706.         }
  707.        
  708.        
  709.         Font.render("Max Energy: " + (makeString(this.energyMax)), screen, 1, 0, Colours.get(-1, -1, -1, 103), 1);
  710.         Font.render("Energy Used: " + (makeString(this.energyUse)), screen, 1, 10, Colours.get(-1, -1, -1, 103), 1);
  711.        
  712.        
  713.         Font.render("Hotkeys:  metal energy", screen, 1, 40,  Colours.get(-1, -1, -1, 050), 1);
  714.         Font.render("1 Extractor - 10m 2en", screen, 1, 50,  Colours.get(-1, -1, -1, 050), 1);
  715.         Font.render("2 Booster - 20m 1en", screen, 1, 60,  Colours.get(-1, -1, -1, 050), 1);
  716.         Font.render("3 Solar Array - 8m 0en", screen, 1, 70,  Colours.get(-1, -1, -1, 050), 1);
  717.        
  718.         if(show_stats){
  719.             Font.render("Level:        " + stat_lvl, screen, 1, 100, Colours.get(-1, -1, -1, 053), 1);
  720.             Font.render("Metal Cost:   " + (makeString(stat_meCost)), screen, 1, 110, Colours.get(-1, -1, -1, 053), 1);
  721.             if(stat_id!=3)Font.render("Energy Cost:  " + (makeString(stat_enCost)), screen, 1, 120, Colours.get(-1, -1, -1, 053), 1);
  722.             if(stat_id!=3)Font.render("Energy Usage: " + (makeString(stat_enUsed)), screen, 1, 130, Colours.get(-1, -1, -1, 053), 1);
  723.             String duct = "error";
  724.             if(stat_id==1) duct = "Metal:        "; if(stat_id==2) duct = "Boosts by:    "; if(stat_id==3) duct = "Energy:       ";
  725.             Font.render(duct + (makeString(stat_product)), screen, 1, 145, Colours.get(-1, -1, -1, 053), 1);
  726.             if(stat_id!=2)Font.render("Boosted to:   " + (makeString(stat_productB)), screen, 1, 155, Colours.get(-1, -1, -1, 053), 1);
  727.             if(stat_id!=2)Font.render("Boostiplier:  " + (makeString((double) stat_pboost)), screen, 1, 165, Colours.get(-1, -1, -1, 053), 1);
  728.            
  729.  
  730.             Font.render("Upgrade", screen, 1, 185, Colours.get(-1, -1, -1, 024), 1);
  731.             Font.render("Move", screen, 70, 185, Colours.get(-1, -1, -1, 024), 1);
  732.             Font.render("Sell", screen, 120, 185, Colours.get(-1, -1, -1, 024), 1);
  733.            
  734.             //Draw the orange selected border cursor
  735.             gridX = (stat_x+12)*16;
  736.             gridY = (stat_y+1)*16-8;
  737.            
  738.             screen.render(gridX, gridY, 20 + 14 * 32, Colours.get(550, -1, -1, -1), 0x03, 1);
  739.             screen.render(gridX+8, gridY, 19 + 14 * 32, Colours.get(530, -1, -1, -1), 0x03, 1);
  740.             screen.render(gridX, gridY+8, 20 + 13 * 32, Colours.get(530, -1, -1, -1), 0x03, 1);
  741.             screen.render(gridX+8, gridY+8, 19 + 13 * 32, Colours.get(550, -1, -1, -1), 0x03, 1);
  742.         }
  743.        
  744.        
  745.        
  746.         Font.render("Click here for metal", screen, 1, 226, Colours.get(-1, -1, -1, 300), 1);
  747.         Font.render("Metal:       " + (makeString(this.metalBank)), screen, 1, 236, Colours.get(-1, -1, -1, 300), 1);
  748.         Font.render("MetalPerSec: " + makeString((double) this.metalPerSecond), screen, 1, 246, Colours.get(-1, -1, -1, 300), 1);
  749.        
  750.         if(can_generate)Font.render("G to generate land", screen, 1, 30,  Colours.get(-1, -1, -1, 050), 1);
  751.        
  752.         if(mouse_x >= 388 && mouse_y >=34
  753.         && mouse_x <=863 && mouse_y <=498){ //Draw the red border cursor
  754.             gridX = (Math.round(mouse_x/32)*16);
  755.             gridY = Math.round(mouse_y/32)*16-8;
  756.            
  757.             screen.render(gridX, gridY, 19 + 13 * 32, Colours.get(500, -1, -1, -1), 0x00, 1);
  758.             screen.render(gridX+8, gridY, (19+1) + 13 * 32, Colours.get(500, -1, -1, -1), 0x00, 1);
  759.             screen.render(gridX, gridY+8, 19 + (13+1) * 32, Colours.get(500, -1, -1, -1), 0x00, 1);
  760.             screen.render(gridX+8, gridY+8, (19+1) + (13+1) * 32, Colours.get(500, -1, -1, -1), 0x00, 1);
  761.         }
  762.  
  763.        
  764.        
  765.         Font.render("v0.09", screen, 400, 248, Colours.get(-1, -1, -1, 103), 1);
  766.        
  767.     }
  768.    
  769.     public void init(Screen screen, Level level){
  770.         this.screen = screen;
  771.         this.level = level;
  772.         String path = "/savegame.txt";//System.getProperty("user.home")+"\\MCsavegame.txt";
  773.         if(new File(path).exists()){
  774.             loadGame();
  775.         }else{
  776.             sendGenerate();
  777.         }
  778.     }
  779.    
  780.     @Override
  781.     public void mouseClicked(MouseEvent e) {
  782.  
  783.     }
  784.    
  785.     @Override
  786.     public void mouseEntered(MouseEvent e) {
  787.        
  788.     }
  789.  
  790.     @Override
  791.     public void mouseExited(MouseEvent e) {
  792.        
  793.     }
  794.  
  795.  
  796.     @Override
  797.     public void mouseReleased(MouseEvent e) {
  798.        
  799.     }
  800.  
  801.     @Override
  802.     public void mouseDragged(MouseEvent arg0) {
  803.        
  804.     }
  805.  
  806.     @Override
  807.     public void mouseMoved(MouseEvent arg0) {
  808.         mouse_x = arg0.getX();
  809.         mouse_y = arg0.getY();
  810.     }
  811.  
  812.     public Container getCc() {
  813.         return cc;
  814.     }
  815.  
  816. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement