-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.java
More file actions
108 lines (99 loc) · 3.51 KB
/
Player.java
File metadata and controls
108 lines (99 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/**
* Write a description of class Player here.
*
* @author (your name)
* @version (a version number or a date)
*/
import java.util.*;
public class Player extends Entity
{
public void interact(String cmd){
cmd = cmd.toLowerCase();
if(cmd.equals("move left")) this.move(-1);
else if(cmd.equals("move right")) this.move(1);
else if(cmd.equals("inspect")) {
WorldStrip curStrip = world.world.get(new Integer(getLoc()));
Iterator<Entity> curEntitiesIterator = curStrip.getEntities().iterator();
Entity curEntity=null;
boolean locationIsImportant=false;
if(curStrip.getBlock() instanceof Important && !((Important) curStrip.getBlock()).isHidden()) {
System.out.println("You are standing near a " + ((Important) curStrip.getBlock()).getName());
locationIsImportant = true;
}
while(curEntitiesIterator.hasNext()){
curEntity=curEntitiesIterator.next();
if(!curEntity.isHidden()) {
System.out.println("You are standing near a " + ((Important) curEntity).getName());
locationIsImportant = true;
}
}
if(!locationIsImportant) System.out.println("There is nothing of interest around you");
}
else if(cmd.equals("inventory")){
System.out.println("you have:\n"+Arrays.toString(inventory));
}
}
public InventoryItem[] inventory = new InventoryItem[15];
public int amntGold=0;
public boolean addToInventory(InventoryItem newItem){
int firstEmptySlot=-1;
for(int i=0;i<inventory.length;i++){
InventoryItem curItem=inventory[i];
if(curItem==null){
firstEmptySlot=i;
continue;
}
if(curItem.name()==newItem.name()){
curItem.setHowMany(curItem.getHowMany()+newItem.getHowMany());
return true;
}
}
if(firstEmptySlot>=0){
inventory[firstEmptySlot]=newItem;
return true;
}
return false;
}
public boolean removeFromInventory(InventoryItem item){
for(int i=0;i<inventory.length;i++){
InventoryItem curItem=inventory[i];
if(curItem==null) continue;
if(curItem.name()==item.name()){
if(curItem.getHowMany()==item.getHowMany()){
inventory[i]=null;
return true;
}
if(curItem.getHowMany()>item.getHowMany()){
curItem.setHowMany(curItem.getHowMany()-item.getHowMany());
return true;
}
}
}
return false;
}
public boolean isInteractCmd(String cmd) {
cmd = cmd.toLowerCase();
if (cmd.equals("move left") || cmd.equals("move right") || cmd.equals("inspect") || cmd.equals("inventory")) return true;
return false;
}
public String getName(){
return "Player";
}
public Player(AdventureWorld wrld, int x) {
super(wrld,x);
}
public boolean move(int i) {
world.genWorld(getLoc()+i);
if(super.move(i)) {
System.out.println("You moved " + (i < 0 ? "Left" : "Right"));
return true;
}
else {
System.out.println("You are at the edge of the World");
return false;
}
}
public boolean isHidden() {
return true;
}
}