-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathController.java
More file actions
66 lines (50 loc) · 1.52 KB
/
Controller.java
File metadata and controls
66 lines (50 loc) · 1.52 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
package controller;
import interfaces.WorldViewer;
import myclasses.Avatar;
import myclasses.Command;
import myclasses.World;
/**
* A controller for the World application.
*
* @author Brian Mukeswe
* @contact b.mukeswe@sms.ed.ac.uk
*
*/
public class Controller {
private Avatar avatar; // An avatar to perform actions on behalf of the user
private WorldViewer viewer; // A viewer to interact with the user interface
/**
* Constructor for the controller
*
* @param viewer the class that receives commands from the user and
* shows the current state of the avatar.
*/
public Controller(WorldViewer viewer) {
// connect the viewer to this controller
this.viewer = viewer;
init();
}
/**
* Initialise the controller
*/
private void init() {
// Create an avatar
avatar = new Avatar(World.getStartLocation());
// Initialise the viewer
viewer.setController(this);
viewer.Initialise(avatar.getCurrentLocation().getView(avatar.getCurrentDirection()),
avatar.getCartItems(),
avatar.getCurrentLocation().getContents());
}
/**
* Process a user command
*/
public void select(Command command) {
// Delegate processing of the command
avatar.perform(command);
// Refresh the viewer with the new state
viewer.refresh(avatar.getCurrentLocation().getView(avatar.getCurrentDirection()),
avatar.getCartItems(),
avatar.getCurrentLocation().getContents());
}
}