7 Copyright (c) 2008, Ryan M. Hope
10 Redistribution and use in source and binary forms, with or without modification,
11 are permitted provided that the following conditions are met:
13 * Redistributions of source code must retain the above copyright notice,
14 this list of conditions and the following disclaimer.
15 * Redistributions in binary form must reproduce the above copyright notice,
16 this list of conditions and the following disclaimer in the documentation
17 and/or other materials provided with the distribution.
18 * Neither the name of the project nor the names of its contributors may be
19 used to endorse or promote products derived from this software without
20 specific prior written permission.
22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
23 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
26 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
29 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 import java
.awt
.font
.TextLayout
;
36 import java
.util
.ArrayList
;
37 import java
.util
.Random
;
39 public class Controller
implements Runnable
{
49 * Create a new controller object for MOTSIM. MOTSIM_Controller does most
50 * of the grunt works for MOTSIM.
52 * @param model MOTSIM_Model
53 * @param view MOTSIM_View
55 public Controller(Model model
, View view
) {
56 rand
= new Random(); // A random number generator
59 /* Generate some data dynamically for testing purposes */
60 tdd
= new TrialDataDynamic(view
.screen_size
);
61 model
.trialThread
= new Thread(this);
62 model
.trialThread
.start();
66 * Update target positions based on X and Y velocities
68 protected synchronized void moveTargets() {
69 int sleep
= 20; // Sleep for 20ms to maintain ~50fps
71 for (int i
=0; i
<model
.targets
.size(); i
++) {
72 Target t
= model
.targets
.get(i
);
75 * If target hits a border flip is angle and velocity so that it
76 * reflects like a mirror
78 if (((t
.x
+ t
.velX
+ t
.bounds
.getWidth()) > view
.screen_size
.width
) ||
79 ((t
.x
+ t
.velX
) < 0)) {
83 if (((t
.y
+ t
.velY
) > view
.screen_size
.height
-view
.trial
.mh
) ||
84 ((t
.y
+ t
.velY
- t
.bounds
.getHeight()) < 0)) {
89 t
.moveAngle
= t
.moveAngle
+ 180;
94 * Add any entropy to the move angle if necessary
96 if (model
.entropy
!= "None") {
97 if (t
.move
== false) {
98 if (model
.entropy
== "Low") {
99 entropy
= tdd
.generateEntropyLow();
100 } else if (model
.entropy
== "Medium") {
101 entropy
= tdd
.generateEntropyMedium();
102 } else if (model
.entropy
== "High") {
103 entropy
= tdd
.generateEntropyHigh();
105 t
.moveAngle
= (t
.moveAngle
+ entropy
[rand
.nextInt(10000)]) % 360;
114 t
.velX
= tdd
.calcAngleMoveX(t
.moveAngle
);
115 t
.velY
= tdd
.calcAngleMoveY(t
.moveAngle
);
117 t
.x
= t
.x
+ t
.velX
* t
.velocityMOD
;
118 t
.y
= t
.y
+ t
.velY
* t
.velocityMOD
;
120 view
.trial
.repaint();
122 model
.duration
= model
.duration
+ sleep
;
124 } catch (InterruptedException e
) {
128 private void generateTargets() {
129 model
.targets
= new ArrayList
<Target
>(tdd
.generate_targets(
130 (Integer
)view
.cp
.targets_spinner
.getValue()));
131 for (int i
=0; i
<model
.targets
.size(); i
++) {
132 Target t
= model
.targets
.get(i
);
133 /* Adjust base speed of targets */
134 t
.velocityMOD
= (Double
)view
.cp
.speed_spinner
.getValue();
135 /* Get bounds of targets */
136 TextLayout tl
= new TextLayout(t
.callsign
, model
.target_font
, view
.trial
.frc
);
137 t
.bounds
= tl
.getBounds();
138 /* Make sure all targets start on screen */
140 max
= view
.screen_size
.width
- (int)Math
.round(t
.bounds
.getWidth());
143 max
= view
.screen_size
.height
- view
.trial
.mh
;
146 if (t
.y
< t
.bounds
.getHeight())
147 t
.y
= t
.bounds
.getHeight() + 1;
159 switch(model
.stage
) {
162 synchronized (model
.trialThread
) {
164 model
.trialThread
.wait();
165 } catch (InterruptedException e1
) {
172 model
.stage
= Model
.TrialStage
.GENERATE_TARGETS
;
174 case GENERATE_TARGETS
:
176 model
.stage
= Model
.TrialStage
.MOVE_TARGETS
;
179 if (model
.duration
< model
.maxduration
) {
183 model
.stage
= Model
.TrialStage
.CHOOSE_TARGET
;
187 view
.trial
.repaint();
188 model
.target
= rand
.nextInt(model
.targets
.size());
189 model
.stage
= Model
.TrialStage
.START_SEARCH
;
192 model
.startTime
= (int)System
.currentTimeMillis();
193 model
.stage
= Model
.TrialStage
.FIND_TARGET
;
196 view
.trial
.repaint();
200 model
.stopTime
= (int)System
.currentTimeMillis();
201 System
.out
.println("Trial " + model
.trialCount
+ ": " +
202 (model
.stopTime
- model
.startTime
) + " ms");
203 model
.stage
= Model
.TrialStage
.TRIAL_CLEANUP
;
206 if (model
.trialCount
>= model
.trialMax
) {
207 view
.trial
.stopTrial();
208 model
.stage
= Model
.TrialStage
.WAIT
;
211 model
.stage
= Model
.TrialStage
.START_TRIAL
;
220 model
.target_font
= new Font(view
.trial
.getFont().getName(),
221 view
.trial
.getFont().getStyle(),
222 (Integer
)view
.cp
.fontsize_spinner
.getValue());
223 model
.query_font
= new Font(view
.trial
.getFont().getName(),
224 view
.trial
.getFont().getStyle(), model
.query_font_size
);
225 view
.trial
.bufferGraphics
.setFont(model
.target_font
);
227 view
.trial
.mh
= model
.query_font
.getSize() + model
.query_font_size
;
228 model
.trialMax
= Integer
.valueOf(view
.cp
.trials_spinner
.getValue().toString());