Android Open Source - Marble-Run Time Based Score






From Project

Back to project page Marble-Run.

License

The source code is released under:

Apache License

If you think the Android project Marble-Run listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package nttu.edu.hud;
//www  . j  av  a2 s . c o m
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import nttu.edu.graphics.RenderView;
import android.app.Activity;
import android.content.res.AssetManager;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;

public class TimeBasedScore implements Runnable {
  private final List<Time> defaultTimes;
  private Time time;
  private Thread timerThread;
  private final Paint paint;
  private boolean hasBeenLoaded;
  private boolean threadRunning;
  
  public String countdown;
  
  private class Time implements Comparable<Time> {
    public int stageID;
    public int minutes;
    public int seconds;
    private boolean running;
    
    public Time(int i, int m, int s) {
      stageID = i;
      minutes = m;
      seconds = s;
    }
    
    public long totalSeconds() {
      return (minutes * 60 + seconds);
    }
    
    public void tick() {
      if (running) {
        seconds--;
        if (seconds < 0) {
          seconds = 59;
          minutes--;
          if (minutes < 0) {
            minutes = 0;
            seconds = 0;
          }
        }
      }
      return;
    }
    
    public void pause() {
      running = false;
    }
    
    public void unpause() {
      running = true;
    }
    
    public int compareTo(Time t) {
      if (this.stageID > t.stageID)
        return -1;
      if (this.stageID < t.stageID)
        return 1;
      return 0;
    }
    
    @Override
    public String toString() {
      String m = minutes > 9 ? String.valueOf(minutes) : "0" + String.valueOf(minutes);
      String s = seconds > 9 ? String.valueOf(seconds) : "0" + String.valueOf(seconds);
      return "Remaining: " + m + ":" + s;
    }
    
    @Override
    public Time clone() {
      Time t = defaultTimes.get(stageID - 1);
      return new Time(t.stageID, t.minutes, t.seconds);
    }
  }
  
  private TimeBasedScore() {
    defaultTimes = new ArrayList<Time>();
    countdown = "Remaining: ";
    threadRunning = false;
    timerThread = new Thread(this);
    hasBeenLoaded = false;
    time = null;
    paint = new Paint();
    paint.setColor(Color.YELLOW);
    paint.setTypeface(Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD));
    paint.setTextSize(20f);
  }
  
  public static TimeBasedScore loadTimers(Activity a, String filename, TimeBasedScore temp) {
    if (temp != null && temp.hasBeenLoaded)
      return temp;
    TimeBasedScore tbs = new TimeBasedScore();
    AssetManager assets = a.getAssets();
    BufferedReader reader = null;
    try {
      BufferedInputStream input = new BufferedInputStream(assets.open(filename));
      reader = new BufferedReader(new InputStreamReader(input));
      String line;
      while ((line = reader.readLine()) != null) {
        String[] parts = line.split(" ");
        int value = Integer.parseInt(parts[0]);
        int minutes = Integer.parseInt(parts[1].split(":")[0]);
        int seconds = Integer.parseInt(parts[1].split(":")[1]);
        final Time t = tbs.new Time(value, minutes, seconds);
        tbs.defaultTimes.add(t);
      }
      Arrays.sort(tbs.defaultTimes.toArray());
    }
    catch (NumberFormatException e) {
      e.printStackTrace();
    }
    catch (IOException e) {
      e.printStackTrace();
    }
    finally {
      try {
        reader.close();
      }
      catch (IOException e) {
        e.printStackTrace();
      }
    }
    tbs.hasBeenLoaded = true;
    return tbs;
  }
  
  public void clearTimers() {
    while (defaultTimes.size() > 0)
      defaultTimes.remove(0);
  }
  
  public void execute() {
    if (!threadRunning) {
      if (timerThread != null) {
        timerThread.start();
        threadRunning = true;
      }
    }
  }
  
  public void run() {
    if (time != null) {
      while (time.totalSeconds() > 0) {
        try {
          Thread.sleep(1000);
        }
        catch (InterruptedException e) {
          break;
        }
        if (Thread.currentThread().isInterrupted())
          break;
        time.tick();
        countdown = time.toString();
      }
      countdown = time.toString();
    }
  }
  
  public void pauseTimer() {
    if (time != null)
      time.pause();
  }
  
  public void unpauseTimer() {
    if (time != null)
      time.unpause();
  }
  
  public void render(Canvas c) {
    if (countdown.length() > 0)
      c.drawText(countdown, 10 * RenderView.AspectRatio, (paint.getTextSize() * 4) * RenderView.AspectRatio, paint);
  }
  
  //Don't check for (time == null). Intended.
  public void resetAndLoad(int stageNumber) {
    boolean check = false;
    hasBeenLoaded = false;
    for (Time t : defaultTimes) {
      if (t.stageID == stageNumber) {
        time = t.clone();
        check = true;
        break;
      }
      else
        check = false;
    }
    checkingDefaultTimes(check);
  }
  
  private void checkingDefaultTimes(boolean check) {
    if (check) {
      countdown = time.toString();
      if (timerThread != null) {
        timerThread.interrupt();
        timerThread = null;
      }
      timerThread = new Thread(this);
      threadRunning = false;
      hasBeenLoaded = true;
    }
    else {
      if (timerThread != null) {
        timerThread.interrupt();
        timerThread = null;
      }
      time = null;
      countdown = "No timer.";
    }
  }
  
  public void reset() {
    boolean check = false;
    hasBeenLoaded = false;
    for (Time t : defaultTimes) {
      if (time == null)
        break;
      if (t.stageID == time.stageID) {
        time = t.clone();
        check = true;
        break;
      }
      else
        check = false;
    }
    checkingDefaultTimes(check);
  }
  
  public boolean isLoaded() {
    return hasBeenLoaded;
  }
  
  public long getScore() {
    if (time != null)
      return time.totalSeconds() * 10;
    return 0;
  }
}




