Advertisement
omegazero

LolTimer - Timers.java

Dec 4th, 2011
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.98 KB | None | 0 0
  1. package lol.jacklyon3;
  2.  
  3. import java.util.Timer;
  4. import java.util.TimerTask;
  5.  
  6. /**
  7.  * @author jacklyon3
  8.  * Handles timing for the display and update rate.
  9.  */
  10. public class Timers {
  11.     public static final int BLUE = 0, RED = 1, TOP = 2, MID = 3, BOT = 4;
  12.     private Display d;
  13.     private Timer primary;
  14.     private int gameTime;
  15.     private int dragon, baron, yours[], enemys[];
  16.     //ALL TIMING IS IN SECONDS
  17.     //6 mins for dragon, 7 mins for baron, 5 mins for red and blue, 5 mins for inhib
  18.     private int dragon_respawn = 6*60, baron_respawn = 7*60, blue_respawn = 5*60, red_respawn = 5*60, inhib_respawn = 5*60;
  19.     private int dragon_spawn = 2*60+30, baron_spawn = 15*60, red_spawn = 60+55, blue_spawn = 60+55;
  20.    
  21.     private boolean gamePaused;
  22.     //this needs to see the display because the timer ticks will call it's label update
  23.     public Timers(Display d) {
  24.         this.d = d;
  25.         this.dragon = 0;
  26.         this.baron = 0;
  27.         this.yours = new int[5];
  28.         this.enemys = new int[5];
  29.         this.gamePaused = false;
  30.         this.gameTime = 0;
  31.     }
  32.    
  33.     public void togglePaused() {
  34.         this.gamePaused = !gamePaused;
  35.     }
  36.    
  37.     public void startGameTimer() {
  38.         //set all params first to avoid problems
  39.         //with arbitrary interleaving and concurrency
  40.         //before the actual timertask
  41.         this.gameTime = 0;
  42.         this.gamePaused = false;
  43.         dragon = dragon_spawn;
  44.         baron = baron_spawn;
  45.         yours[BLUE] = blue_spawn;
  46.         enemys[BLUE] = blue_spawn;
  47.         yours[RED] = red_spawn;
  48.         enemys[RED] = red_spawn;
  49.        
  50.         yours[TOP] = -1;
  51.         yours[MID] = -1;
  52.         yours[BOT] = -1;
  53.        
  54.         enemys[TOP] = -1;
  55.         enemys[MID] = -1;
  56.         enemys[BOT] = -1;
  57.         //update every second
  58.         this.primary = new Timer();
  59.         this.primary.scheduleAtFixedRate(new TimerTask(){
  60.             public void run() {
  61.                 updateTime();
  62.             }
  63.         }, 0, 1000);
  64.        
  65.     }
  66.    
  67.     public void resetGameTimer() {
  68.         this.primary.cancel();
  69.         this.gameTime = -1;
  70.         this.gamePaused = false;
  71.     }
  72.    
  73.     public void updateTime() {
  74.         if ( !gamePaused ) {
  75.             this.gameTime++;
  76.             dragon--;
  77.             baron--;
  78.             for ( int i = 0; i < yours.length; i++ ) {
  79.                 yours[i]--;
  80.                 enemys[i]--;
  81.             }
  82.             d.updateLabels();
  83.         }
  84.     }
  85.    
  86.     public int gameTime() {
  87.         return this.gameTime;
  88.     }
  89.    
  90.     public int getDragon() {
  91.         return this.dragon;
  92.     }
  93.    
  94.     public int getBaron() {
  95.         return this.baron;
  96.     }
  97.    
  98.     public int yourTime(int timer) {
  99.         return yours[timer];
  100.     }
  101.    
  102.     public int enemyTime(int timer) {
  103.         return enemys[timer];
  104.     }
  105.    
  106.     public void startBaron() {
  107.         this.baron = baron_respawn;
  108.     }
  109.    
  110.     public void startDragon() {
  111.         this.dragon = dragon_respawn;
  112.     }
  113.    
  114.     public void startYour(int timer) {
  115.         if (timer == BLUE ) {
  116.             this.yours[timer] = blue_respawn;
  117.         }
  118.         else if ( timer == RED ) {
  119.             this.yours[timer] = red_respawn;
  120.         }
  121.         else {
  122.             this.yours[timer] = inhib_respawn;
  123.         }
  124.     }
  125.    
  126.     public void startEnemy(int timer) {
  127.         if (timer == BLUE ) {
  128.             this.enemys[timer] = blue_respawn;
  129.         }
  130.         else if ( timer == RED ) {
  131.             this.enemys[timer] = red_respawn;
  132.         }
  133.         else {
  134.             this.enemys[timer] = inhib_respawn;
  135.         }
  136.     }
  137.    
  138. }
  139.  
  140.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement