Advertisement
Filipinex

CFish

Mar 19th, 2024
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.32 KB | None | 0 0
  1. import greenfoot.*;
  2. import javax.swing.JOptionPane;
  3.  
  4. public class CFish extends CMotherFish
  5. {
  6.     protected int Shift;
  7.     protected int DefShift = Shift;
  8.    
  9.     protected boolean RotationRight;
  10.     protected String KeysText;
  11.    
  12.     protected String Up;
  13.     protected String Left;
  14.     protected String Down;
  15.     protected String Right;
  16.    
  17.     protected String Name;
  18.    
  19.     private int SpeedTimer;
  20.     private int ParalysisTimer;
  21.    
  22.     protected boolean EatingGood = true;
  23.    
  24.     private boolean MoveIsGood = true;
  25.    
  26.     //konstruktor bez urcovania klaves
  27.     public CFish(String name, int size)
  28.     {  
  29.         super(size);
  30.         this.Name = name;
  31.        
  32.         Initialize();
  33.     }
  34.    
  35.     //konstruktor s urcovanim klaves
  36.     public CFish(String name, int size, String up,String left, String down, String right)
  37.     {    
  38.         super(size);
  39.         this.Up = up;
  40.         this.Left = left;
  41.         this.Down = down;
  42.         this.Right = right;
  43.        
  44.         this.Name = name;
  45.        
  46.         Initialize();
  47.     }
  48.    
  49.     //inicializacia konstruktora
  50.     public void Initialize()
  51.     {
  52.         this.getImage().scale(75, 50);
  53.         this.Shift = 5;
  54.         this.RotationRight = true;  
  55.         this.SpeedTimer = 0;
  56.         this.ParalysisTimer = 0;
  57.         this.Accelerate();
  58.     }
  59.    
  60.     public void ConfigureKeys()
  61.     {
  62.         KeysText = "Zadaj klávesu, ktorou budeš chcieť ovládať rybičku smerom";
  63.         this.Up = JOptionPane.showInputDialog(KeysText + " HORE");//"W";
  64.         this.Left = JOptionPane.showInputDialog(KeysText + " do ĽAVA");//"A";
  65.         this.Down = JOptionPane.showInputDialog(KeysText + " DOLE");//"S";
  66.         this.Right = JOptionPane.showInputDialog(KeysText + " do PRAVA");//"D";
  67.     }
  68.    
  69.     public void act()
  70.     {
  71.         MoveAround();
  72.        
  73.         UpdateSpeedTimer();
  74.         UpdateParalysisTimer();
  75.        
  76.         ControlTrashColision();
  77.         ControlFishColision();
  78.     }
  79.    
  80.     public void TunrOffEating()
  81.     {
  82.         this.EatingGood = false;
  83.     }
  84.    
  85.     public void TurnOnEating()
  86.     {
  87.         this.EatingGood = true;
  88.     }
  89.    
  90.     public void MoveAround()
  91.     {
  92.         if(MoveIsGood)
  93.         {
  94.             if(Greenfoot.isKeyDown(this.Up)) //up
  95.             {
  96.                 this.setLocation(this.getX(), this.getY() - Shift);
  97.             }
  98.        
  99.             if(Greenfoot.isKeyDown(this.Left)) //left
  100.             {
  101.                 if (this.RotationRight == true)
  102.                 {
  103.                     this.getImage().mirrorHorizontally();
  104.                     this.RotationRight = false;
  105.                 }
  106.                 this.setLocation(this.getX() - Shift, this.getY());
  107.             }
  108.        
  109.             if(Greenfoot.isKeyDown(this.Down)) //down
  110.             {
  111.                 this.setLocation(this.getX(), this.getY() + Shift);
  112.             }
  113.        
  114.             if(Greenfoot.isKeyDown(this.Right)) //right
  115.             {
  116.                 if (this.RotationRight == false)
  117.                 {
  118.                     this.getImage().mirrorHorizontally();
  119.                     this.RotationRight = true;
  120.                 }
  121.                 this.setLocation(this.getX()+ Shift, this.getY());
  122.             }
  123.         }
  124.     }
  125.    
  126.     public void ControlEat()
  127.     {
  128.         CFood p = (CFood)this.getOneIntersectingObject(CFood.class);
  129.         if (p != null && this.EatingGood)
  130.         {
  131.             this.Eat(p);    
  132.         }
  133.     }
  134.  
  135.    
  136.     public void Eat(CFood p)
  137.     {
  138.         if (this.EatingGood)
  139.         {
  140.             this.Size += p.Size;
  141.             this.getWorld().removeObject(p);
  142.              
  143.             int Width = this.getImage().getWidth();
  144.             int Height = this.getImage().getWidth();
  145.            
  146.             this.setImage("Rybka.png");
  147.             this.getImage().scale(Width + Size, Height + Size);  
  148.             //this.getImage().mirrorHorizontally();
  149.            
  150.        
  151.         }
  152.     }
  153.    
  154.     public void Reset()
  155.     {
  156.         this.setLocation(10, 10); // povodne suradnice ryby
  157.         //metoda sa vola t triede CShark
  158.     }
  159.    
  160.     public void Accelerate()
  161.     {
  162.         if (this.Shift < 28) //shift casom meni svoju hodnotu a ak bude mensi ako  tak
  163.         {
  164.             this.Shift += 3; //sa zvacsi o 3 (ryba zrychli o 3 jednotky rychlosti)
  165.             this.SpeedTimer = 10;
  166.         }
  167.     }
  168.    
  169.     public void Decelerate()
  170.     {
  171.         if (this.Shift > 7)
  172.         {
  173.             this.Shift -= 3; // zmensi sa o 3
  174.         }
  175.     }
  176.    
  177.     public void UpdateSpeedTimer()
  178.     {
  179.         if (this.SpeedTimer > 0) //ak ma SpeedTimer nejaku hodnotu
  180.         {
  181.             this.SpeedTimer--; //zacne ju znizovat
  182.            
  183.             if (this.SpeedTimer == 0)
  184.             {
  185.                 this.Decelerate(); //zance spomalovat ryba
  186.             }
  187.         }
  188.     }
  189.    
  190.     //rovnaky princip ako SpeedTimer
  191.     public void UpdateParalysisTimer()
  192.     {
  193.         if (this.ParalysisTimer > 0)
  194.         {
  195.             this.ParalysisTimer--;
  196.            
  197.             if (this.ParalysisTimer == 0)
  198.             {
  199.                 this.MoveIsGood = true;
  200.             }
  201.         }
  202.     }
  203.    
  204.     public void ControlTrashColision()
  205.     {
  206.         //ukladanie do premennej Trash typu CTrash
  207.         //nadobuda hodnotu objektu s ktorou ryba momentalne interaguje
  208.         CTrash Trash = (CTrash)this.getOneIntersectingObject(CTrash.class);
  209.         if (Trash != null) //ak Trash nadobudol nejaku hodnotu
  210.         {
  211.             Trash.ApplyOnFish(this); //aplikacia na rybu
  212.             //Trash moze byt CApple/CFries/CMacroPlast
  213.             //kazdy z tychto typov ma iny proces metody ApplyOnFish
  214.         }
  215.     }
  216.    
  217.     //rovnaky princip ako ControlTrashColision, len sa jedna o ryby
  218.     public void ControlFishColision()
  219.     {
  220.         CInterFish p = (CInterFish)this.getOneIntersectingObject(CInterFish.class);
  221.         if (p != null)
  222.         {
  223.             p.InteractWithFish(this);
  224.         }
  225.     }
  226.    
  227.     //Properties pre natstavovanie hodnot premennych
  228.     public void SetMove(boolean status)
  229.     {
  230.          this.MoveIsGood = status;
  231.     }
  232.    
  233.     public void SetSpeedTimer(int value)
  234.     {
  235.         this.SpeedTimer = value;
  236.     }
  237.    
  238.     public void SetParalysisTimer(int value)
  239.     {
  240.         this.ParalysisTimer = value;
  241.     }
  242. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement