1 package org.argosfields.battlefield;
2
3 import org.apache.commons.logging.Log;
4 import org.apache.commons.logging.LogFactory;
5 import org.argosfields.image.UnitImageSet;
6 import org.argosfields.model.Direction;
7 import org.argosfields.model.IMobile;
8 import org.argosfields.model.Unit;
9 import org.eclipse.jface.util.Assert;
10 import org.eclipse.swt.SWT;
11 import org.eclipse.swt.events.PaintEvent;
12 import org.eclipse.swt.graphics.Image;
13 import org.eclipse.swt.graphics.Rectangle;
14 import org.eclipse.swt.widgets.Display;
15
16 /***
17 * MovingState.java
18 *
19 * @author Xavier Cho
20 * @version $Revision: 1.8 $ $Date: 2003/12/01 07:38:05 $
21 */
22 public class MovingState extends State {
23 private Log log = LogFactory.getLog(MovingState.class);
24
25 private BattleFieldView battleFieldView;
26 private Unit unit;
27 private int[] path;
28
29 private Rectangle currentBounds;
30 private Rectangle targetBounds;
31
32 /***
33 * @param context
34 */
35 public MovingState(
36 final Unit unit,
37 final int[] path,
38 final Context context) {
39 super(context);
40
41 if (unit == null) {
42 SWT.error(SWT.ERROR_NULL_ARGUMENT);
43 }
44
45 if (path == null) {
46 SWT.error(SWT.ERROR_NULL_ARGUMENT);
47 } else if (path.length < 2) {
48 SWT.error(SWT.ERROR_INVALID_ARGUMENT);
49 }
50
51 if (!(unit instanceof IMobile)) {
52 SWT.error(SWT.ERROR_INVALID_ARGUMENT);
53 }
54
55 this.battleFieldView = context.getBattleFieldView();
56 this.unit = unit;
57 this.path = path;
58
59 battleFieldView.redraw();
60
61 Display display = battleFieldView.getDisplay();
62
63 Assert.isNotNull(display);
64 display.asyncExec(new UnitAnimator());
65 }
66
67 /***
68 * @see org.argosfields.widget.state.State#getName()
69 */
70 public String getName() {
71 return "Moving";
72 }
73
74 public void paintControl(final PaintEvent event) {
75 Image image = battleFieldView.getMapImage();
76
77 if (image != null) {
78 event.gc.drawImage(
79 image,
80 event.x,
81 event.y,
82 event.width,
83 event.height,
84 event.x,
85 event.y,
86 event.width,
87 event.height);
88 }
89
90 if (currentBounds != null) {
91 UnitImageSet unitSet = UnitImageSet.getInstance();
92
93 unitSet.drawIcon(
94 unit.getIconIndex(),
95 currentBounds,
96 battleFieldView,
97 event.gc);
98 }
99 }
100
101 private class UnitAnimator implements Runnable {
102 /***
103 * @see java.lang.Runnable#run()
104 */
105 public void run() {
106 int size = path.length - 1;
107
108 int srcIndex = path[0];
109 int targetIndex = path[size];
110
111 BattleField battleField = battleFieldView.getBattleField();
112 battleField.getTile(srcIndex).setUnit(null);
113
114 battleFieldView.updateMapImages(srcIndex);
115
116 for (int i = 0; i < size; i++) {
117 currentBounds = battleField.getTileBounds(path[i]);
118 targetBounds = battleField.getTileBounds(path[i + 1]);
119
120 Rectangle originalBounds = clone(currentBounds);
121
122 float stepX = (float) (targetBounds.x - originalBounds.x) / 10f;
123 float stepY = (float) (targetBounds.y - originalBounds.y) / 10f;
124
125 unit.setFacing(getFacing(stepX, stepY));
126
127 for (int j = 1; j <= 10; j++) {
128 Rectangle previousBounds = currentBounds;
129
130 currentBounds.x = originalBounds.x + (int) (stepX * j);
131 currentBounds.y = originalBounds.y + (int) (stepY * j);
132
133 Rectangle refreshArea =
134 grow(previousBounds.union(currentBounds), 2);
135
136 battleFieldView.redraw(
137 refreshArea.x,
138 refreshArea.y,
139 refreshArea.width,
140 refreshArea.height,
141 false);
142
143 battleFieldView.getDisplay().update();
144
145 try {
146 Thread.sleep(10);
147 } catch (InterruptedException e) {
148 if (log.isWarnEnabled()) {
149 log.warn("Thread has been interrupted.", e);
150 }
151 }
152 }
153 }
154
155 battleField.getTile(targetIndex).setUnit(unit);
156
157 battleFieldView.updateMapImages(targetIndex);
158
159 Context context = getContext();
160 context.setCurrentState(new DefaultState(context));
161
162 battleFieldView.setSelectedIndex(targetIndex);
163 }
164
165 private Direction getFacing(final float x, final float y) {
166 Direction facing;
167
168 if (x == 0) {
169 if (y < 0) {
170 facing = Direction.NORTH;
171 } else {
172 facing = Direction.SOUTH;
173 }
174 } else if (x > 0) {
175 if (y < 0) {
176 facing = Direction.NORTHEAST;
177 } else {
178 facing = Direction.SOUTHEAST;
179 }
180 } else {
181 if (y < 0) {
182 facing = Direction.NORTHWEST;
183 } else {
184 facing = Direction.SOUTHWEST;
185 }
186 }
187
188 return facing;
189 }
190
191 private Rectangle clone(final Rectangle src) {
192 Rectangle target =
193 new Rectangle(src.x, src.y, src.width, src.height);
194 return target;
195 }
196
197 private Rectangle grow(final Rectangle src, final int size) {
198 Rectangle target =
199 new Rectangle(
200 src.x - size,
201 src.y - size,
202 src.width + size * 2,
203 src.height + size * 2);
204 return target;
205 }
206 }
207 }
This page was automatically generated by Maven