Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.46 KB | None | 0 0
  1. package controllers.player;
  2.  
  3. import controllers.input.InputType;
  4. import controllers.main.GameController;
  5. import javafx.fxml.FXMLLoader;
  6. import javafx.scene.Node;
  7. import javafx.scene.layout.AnchorPane;
  8. import models.players.Player;
  9. import models.players.PlayerFactory;
  10.  
  11. import java.io.File;
  12. import java.io.IOException;
  13. import java.net.MalformedURLException;
  14. import java.net.URL;
  15. import java.util.HashMap;
  16. import java.util.Map;
  17.  
  18. public class PlayerController {
  19.     private Map<String, Node> players;
  20.  
  21.     public PlayerController() {
  22.         players = new HashMap<>();
  23.     }
  24.  
  25.     public Node createPlayer(String path, String playerName, InputType inputType) throws IOException {
  26.         URL url = new File(path).toURI().toURL();
  27.         Node player = FXMLLoader.load(url);
  28.         players.put(playerName, player);
  29.         PlayerFactory.getFactory().registerPlayer(playerName).setInputType(inputType);
  30.         PlayerFactory.getFactory().getPlayer(playerName).setSpeed(1); // 5 for primary joystick as it's too fast
  31.                                                                         // 20 is default
  32.         return player;
  33.     }
  34.  
  35.     public void moveLeft(String playerName) {
  36.         double playerWidth = ((AnchorPane) players.get(playerName)).getWidth();
  37.         double maxDistance = GameController.getInstance().getStageWidth() - playerWidth;
  38.  
  39.         double transition = players.get(playerName).getLayoutX()
  40.                 - PlayerFactory.getFactory().getPlayer(playerName).getSpeed();
  41.         double newX = Math.max(0, Math.min(transition, maxDistance));
  42.         players.get(playerName).setLayoutX(newX);
  43.     }
  44.  
  45.     public void moveRight(String playerName) {
  46.         double playerWidth = ((AnchorPane) players.get(playerName)).getWidth();
  47.         double maxDistance = GameController.getInstance().getStageWidth() - playerWidth;
  48.  
  49.         double transition = players.get(playerName).getLayoutX()
  50.                 + PlayerFactory.getFactory().getPlayer(playerName).getSpeed();
  51.         double newX = Math.max(0,
  52.                 Math.min(transition, maxDistance));
  53.         players.get(playerName).setLayoutX(newX);
  54.     }
  55.  
  56.     public Node createStick(String path) throws IOException {
  57.         URL url = new File(path).toURI().toURL();
  58.         Node plate = FXMLLoader.load(url);
  59.  
  60.         return plate;
  61.     }
  62.  
  63.     public void bindStickWithPlayer(Node player, Node stick) {
  64. //        stick.layoutXProperty().bindBidirectional(player.layoutXProperty());
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement