1 package se
.umu
.cs
.dit06ajnajs
;
3 import java
.awt
.Graphics
;
4 import java
.awt
.event
.MouseAdapter
;
5 import java
.awt
.event
.MouseEvent
;
7 import java
.util
.logging
.Logger
;
9 import se
.umu
.cs
.dit06ajnajs
.agent
.Agent
;
10 import se
.umu
.cs
.dit06ajnajs
.agent
.BasicTower
;
11 import se
.umu
.cs
.dit06ajnajs
.agent
.Direction
;
12 import se
.umu
.cs
.dit06ajnajs
.agent
.FootmanUnit
;
14 public class ATDController
{
15 private static Logger logger
= Logger
.getLogger("AntiTD");
17 private final int FRAMES_PER_SECOND
= 20;
19 private ATDModel model
;
22 private boolean running
;
23 private Thread animationThread
;
25 public ATDController() {
26 // Create model and view
27 model
= new ATDModel();
28 view
= new ATDView(model
);
30 model
.addAgent(new FootmanUnit(100,200, 20, 20, 1, Direction
.UP
));
31 model
.addAgent(new FootmanUnit(140,200, 20, 20, 2, Direction
.UP
));
32 model
.addAgent(new FootmanUnit(-20,50, 20, 20, 1, Direction
.RIGHT
));
33 model
.addAgent(new FootmanUnit(-20,100, 20, 20, 4, Direction
.RIGHT
));
34 model
.addAgent(new FootmanUnit(-20,150, 20, 20, 2, Direction
.RIGHT
));
36 model
.addTower(new BasicTower(20, 20, 10, 1));
40 animationThread
= new Thread(new AnimationThread());
41 animationThread
.start();
44 public void initGame() {
45 view
.addMapListener(new MapListener());
49 private class AnimationThread
implements Runnable
{
51 // TODO Auto-generated method stub
57 List
<Agent
> agents
= model
.getAgents();
58 for (Agent agent
: agents
) {
63 Graphics g
= view
.getGameGraphics();
64 for (Agent agent
: agents
) {
65 ((Paintable
) agent
).paint(g
);
68 // Refresh the game view
71 // Try to keep a given number of frames per second.
73 Thread
.sleep(1000 / FRAMES_PER_SECOND
);
74 } catch (InterruptedException e
) {
75 System
.err
.println("Error in thread, exiting.");
82 private class MapListener
extends MouseAdapter
{
83 public void mouseClicked(MouseEvent me
) {
86 Map map
= model
.getMap();
87 MapSquare square
= map
.getMapSquareAtPoint(x
, y
);
88 logger
.info("Mouse clicked @ (" + x
+ ", " + y
+ ")");
89 logger
.info("MapSquare @ " + square
);