-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObstacle.java
More file actions
108 lines (96 loc) · 3.17 KB
/
Obstacle.java
File metadata and controls
108 lines (96 loc) · 3.17 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
import java.awt.Color;
import java.awt.Graphics;
import java.io.BufferedWriter;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import org.xml.sax.Attributes;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
public class Obstacle extends Agent {
/**
* Class information
*/
/** XML element tag */
static final String XML_NAME = "rock";
/** Record that allows XML files to set obstacle defaults */
static FixedAgentAttributes defaultFixedAgentAttributes =
new Agent.FixedAgentAttributes(10, 0, 0, 0, 0, 0, 0, HALF_CIRCLE, Color.BLACK, false, false);
/** Record that allows XML files to set obstacle defaults */
static DynamicAgentAttributes defaultDynamicAgentAttributes =
new Agent.DynamicAgentAttributes(250, 250, 0, 0);
/**
* Constructor: initialize general agent fields to describe
* agent that does not move.
*
* @param w world to which agent belongs
* @param id number to identify agent in its world
* @param atts SAX attributes corresponding to XML agent spec
* @param loc file information for error messages
* @throws SAXException if data is formatted incorrectly
*/
public Obstacle(World w, int id, Attributes atts, Locator loc)
throws SAXException {
myWorld = w;
this.id = id;
form = new FixedAgentAttributes(atts, defaultFixedAgentAttributes, loc);
status = new DynamicAgentAttributes(atts, defaultDynamicAgentAttributes, loc);
}
/**
* Output an XML element describing the current state of
* this obstacle.
*
* @param out an open file to write to, wrapped in BufferedWriter
* convenience class
*/
@Override
public void log(BufferedWriter out)
throws IOException {
out.write(" <" + XML_NAME + " " + ID_PARAM + OPEN + Integer.toString(id) + CLOSE +
"\n ");
form.log(out);
out.write(" ");
status.log(out);
out.write(" />\n");
}
/**
* Draw the obstacle as a filled square
*
* @param g object to control drawing mechanism
*/
@Override
public void draw(Graphics g) {
g.setColor(form.color);
myWorld.fillRect((int) Math.round(status.locX - form.size / 2),
(int) Math.round(status.locY - form.size / 2),
form.size, form.size, g);
}
/**
* describe the obstacle as other agents see it
*
* @return how the obstacle appears: as an obstacle
*/
@Override
public Percept.ObjectCategory looksLike() {
return Percept.ObjectCategory.OBSTACLE;
}
/**
* obstacles get in other agents' way
*
* @param appearance of agent that runs into obstacle
* @return constant OBSTRUCT indicating collision stops others
*/
@Override
public InteractiveBehavior behaviorOnApproach(Percept.ObjectCategory neighbor) {
return InteractiveBehavior.OBSTRUCT;
}
/**
* Never do anything
*
* @param ps what the light source would "see"
*/
@Override
public void deliberate(List<Percept> ps) {
todo = new LinkedList<Intention>();
}
}