summaryrefslogtreecommitdiff
path: root/jeu-test/Lemmini/0.84/src/Game/LemmCursor.java
blob: 9ace85f89e471c9b3fc42a55cfef39fa8094ddc1 (plain)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package Game;

import java.awt.Cursor;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.Transparency;
import java.awt.image.BufferedImage;

import Tools.ToolBox;

/*
 * Copyright 2009 Volker Oth
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * Implementation of the Lemmini selection cursor.
 *
 * @author Volker Oth
 */
public class LemmCursor  {

	/** distance from center of cursor to be used to detect Lemmings under the cursor */
	private final static int HIT_DISTANCE = 12;

	/** cursor type */
	public enum Type {
		/** normal cursor */
		NORMAL,
		/** select left cursor */
		LEFT,
		/** select right cursor */
		RIGHT,
		/** select walkers cursor */
		WALKER,
		/** normal cursor with selection box */
		BOX_NORMAL,
		/** select left cursor with selection box */
		BOX_LEFT,
		/** select right cursor with selection box */
		BOX_RIGHT,
		/** select walkers cursor with selection box */
		BOX_WALKER
	}

	/** x position in pixels */
	private static int x;
	/** y position in pixels */
	private static int y;
	/** current cursor type */
	private static Type type;
	/** array of images - one for each cursor type */
	private static BufferedImage img[];
	/** array of AWT cursor Objects */
	private static Cursor cursor[];

	/**
	 * Initialization.
	 * @throws ResourceException
	 */
	public static void init() throws ResourceException {
		img = ToolBox.getAnimation(Core.loadImage("misc/cursor.gif"), 8, Transparency.BITMASK);
		cursor = new Cursor[4];
		int w = getImage(Type.NORMAL).getWidth()/2;
		int h = getImage(Type.NORMAL).getHeight()/2;
		cursor[Type.NORMAL.ordinal()] = Toolkit.getDefaultToolkit().createCustomCursor( LemmCursor.getImage(Type.NORMAL), new Point(w,h), "" );
		cursor[Type.LEFT.ordinal()] = Toolkit.getDefaultToolkit().createCustomCursor( LemmCursor.getImage(Type.LEFT),   new Point(w,h), "" );
		cursor[Type.RIGHT.ordinal()] = Toolkit.getDefaultToolkit().createCustomCursor( LemmCursor.getImage(Type.RIGHT),  new Point(w,h), "" );
		cursor[Type.WALKER.ordinal()] = Toolkit.getDefaultToolkit().createCustomCursor( LemmCursor.getImage(Type.WALKER), new Point(w,h), "" );

		type = Type.NORMAL;
		setX(0);
		setY(0);
	}

	/**
	 * Get image for a certain cursor type.
	 * @param t cursor type
	 * @return image for the given cursor type
	 */
	public static BufferedImage getImage(final Type t) {
		return img[t.ordinal()];
	}

	/**
	 * Get image for current cursor type.
	 * @return image for current cursor type
	 */
	public static BufferedImage getImage() {
		return getImage(type);
	}

	/**
	 * Get boxed version of image for the current cursor type.
	 * @return boxed version of image for the current cursor type
	 */
	public static BufferedImage getBoxImage() {
		Type t;
		switch (type) {
			case NORMAL:
				t = Type.BOX_NORMAL;
				break;
			case LEFT:
				t = Type.BOX_LEFT;
				break;
			case RIGHT:
				t = Type.BOX_RIGHT;
				break;
			case WALKER:
				t = Type.BOX_WALKER;
				break;
			default:
				t = type; // should never happen
		}
		return getImage(t);
	}

	/**
	 * Get current cursor as AWT cursor object.
	 * @return current cursor as AWT cursor object
	 */
	public static Cursor getCursor() {
		return cursor[type.ordinal()];
	}

	/**
	 * Get current cursor type.
	 * @return current cursor type
	 */
	public static Type getType() {
		return type;
	}

	/**
	 * Set current cursor type.
	 * @param t cursor type
	 */
	public static void setType(final Type t) {
		type = t;
	}

	/**
	 * Check if a Lemming is under the cursor.
	 * @param l Lemming to check
	 * @param xOfs screen x offset
	 * @return true if the Lemming is under the Cursor, else false.
	 */
	public static boolean doesCollide(final Lemming l, final int xOfs) {
		// get center of lemming
		int lx = l.midX() - xOfs;
		int ly = l.midY();

		// calculate center of cursor
		int cx = getX();
		int cy = getY();

		// calculate distance
		int dx = Math.abs(lx-cx);
		int dy = Math.abs(ly-cy);

		if (dx <= HIT_DISTANCE && dy <= HIT_DISTANCE)
			return true;
		else
			return false;
	}

	/**
	 * Set x position in pixels.
	 * @param x x position in pixels.
	 */
	public static void setX(final int x) {
		LemmCursor.x = x;
	}

	/**
	 * Get x position in pixels.
	 * @return x position in pixels
	 */
	public static int getX() {
		return x;
	}

	/**
	 * Set y position in pixels.
	 * @param y y position in pixels
	 */
	public static void setY(final int y) {
		LemmCursor.y = y;
	}

	/**
	 * Get y position in pixels.
	 * @return y position in pixels
	 */
	public static int getY() {
		return y;
	}
}