001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.gui.animation; 003 004import java.awt.event.MouseMotionListener; 005import java.util.ArrayList; 006 007import java.awt.Color; 008import java.awt.Font; 009import java.awt.FontMetrics; 010import java.awt.Dimension; 011import java.awt.Image; 012import java.awt.Graphics; 013import java.awt.Graphics2D; 014 015import java.awt.event.MouseEvent; 016import java.awt.event.MouseListener; 017 018import javax.swing.JPanel; 019 020import org.openstreetmap.josm.tools.ImageProvider; 021 022/** 023 * Birthday animation game extension. 024 * @author Pauline Thiele 025 * @since 19254 026 */ 027public class BirthdayExtension extends JPanel implements AnimationExtension, MouseListener, MouseMotionListener { 028 private static final int SIZE = 40; 029 private final Image myImage; 030 private double w; 031 private double h; 032 private double Mx; 033 private double My; 034 private int originX; 035 private int originY; 036 private boolean isClicked; 037 private boolean isHappyBirthday; 038 private int giftNumber = 1; 039 040 private final ArrayList<Gift> giftList = new ArrayList<>(); 041 private long startTime; 042 private long lastSpawnTime; 043 044 /** 045 * Creates a circle with a position, radius and checks if two circles ovelaps 046 */ 047 private static final class Circle { 048 double x; 049 double y; 050 double radius = 1; 051 052 void setPosition(double x, double y) { 053 this.x = x; 054 this.y = y; 055 } 056 057 void setRadius(double radius) { 058 this.radius = radius; 059 } 060 061 boolean overlaps(Circle other) { 062 double dx = x - other.x; 063 double dy = y - other.y; 064 double dr = radius + other.radius; 065 return dx*dx + dy*dy < dr*dr; 066 } 067 } 068 069 /** 070 * Creates a player with a position, radius and updates position 071 */ 072 private final class Player { 073 Circle circle = new Circle(); 074 075 void setPosition(double x, double y) { 076 circle.setPosition(x, y); 077 } 078 079 void setRadius(double radius) { 080 circle.setRadius(radius); 081 } 082 083 void update() { 084 setPosition(Mx, My); 085 } 086 } 087 088 /** 089 * Creates a gift with a position, radius, updates position and renders the image 090 */ 091 private final class Gift { 092 Image myImage = getImage(); 093 Circle circle = new Circle(); 094 int offsX = 4; 095 int offsY = 4; 096 097 void setPosition(double x, double y) { 098 circle.setPosition(x, y); 099 } 100 101 void setRadius(double radius) { 102 circle.setRadius(radius); 103 } 104 105 void update() { 106 circle.x += offsX; 107 circle.y += offsY; 108 109 if (circle.x <= 0) { // left 110 offsX *= -1; 111 circle.x = 0; 112 if (circle.y == 0) { 113 offsY *= -1; 114 } 115 } else if (circle.x >= (w - SIZE)) { // right 116 offsX *= -1; 117 circle.x = w - SIZE; 118 } 119 120 if (circle.y <= 0) { // top 121 offsY *= -1; 122 circle.y = 0; 123 } else if (circle.y >= (h - SIZE)) { // bottom 124 offsY *= -1; 125 circle.y = h - SIZE; 126 } 127 } 128 129 void render(Graphics g) { 130 double x = circle.x; 131 double y = circle.y; 132 g.drawImage(myImage, (int) x, (int) y, null); 133 } 134 } 135 136 Player player = new Player(); 137 138 BirthdayExtension() { 139 this.myImage = new ImageProvider("presets/shop/present"). 140 setMaxSize(new Dimension(SIZE, SIZE)).get().getImage(); 141 player.setRadius(11); 142 lastSpawnTime = System.currentTimeMillis(); 143 } 144 145 /** 146 * Creates gifts on a random position on screen 147 */ 148 void spawnGifts() { 149 final byte maxGifts = 30; 150 if (giftList.size() < maxGifts) { 151 lastSpawnTime = System.currentTimeMillis(); 152 153 Gift gift = new Gift(); 154 giftList.add(gift); 155 gift.myImage = getImage(); 156 gift.setRadius(32); 157 158 double x = (w - originX) / 2; 159 double y = (h - originY) / 2; 160 161 if (giftList.size() > 1) { 162 double radius = w / 2; 163 double angle = 2 * Math.PI * Math.random(); 164 x += Math.cos(angle) * radius; 165 y += Math.sin(angle) * radius; 166 gift.setPosition(x, y); 167 } else { 168 gift.setPosition(x, y); 169 } 170 } 171 } 172 173 /** 174 * Manages the creation, updating and deletion of gifts 175 */ 176 private void gameLogic() { 177 if ((giftList.isEmpty() && !isHappyBirthday) || 178 (System.currentTimeMillis() - lastSpawnTime > 5000 && !isHappyBirthday)) { 179 spawnGifts(); 180 lastSpawnTime = System.currentTimeMillis(); 181 } 182 183 player.update(); 184 185 for (Gift gift : giftList) { 186 gift.update(); 187 } 188 189 int index = 0; 190 for (Gift gift : giftList) { 191 if (gift.circle.overlaps(player.circle) && isClicked) { 192 startTime = System.currentTimeMillis(); 193 giftList.remove(index); 194 break; 195 } 196 index += 1; 197 } 198 } 199 200 @Override 201 public void mousePressed(MouseEvent e) { 202 isClicked = true; 203 } 204 205 @Override 206 public void mouseReleased(MouseEvent e) { 207 isClicked = false; 208 } 209 210 @Override 211 public void mouseEntered(MouseEvent e) {} 212 213 @Override 214 public void mouseExited(MouseEvent e) {} 215 216 @Override 217 public void mouseClicked(MouseEvent e) {} 218 219 @Override 220 public void mouseDragged(MouseEvent e) {} 221 222 @Override 223 public void mouseMoved(MouseEvent e) { 224 Mx = e.getXOnScreen()- originX; 225 My = e.getYOnScreen()- originY; 226 227 player.setPosition(Mx, My); 228 229 huntGift(); 230 } 231 232 @Override 233 public final void adjustForSize(int w, int h, int x, int y) { 234 this.w = w; 235 this.h = h; 236 originX = x; 237 originY = y; 238 } 239 240 /** 241 * Visualizes the gifts and the "Happy Birthday JOSM!" text 242 */ 243 @Override 244 public void paint(Graphics g) { 245 gameLogic(); 246 for (Gift gift : giftList) { 247 gift.render(g); 248 } 249 250 Graphics2D g2d = (Graphics2D) g; 251 252 /* keep a bit of debugging code 253 if (Logging.isTraceEnabled()) { 254 // draws circles around mouse and gifts 255 for (Gift gift : giftList) { 256 Ellipse2D.Double circleG = new Ellipse2D.Double(gift.circle.x - gift.circle.radius / 2, 257 gift.circle.y - gift.circle.radius / 2, gift.circle.radius * 2, gift.circle.radius * 2); 258 g2d.draw(circleG); 259 } 260 Ellipse2D.Double circleM = new Ellipse2D.Double(player.circle.x - player.circle.radius / 2, 261 player.circle.y - player.circle.radius / 2, player.circle.radius * 2, player.circle.radius * 2); 262 g2d.draw(circleM); 263 } 264 */ 265 266 if (giftList.isEmpty()) { 267 if (System.currentTimeMillis() - startTime <= 5000) { 268 int fontSize = SIZE /2; 269 g2d.setFont(new Font("Arial", Font.BOLD, fontSize)); 270 g2d.setColor(Color.RED); 271 272 String text = "Happy Birthday JOSM! "; 273 274 FontMetrics fm = g2d.getFontMetrics(); 275 int textWidth = fm.stringWidth(text); 276 int x = 0; 277 int y = SIZE /2; 278 279 for (int i = 0; i < (w / textWidth) + 1; i++) { 280 g2d.drawString(text, x, y); 281 g2d.drawString(text, x, (int) h-y); 282 x += textWidth; 283 } 284 isHappyBirthday = true; 285 } else { 286 giftNumber += 1; 287 for (int i = 0; i < giftNumber; i += 1) { 288 spawnGifts(); 289 } 290 startTime = System.currentTimeMillis(); 291 isHappyBirthday = false; 292 } 293 } 294 } 295 296 @Override 297 public void animate() { 298 } 299 300 /** 301 * Hunts the gift 302 */ 303 private void huntGift() { 304 for (Gift gift : giftList) { 305 double dx = gift.circle.x - player.circle.x; 306 double dy = gift.circle.y - player.circle.y; 307 308 double len = Math.sqrt(dx*dx + dy*dy); 309 310 if ((dx < -100 || dx > 100) && (dy < -100 || dy > 100)) { 311 dx = 0; 312 dy = 0; 313 } else if (len > 0 && dx != 0 && dy != 0) { 314 dx /= len; 315 dy /= len; 316 } 317 318 double giftSpeed; 319 320 if (dx != 0 && dy != 0 && gift.circle.x == 0 && gift.circle.y == 0) { // top left corner 321 giftSpeed = 5.0f; 322 dy += 5; 323 } else if (dx != 0 && dy != 0 && gift.circle.x >= w - SIZE && gift.circle.y == 0) { // top right corner 324 giftSpeed = 5.0f; 325 dy += 5; 326 } else if (dx != 0 && dy != 0 && gift.circle.x == 0 && gift.circle.y >= h - SIZE) { // bottom left corner 327 giftSpeed = 5.0f; 328 dx += 5; 329 } else if (dx != 0 && dy != 0 && gift.circle.x >= w - SIZE && gift.circle.y >= h - SIZE) { // bottom right corner 330 giftSpeed = 5.0f; 331 dx -= 5; 332 } else { 333 giftSpeed = 3.0f; 334 } 335 336 gift.circle.x += dx * giftSpeed; 337 gift.circle.y += dy * giftSpeed; 338 } 339 } 340 341 private Image getImage() { 342 return this.myImage; 343 } 344}