Mercurial > hg > ristipolku
annotate game/AnimatedPointElement.java @ 204:dd2caf5321ba
Improve build system.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Fri, 25 Feb 2022 15:46:07 +0200 |
parents | e8eeac403e5f |
children |
rev | line source |
---|---|
125
054dd486ea84
Add animated point element class.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
1 /* |
054dd486ea84
Add animated point element class.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
2 * Ristipolku animated game element |
151 | 3 * (C) Copyright 2011 Matti 'ccr' Hämäläinen <ccr@tnsp.org> |
125
054dd486ea84
Add animated point element class.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
4 */ |
054dd486ea84
Add animated point element class.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
5 package game; |
054dd486ea84
Add animated point element class.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
6 |
054dd486ea84
Add animated point element class.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
7 import java.awt.*; |
054dd486ea84
Add animated point element class.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
8 import java.awt.geom.*; |
054dd486ea84
Add animated point element class.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
9 import java.util.*; |
054dd486ea84
Add animated point element class.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
10 import java.math.*; |
054dd486ea84
Add animated point element class.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
11 |
054dd486ea84
Add animated point element class.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
12 |
054dd486ea84
Add animated point element class.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
13 public class AnimatedPointElement |
054dd486ea84
Add animated point element class.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
14 { |
127
e60b4885eafe
Fix the AnimatedPointElement class to actually work.
Matti Hamalainen <ccr@tnsp.org>
parents:
125
diff
changeset
|
15 private float stime, value, steps; |
e60b4885eafe
Fix the AnimatedPointElement class to actually work.
Matti Hamalainen <ccr@tnsp.org>
parents:
125
diff
changeset
|
16 private IDMPoint pos; |
e60b4885eafe
Fix the AnimatedPointElement class to actually work.
Matti Hamalainen <ccr@tnsp.org>
parents:
125
diff
changeset
|
17 private Interpolate lerpV, lerpX, lerpY; |
125
054dd486ea84
Add animated point element class.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
18 boolean active, first; |
054dd486ea84
Add animated point element class.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
19 String text; |
134
4c0dec72e2f0
Whitespace cosmetic cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
129
diff
changeset
|
20 |
127
e60b4885eafe
Fix the AnimatedPointElement class to actually work.
Matti Hamalainen <ccr@tnsp.org>
parents:
125
diff
changeset
|
21 public AnimatedPointElement(IDMPoint start, String text) |
125
054dd486ea84
Add animated point element class.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
22 { |
127
e60b4885eafe
Fix the AnimatedPointElement class to actually work.
Matti Hamalainen <ccr@tnsp.org>
parents:
125
diff
changeset
|
23 Random rnd = new Random(); |
e60b4885eafe
Fix the AnimatedPointElement class to actually work.
Matti Hamalainen <ccr@tnsp.org>
parents:
125
diff
changeset
|
24 steps = 100; |
129 | 25 |
127
e60b4885eafe
Fix the AnimatedPointElement class to actually work.
Matti Hamalainen <ccr@tnsp.org>
parents:
125
diff
changeset
|
26 lerpX = new Interpolate(start.x - rnd.nextInt(100) + 50, start.x, steps); |
e60b4885eafe
Fix the AnimatedPointElement class to actually work.
Matti Hamalainen <ccr@tnsp.org>
parents:
125
diff
changeset
|
27 lerpY = new Interpolate(start.y - rnd.nextInt(100) + 50, start.y, steps); |
e60b4885eafe
Fix the AnimatedPointElement class to actually work.
Matti Hamalainen <ccr@tnsp.org>
parents:
125
diff
changeset
|
28 lerpV = new Interpolate(0.0f, 1.0f, steps); |
125
054dd486ea84
Add animated point element class.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
29 |
127
e60b4885eafe
Fix the AnimatedPointElement class to actually work.
Matti Hamalainen <ccr@tnsp.org>
parents:
125
diff
changeset
|
30 pos = new IDMPoint(start); |
125
054dd486ea84
Add animated point element class.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
31 active = false; |
054dd486ea84
Add animated point element class.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
32 first = true; |
054dd486ea84
Add animated point element class.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
33 this.text = text; |
054dd486ea84
Add animated point element class.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
34 } |
134
4c0dec72e2f0
Whitespace cosmetic cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
129
diff
changeset
|
35 |
125
054dd486ea84
Add animated point element class.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
36 public void animate(float time) |
054dd486ea84
Add animated point element class.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
37 { |
054dd486ea84
Add animated point element class.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
38 if (first) |
054dd486ea84
Add animated point element class.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
39 { |
054dd486ea84
Add animated point element class.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
40 first = false; |
054dd486ea84
Add animated point element class.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
41 active = true; |
054dd486ea84
Add animated point element class.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
42 stime = time; |
054dd486ea84
Add animated point element class.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
43 } |
134
4c0dec72e2f0
Whitespace cosmetic cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
129
diff
changeset
|
44 |
125
054dd486ea84
Add animated point element class.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
45 if (active) |
054dd486ea84
Add animated point element class.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
46 { |
127
e60b4885eafe
Fix the AnimatedPointElement class to actually work.
Matti Hamalainen <ccr@tnsp.org>
parents:
125
diff
changeset
|
47 float t = (time - stime); |
e60b4885eafe
Fix the AnimatedPointElement class to actually work.
Matti Hamalainen <ccr@tnsp.org>
parents:
125
diff
changeset
|
48 if (t >= steps) |
125
054dd486ea84
Add animated point element class.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
49 { |
127
e60b4885eafe
Fix the AnimatedPointElement class to actually work.
Matti Hamalainen <ccr@tnsp.org>
parents:
125
diff
changeset
|
50 t = steps; |
125
054dd486ea84
Add animated point element class.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
51 active = false; |
054dd486ea84
Add animated point element class.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
52 } |
054dd486ea84
Add animated point element class.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
53 |
054dd486ea84
Add animated point element class.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
54 value = lerpV.getValue(t); |
054dd486ea84
Add animated point element class.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
55 pos.x = lerpX.getValue(t); |
054dd486ea84
Add animated point element class.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
56 pos.y = lerpY.getValue(t); |
054dd486ea84
Add animated point element class.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
57 } |
054dd486ea84
Add animated point element class.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
58 } |
134
4c0dec72e2f0
Whitespace cosmetic cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
129
diff
changeset
|
59 |
125
054dd486ea84
Add animated point element class.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
60 public void paint(Graphics2D g) |
054dd486ea84
Add animated point element class.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
61 { |
054dd486ea84
Add animated point element class.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
62 if (!active) |
054dd486ea84
Add animated point element class.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
63 return; |
134
4c0dec72e2f0
Whitespace cosmetic cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
129
diff
changeset
|
64 |
125
054dd486ea84
Add animated point element class.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
65 Paint psave = g.getPaint(); |
143
5eea4719b429
Remove useless state saving.
Matti Hamalainen <ccr@tnsp.org>
parents:
134
diff
changeset
|
66 // AffineTransform tsave = g.getTransform(); |
125
054dd486ea84
Add animated point element class.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
67 |
127
e60b4885eafe
Fix the AnimatedPointElement class to actually work.
Matti Hamalainen <ccr@tnsp.org>
parents:
125
diff
changeset
|
68 g.setFont(G.fonts[2]); |
e60b4885eafe
Fix the AnimatedPointElement class to actually work.
Matti Hamalainen <ccr@tnsp.org>
parents:
125
diff
changeset
|
69 g.setPaint(new Color(value, value, value, value)); |
e60b4885eafe
Fix the AnimatedPointElement class to actually work.
Matti Hamalainen <ccr@tnsp.org>
parents:
125
diff
changeset
|
70 g.drawString(text, pos.x, pos.y); |
125
054dd486ea84
Add animated point element class.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
71 |
143
5eea4719b429
Remove useless state saving.
Matti Hamalainen <ccr@tnsp.org>
parents:
134
diff
changeset
|
72 // g.setTransform(tsave); |
125
054dd486ea84
Add animated point element class.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
73 g.setPaint(psave); |
054dd486ea84
Add animated point element class.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
74 } |
054dd486ea84
Add animated point element class.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
75 } |