Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import greenfoot.*;
- import javax.swing.JOptionPane;
- public class CFish extends CMotherFish
- {
- protected int Shift;
- protected int DefShift = Shift;
- protected boolean RotationRight;
- protected String KeysText;
- protected String Up;
- protected String Left;
- protected String Down;
- protected String Right;
- protected String Name;
- private int SpeedTimer;
- private int ParalysisTimer;
- protected boolean EatingGood = true;
- private boolean MoveIsGood = true;
- //konstruktor bez urcovania klaves
- public CFish(String name, int size)
- {
- super(size);
- this.Name = name;
- Initialize();
- }
- //konstruktor s urcovanim klaves
- public CFish(String name, int size, String up,String left, String down, String right)
- {
- super(size);
- this.Up = up;
- this.Left = left;
- this.Down = down;
- this.Right = right;
- this.Name = name;
- Initialize();
- }
- //inicializacia konstruktora
- public void Initialize()
- {
- this.getImage().scale(75, 50);
- this.Shift = 5;
- this.RotationRight = true;
- this.SpeedTimer = 0;
- this.ParalysisTimer = 0;
- this.Accelerate();
- }
- public void ConfigureKeys()
- {
- KeysText = "Zadaj klávesu, ktorou budeš chcieť ovládať rybičku smerom";
- this.Up = JOptionPane.showInputDialog(KeysText + " HORE");//"W";
- this.Left = JOptionPane.showInputDialog(KeysText + " do ĽAVA");//"A";
- this.Down = JOptionPane.showInputDialog(KeysText + " DOLE");//"S";
- this.Right = JOptionPane.showInputDialog(KeysText + " do PRAVA");//"D";
- }
- public void act()
- {
- MoveAround();
- UpdateSpeedTimer();
- UpdateParalysisTimer();
- ControlTrashColision();
- ControlFishColision();
- }
- public void TunrOffEating()
- {
- this.EatingGood = false;
- }
- public void TurnOnEating()
- {
- this.EatingGood = true;
- }
- public void MoveAround()
- {
- if(MoveIsGood)
- {
- if(Greenfoot.isKeyDown(this.Up)) //up
- {
- this.setLocation(this.getX(), this.getY() - Shift);
- }
- if(Greenfoot.isKeyDown(this.Left)) //left
- {
- if (this.RotationRight == true)
- {
- this.getImage().mirrorHorizontally();
- this.RotationRight = false;
- }
- this.setLocation(this.getX() - Shift, this.getY());
- }
- if(Greenfoot.isKeyDown(this.Down)) //down
- {
- this.setLocation(this.getX(), this.getY() + Shift);
- }
- if(Greenfoot.isKeyDown(this.Right)) //right
- {
- if (this.RotationRight == false)
- {
- this.getImage().mirrorHorizontally();
- this.RotationRight = true;
- }
- this.setLocation(this.getX()+ Shift, this.getY());
- }
- }
- }
- public void ControlEat()
- {
- CFood p = (CFood)this.getOneIntersectingObject(CFood.class);
- if (p != null && this.EatingGood)
- {
- this.Eat(p);
- }
- }
- public void Eat(CFood p)
- {
- if (this.EatingGood)
- {
- this.Size += p.Size;
- this.getWorld().removeObject(p);
- int Width = this.getImage().getWidth();
- int Height = this.getImage().getWidth();
- this.setImage("Rybka.png");
- this.getImage().scale(Width + Size, Height + Size);
- //this.getImage().mirrorHorizontally();
- }
- }
- public void Reset()
- {
- this.setLocation(10, 10); // povodne suradnice ryby
- //metoda sa vola t triede CShark
- }
- public void Accelerate()
- {
- if (this.Shift < 28) //shift casom meni svoju hodnotu a ak bude mensi ako tak
- {
- this.Shift += 3; //sa zvacsi o 3 (ryba zrychli o 3 jednotky rychlosti)
- this.SpeedTimer = 10;
- }
- }
- public void Decelerate()
- {
- if (this.Shift > 7)
- {
- this.Shift -= 3; // zmensi sa o 3
- }
- }
- public void UpdateSpeedTimer()
- {
- if (this.SpeedTimer > 0) //ak ma SpeedTimer nejaku hodnotu
- {
- this.SpeedTimer--; //zacne ju znizovat
- if (this.SpeedTimer == 0)
- {
- this.Decelerate(); //zance spomalovat ryba
- }
- }
- }
- //rovnaky princip ako SpeedTimer
- public void UpdateParalysisTimer()
- {
- if (this.ParalysisTimer > 0)
- {
- this.ParalysisTimer--;
- if (this.ParalysisTimer == 0)
- {
- this.MoveIsGood = true;
- }
- }
- }
- public void ControlTrashColision()
- {
- //ukladanie do premennej Trash typu CTrash
- //nadobuda hodnotu objektu s ktorou ryba momentalne interaguje
- CTrash Trash = (CTrash)this.getOneIntersectingObject(CTrash.class);
- if (Trash != null) //ak Trash nadobudol nejaku hodnotu
- {
- Trash.ApplyOnFish(this); //aplikacia na rybu
- //Trash moze byt CApple/CFries/CMacroPlast
- //kazdy z tychto typov ma iny proces metody ApplyOnFish
- }
- }
- //rovnaky princip ako ControlTrashColision, len sa jedna o ryby
- public void ControlFishColision()
- {
- CInterFish p = (CInterFish)this.getOneIntersectingObject(CInterFish.class);
- if (p != null)
- {
- p.InteractWithFish(this);
- }
- }
- //Properties pre natstavovanie hodnot premennych
- public void SetMove(boolean status)
- {
- this.MoveIsGood = status;
- }
- public void SetSpeedTimer(int value)
- {
- this.SpeedTimer = value;
- }
- public void SetParalysisTimer(int value)
- {
- this.ParalysisTimer = value;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement