Package bunny

Source Code of bunny.Bunny

package bunny;

import game_timer.GameTimer;
import game_timer.TimerNotify;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import route.Route;

public class Bunny implements TimerNotify{
  private static Bunny instance = null;
  public static Bunny get(){
    return instance;
  }
  public static Bunny get(List<Route> routes){
    if(instance == null){
      instance = new Bunny(routes);
    }
   
    return instance;
  }
 
  //pozicios adatok
  private List<Route> routes;
  private Route route;
  private int position;
 
  //az ujboli athelyezeshez szukseges adatok
  private int immuneTime = 30;
  private int immuneCounter = 0;
  private int respawnLimit = 500;
  private int respawnCounter = 0;
  private boolean isHit = false;
 
  public int getImmuneTime() {
    return immuneTime;
  }
 
  public void setImmuneTime(int immuneTime) {
    this.immuneTime = immuneTime;
  }
 
  private Bunny(){
    GameTimer.getInstance().registObserver(this);
    generateCreate();
  }

  private Bunny(List<Route> routes) {
    this.routes = new ArrayList<Route>(routes);
    GameTimer.getInstance().registObserver(this);
    generateCreate();
  }
  @Override
  public void onTick() {
    if(isHit){
      if(immuneCounter < immuneTime){
        immuneCounter++;
      } else {
        generateCreate();
        isHit = false;
        immuneCounter = 0;
      }
    } else {
      if(respawnCounter == respawnLimit){
        generateCreate();
        respawnCounter = 0;
      } else {
        respawnCounter++;
      }
    }
  }
 
  /**
   * A nyulat egy bizonyos Route, bizonyos poziciojara pakolja.
   */
  private void generateCreate(){
    //ha mar volt valahol a nyul, akkor onnan eltavolitjuk
    if(route != null)
      route.removeBunny();
   
    //uj veletlen utat-poziciot generalunk
    Random generator = new Random();
    double rand = generator.nextDouble();
   
    double routesSize = routes.size();
   
    //select a random route
    int selectedRoute = (int)(routesSize * rand);
    Route route = routes.get(selectedRoute);
   
    //select a random position
    double length = route.getLength();
    int randomPos = (int)(length * generator.nextDouble());
   
    //beallitjuk az uj ertekeket
    this.route = route;
    this.position = randomPos;
   
    route.setBunny(this);
  }
 
  public void setBunnyPlace(Route r, int pos){
    //eloszor,ha volt regi route, onnan vegyuk le.
    if(route != null)
      route.removeBunny();
     
    //allitsuk be az uj helyet
    route = r;
    position = pos;
   
    route.setBunny(this);
 
 
  public Route getRoute(){
    return route;
  }
 
  public int getPos(){
    return position;
  }
 
  /**
   * Ha elutottek a nyulat, akkor ezzel a fuggvennyel lehet jelezni a nyulnak
   */
  public void setHit(){
    this.isHit = true;
  }
  public boolean getHit(){
    return isHit;
  }
  public Integer getRespawnTime() {
    return respawnLimit;
  }
}
TOP

Related Classes of bunny.Bunny

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.