Java Source Code List

nttu.edu.activities.LevelSelectionActivity.java
nttu.edu.activities.MenuActivity.java
nttu.edu.activities.NewLoadingActivity.java
nttu.edu.activities.PlayActivity.java
nttu.edu.activities.ScoreActivity.java
nttu.edu.activities.SettingsActivity.java
nttu.edu.alt.NewBall.java
nttu.edu.alt.NewCue.java
nttu.edu.alt.NewHole.java
nttu.edu.alt.Obstacle.java
nttu.edu.ball.Ball.java
nttu.edu.ball.Cue.java
nttu.edu.ball.Marble.java
nttu.edu.entity.Border.java
nttu.edu.entity.Bumper.java
nttu.edu.entity.Coin.java
nttu.edu.entity.Connector.java
nttu.edu.entity.CurvePipe.java
nttu.edu.entity.Entity.java
nttu.edu.entity.Funnel.java
nttu.edu.entity.Hole.java
nttu.edu.entity.Path.java
nttu.edu.entity.Pipe.java
nttu.edu.entity.Ramp.java
nttu.edu.entity.ShortFunnel.java
nttu.edu.entity.Tee.java
nttu.edu.entity.Terrain.java
nttu.edu.entity.Void.java
nttu.edu.graphics.Art.java
nttu.edu.graphics.RenderView.java
nttu.edu.handler.Accelero.java
nttu.edu.handler.ImageInfo.java
nttu.edu.handler.Info.java
nttu.edu.handler.Loading.java
nttu.edu.handler.ModPlayer.java
nttu.edu.handler.MusicHandler.java
nttu.edu.hud.BestScore.java
nttu.edu.hud.Compass.java
nttu.edu.hud.GoalCompass.java
nttu.edu.hud.HUDMenu.java
nttu.edu.hud.HUDScore.java
nttu.edu.hud.MarbleCompass.java
nttu.edu.hud.TimeBasedScore.java
nttu.edu.level.HUD.java
nttu.edu.level.Stage.java
nttu.edu.score.Format.java
nttu.edu.score.Score.java
nttu.edu.sound.Sound.java