1 package de.jos.game.actions;
2
3 import java.awt.AlphaComposite;
4 import java.awt.Composite;
5 import java.awt.Graphics2D;
6
7 import com.golden.gamedev.GameObject;
8
9 public class ActionTextFader extends AbstractAction implements SpecialActionEvent {
10
11 public static final float TRANSPARENCY_INIT = 1.0f;
12 public static final float TRANSPARENCY_INCREMENT = -0.02f;
13 public static final double MOVING_X_INCREMENT = 0.7;
14
15 private float transparency = TRANSPARENCY_INIT;
16
17 private String text = null;
18 private double x = -1;
19 private double y = -1;
20
21 public static final int STATE_FADING = 1;
22 public static final int STATE_FINISHED = 2;
23
24 private int state = STATE_FADING;
25
26 public ActionTextFader() {}
27
28 public void init() {
29 super.init();
30 state = STATE_FADING;
31 }
32
33 public void update(long elapsedTime, GameObject gameObject) {
34 transparency += TRANSPARENCY_INCREMENT;
35 if (transparency < 0.0f) {
36 transparency = 0.0f;
37 state = STATE_FINISHED;
38 }
39 y -= MOVING_X_INCREMENT;
40 }
41
42 public void render(Graphics2D graphics, GameObject gameObject) {
43 Composite old = graphics.getComposite();
44
45 Composite scoreComposite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, getTransparency());
46 graphics.setComposite(scoreComposite);
47 getGameFont().drawString(graphics, getText(), (int) getX(), (int) getY());
48
49 graphics.setComposite(old);
50 }
51
52 public Integer getLayer() {
53 return LayerConstants.LAYER_2;
54 }
55
56 public boolean isFinished() {
57 return (state == STATE_FINISHED);
58 }
59
60 public double getX() {
61 return x;
62 }
63
64 public void setX(double x) {
65 this.x = x;
66 }
67
68 public double getY() {
69 return y;
70 }
71
72 public void setY(double y) {
73 this.y = y;
74 }
75
76 private float getTransparency() {
77 return transparency;
78 }
79
80 public String getText() {
81 return text;
82 }
83
84 public void setText(String text) {
85 this.text = text;
86 }
87
88 public String getName() {
89 return this.getClass().getName();
90 }
91
92 }