-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMonster.java
More file actions
116 lines (106 loc) · 3.39 KB
/
Monster.java
File metadata and controls
116 lines (106 loc) · 3.39 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
109
110
111
112
113
114
115
116
import java.awt.*;
import java.util.*;
/** Contains information about a Monster */
public class Monster extends Unit {
String type;
int playerRow;
int playerCol;
Monster(int level, int row, int col, Map map, Info info) {
super(level, row, col, map, info);
playerRow = -1;
playerCol = -1;
//Choose a monster of the correct level
Vector<String> possible = info.listOf("monster");
Vector<String> goodones = new Vector<String>();
for(String m : possible) {
int lolev = Integer.parseInt(info.stats.get(m).get("lolev"));
int hilev = Integer.parseInt(info.stats.get(m).get("hilev"));
if(lolev <= level && level <= hilev)
goodones.add(m);
}
type = goodones.get(Game.rand(0, goodones.size()));
//Now choose a weapon and some armor for this guy
inv.addItem(new Item(info, "weapon", info.tags.get(type)));
//Add some armor
for(int i = 0; i < 4; i++) {
Item armor = new Item(info, "armor", info.tags.get(type));
if(!inv.slotUse(armor.getSlot()))
inv.addItem(armor);
}
}
//The monster's turn has come, it should perform an action (like moving or attacking)
public void takeTurn() {
//Update the location of the player if we can see it
if(getMap().sight(getRow(), getCol(), getMap().getPlayer().getRow(), getMap().getPlayer().getCol())) {
playerRow = getMap().getPlayer().getRow();
playerCol = getMap().getPlayer().getCol();
//Check whether in range for an attack
if(Math.max(Math.abs(playerRow - getRow()), Math.abs(playerCol - getCol())) <= getRange()) {
attack(getMap().getPlayer());
return;
}
}
//Move towards the player or don't move at all
if(playerRow != -1) {
int rowChange = 0;
int colChange = 0;
if(playerRow > getRow()) rowChange = 1;
if(playerRow < getRow()) rowChange = -1;
if(playerCol > getCol()) colChange = 1;
if(playerCol < getCol()) colChange = -1;
int oldRow = getRow();
int oldCol = getCol();
move(rowChange, colChange);
if(getRow() == oldRow && getCol() == oldCol) {
move(rowChange, 0);
if(getRow() == oldRow && getCol() == oldCol) {
move(0, colChange);
if(getRow() == oldRow && getCol() == oldCol) {
setWaiting(true);
}
}
}
} else {
setWaiting(true);
}
}
public String getName() {
return type;
}
/** Returns the character that should be used to represent the player */
public char getChar() {
return info.stats.get(type).get("disp").charAt(0);
}
/** Returns the character colour that should be used to represent the player */
public CharCol getCharCol() {
String c = info.stats.get(type).get("disp").substring(1);
Color cc = Color.WHITE;
if(c.equals("blue"))
cc = Color.BLUE;
else if(c.equals("cyan"))
cc = Color.CYAN;
else if(c.equals("green"))
cc = Color.GREEN;
else if(c.equals("magenta"))
cc = Color.MAGENTA;
else if(c.equals("orange"))
cc = Color.ORANGE;
else if(c.equals("pink"))
cc = Color.PINK;
else if(c.equals("red"))
cc = Color.RED;
else if(c.equals("white"))
cc = Color.WHITE;
else if(c.equals("yellow"))
cc = Color.YELLOW;
return new CharCol(cc);
}
}
class Pos {
int row;
int col;
Pos(int row, int col) {
this.row = row;
this.col = col;
}
}