Advertisement
Guest User

Untitled

a guest
Oct 25th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.08 KB | None | 0 0
  1. public class Reactor extends AbstractActor implements Switchable{
  2.  
  3.     private int temperature;
  4.     private int damage;
  5.  
  6.     private Animation normalAnimation;
  7.     private Animation hotAnimation;
  8.     private Animation brokenAnimation;
  9.     private Animation offAnimation;
  10.     private boolean isRunning = false;
  11.     private Light light;
  12.     private String manufacturer;
  13.     private int yearOfProduction;
  14.     private  List<EnergyConsumer> devices;
  15.     private EnergyConsumer zariadenie;
  16.  
  17.     public Reactor() {
  18.         this("TUKE", 1995);
  19.     }
  20.  
  21.     public Reactor(String manufacturer, int yearOfProduction) {
  22.         temperature = 0;
  23.         damage = 0;
  24.  
  25.         offAnimation = new Animation("images/reactor.png",80,80,100);
  26.  
  27.         hotAnimation = new Animation ("images/reactor_hot.png", 80, 80, 100);
  28.         hotAnimation.setPingPong(true);
  29.  
  30.         normalAnimation = new Animation ("images/reactor_on.png", 80, 80, 100);
  31.         normalAnimation.setPingPong(true);
  32.  
  33.         brokenAnimation = new Animation ("images/reactor_broken.png",80,80,100);
  34.         brokenAnimation.setPingPong(true);
  35.  
  36.         setAnimation(offAnimation);
  37.  
  38.         this.manufacturer = manufacturer;
  39.         this.yearOfProduction = yearOfProduction;
  40.        
  41.         this.devices = new ArrayList<EnergyConsumer>();
  42.         System.out.println(yearOfProduction);
  43.         System.out.println(manufacturer);
  44.     }
  45.  
  46.      
  47.     public int getTemperature(){
  48.         return temperature;
  49.     }
  50.  
  51.     public int getDamage(){
  52.         return damage;
  53.     }
  54.    
  55.     public String setManufacturer() {
  56.         return this.manufacturer;
  57.     }
  58.  
  59.     public int setyearOfProduction() {
  60.         return this.yearOfProduction;
  61.     }
  62.  
  63.      public boolean isServiceNeeded(){
  64.         if(temperature > 3000 && damage > 50){
  65.             return true;
  66.         }
  67.         else return false;
  68.     }
  69.  
  70.  
  71.     public void increaseTemperature(int increment) {
  72.         if (increment < 0){
  73.             return;
  74.         }
  75.         if (increment >= 0 && isRunning) {
  76.             if (damage < 33) {
  77.                 temperature = temperature + increment;
  78.             } else if (damage >= 33 && damage < 66) {
  79.                 temperature = temperature + (increment *= 1.5);
  80.             } else if (damage >= 66) {
  81.                 temperature = temperature + (increment * 2);
  82.             }
  83.  
  84.             if (temperature >= 2000) {
  85.                 int d = (int) ((temperature - 2000) * 100 / 4000);
  86.                 damage = d > damage ? d : damage;
  87.                 if (damage > 100) {
  88.                     isRunning = false;
  89.                     damage = 100;
  90.                 }  
  91.                
  92.                   if (this.temperature >= 5000) {
  93.                   this.temperature = 5000;
  94.                   this.damage = 100;
  95.                  }
  96.             }
  97.             updateAnimation();
  98.         }      
  99.     }
  100.  
  101.  
  102.     public void decreaseTemperature(int decrement) {
  103.         if (decrement < 0){
  104.             return;
  105.         }
  106.        if(temperature > 0) {
  107.            if (damage != 100 && isRunning) {
  108.                if (damage >= 50) {
  109.                    temperature = temperature - (decrement *= 0.5);
  110.                } else temperature -= decrement;
  111.                updateAnimation();
  112.            }
  113.        }
  114.     }
  115.  
  116.  
  117.     private void updateAnimation(){
  118.         if(isRunning && temperature < 4000){
  119.             setAnimation(normalAnimation);
  120.         }
  121.  
  122.         if (isRunning && temperature >= 4000){
  123.             setAnimation(hotAnimation);
  124.         }
  125.  
  126.         if(damage == 100 ) {
  127.             setAnimation(brokenAnimation);
  128.         }
  129.  
  130.         if(!isRunning)
  131.             setAnimation(offAnimation);
  132.     }
  133.  
  134.  
  135.     public void repairWith(AbstractTool hammer){
  136.         if(hammer.getPossibleUses() != 0){
  137.             if(hammer != null && damage > 0 && damage < 100){
  138.                 if(damage >= 50){
  139.                     damage -= 50;
  140.                     temperature = 40 * damage + 2000;
  141.                 } else {
  142.                     int d = damage - 50;
  143.                     int t = 40 * d + 2000;
  144.                     temperature = t < temperature ? t : temperature;
  145.                     damage = 0;
  146.                 }
  147.                 hammer.use();
  148.                 updateAnimation();
  149.             }
  150.         }
  151.     }
  152.     public void turnOn(){
  153.         this.isRunning = false;
  154.         if(zariadenie != null){
  155.             zariadenie.setElectricityFlow(false);
  156.         }
  157.     }
  158.  
  159.     public void turnOff() {
  160.        
  161.         this.isRunning = true;
  162.         if(zariadenie != null){
  163.             zariadenie.setElectricityFlow(true);
  164.         }
  165.        
  166.     }
  167.  
  168.  
  169.     public boolean isOn(){
  170.  
  171.         return isRunning;
  172.     }
  173.  
  174.  
  175.     public void addLight(Light light){
  176.         if (light != null){
  177.             this.light = light;
  178.             if( this.damage < 100 && isOn() )
  179.                 this.light.setElectricityFlow(true);
  180.             else
  181.                 this.light.setElectricityFlow(false);
  182.  
  183.         }
  184.     }
  185.  
  186.     public void removeLight(){
  187.         this.light.setElectricityFlow(false);
  188.         this.light = null;
  189.     }
  190.  
  191.     public void act(){
  192.         if(damage != 100  && isRunning == true ){
  193.             temperature = temperature + 1;
  194.         }
  195.         updateAnimation();
  196.         increaseTemperature(0);
  197.     }
  198.  
  199.     public void extinguishWith(AbstractTool tool) {
  200.         if (this.damage == 100 && tool instanceof FireExtinguisher) {
  201.             temperature=4000;
  202.             tool.use();
  203.             updateAnimation();
  204.         }
  205.     }
  206.  
  207.  
  208.     public void addDevice(EnergyConsumer zariadenie) {
  209.  
  210.         this.devices.add(zariadenie);
  211.         this.zariadenie = zariadenie;
  212.  
  213.  
  214.         if (this.running == true) {
  215.             for (int i = 0; i < this.devices.size(); i++) {
  216.                 this.devices.get(i).setElectricityFlow(true);
  217.             }
  218.         }
  219.  
  220.     }
  221.  
  222.     public void removeDevice(EnergyConsumer zariadenie) {
  223.         if (devices.contains(zariadenie)) {
  224.             zariadenie.setElectricityFlow(false);
  225.             devices.remove(zariadenie);
  226.         }
  227.     }
  228. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement