1 package se
.umu
.cs
.dit06ajnajs
;
4 import java
.awt
.Graphics
;
5 import java
.awt
.event
.MouseAdapter
;
6 import java
.awt
.event
.MouseEvent
;
9 import java
.util
.logging
.Logger
;
11 import se
.umu
.cs
.dit06ajnajs
.agent
.Agent
;
12 import se
.umu
.cs
.dit06ajnajs
.agent
.BasicTower
;
13 import se
.umu
.cs
.dit06ajnajs
.agent
.Direction
;
14 import se
.umu
.cs
.dit06ajnajs
.agent
.FootmanUnit
;
15 import se
.umu
.cs
.dit06ajnajs
.map
.Map
;
16 import se
.umu
.cs
.dit06ajnajs
.map
.MapSquare
;
18 public class ATDController
{
19 private static Logger logger
= Logger
.getLogger("AntiTD");
21 private final int FRAMES_PER_SECOND
= 20;
23 private ATDModel model
;
26 private boolean running
;
27 private Thread animationThread
;
29 public ATDController(List
<Map
> maps
) {
30 // Create model and view
31 model
= new ATDModel(maps
);
32 view
= new ATDView(model
);
34 model
.addAgent(new FootmanUnit(100,200, 20, 20, 1, Direction
.UP
));
35 model
.addAgent(new FootmanUnit(140,200, 20, 20, 2, Direction
.UP
));
36 model
.addAgent(new FootmanUnit(-20,50, 20, 20, 1, Direction
.RIGHT
));
37 model
.addAgent(new FootmanUnit(-20,100, 20, 20, 4, Direction
.RIGHT
));
38 model
.addAgent(new FootmanUnit(-20,150, 20, 20, 2, Direction
.RIGHT
));
40 model
.addTower(new BasicTower(20, 20, 10, 1));
44 animationThread
= new Thread(new AnimationThread());
45 animationThread
.start();
48 public void initGame() {
49 view
.addMapListener(new MapListener());
53 private class AnimationThread
implements Runnable
{
55 // TODO Auto-generated method stub
61 List
<Agent
> agents
= model
.getAgents();
62 for (Agent agent
: agents
) {
67 Graphics g
= view
.getGameGraphics();
68 for (Agent agent
: agents
) {
69 ((Paintable
) agent
).paint(g
);
72 // Refresh the game view
75 // Try to keep a given number of frames per second.
77 Thread
.sleep(1000 / FRAMES_PER_SECOND
);
78 } catch (InterruptedException e
) {
79 System
.err
.println("Error in thread, exiting.");
86 private class MapListener
extends MouseAdapter
{
87 public void mouseClicked(MouseEvent me
) {
90 Map map
= model
.getMap();
91 MapSquare square
= map
.getMapSquareAtPoint(x
, y
);
92 logger
.info("Mouse clicked @ (" + x
+ ", " + y
+ ")");
93 logger
.info("MapSquare @ " + square
);