Skip to content
Snippets Groups Projects
GameController.java 3.16 KiB
Newer Older
Jan Kožusznik's avatar
Jan Kožusznik committed
	package lab;
Jan Kožusznik's avatar
Jan Kožusznik committed

import java.io.IOException;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.Set;

Jan Kožusznik's avatar
Jan Kožusznik committed
import javafx.animation.AnimationTimer;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.fxml.FXML;
Jan Kožusznik's avatar
Jan Kožusznik committed
import javafx.scene.canvas.Canvas;
Jan Kožusznik's avatar
Jan Kožusznik committed
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.Slider;
import javafx.scene.control.TextField;
Jan Kožusznik's avatar
Jan Kožusznik committed

public class GameController {

	private World world;
	
	@FXML
	private Slider angleSlider;
	
Jan Kožusznik's avatar
Jan Kožusznik committed
	@FXML
	private Slider strengthSlider;
	
Jan Kožusznik's avatar
Jan Kožusznik committed
	private Canvas canvas;
Jan Kožusznik's avatar
Jan Kožusznik committed
	@FXML
	private Label shoots;
	
	@FXML
	private Label hits;
	
	@FXML 
	private ListView<Score> scoreList;
	
	@FXML
	private TextField nameTextField;
	
	private List<Score> highScores = new LinkedList<>();
	
Jan Kožusznik's avatar
Jan Kožusznik committed
	private AnimationTimer animationTimer;
	
koz01's avatar
koz01 committed
	private ScoreDAO scoreDAO;
	
	public GameController() {
Jan Kožusznik's avatar
Jan Kožusznik committed
	}
	
	public void startGame() {
koz01's avatar
koz01 committed
		scoreDAO = new ScoreDAO();
		initScoreList();

koz01's avatar
koz01 committed
		this.world = new World(canvas.getWidth(), canvas.getHeight());
		this.world.setGameListener(new GameListenerImpl());
Jan Kožusznik's avatar
Jan Kožusznik committed
		//Draw scene on a separate thread to avoid blocking UI.
Jan Kožusznik's avatar
Jan Kožusznik committed
		animationTimer = new DrawingThread(canvas, world);
		angleSlider.valueProperty().addListener(this::angleChanged);
		world.setCannonAngle(angleSlider.getValue());
Jan Kožusznik's avatar
Jan Kožusznik committed
		
		strengthSlider.valueProperty().addListener(this::strenghtChanged);
		world.setCannonStrength(strengthSlider.getValue());
Jan Kožusznik's avatar
Jan Kožusznik committed
		animationTimer.start();
	}


	public void stopGame() {
		animationTimer.stop();
	}
	
	
	@FXML
	private void firePressed() {
Jan Kožusznik's avatar
Jan Kožusznik committed
		world.fire();
	}
	
	
	private void angleChanged(ObservableValue<? extends Number> observable
								, Number oldValue, Number newValue) {
		world.setCannonAngle(newValue.doubleValue());
	}
Jan Kožusznik's avatar
Jan Kožusznik committed

Jan Kožusznik's avatar
Jan Kožusznik committed
	private void strenghtChanged(ObservableValue<? extends Number> observable
			, Number oldValue, Number newValue) {
		world.setCannonStrength(newValue.doubleValue());
	}
	@FXML
	private void gameOverPressed() {
		Score score = new Score(new Random().nextInt(11), nameTextField.getText());
		scoreList.getItems().add(score);
koz01's avatar
koz01 committed
		sortScores();
	}
	
	@FXML
	private void highScoresPressed() {
		Set<Score> tempScores = new HashSet<>();
		tempScores.addAll(highScores);
		highScores.clear();
		highScores.addAll(tempScores);
		Collections.sort(highScores,  new ScoreComparator().reversed());
		initScoreList();
	}

	@FXML
	private void savePressed() throws IOException {
koz01's avatar
koz01 committed
		scoreDAO.save(highScores);
	}
	
	
	@FXML
	private void loadPressed() throws IOException{
		highScores.clear();
koz01's avatar
koz01 committed
		highScores.addAll(scoreDAO.load());
		sortScores();
		initScoreList();
	}

	private void initScoreList() {
		scoreList.setItems(FXCollections.observableList(highScores));
	}
	
koz01's avatar
koz01 committed
	private void sortScores() {
		Collections.sort(scoreList.getItems(),  new ScoreComparator().reversed());
	}

koz01's avatar
koz01 committed
	private class GameListenerImpl implements GameListener {

		@Override
		public void stateChanged(int shoots, int hits) {
			GameController.this.shoots.setText("" + shoots);
			GameController.this.hits.setText("" + hits);
		}

		@Override
		public void gameOver() {
			stopGame();
		}
		
	}
Jan Kožusznik's avatar
Jan Kožusznik committed
}