001import java.util.HashMap; 002import java.util.Stack; 003import java.util.Set; 004import javax.swing.Timer; 005import java.awt.event.ActionListener; 006import java.awt.event.ActionEvent; 007 008public class Player { 009 private Room aCurrentRoom; 010 private ItemList aItems; 011 private double aBalance; 012 private Stack<Room> aPrevRooms; 013 private String aName; 014 015 public final static int ONE_SECOND = 1000; 016 public final static int MAX_TIME = 600; 017 public static int TIME = 0; 018 public static Timer MY_TIMER; 019 020 public Player(final Room pRoom, final GameEngine pGameEngine) { 021 this.aCurrentRoom = pRoom; 022 this.aItems = new ItemList(); 023 this.aBalance = 1000; 024 this.aPrevRooms = new Stack<Room>(); 025 this.aName = "Laylow"; 026 // GAME_ENGINE=pGameEngine; 027 this.startTimer(pGameEngine); 028 } 029 030 /** 031 * Démarre le timer du jeu (compte le nombre de secondes depuis le lancement). 032 * 033 * @param GAME_ENGINE 034 */ 035 public void startTimer(final GameEngine GAME_ENGINE) { 036 ActionListener taskPerformer = new ActionListener() { 037 public void actionPerformed(ActionEvent evt) { 038 TIME++; 039 if (TIME == MAX_TIME) { 040 GAME_ENGINE.interpretCommand("quit"); 041 MY_TIMER.stop(); 042 } 043 } 044 }; 045 MY_TIMER = new Timer(ONE_SECOND, taskPerformer); 046 MY_TIMER.start(); 047 } 048 049 // Getters & setters 050 public double getBalance() { 051 return this.aBalance; 052 } 053 054 public void setBalance(final int aBalance) { 055 this.aBalance = aBalance; 056 } 057 058 public Room getCurrentRoom() { 059 return this.aCurrentRoom; 060 } 061 062 public void setCurrentRoom(final Room pRoom) { 063 this.aCurrentRoom = pRoom; 064 } 065 066 public HashMap<String, Item> getItems() { 067 return this.aItems.getItems(); 068 } 069 070 public Stack<Room> getPrevRooms() { 071 return this.aPrevRooms; 072 } 073 074 public void setPrevRooms(final Stack<Room> pRooms) { 075 this.aPrevRooms = pRooms; 076 } 077 078 public String getName() { 079 return this.aName; 080 } 081 082 /** 083 * Change de salle et modifie la pile des rooms 084 * 085 * @param pNextRoom salle où se déplacer 086 */ 087 public void changeRoom(Room pNextRoom) { 088 this.aPrevRooms.push(this.aCurrentRoom); 089 this.aCurrentRoom = pNextRoom; 090 } 091 092 public void goBack() { 093 this.aCurrentRoom = this.aPrevRooms.pop(); 094 } 095 096 /** 097 * Pour prendre une item dans une salle 098 * 099 * @param pName l'item en question 100 * @return Informations sur le résultat (échec de la prise, ...) 101 */ 102 public String takeItem(final String pName) { 103 Item vItem = this.aCurrentRoom.getItems().get(pName); 104 if (vItem != null) { 105 if (this.aBalance >= vItem.getPrice()) { 106 this.aCurrentRoom.getItems().remove(pName); 107 this.aItems.getItems().put(pName, vItem); 108 this.aBalance -= this.aItems.getPrice(pName); 109 return "You've just taked the " + pName; 110 } else { 111 return "You don't have enough money. You're poor."; 112 } 113 } else { 114 return "There are no item named " + pName + "in " + this.aCurrentRoom; 115 } 116 } 117 118 /** 119 * Pour déposer un item dans une salle. 120 * 121 * @param pName l'item en question 122 * @return Informations sur le résultat (échec du dépot, ...) 123 */ 124 public String dropItem(final String pName) { 125 Item vItem = this.aItems.getItems().get(pName); 126 if (vItem != null) { 127 this.aItems.removeItem(pName); 128 this.aCurrentRoom.getItems().put(pName, vItem); 129 return vItem.getName() + " dropped."; 130 } else { 131 return "You don't have an item named " + pName + "on you."; 132 } 133 } 134 135 public String getItemsString() { 136 return this.aItems.getItemsString(); 137 } 138 139 /** 140 * Pour afficher l'inventaire 141 * 142 * @return string contenant tous les items dans votre inventaire. 143 */ 144 public String lookItems() { 145 String vItems = this.getItemsString(); 146 if (vItems.equals("")) { 147 return "You don't have items yet"; 148 } else { 149 return "Your items are :" + vItems; 150 } 151 } 152 153 public String getBalanceString() { 154 return "You have : " + this.aBalance + "$ in your wallet."; 155 } 156 157 /** 158 * Fonction pour manger le gâteau magique 159 * 160 * @param pCommand commande eat item 161 * @return chaine de caractère nous informant de si le gâteau a été manger ou 162 * non... 163 */ 164 public String eatCake(final Command pCommand) { 165 if (!pCommand.hasSecondWord()) { 166 return "Eat what ?"; 167 } 168 Set<String> allKeys = this.aItems.getItems().keySet(); 169 for (String vKey : allKeys) { 170 if (pCommand.getSecondWord().equals(vKey) && !vKey.equals("cake")) { 171 return "You can't it a " + vKey + "."; 172 } 173 } 174 if (pCommand.getSecondWord().equals("cake")) { 175 Item vCake = this.aItems.getItem("cake"); 176 if (vCake == null) { 177 return "You don't have a cake in your inventory."; 178 } else { 179 this.aBalance *= 1.20; 180 return "You eated a magic cake ! It increases your wallet balance by 20%"; 181 } 182 } 183 return "Eat what ?"; 184 } 185}